Solved

Simple C Question

Posted on 2000-02-23
5
186 Views
Last Modified: 2010-04-15
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);

      }
0
Comment
Question by:SaraBiz
5 Comments
 
LVL 16

Expert Comment

by:imladris
ID: 2552507
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
 
LVL 16

Expert Comment

by:imladris
ID: 2552553
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
 
LVL 2

Accepted Solution

by:
mattrope earned 50 total points
ID: 2552555
  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
 

Expert Comment

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

int *size;   !!!
0
 

Expert Comment

by:dbehera
ID: 2554128
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

Featured Post

Is Your AD Toolbox Looking More Like a Toybox?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.

895 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now