I’m trying to do a program with matrix. In four numbers, only the first less 1. When the first matrix finish, just the second matris less 1. And goes by to the end. The 0 number is not allowed.
For example, when I run the code, need to return this:
[2][1][3][3]
[1][1][3][3]
[1][3][3]
[3][3]
[2][3]
[1][3]
[3]
[2]
[1]
But that is not happening with this code:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main()
{
int fila[4], var;
for (var=0;var<4;var++) {
fila[var]=0;
srand(rand());
fila[var]=rand()%3+1;
printf("[%d]",fila[var]);
fila[0]=fila[0]-1;
}
system("pause>NULL");
while(fila[0]>0)
{
printf("[%d][%d][%d][%d]\n",fila[0],fila[1],fila[2],fila[3]);
fila[0]--;
system("pause");
}
fila[1]=fila[1]-1;
while(fila[1]>0)
{
printf("[%d][%d][%d]\n",fila[1],fila[2],fila[3]);
fila[1]--;
system("pause");
}
fila[2]=fila[2]-1;
while(fila[2]>0)
{
printf("[%d][%d]\n",fila[2],fila[3]);
fila[2]--;
system("pause");
}
fila[3]=fila[3]-1;
while(fila[3]>0)
{
printf("[%d]\n",fila[3]);
fila[3]--;
system("pause");
}
return 0;
}
What I’m missing?
Thanks.
That code prints:
[2][1][2][1]
[1][1]
You need two nested loops to do it:
The outer loop iterates until you get to the end of
fila; inner loop prints the remaining elements offilastarting at the index ofi.Note that you shouldn’t be re-seeding
srandin a loop – you do it only once before the loop.Here is the link to this program on ideone.