简体   繁体   中英

Rotate matrix in C

I've write a program to rotate 2d matrix(arrow clockwise) now i try to use functions rotate and print,to separate the program to two section. one section will rotate, second will print. but when i run i get an error i think related to function declaration/call please see what is the problem:

    void DisplayArray2D(char arr[][7]);
    void RotateArray2D(char arr[][7]);

int main() {

    int m=7,n=7;
    //int p,q;
    int temp;
    int i,j;
    char arr[7][7] = {
                     { '*',' ',' ',' ',' ',' ',' '},
                     { '*','*',' ',' ',' ',' ',' '},
                     { '*','*','*',' ',' ',' ',' '},
                     { '*','*','*','*','*','*','*'},
                     { '*','*','*',' ',' ',' ',' '},
                     { '*','*',' ',' ',' ',' ',' '},
                     { '*',' ',' ',' ',' ',' ',' '},
                                                  };


  RotateArray2D(arr);

  DisplayArray2D(arr);

    void RotateArray2D(char arr[][7]) ;

    for(i = 0; i < m; i++)

    {
        for (j = i + 1; j < n; j++)
        {
            temp = arr[i][j];
            arr[i][j] = arr[j][i];
            arr[j][i] = temp;
        }
    }

    for (i = 0; i < m; i++)
    {
        for (j = 0; j < n / 2; j++)
        {
            temp = arr[i][j];
            arr[i][j] = arr[i][n - 1 - j];
            arr[i][n - 1 - j] = temp;
        }
    }

  void DisplayArray2D(char arr[][7]);

 {

    for (i = 0; i < n; i++)

    {

        for (j = 0; j < n; j++)

            print f("%c ", arr[i][j]);


        print f("\n");

    }

    print f("\n");


    return 0;

}

    }

You need to do something like that :
(By the way I didn't check if the code was correct, I only rearrange your code to put the functions before the main() )

#include <stdio.h>

void RotateArray2D(char arr[][7]){
    int m=7,n=7;
    int temp;

    for (int i = 0; i < m; i++) {
        for (int j = i + 1; j < n; j++) {
            temp = arr[i][j];
            arr[i][j] = arr[j][i];
            arr[j][i] = temp;
        }
    }

    for (int i = 0; i < m; i++) {
        for (int j = 0; j < n / 2; j++) {
            temp = arr[i][j];
            arr[i][j] = arr[i][n - 1 - j];
            arr[i][n - 1 - j] = temp;
        }
    }
}

void DisplayArray2D(char arr[][7]) {
    for (int i = 0, m = 7; i < m; i++) {
        for (int j = 0, n = 7; j < n; j++)
            printf("%c ", arr[i][j]);
        printf("\n");
    }
    printf("\n");
}

int main() {
    char arr[7][7] = {
                    { '*',' ',' ',' ',' ',' ',' '},
                    { '*','*',' ',' ',' ',' ',' '},
                    { '*','*','*',' ',' ',' ',' '},
                    { '*','*','*','*','*','*','*'},
                    { '*','*','*',' ',' ',' ',' '},
                    { '*','*',' ',' ',' ',' ',' '},
                    { '*',' ',' ',' ',' ',' ',' '},
                                                };

    RotateArray2D(arr);
    DisplayArray2D(arr);

    return 0;
}

BTW, here's a fully working code:

#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>

void nl(void) {     //function to print new line
    printf("\n");
}

int** allocate(_size) {
    int** matrix=(int**)malloc(sizeof(int*)*_size);
    for(int i=0;i<_size;i++) {
        matrix[i]=(int*)malloc(sizeof(int)*_size);
    }
    return matrix;
}

void scanMatrix(_matrix,_size)
int** _matrix;
{
    printf("Enter the values: ");
    for(int i=0;i<_size;i++) {
        for(int j=0;j<_size;j++) {
            scanf("%d",&_matrix[i][j]);
        }
    }
}

void printMatrix(_matrix,_size)
int** _matrix;
{
    for(int i=0;i<_size;i++) {
        for(int j=0;j<_size;j++) {
            printf("%d ",_matrix[i][j]);
        }
        nl();
    }
}

void swap(num1,num2)
int* num1;
int* num2;
{
    int temp = *num1;
    *num1 = *num2;
    *num2 = temp;
}

void rotateMatrix(_matrix,_size)
int** _matrix;
{
    for(int i=0;i<_size;i++) {
        for(int j=i+1;j<_size;j++) {
            swap(&_matrix[i][j],&_matrix[j][i]);
        }
    }
    for(int i=0;i<_size;i++) {
        for(int j=0;j<_size/2;j++) {
            swap(&_matrix[i][j],&_matrix[i][_size-1-j]);
        }
    }
}

main(argc,argv)
const char** argv;
{
    int size=0;
    printf("Enter the size of the square matrix: ");
    scanf("%d",&size);
    int** matrix=allocate(size);
    scanMatrix(matrix,size);
    printMatrix(matrix,size);
    nl();
    rotateMatrix(matrix,size);
    nl();
    printMatrix(matrix,size);
    return 0;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM