-5

Well, This is the piece of code i wrote to print a matrix in spiral form in c. But it started showing me an error during compilation. i,basically, don't know how to write recursive functions properly. can someone fix the code ? please.. the error in this code was that solution.c:23:1: error: expected identifier or '(' before '{' token can anyone tell me what that means...

     #include <stdio.h>

     matrix(int B[20][20],int M,int N);

     main() 
     {
     int M,N,j,i=1;
     scanf("%d %d",&M,&N);
    int B[M][N];

    for(i=1; i<=M; i++)
    {
    for(j=1; j<=N; j++)
    {
        scanf("%d",&B[i][j]);
    }
    }

    matrix(B[20][20],M,N); 
    return 0; 
    }
    matrix(int B[20][20], int M,int N);
    {
    while(M!=0||N!=0)
    { 
    for (i=1,j=1; j<=N; j++)
    {
        printf("%d ",(B[i][j]));
    } 
    for(i=2,j=N; i<=M; i++)
    {
        printf("%d ",(B[i][j])); 
    }
    for(i=M,j=N-1; j>0; j--)
    {
        printf("%d ",(B[i][j]));
    } 
    for(i=M-1,j=1; i<=2; i--)
    {
        printf("%d ",(B[i][j])); 
    } 
   }
    matrix(B[20][20],--M,--N); 
   }
Satya Kavya
  • 11
  • 1
  • 3
  • what is it meant to be doing? what errors are you getting? –  Sep 22 '14 at 15:49
  • solution.c:35:8: error: unknown type name 'B' matrix(B[20][20],M,N); ^ solution.c:35:18: error: unknown type name 'M' matrix(B[20][20],M,N); ^ solution.c:35:20: error: unknown type name 'N' matrix(B[20][20],M,N); ^ solution.c:37:1: error: expected identifier or '(' before '{' token { – Satya Kavya Sep 22 '14 at 15:51
  • These are the errors it was showing.I don't know what to do. can you help me – Satya Kavya Sep 22 '14 at 15:51
  • main need to look like this: void main() { /*..*/ } or int main() { /*..*/ return 0; } – eitank Sep 22 '14 at 15:52
  • I want it to print a matrix in a spiral form – Satya Kavya Sep 22 '14 at 15:52
  • 1
    `main()` at **least** shall return `int`! @eitank – alk Sep 22 '14 at 15:54
  • the error is with the function matrix. – Satya Kavya Sep 22 '14 at 15:55
  • 3
    Please update your question, if you want to add something to it! Do not squezze it into comments. – alk Sep 22 '14 at 15:56
  • @eitank i hate this form, but it's valid, unfortunately. compiler will add `int` before `main()`. – Jason Hu Sep 22 '14 at 16:02
  • i did this once.recursive is a good way to write it, but you will have to calculate the step yourself. you will need a help function to act as an actuator and it needs more info than you give now. – Jason Hu Sep 22 '14 at 16:03
  • Maybe indent the code properly first. As alk mentioned, a valid declaration of main is `int main()`, and an appropriate `return` is necessary at the end of it. You also shouldn't declare `int B[M][N];` rather `int B[20][20];`, so the function at least has a chance to take it right. In the function call, the dimensions select a member, not the entire array as intended (pass just `B`). The loops should go from 0, to ` – Jubatian Sep 22 '14 at 16:42

3 Answers3

1

I will try and address the issues at hand while giving you a recursive solution for your problem.

typing: C is a static typed language which requires you to give all variables AND function declarations a type. This lead your first compile errors.

matrix(int B[20][20],int M,int N);
//should have been (though be careful there is another issue addressed later)
void matrix(int B[20][20],int M,int N);

as well as

 main() {
 //shoud have been
 int main() {

counting: as a programmer you should get used to start counting at 0 as most languages do so. An array of size 4 has the elements array[0] array[1] array[2] array[3]. Trying to access an address above size-1 will lead to undefined behavior.

Therefore every loop you had would have given you unintended behavior

for(i=1; i <= M; i++)
//should have been (as an example)
for(i=0; i <  M; i++)

array as a parameter I: if you want to pass an array as a parameter you have give just the variable name as any [X][Y] would be an access to the array and result in the element stored at that location being passed instead of the intended array.

matrix(B[20][20],--M,--N); 
//should have been
matrix(B,--M,--N);

array as a parameter II: if you want a function to take an array as a parameter you have to declare the function head in a specific way. Which is quite counter intuitive. You can read more on that topic here. But in the end

void matrix(int B[20][20],int M,int N);
//should have been
void matrix(int M,int N,int B[][N]);

scope: Local variables just have a limited scope therefore

    void matrix(int B[20][20], int M,int N) {
      while(M!=0||N!=0) { 
        for (i=1,j=1; j<=N; j++)
          { /*....*/

cannot work as either i and j are unknown in this function. This would be correct:

    void matrix(int B[20][20], int M,int N) {
      int i,j;
      while(M!=0||N!=0) { 
        for (i=1,j=1; j<=N; j++)
          { /*....*/

while-loop: The while loop will end as soon as the head of the loop evaluates to false

   while(M!=0||N!=0)
    { 
     /*stuff*/
    }

would have therefore resulted in an endless loop, as neither M nor N are changed in the body. Or it would have just never been executed.

recursion: You did write a (theoretically) recursive function, though without any aboard rule. A recursion is nothing but a loop and therefore it carries on until an aboard/break is issued. Basically speaking you have to have something like

  if(/*aboard_condition*/) return;

to ensure that your recursion will end at some point .

semicolons: addressing the part of your question

solution.c:23:1: error: expected identifier or '(' before '{' token

you just had a semicolon to much:

matrix(int B[20][20],int M,int N);
{ ....
//should have been (though do not forget about the other issues)
matrix(int B[20][20],int M,int N)
{

as a semicolon seperated the function header from the function block

As your print function was quite broken I used a print_sprial function borrowed from here and turned it into a recursive one by taking the variables m and n out of the body and into the header. I further moved the print function in front of the main function to avoid having to give a header declaration.

#include <stdio.h>

void print_matrix(int matrix_y, int matrix_x, int a[matrix_y][matrix_x],int k,int l)
{
    int i;
    int m = matrix_y;
    int n = matrix_x;
    /*  k - starting row index
        m - ending row index
        l - starting column index
        n - ending column index
        i - iterator
    */

    while (k < m && l < n)
    {
        /* Print the first row from the remaining rows */
        for (i = l; i < n; ++i)
        {
            printf("%d ", a[k][i]);
        }
        k++;

        /* Print the last column from the remaining columns */
        for (i = k; i < m; ++i)
        {
            printf("%d ", a[i][n-1]);
        }
        n--;

        /* Print the last row from the remaining rows */
        if ( k < m)
        {
            for (i = n-1; i >= l; --i)
            {
                printf("%d ", a[m-1][i]);
            }
            m--;
        }

        /* Print the first column from the remaining columns */
        if (l < n)
        {
            for (i = m-1; i >= k; --i)
            {
                printf("%d ", a[i][l]);
            }
            l++;    
        }        
    }
}

int main()
{
    int M,N,j,i=1;
    scanf("%d %d",&M,&N);
    int B[M][N];

    for(i=0; i != M; i++)
    {
        for(j=0; j != N; j++)
        {
            printf("%d/%d:",i,j);
            scanf("%d",&B[i][j]);
        }
    }

    print_matrix(M,N,B,0,0);
    return 0;
}
Community
  • 1
  • 1
Sim
  • 4,199
  • 4
  • 39
  • 77
0

As we got into the problem, your matrix dimensions depends on user's choice. Therefor, you wouldn't be able to allocate your matrix in the stack using the following code:

int B[N][M];

because the dimensions aren't known in compilation time. so it required to use malloc.

The code using malloc:

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

void doSomething(int** matrix, int size0, int size1);

int main() {
    int rows, cols, i, j;
    int **matrix;
    // scanf for row, cols

    // allocate matrix (basically using malloc)
    matrix = (int**)malloc(rows * sizeof(int*));
    for (i = 0; i < rows; i++) {
        matrix[i] = (int*)malloc(cols * sizeof(int));
    }

    // scanf for matrix values

    // call recursive function
    doSomething(matrix, rows, cols);

    // free in reverse order
    for (i = 0; i < rows; i++) {
        free(matrix[i]);
    }
    free(matrix);

    return 0;
}

void doSomething(int** matrix, int size0, int size1) {
    int i,j;
    // stop condition

    // some code here

}
eitank
  • 330
  • 1
  • 5
-1

BETTER SOLUTION

   #include<stdio.h>
    #include<stdlib.h>
    int i=0,j=0,k=0,flag=0;
    int function_Matrix(int arr[][4],int i,int j)
    {
        if(j==4||i==3)
            return 1;

            if(flag==0)
            {
                printf("\t%d",arr[i][j]);
            }
        function_Matrix(arr,i,j+=1);
        printf("\n");
        function_Matrix(arr,i+=1,j=0);
        flag=1;
    }
    int main()
    {
        int x;
        int arr[][4]={{1,2,3,4},
        {5,6,7,8},
        {9,7,6,5}};
        function_Matrix(arr,0,0);
    }

FLOW!

Ruchir
  • 1,018
  • 7
  • 17