0

someone helps how can i make two for loops over a 2d array in c ( i didn't learned pointers yet )

i did not understand why score [j][i] instead of score[i][j] in Sb function .Scondly i would like to know also why shall declare intialite the array named Total before the for loop and in the for loop . Finally , in the same function i would like to understand why the use of the final for loop and why we don't print the total in the first loop . #include<stdio.h> #include<cs50.h> void SB(int n,int m, int score[][100]); void remplir_mat(int n,int m, int score[][100]); int main(void){ int N=get_int("Enter players numbers:");int M= get_int(" Enter rounds number:"); int score[100][100]; remplir_mat(N,M,score); SB(N,M,score); } void SB(int n,int m, int score[][100]){ printf("SB func"); int Total[10]={0} ; for(int i=0;i<m;i++){ Total[i]=0; for(int j=0;j<n;j++){ Total[i]=Total[i]+score[j][i]; //printf("%d",Total[i]); } } for(int j=0;j<n;j++){ printf("%d",Total[j]); } } void remplir_mat(int n,int m, int score[][100]){ for(int i=0;i<m;i++){ for(int j=0;j<n;j++){ score[i][j]=get_int("enter the score of the player %i in the round %i is:%i ",i,j,score[i][j]); printf("%i\n ",score[i][j]); } } }

31st Jul 2025, 8:39 AM
Jalel Khadraoui
Jalel Khadraoui - avatar
2 Respostas
+ 1
If you use score[i][j] your function will go through this array line by line, otherwise (score[j][i]) will go through it column by column. You don't need inicialize the Total two times, just one.
31st Jul 2025, 2:18 PM
Nielson Rodrigues Pereira
Nielson Rodrigues Pereira - avatar
31st Jul 2025, 2:30 PM
Nielson Rodrigues Pereira
Nielson Rodrigues Pereira - avatar