Answer to Question #16472 in C++ for jhon

Question #16472
Write a C++ program that does the following:

1. Asks the user to enter an odd positive integer m.

2. Reads m from the user. If the value is not legal, the program repeatedly makes the user type in another value until a legal value of m has been entered.

3. Prints an m x m pattern in the shape of an asterisk. The pattern should appear as a large O printed from copies of the letter O that lies over a large - printed from copies of the character -.
Example:
0 - 0
0 - 0
0 - 0
0 - 0
-----0----
0 - 0
0 - 0
0 - 0
0 - 0

How am i suppose to get the program to do this? I know im supposed to use nested for loops but how am i suppose to get the 0 and - in the lines i need?
1
Expert's answer
2012-10-16T09:01:11-0400
#include <iostream>
using namespace std;

int main (){
int m;
cout<<"Enter m\n";
cin>>m;

int j = 0;
for (int i = 0 ; i < 2*m ; i++){
& if (i < m){
& cout.width(i); cout<<"o"; cout.width(m-i+1); cout<<"-"; cout.width(m-i+1); cout<<"o"; cout<<endl;
& } else if (i > m) {
& cout.width(m*2-i); cout<<"o"; cout.width(i-m+1); cout<<"-"; cout.width(i-m+1); cout<<"o"; cout<<endl;
& } else {
& for (int i = 0 ; i < m ; i++) cout<<'-';
& cout<<"o";
& for (int i = 0 ; i < m ; i++) cout<<'-';
& cout<<endl;
& }
}

system("pause");
return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
APPROVED BY CLIENTS