Need help fixing problem!

I am still relatively new to c++ and I need help fixing my code so it will work properly.

The problem given:
Input a number then make an X in the following way:
for instance, if you input a 5 (odd number),
5----5
-4--4
---3
-2--2
1----1
And if you input an even number, such as 4,
1--1
-22
-33
4--4

(there should be spaces instead of -)
So far the outputs for even numbers are working just fine, but the outputs for odd numbers look more like a Y than a full X. This is my code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
  #include <iostream>

using namespace std; 
int main(int argc, char** argv) {
    //Declaration of Variables
    int num;      //number
    
    //Input values
    cout<<"Enter an integer: "<<endl;
    cin>>num;
    
    //Process values -> Map inputs to Outputs
    
    //Odd numbers
    if(num%2==1){
        for(int rows=num; rows>=1; rows--){   //rows
        for(int cols=1; cols<=rows; cols++){   //columns
            if(rows==cols||cols==(num+1)-rows)
                cout<<rows;
            else cout<<" ";
            }
            cout<<endl;
        }
    }
    
    //Even numbers
    else{
        for(int rows=1; rows<=num; rows++){   //rows
        for(int cols=1; cols<=num; cols++){   //columns
            if(rows==cols || rows+cols==(num+1))
                cout<<rows;
            else cout<<" ";
            }
           cout<<endl; 
        }
    }
    
    //Display Output
    
    
    //Exit Program
    return 0;
}
.
Last edited on
1
2
3
4
5
6
7
8
9
10
11
    //Odd numbers
    if(num%2==1){
        for(int rows=num; rows>=1; rows--){ 
        for(int cols=1; cols<=num; cols++){   //////change rows to num in this line
            if(rows==cols||cols==(num+1)-rows)
                cout<<rows;
            else cout<<" ";
            }
            cout<<endl;
        }
    }
Topic archived. No new replies allowed.