Solved

# Simple C Question

Posted on 2000-02-23
194 Views
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);

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

print_square(size, square, ismagic, magic_num);

}
0
Question by:SaraBiz
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points

LVL 16

Expert Comment

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

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

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

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

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);

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

print_square(size, square, ismagic, magic_num);

return 0;

}

0

## Featured Post

Question has a verified solution.

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

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.
###### Suggested Courses
Course of the Month2 days, 12 hours left to enroll