Hello, I'm not asking anyone to write the code for me but maybe someone can give me some pointers on getting started with this assignment? The syntax is giving me fits.
Here are the directions: Write a program using for loops to create the figure below.
Think about the relationship between the line number and the number of each of characters in each line.
Hint: You can use either an incrementing loop and subtract, or a decrementing loop for the outer loop.
#include <iostream>
usingnamespace std;
int main ()
{
int printcount;
int symcount=0;
int linecount;
char symbol;
char print;
for (linecount=1;linecount<=7; linecount++)
{
for (symbol='<'; printcount=(7-linecount);printcount++)
cout<<symbol<<endl;
}
return 0;
}
I realize I've got some variables in there I probably don't need.
You've got the right idea on the outer loop, although it will execute 7 times (there are only 6 lines).
The inner loop has a couple of problems.
1) The first expression in a for loop is meant to initialize the loop count. You're initializing symbol instead.
2) Your endl is inside your inner loop, so each symbol will print on a separate line.
Now, think about adding the #, * and > symbols on each line.
#include <iostream>
usingnamespace std;
int main ()
{
int printcount;
int linecount;
for (linecount=1;linecount<=7; linecount++)
{
for (printcount=1; printcount<=(7-linecount);printcount++)
cout<<"<";
cout<<endl;
}
}
What happens to the # symbol? It goes from 12, 10, 8, down to 0.
You want another loop, like the existing inner loop, that outputs two # symbols each time.
The asterisks are a little trickier. They go 0, 2, 5, 10, 13, 15 (assuming the example in the OP is transcribed correctly.
int main ()
{
int printcount;
int linecount;
for (linecount=1;linecount<=7; linecount++)
{
for (printcount=0; printcount<=(6-linecount);printcount++)
cout<<"<";
for (printcount=0; printcount<=(linecount);printcount++)
cout<<" ";
for (printcount=0;printcount<(14-(2*linecount));printcount++)
cout<<"#";
for (printcount=0;printcount<((linecount*2)-2);printcount++)
cout<<"*";
for (printcount=0; printcount<=(linecount);printcount++)
cout<<" ";
for (printcount=0; printcount<=(6-linecount);printcount++)
cout<<">";
cout<<endl;
}
}
I have a question though. You mentioned earlier there are only 6 lines, but there are 7;but I couldn't get the output with the "<" and the ">" symbols correct until I changed "linecount" to 6 like you suggested. Is that because those symbols only appeared on 6 of the 7 lines? Dumb question, I know :)