Design a C++ program by considering the following conditions;
1. User must keyed-in “start” to start the program and “stop” to 
end the program.
.
2. If the user keyed-in “choice1”, print out PATTERN A.
3. If the user keyed-in “choice2”, print out PATTERN B.
Use if else statement, while loop and nested for loop in your program.
PATTERN A PATTERN B
X X X X X
  X
    X
      X
X X X X X
X X X X X
      X
    X
  X
X X X X X

i really need help for this..

Recommended Answers

All 5 Replies

What do you have so far? We only give homework help to those that show effort.

I'm trying for the whole day and can't even reach to get the pattern. only the pattern, not the whole question.

That still doesnt show us what you have. Even if it doesnt compile show us what you are doing and we can help to get it to work.

#include <iostream>
#include <string>

using namespace std;

int main() 
{



    for (int i = 1; i <= 5; i++){
        for (int j = 1; j <= 5; j++)
        {
            if ((i >= j) && (i<=j))
            {
                cout << "  ";
            }
            else
            {
                cout << "X ";

            }
        }
        cout << endl;
    }
    cout << endl;
    system("pause");
}

I can't get the Z pattern afterall, i tried editing and get many-many pattern but not Z. And, i'm still not fully understand about nested loop.

Okay, lets break this down a little. If we want to print the backwards Z and we have a nested for loop like you have what are the conditions of i and j that would print an X

   jjjjj
   01234
i0 XXXXX
i1  X      
i2   X    
i3    X  
i4 XXXXX

From the above picture we can see that if i == 0 or i == 4 print X so that coves the top and bottom rows. Now to get the diagonal you can see that i and j equal each other where there is an X so that would be if i == j print X. So to pat that all together we get (this is pusedo code):

for i = 0 to i == 4
    for j = 0 to j == 4
        if i == 0  or i == 4 print "X"
        else if i == j print "X"
        else print " "

For printing the normal Z the first if is still code but wee need to change the second one.

   jjjjj
   01234
i0 XXXXX
i1    X    
i2   X    
i3  X    
i4 XXXXX

From the above we can see a patter amerge with when to print an X. The X's are at (i, j): (1, 3) , (2, 2) , (3, 1). If you add the pairs for each one of those you get 4 so you would have an if like if i + j == 4 print X and the code would look like (this is pusedo code):

for i = 0 to i == 4
    for j = 0 to j == 4
        if i == 0  or i == 4 print "X"
        else if i + j == 4 print X
        else print " "
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.