So I have this lab to do for class and I'm stuck on it. It has to do with nested loops, and it needs to output rows that are equal to the number that you put in. Each row has to output a differnt number of "." and "*" based on what number you put in. A correct one should be
....*
...**
..***
.****
*****
for the number five however I'm not so lucky. I have the nested loop set up but I have no idea how to get started on getting it to output like that mine simply outputs
.*
.*
.*
.*
.*
for the number 5 of course I know thats because of how I have the output message set up I just don't really know where to get started. Any help would be awesome its due tuesday and I've been working on it all weekend. *mostly reading the text book haha*
Here is my code
#include <iostream>
using namespace std;
int main()
{
int num;
int count;
cout << "Enter number of rows: " << endl;
cin >> num;
for (count=0;count<num;count++)
for(count=0;count<num;count++)
{cout << ".";
cout << "*" << endl;
if (count==num) break;
}
Try this :) You just needed to add a conditional check to check to see if '*' should be printed or '.'. Also I would avoid using the same variable as the conditional variable in two nested loops. That can be dangerous and confusing!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
#include <iostream>
usingnamespace std;
int main(){
int num;
cout << "Enter number of rows: " << endl;
cin >> num;
for(int i = 0; i <= num; i++){
for(int j = 0; j <= num; j++){
if(j >= num - i)
cout << "*";
else
cout << ".";
}
cout << endl;
}
}
Thats extremely close. I didn't even think about using an IF I feel even more noob now lol. The only problem with the output is that it has one extra character. The output ends up being
.....*
....**
...***
..****
.*****
******
Where is our instructions are it has to have the max characters of the number put in.
so it would be like
....*
...**
..***
.****
*****
Thats pretty impressive how you got it to do it so quickly I've been looking at this text book for hours and it just never occured to me to ALSO use a IF haha.