I did this pattern with just 2 for loops and some if-statements in between. The entire function is about 15 lines of code only. I'd be happy to help out if you show your attempt.
Your inner j loop puts out enough stars to give a row of length i (which is correct).
Your outer i loop runs from 1 to rows, increasing by 1 each time (also correct).
This gives the upper part of the figure (correct).
Now, all you need to do is repeat with a similar nesting to get the lower part. The inner loop (on j) won't change. What matters is the length of the row, which is set by the i loop.
This time you will START at i=rows-1 (not 1).
This time you will END at i=1 (not rows). Remember also that you will be looping whilst >=, not <=.
This time you will decrement i each time: simply use --i, rather than ++i.
Can you see how to change the for loop involving i so as to do this?
Could you possibly use code tags on your snippets of code, please. It's the first item in the format menu to the right; i.e. select your code and press the <> button.
Hi, I remember having to do these applications in HighSchool when I started taking C++ courses, they can be a little frustrating but you'll grow to like them soon hopefully. Anyway, here's how I created the pattern:
#include <iostream>
int main()
{
int h = 0;
for (int i = -5; i <= 5; ++i)
{
h = i;
if (h < 0)
h *= -1;
for (int j = 0; j <= 5; ++j)
{
if (h <= j)
std::cout << "* ";
}
std::cout << "\n";
}
int iTemp = 0;
std::cin >> iTemp;
return 0;
}
I am using only two for loops to create your star pattern. The first for-loop handles the number of rows. The inner for-loop handles the columns. I am using a small logic with variable h to accomplish the pattern.
Like I said, the first for-loop handles the number of rows while the inner for-loop handles the number of columns. Simply change the value 5 to whatever you want.
int main()
{
int h = 0;
int number = 0;
std::cin >> number;
for (int i = -number; i <= number; ++i)
{
h = i;
if (h < 0)
h *= -1;
for (int j = 0; j <= number; ++j)
{
if (h <= j)
std::cout << "* ";
}
std::cout << "\n";
}
int iTemp = 0;
std::cin >> iTemp;
return 0;
}