tags:

views:

74

answers:

4

First, sorry for my poor language. I'm using VC++ Express myself.

Now I'm studying about arrangements. The book gave me a project like so:

  • Make a 5x5 matrix.
  • Each column is for subjects (4 of them)
  • Each row is for students (same, 4 of them)
  • Each cell saves a score.
  • At the end of each row/column, sum the row/column.

alt text

And this is my answer program:

int main(void)
{
int arr[5][5];
int i,j;

while(1)
{
 printf("student: 1.Jim,2.Jombi,3.Joly,4.Moran if you done, type 0\n");
 scanf("%d", &i);

 if(i=0)
  break;

 printf("subject: 1.english,2.spanish,3.poolish,4.flash\n");
 scanf("%d", &j);

 printf("insult score!\n");
 scanf("%d", arr[i-1][j-1]);//insulting score
}

for(i=0;i<4;i++)//initialization of sum parts
{
 arr[i][4]=0;
 arr[4][i]=0;
}

for(i=0;i<4;i++)
 for(j=0;j<4;j++)
  arr[4][i]+=arr[j][i];

for(i=0;i<4;i++)
 for(j=0;j<4;j++)
  arr[i][4]+=arr[i][j];


for(i=0;i<5;i++)
{
 for(j=0;j<5;j++)
  printf("%d ",arr[i][j]);//printing result
 printf("\n");
}
return 0;
}

I completely don't know why this code doesn't work. Even when I tried only "while" part, it didn't work also. Why is this?

+1  A: 
if(i=0)

I don't know if this is the source of your problem, but this should be:

if(i==0)
Konamiman
hm. i tried right after saw your answar/ but still, it doesn't work
Rhee
A: 

The index in your last for loop is one-off. Should be 4, you use 5.

Alexander Pogrebnyak
that's just for printing entire arrangement. hm.. i think so arr[0][0]to arr[4][4]
Rhee
But you've got `i<4` as the exit condition, so the loop never gets run for `i==4`. You could use `i<5` or `i<=4`
Mark Pim
i think there are two kind of 'for'partsone- to insult data -case i need just arr[0][0]to arr[3][3]and two- print whole data- case i need arr[0][0]to arr[4][4]
Rhee
-1 It is correct to use i<5 to loop through 0, 1, 2, 3 and 4 for the result.
Ewan Todd
A: 

A couple of points:

  • You're missing the & (address-of) operator in your scanf() call, so you'll pass the integer in the array instead.
  • Consider just doing a simple memset(arr, 0, sizeof arr); before the input loop to make sure the array is clear, rather than the nested fors to zero out just the sums. Safer, shorter, easier to understand.
  • Add instrumentation, such as printing the matrix after the input step, to make sure it looks alright.
unwind
quote "memset(arr, 0, sizeof arr);" means memset(matrixname, initialization, sizeof arr matrix)?I should try this part. thx!
Rhee
+2  A: 

As has been pointed, you have an if wrong

if (i == 0)
/*   ^^^ */

And you have a scanf wrong too

scanf("%d", &arr[i-1][j-1]);
/*         ^^^ */

And you really, Really, REALLY shoud initialize the array with something (possibly zeroes)

int arr[5][5] = {0};
pmg
int arr[5][5] = {0}; can this code initialize whole matrix to 0?
Rhee
oh, the second stuff you pointed were my major mistake.it works now! thx!
Rhee
This, together with stdio.h and main() (both assumed) has all that I found. I, too, didn't know about {0} for initialization. I would have gone for memset or simply looping through.
Ewan Todd
donno ewan ill try and post again!
Rhee
Yes, `arr[5][5] = {0};` will initialize all 25 elements to zero (your compiler might complain because `arr` elements are arrays themselves and the "proper" _compact_ initialization should be `= {{0}};`, **but the initializer I used works by definition**).
pmg
yeah it works! and i found that if make arr[5][5]={1,2}; it initialize whole matrix to 0 except [0][0],[0][1].
Rhee