Hello all,

I am having issues with a homework assignment. My assignment is as follows:
Write a program that uses a loop to display Pattern A below, followed by another loop that displays Pattern B.

Pattern A:

+
++
+++
++++
+++++
++++++
+++++++
++++++++
+++++++++
++++++++++

Pattern B:

++++++++++
+++++++++
++++++++
+++++++
++++++
+++++
++++
+++
++
+

So the above is how the program is supposed to be displayed, but the program I wrote, just basically has Pattern B configured exactly like Pattern A. I am completely stumped and do not know where to go from here, so if I could please get a hint in the right direction, I would greatly appreciate it. Thanks!

//Pattern Displays.
#include<iostream>
using namespace std;

int main()
{
    const int columns=9;
    const int rows=10;

    const int height=10;

    cout<<endl<<"============"<<endl;
    cout<<endl<<" Pattern A:"<<endl;
    cout<<endl<<"============"<<endl;
    for(int rows=0; rows<height; rows++)
    {
        for(int a=0; a<1*rows+1;a++)
            cout<<"+";
        cout<<endl;
    }
    cout<<endl<<"=============="<<endl;
    cout<<endl<<" Pattern B:"<<endl;
    cout<<endl<<"=============="<<endl;
    for(int rows=0; height>rows; rows++)
    {
        for(int b=0; b<1*rows+1;b++)
            cout<<"+";
        cout<<endl;
    }
    system("pause");
    return 0;
}

Dani AI

Generated

Nice progress here. Two issues tripped you up early on: (1) shadowing names and (2) using a constant upper bound for the inner loop. In your first attempts you had const int rows = 10; and then also for (int rows = 0; ...), which compiles but hides the outer rows and makes the code harder to reason about. And in the later revision, the inner loop for Pattern B used a constant like 10*columns-1, so each line printed the same number of + characters regardless of the current row. As hinted, the simplest fix for Pattern B is to count down. Also note ’s header for(int b=10,b>0,b--) has commas where semicolons belong; the correct form is for (int b = 10; b > 0; --b).

A compact approach that avoids off-by-one mistakes is to build each line from a string of the desired length. This keeps the loop logic focused on how many characters you want, not how to print them one by one:

#include <iostream>
#include <string>

int main() {
    const int n = 10;

    std::cout << "Pattern A:\n";
    for (int i = 1; i <= n; ++i)
        std::cout << std::string(i, '+') << '\n';

    std::cout << "\nPattern B:\n";
    for (int i = n; i >= 1; --i)
        std::cout << std::string(i, '+') << '\n';
}

Troubleshooting checklist tied to this thread:

  • Use distinct names (rows, cols, width) to avoid shadowing.
  • For descending patterns, either start at n and decrement, or compute width = n - row.
  • Prefer '\n' over std::endl for speed and clarity.
  • system("pause") is Windows-only; for a pause, read a dummy character with std::cin.get() instead.

Recommended Answers

All 6 Replies

Instead of starting at zero and incrementing try starting at columns value and decrementing to zero..

Hi Gerard 4143,
Thanks for responding, I'm still lost. It's my assumption that my line 26 that starts with for(int b=1..etc) is going to be the lets say "action line" for lack of a better word right now.
I did a revision to my program from what I though you were hinting at, but now every odd number line (under pattern B only)has an infinite number of characters, and every even number line (under pattern B only) has exactly 8 characters. I'm very confused about the logic of this so maybe explaining that would help me a little more.

//Pattern Displays.
#include<iostream>
using namespace std;

int main()
{
    const int columns=9;
    const int rows=10;

    const int height=10;

    cout<<endl<<"============"<<endl;
    cout<<endl<<" Pattern A:"<<endl;
    cout<<endl<<"============"<<endl;
    for(int rows=0; rows<height; rows++)
    {
        for(int a=0; a<1*rows+1;a++)
            cout<<"+";
        cout<<endl;
    }
    cout<<endl<<"=============="<<endl;
    cout<<endl<<" Pattern B:"<<endl;
    cout<<endl<<"=============="<<endl;
    for(int rows=0; rows<height; rows++)
    {
        for(int b=1; b<10*columns-1;b++)
            cout<<"+";
        cout<<endl;
    }
    system("pause");
    return 0;
}

Hi Gerard 4143,
Thanks for responding, I'm still lost. It's my assumption that my line 26 that starts with for(int b=1..etc) is going to be the lets say "action line" for lack of a better word right now.
I did a revision to my program from what I though you were hinting at, but now every odd number line (under pattern B only)has an infinite number of characters, and every even number line (under pattern B only) has exactly 8 characters. I'm very confused about the logic of this so maybe explaining that would help me a little more.

//Pattern Displays.
#include<iostream>
using namespace std;

int main()
{
const int columns=9;
const int rows=10;

const int height=10;

cout<<endl<<"============"<<endl;
cout<<endl<<" Pattern A:"<<endl;
cout<<endl<<"============"<<endl;
for(int rows=0; rows<height; rows++)
{
for(int a=0; a<1*rows+1;a++)
cout<<"+";
cout<<endl;
}
cout<<endl<<"=============="<<endl;
cout<<endl<<" Pattern B:"<<endl;
cout<<endl<<"=============="<<endl;
for(int rows=0; rows<height; rows++)
{
for(int b=1; b<10*columns-1;b++)
cout<<"+";
cout<<endl;
}
system("pause");
return 0;
}

for(int b=10,b>0,b--) {
.....
}

for(int b=10,b>0,b--) {
.....
}

Thank you both so much for your responses, I really appreciate the help. I had to slightly rework my program, but the result was successful! So thanks again!!!!

//Pattern Displays.
#include<iostream>
using namespace std;

int main()
{
    int col=10;
    int row=10;

    cout<<endl<<"Pattern A:"<<endl;
    for(int row=0; row<10; row++)
    {
        for(int col=0; col<row;col++)
            cout<<"+";
        cout<<endl;
    }
    cout<<endl<<"Pattern B:"<<endl;
    for(int row=10; row>=1; row--)
    {
        for(int col=1; col<row;col++)
            cout<<"+";
        cout<<endl;
    }
    system("pause");
    return 0;
}
      *
   *    *
*     *     *   
*     *     *
   *     *
      *

Please help me! Thanks

This thread has already been answered. Please start a new thread with your question. I strongly suggest you show us what you have done to try to solve the problem yourself. You can't expect us to show any effort if you haven't. You may want to read the Daniweb Posting Rules as well as Read This Before Posting a Question to increase your chances of getting a timely (and more useful) response.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.