0

Problem on struct variables in c

#include<stdio.h> struct student{ int rnumber; int r2number; int r3number; }; int average=0; main ( ){ struct student raja[10][10]; struct student *p1=&raja[10][10]; for(int i=0;i<10;i++) { for(int j=0;j<10;j++) { raja[i][j].rnumber=i+j; raja[i][j].r2number=i*j; raja[i][j].r3number=i-j; } } printf("\n%d:%d:%d",raja[1][0].r2number,raja[1][0].rnumber,raja[1][0].r3number); for(int h=0;h<10;h++) { for(int y=0;y<10;y++) { average=(raja[h][y].r2number+raja[h][y].rnumber+raja[h][y].r3number)/3; raja[h][y].rnumber=average; raja[h][y].r2number=average; raja[h][y].r3number=average; } } printf("\n%d:%d:%d:%d",raja[1][0].r2number,raja[1][0].rnumber,raja[1][0].r3number,average); } Output: 0:1:1 0:0:0:33 Why did the average did not assign to struct variables

21st Mar 2023, 3:50 PM
Vennimalai raja
2 Réponses
+ 5
Vennimalai raja it did, but most likely you expected a different result because your implementation isn't the same as what you think. raj[1][0].rnum = 1 + 0; raj[1][0].r2num = 1 * 0; raj[1][0].r3num = 1 - 0; in next loop: avg= (0 + 1 + 1) / 3; // ->0 then rnum, r2num, r3num = avg finally you print them: 0:0:0 and 33 comes from last element member values (9+9 + 9*9 + 9-9) / 3 = 33 also p1 is out of boundaries.
21st Mar 2023, 6:52 PM
Tina
Tina - avatar
0
Thankyou
22nd Mar 2023, 2:08 AM
Vennimalai raja