So basically I am trying to create a loop that fills a matrix with random numbers. I need to make it so every column has a different range, unique to it.
//Variables
int lsx = 3;
int lsy = 10;
int lust[lsy][lsx];
int i = 0;
int l = 0;
int shpp = 7;
//List setup
for (i = 0; i < lsy; i )
{
for (l = 0; l < lsx; l )
{
lust[i][l] = 0;
}
}
while (true)
{
//List generator
for (i = 0; i < lsy; i )
{
for (l = 0; l < lsx; l )
{
//Column 1
if (i == 0)
{
lust[i][l] = rand() % flx;
cout << lust[i][l] << '\n';
}
//Column 2
if (i == 1)
{
lust[i][l] = rand() % fly;
cout << lust[i][l] << '\n';
}
//Column 3
if (i == 2)
{
lust[i][l] = rand() % shpp;
cout << lust[i][l] << '\n';
}
}
cout << "Endline reached! \n \n";
}
for (i = 0; i < lsy; i )
{
for (l = 0; l < lsx; l )
{
cout << lust[i][l] << " ";
}
cout << "\n";
}
}
}
This only generates 3 lines. Does anyone have any ideas on why this could happen? I tried changing some stuff around but only got weirder results that wouldn't fill the array in completely eitherThis is what the program displays when I try and run it
CodePudding user response:
for (l = 0; l < lsx; l )
{
Column 1
if (i == 0)
}
lust[i][l] = rand() % flx;
cout << lust[i][l] << '\n';
}
Column 2
if (i == 1)
}
lust[i][l] = rand() % fly;
cout << lust[i][l] << '\n';
}
Column 3
if (i == 2)
{
lust[i][l] = rand() % shpp;
cout << lust[i][l] << '\n';
}
}
cout << "Endline reached! \n \n";
You're using i (the line iterator) to evaluate what you're going to fill. Which means your code will only concern itself with lines 0, 1 and 2. Instead, shift that i to l - your column iterator. It should work.
Also, consider removing the while true loop. Not only is it redundant, it's also pretty dangerous considering there's no break condition - in this case it's safe, since you'll be around to shut it down, but as a good practice stay out of while(true) unless you can't write your break condition as a boolean expression