Diagonal Lines problem!

I have been working on this for 2 days now and cant seem to get it right. So any help would be greatly appreciated. For this problem i should be able to input a number between 1 and 10. And that number must decrement down to one using a for loop and be displayed diagonally. As for the other side it should increment upwards from 1 to the number inputted and also displayed diagonally. The periods are place holders for spaces.

Example of an output if the input was 7:
7.....1
.6...2
..5.3
...4
..5.3
.6...2
7.....1

Must be done with for-loops.Thank you

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
 #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 increments 
for(int i = 1, j = X; i > X, j > 0; ++i, --j)
cout << i << " " << j << endl;

return 0;
}
Last edited on
Why not try something like this...

1
2
3
4
5
6
7
8
9
10
11
12
13
do{
cout << "Input a positive number." << endl;
cin >> X; 
cout << endl;

if(x<=0){
cout << "Invalid entry." << endl;
}
else{
for(int i = 1,  X; i > X, j > 0; ++i, --X)
cout << i << " " << X << endl;
}
while( x <= 0);


I will not being doing this in the future, where I write out the code as I think of it, but I was typing what I was thinking bc Im tired hope it helps. By the way I can't say I completely understand what your saying
"7.....1
.6...2
..5.3
...4
..5.3
.6...2
7.....1" here.
Last edited on
Im sorry i may have made it difficult to understand, and that is what must be outputted. If i input a 7 then it should decrements down to one diagonally, and then the opposite way should be incremented from 1 to 7. That is the purpose of the 'X'. The periods are just a place holder for spaces.
Im sorry you got me on this one. Maybe set a char variable = " "; and then ... Man?.. I don't know...
it okay i understand, just i need it done asap. I am just stuck on what to do.
How are even numbers supposed to make a cross?
supa,

I thought I had solved it without an if.. else... but in fact it printed a zero value instead of a space which I can't think of a way to change to a space without using an if... else...
50001
04020
00300
04040
10005


The problem is I have an answer and I have given you all the clues needed (if you use an if.. else..) without actually giving the complete answer and thus do the assignment for you.

The question is do you want to learn c++ or just have an answer to the assignment?
There is no shame in not working it out as it is probably too hard for a beginner.
Last edited on
The inputs should only be odd numbers
Honestly i tried being a good student and try and learn it with only hints, just i really need this problem done. Just means i need to ask my tutor to go over the things im unsure. So if you wouldn't mind giving me the answer, i would greatly appreciate it.
Ok. But don't pretend you worked it out as I am sure the tutor knows about this forum.

This is the one without an if... else...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>

using namespace std;

int main()
{
    int n = 9;

    for (int j = 1; j <= n ; j++)
    {
        for (int i = n; i>0; i--){
            cout << ((i == j) |  (i == (n-j+1)))*i;
        }
        cout << endl;
    }
}


And this is the one with an if... else...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
#include <stdlib.h>     /* abs */
using namespace std;

int main()
{
int n = 7;

    for (int j = 1; j<=n;j++){
        for (int i = (n-abs(n-j-j+1))/2+1;i>1;i--){
            cout << " ";
        }
        cout << n-j+1;
        for (int i = 0;i<= (((abs(n-j-j+1)*2))/2)-1;i++){
            cout << " ";
        }
        cout << j << endl;
    }
    return 0;
}


or,

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
#include <iostream>
#include <iomanip>
using namespace std;

int main(int argc, char** argv) {
    int n = 9;
    for (int i=1;i<=n;i++){
        for (int j=n;j>0;j--){
            if (i==j)
            {
                cout << j << " ";
            }
            else
            {
                if (i-1 == n-j)
                {
                    cout << n-i+1 << " ";
                }
                else{
                    cout << "  ";
                }
              }
            }

        cout << endl << endl;
    }
    return 0;
}

Last edited on
Trust me he knows i have been asking for help. Thank you for your help!! i will see if i can get the best answer and post it on here soon. Thank you again for your help.
closed account (48T7M4Gy)
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
#include <string>
#include <iostream>

int main()
{
    int L = 0, R = 0, X = 0, temp = 0;
    std::string left = "", middle = "";
    
    std::cout << "Enter a number:\n";
    std::cin >> X;
    
    for (int i = 1; i <= X; i++)
    {
        L = X - i + 1 ;
        R = i;
        
        if ( L < R )
        {
            temp = L;
            L = R;
            R = temp;
        }
        
        left.assign( R-1,'*' );
        
        if (L == R)
            std::cout << left << L << std::endl;
        else
        {
            middle.assign( L-R-1, '*' );
            std::cout << left << L << middle << R << std::endl;
        }
    }
    return 0;
}
Topic archived. No new replies allowed.