• C

Simple C Question

I'm getting some God-awful syntax errors...all that I know are not true, so it must be because of some other problem.  Thanks!


 # include <stdio.h>



void input_data(int *size, int square[][])

      {
      int i, j;

      scanf("%d", size);

      for(i=0; i<size; i++)
            for(j=0; j<size; j++)
                  scanf("%d", &square[i][j]);
      
      }



void add_rows(int size, const int square[][], int row_sum[])

      {
      int i, j;

      for(i=0; i<size; i++)
            for(j=0; j<size; j++)
                  row_sum[i]=square[i][j]+row_sum[i];
      
      }


void add_columns(int size; const int square[][], int col_sum[])
      
      {
      int i, j;

      for(j=0; j<size; j++)
            for(i=0; i<size; i++)
                  col_sum[j]=square[i][j]+col_sum[j];

      }

void add_diag(int size; const int square[][])

      {
      int i, j=3;

      for(i=0; i<size; i++)
            {
            diag_sum[0]=diag_sum[0]+square[i][i];
            diag_sum[1]=diag_sum[1]+square[i][j];
            j--;
            }
      }


void compare(int size, int row_sum[], int col_sum[], int diag_sum[], *ismagic, *magic_num)
      {
      int i;
      
      *magic_num=diag_sum[0];

      if(*magic_num!=diag_sum[1])
            *ismagic=0;

      for(i=0; i<size; i++)
            {
            if(row_sum[i]!=*magic_num)
                  *ismagic=0;
            
            if(col_sum[i]!=*magic_num)
                  *ismagic=0;
            }
            
      }

void print_square(size, square[][], ismagic, magic_num)

      {
      int i, j;      

      for(i=0; i<size; i++)
            for(j=0; j<size; j++)
                  {
                  printf("%3d", square[i][j];
                  if(j==size-1)
                        printf("\n");
                  }

      if(ismagic==0)
            printf("\n\n\nThis is not a magic square.\n\n);

      else
            {
            printf("\n\n\nThis is a magic square.\n\n);
            printf("The magic number is %d.", magic_num);
            }

      }


int main(void)

      {
      int size=0, ismagic=1, square[10][10], magic_num;
      int row_sum[10]={0,0,0,0,0,0,0,0,0,0};
      int col_sum[10]={0,0,0,0,0,0,0,0,0,0}, diag_sum[2];

      input_data(&size, square);

      add_rows(size, square, row_sum);

      add_columns(size, square, col_sum);

      add_diag(size, square, diag_sum);

      compare(size, row_sum, col_sum, diag_sum, &ismagic, &magic_num);
      
      print_square(size, square, ismagic, magic_num);

      }
SaraBizAsked:
Who is Participating?
 
mattropeConnect With a Mentor Commented:
  scanf("%d", size);

should be

   scanf("%d", &size);

   void compare(int size, int row_sum[], int col_sum[], int diag_sum[], *ismagic, *magic_num)

should have a type defined for the pointers *ismagic and *magic_num

   void print_square(size, square[][], ismagic, magic_num)

should have types for size, ismagic, and magic_num

   printf("\n\n\nThis is a magic square.\n\n);

should have a closing quote mark.


Those are what jump out at me.  If there are more errors, please post the error messages you receive.
0
 
imladrisCommented:
A hint! A hint! Our collective kingdoms for a hint! Please give us the text of at least the first couple of errors, and what line they're associated with...
0
 
imladrisCommented:
OK, well, I ran it through my compiler and found the following problems. I cannot be sure these are the problems you are experiencing, however, assuming you pasted the source in (not retyped it) there are some identifiable errors.

void input_data(int *size, int square[][])


Firstly you don't appear to have intended for size to actually be a pointer. It's not passed that way in other functions, nor used that way.
Secondly, and probably most importantly in all this, you cannot pass a arbitrarily sized two dimensional array. You must tell the compiler the size of the lowest dimension so that it can do the address calculations (i.e. the compiler needs to know how long the rows are. So it should be something like:

void input_data(int size,int square[][10])

I think. This array subscript comment applies to all other such uses as well.
This line:

void add_diag(int size; const int square[][])

contains a spurious semi-colon after the size parameter.
As far as I can tell that accounts for all the errors I am getting.


0
 
XumukCommented:
The mattrope is right in all
except the first:
   
   scanf("%d",size) - is correct because
the variable 'size' is implemented like

int *size;   !!!
0
 
dbeheraCommented:
Here is a complete clean compiled code.There were lot of quotes ,commas missing.Also argument passing was not proper.

Check if it works

# include <stdio.h>



void input_data(int *size, int square[][10])

{
int i, j;

scanf("%d", size);

for(i=0; i<*size; i++)
for(j=0; j<*size; j++)
scanf("%d", &square[i][j]);

}



void add_rows(int size, const int square[][10], int row_sum[])

{
int i, j;

for(i=0; i<size; i++)
for(j=0; j<size; j++)
row_sum[i]=square[i][j]+row_sum[i];

}


void add_columns(int size, const int square[][10], int col_sum[])
{
int i, j;

for(j=0; j<size; j++)
for(i=0; i<size; i++)
col_sum[j]=square[i][j]+col_sum[j];

}

void add_diag(int size, const int square[][10],int diag_sum[2])

{
int i, j=3;


for(i=0; i<size; i++)
{
diag_sum[0]=diag_sum[0]+square[i][i];
diag_sum[1]=diag_sum[1]+square[i][j];
j--;
}
}


void compare(int size, int row_sum[], int col_sum[], int diag_sum[], int *ismagic, int *magic_num)
{
int i;

*magic_num=diag_sum[0];

if(*magic_num!=diag_sum[1])
*ismagic=0;

for(i=0; i<size; i++)
{
if(row_sum[i]!=*magic_num)
*ismagic=0;

if(col_sum[i]!=*magic_num)
*ismagic=0;
}

}

void print_square(int size, const int square[][10], int ismagic, int magic_num)

{
int i, j;

for(i=0; i<size; i++)
for(j=0; j<size; j++)
{
printf("%3d", square[i][j]);
if(j== size-1)
printf("\n");
}

if(ismagic==0)
printf("\n\n\nThis is not a magic square.\n\n");

else
{
printf("\n\n\nThis is a magic square.\n\n");
printf("The magic number is %d.", magic_num);
}

}


int main(void)

{
int size=0, ismagic=1, square[10][10], magic_num;
int row_sum[10]={0,0,0,0,0,0,0,0,0,0};
int col_sum[10]={0,0,0,0,0,0,0,0,0,0}, diag_sum[2];

input_data(&size, square);

add_rows(size, square, row_sum);

add_columns(size, square, col_sum);

add_diag(size, square, diag_sum);

compare(size, row_sum, col_sum, diag_sum, &ismagic, &magic_num);

print_square(size, square, ismagic, magic_num);

return 0;

}

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.