دنبال کننده ها

۱۳۹۶ دی ۱۲, سه‌شنبه

Why does c++ print a different array the second time it's printed?

[ad_1]



My cousin has a school project and we can't figure out why is the array different the second time it's printed when there is no values changing in between?



Basically you enter a number which states how many rows/columns will the matrix have, and during first loop he assigns a number to every position and prints out the random number. However, the second time we go through the matrix the numbers are different and it seems that they are copied through the matrix from bottom left corner to top right corner for some reason. It seems strange to us because we never assign a different value to a position in the array after defining it for the first time.



int i,j,n,matrica[i][j],suma=0;
srand(time(NULL));
printf("nunesi prirodan broj N[3,20] = n");
scanf("%d",&n);

for(i=0;i<n;i++)
for(j=0;j<n;j++)
matrica[i][j]=rand()%100;
printf("%d, %d = %4d ",i, j, matrica[i][j]);

if(j==n-1)
printf("n");




printf("n");

for(i=0;i<n;i++)
for(j=0;j<n;j++)
printf("%d, %d = %4d ", i, j, matrica[i][j]);

if(j==n-1)
printf("n");





And here is the result of this (the code I pasted here has 2 prints, and in the image there is 3 but every time you go through the matrix after the first time it's going to be the same):



enter image description here




[ad_2]

لینک منبع