Need help fixing a problem!

I am still relatively new to learning 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:

#include <iostream> //Input/Output objects
using namespace std; //Name-space used in the System Library

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;
}
}

//Exit Program
return 0;
Last edited on
Duplicate post waist time and effort.

http://www.cplusplus.com/forum/beginner/200472/

Topic archived. No new replies allowed.