Design Using a For-Loop

Input the number than make an X in the following way,
for instance, when you input a 5 you output

5 1
4 2
3
4 2
5 1

when you input a 7 you output

7 1
6 2
5 3
4
5 3
6 2
7 1

I have two codes at the moment: one that makes the X shape but not incremented and another that increments the numbers just vertically in two rows. Any suggestions on how to do this? or incorporate the two codes together to solve the problem?? Thanks!


#include <iostream>

using namespace std;

int main(int argc, char** argv) {
//Declare Variables
int X;

cout << "Input a positive number." << endl;
cin >> X;
cout << endl;

//Input Validation
while (X<0) {
cout << "Invalid entry. Enter a number greater than 0." << endl;
cin >> X ;
}

//This section makes the X
for (int r=1; r<=X; r++) {
for (int c=1; c<=X; c++){
if(r==c||c==(X+1)-r) cout << X;
else cout << " ";
}
cout << endl;
}

//This section increments
for(int i = 1, j = X; i > X, j > 0; ++i, --j)
cout << i << " " << j << endl;

return 0;
}
First make sure that the user entered an odd number -> (X % 2) != 0

You need two not nested loops. Each 0 -> X/2

First print: X - i << " " << i
Second print: X/2 + 1 + i << " " << X/2 - 1 - i

Between the first and the second loop you need to print of course the center number.
Last edited on
Topic archived. No new replies allowed.