Apr 18, 2016 at 3:42pm UTC
Hear are 4 code i wrote but in order to get the output i wanted, i changed the codes into statements which isnt a nested loop and i cant figure out how or what im doing wrong please help.
1.output
* * * * *
* * * * *
* * * * *
* * * * *
my code:
#include <iostream>
#include<conio.h>
using namespace std;
int main()
{
int row, column;
for (row = 0; row < 4; row++)
{
for (column = 0; column < 1; column++)
{
cout << "*****";
}
cout << endl;
}
system("pause");
return 0;
}
2.output
5 4 3 2 1
5 4 3 2 1
5 4 3 2 1
my code:
#include <iostream>
#include<conio.h>
using namespace std;
int main()
{
int row, column;
for (row = 0; row < 3; row++)
{
for (column = 0; column < 1; column++)
{
cout << "5 4 3 2 1";
}
cout << endl;
}
system("pause");
return 0;
}
3.output
8 7 6 5 4 3
8 7 6 5 4 3
my code:
#include <iostream>
#include<conio.h>
using namespace std;
int main()
{
int row, column;
for (row = 0; row < 2; row++)
{
for (column = 0; column < 1; column++)
{
cout << "8 7 6 5 4 3";
}
cout << endl;
}
system("pause");
return 0;
}
4.output
5 5 5 5 5 5
4 4 4 4 4 4
3 3 3 3 3 3
2 2 2 2 2 2
my code:
#include <iostream>
#include<conio.h>
using namespace std;
int main()
{
int row, column;
for (row = 0; row < 1; row++)
{
for (column = 0; column < 1; column++)
{
cout << "\n 5 5 5 5 5 5 \n 4 4 4 4 4 4 \n 3 3 3 3 3 3 \n 2 2 2 2 2 2";
}
cout << endl;
}
system("pause");
return 0;
}
If anyone has any helpful input to at least one of my codes that would be so great! I honestly really need help with nested loops
Apr 18, 2016 at 4:13pm UTC
@ashley21466
For the first code, you don't need a nested loop. You're already print one whole row.
1 2 3 4 5 6 7 8 9
for (row = 0; row < 4; row++)
{
for (column = 0; column < 1; column++)
{
cout << "*****" ;
}
cout << endl;
}
To use a nested loop to get your result, you would do..
1 2 3 4 5 6 7 8
for (row = 0; row < 4; row++)
{
for (column = 0; column < 5; column++)
{
cout << "*" ;
}
cout << endl;
}
And for section two..
1 2 3 4 5 6 7 8
for (row = 0; row < 4; row++)
{
for (column = 5; column >0; column--) // Counts down
{
cout << column; // Prints out the value of column
}
cout << endl;
}
This help out enough to understand nested loops. Try the other ones, and come back here, if you need more help.
Please use code tags though. Hilite the coding, and click on the <> symbol on the right, under the word 'Format:'
Last edited on Apr 18, 2016 at 4:14pm UTC
Apr 18, 2016 at 4:42pm UTC
Thank you so much for your help