Menu
Coddy logo textTech

Recap Challenge #2

Part of the Fundamentals section of Coddy's C journey — lesson 60 of 63.

challenge icon

Challenge

Easy

Let's test your array manipulation skills in C!

Create a program that performs operations on a 2D array (matrix):

  1. Create a function rotateMatrix that:
    • Takes a 2D integer array and its size
    • Rotates the matrix 90 degrees clockwise
      Use this to rotate the matrix: rotated[j][size-1-i] = original[i][j];
    • Returns the rotated matrix
  2. Create a function findElement that:
    • Takes a 2D integer array, its size, and a target value
    • Returns 1 if the target is found in the matrix, 0 otherwise

Try it yourself

#include <stdio.h>

void rotateMatrix(int original[][10], int rotated[][10], int size);
int findElement(int matrix[][10], int size, int target);

void rotateMatrix(int original[][10], int rotated[][10], int size) {
    // Your code here
}

int findElement(int matrix[][10], int size, int target) {
    // Your code here
}

// Don't modify the code below
int main() {
    int size;
    scanf("%d", &size);
    int matrix[10][10];
    int rotated[10][10];
    
    int value = 1;
    for (int i = 0; i < size; i++) {
        for (int j = 0; j < size; j++) {
            matrix[i][j] = value++;
        }
    }
    
    printf("Original Matrix:\n");
    for (int i = 0; i < size; i++) {
        for (int j = 0; j < size; j++) {
            printf("%d ", matrix[i][j]);
        }
        printf("\n");
    }
    printf("\n");
    
    rotateMatrix(matrix, rotated, size);
    
    printf("Rotated Matrix:\n");
    for (int i = 0; i < size; i++) {
        for (int j = 0; j < size; j++) {
            printf("%d ", rotated[i][j]);
        }
        printf("\n");
    }
    printf("\n");
    
    int result = findElement(matrix, size, 10);
    if (result) {
        printf("Found\n");
    } else {
        printf("Not found\n");
    }
    
    return 0;
}

All lessons in Fundamentals