Simple C programming issuse Please help urgent!
#include <stdio.h> #include <stdlib.h> #include <time.h> #include <string.h> void sum_row1(int matrix[30][30], int *sum1); void sum_diagonal(int matrix[][30], double *sumd, double *y); int main(void) { int matrix[30][30], matrix_null[30][30]; int i, j, k=0, sum1, count_num_zero=0, count=0; double sumd, avg_sumd; srand(time(NULL)); for(k=0;k<3;k++) { for(i=0;i<30;i++) {for(j=0; j<30 ;j++) {matrix[i][j]= (rand()%10);} } sum_diagonal(matrix, &sumd, &avg_sumd); printf("%lf\n", avg_sumd); if (avg_sumd<7) {for(i=0; i<30;i++) {for(j=0; j<30;j++) matrix[i][j]= 0; } } else { for(i=0; i<30;i++) {for(j=0; j<30;j++) matrix[i][j]= matrix[i][j]; } for(i=0;i<30;i++) {for(j=0; j<30 ;j++) printf("%d ", matrix[i][j]); printf("\n"); } } avg_sumd=0; } return(0); } void sum_diagonal(int matrix[][30], double *sumd, double *y) { int i, j; *y=0; for(i=0;i<30;i++) for(j=i ;j!=i+1; j++) {*sumd+=matrix[i][j]; } *y= *sumd/30; }
I want a program to keep generating that matrix, and keep calculating a new average for its diagonal, but this code keeps writing over the old average of the diagonal... could someone help me fix this bug, its driving me crazyyyyyyyyyyyyyyyyyyyyyy
@Mertsj @hartnn
@ParthKohli
@rajathsbhat @timo86m @thomaster @Callisto
Mercy, I don't know C.
:( ok thanks
do you know anyone that knows C?
I'm not sure
you need to create a new array?
In your function, sum_diagonal, you don't need the 2nd for loop. for(i=0;i<30;i++) for(j=i ;j!=i+1; j++) { *sumd+=matrix[i][j]; } This code will produce the diagonal: for(i=0;i<30;i++) *sumd+=matrix[i][i]; The specific problem you're having, though, is that you're storing avg_sumd in the same variable. Instead, replace avg_sumd with avg_sumd[k] - the declaration should be avg_sumd[3].
Join our real-time social learning platform and learn together with your friends!