Displaying Diagonal Lines that intersect using for-loops

Pages: 12
Hello, in my assignment i need to 1)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

Use for-loops.
I managed to get the first diagonal line but i am having trouble doing the second one, i have tried many different ways but i feel like i am just confusing myself, any tips would be appreciated.

#include <iostream>
#include <iomanip>
using namespace std;

int main(int argc, char** argv) {
int num;
cout<<"Input any number: ";
cin>>num;
for(int count=1;count<=num;count++){
for(int space=2;space<=count;space++){
cout<<" ";
}
cout<<setw(10);
cout<<count;
cout<<" "<<endl;
}
return 0;
}

Last edited on
Well it didn't take long to solve that one :)
Hint: As you print a line how would you decide if to print - or a particular number?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#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=1;j<=n;j++)
        {
            cout << " - ";    // how to decide when to print what number instead of a -?

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

Last edited on
Hello, maybe i didn't make myself clear. The output should be an 'X' for example 54321 in a diagonal and another going 12345 the opposite way to make an 'X'. Using for loops and if any other character is inputted then it would be outputted as a ?
Oops didn't notice the reversal.
You need to decrement the inner loop.
The printout below may give another hint of how I did it?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#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--){
              cout << i << " " << j << " : ";
            }

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

Last edited on
I'm sorry im still not getting what i want. This is what its outputting-

1 9 : 1 8 : 1 7 : 1 6 : 1 5 : 1 4 : 1 3 : 1 2 :

2 9 : 2 8 : 2 7 : 2 6 : 2 5 : 2 4 : 2 3 : 2 2 :

3 9 : 3 8 : 3 7 : 3 6 : 3 5 : 3 4 : 3 3 : 3 2 :

4 9 : 4 8 : 4 7 : 4 6 : 4 5 : 4 4 : 4 3 : 4 2 :

5 9 : 5 8 : 5 7 : 5 6 : 5 5 : 5 4 : 5 3 : 5 2 :

6 9 : 6 8 : 6 7 : 6 6 : 6 5 : 6 4 : 6 3 : 6 2 :

7 9 : 7 8 : 7 7 : 7 6 : 7 5 : 7 4 : 7 3 : 7 2 :

8 9 : 8 8 : 8 7 : 8 6 : 8 5 : 8 4 : 8 3 : 8 2 :

9 9 : 9 8 : 9 7 : 9 6 : 9 5 : 9 4 : 9 3 : 9 2 :
My first post i believe was closer, but i did try ur way and implement it into mine just still having trouble :(
Well I haven't given you the answer that would spoil the fun of figuring it out yourself.
Another hint: Look at the printout. See the numbers going 9 to 1 along the rows and 1 to 9 along the columns. How do you only print the number you want or a '-" instead of both numbers?
I know haha I am trying to figure it out without you giving me the answer.Well i want 2 numbers on the same row and it should be in the shape of an 'X'. Wouldn't i use a for loop to increment a space that adds on each time? Like for(space=1;space<number;space++)
cout<<" ";
Last edited on

My suggestion was to print a n x n array where each cell may be a number or a space.

There are probably many ways of solving the problem. Don't get stuck on one way of looking at the problem. Your task is to work out how you would do it step by step and then how to convert those steps into instructions the computer can execute.

Another hint:
As you print the array how would you use i, j and n to decide what number to print or if to simply print a space?


Last edited on
I just learned arrays today haha, but from my knowledge i know how to access them and store information just how would i have the user input a number or letter then convert it. To me an array brings a whole new list of problems. im sorry im a newbie
Wait!!! looking back at my output it works diagonally just how do we clear all the other numbers besides the diagonal ones.
What I meant was print an array not create an array and that is what the two loops in the above program do.

My impression is you don't know enough to solve the problem?

Within the loop you need to make decisions as to what to print and for that you use the if statement.

Your right i don't know too much, just my teacher wanted us not to use a whole list of if statements we were told its possible with for loops.
Ok. That is interesting. I will think about that one see if I can do it as well.
Thank you for trying to help me, this is due in 2 days haha.
whole list of if statements

The line 9 in CodeWriter's program requires only a three-branch if .. else if .. else ... Does that warrant the "whole list" moniker?


The "use for loops" probably means using two for-loops to print one line.
The first loop will print required number of '-' before the first digit.
Then you print the digit.
The second loop will print required number of '-' before the second digit.
Then you print the digit.
Line is complete.

Outer loop repeats the above block for each line.


In CodeWriter's version your math has to resolve two things: which "cells of 2D array" should show digits, and what are those digits.

In "loops, no ifs" version your math has to resolve two things too: how many '-' before each digit, and what are those digits.



(I do assume that the input number is always less than ten. The "alignment" gets unnecessarily "interesting", when numbers can have more than one digit.)
I like your explanation! Just question how would i get the v space in between the top of the X. So that it doesn't create a large space in between.

And yes the number inputed will always be less than 10.
suppa,
After some effort this was the best I could do without using any if statements.
My problem was the center number has to be printed in the same place.
This was the best I could do with a single loop...

7......1
.6....2
..5..3
...44
..3..5
.2....6
1......7


The spaces at the front have to increase and then decrease while the middle spaces have to do the inverse. As a hint which may or may not be useful the code below illustrates the equation I used to do this. It seems to me the number n must be an odd number 3,5,7, or 9.

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

int main()
{
int n = 9;

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


I would be interested to see what solution there is without any if statements.

Last edited on
Thank you for your help CodeWriter!! just feel like we are missing something but ill let you know if i get it. This is what i have so far that i believe is pretty close.
#include <iostream>

using namespace std;

int main(int argc, char** argv) {

int num;
cout<<"Input any number: ";
cin>>num;

for(int row=1;row<=num;row++){
for(int col=1;col<=num;col++){
for(int counter=1;counter<=num;counter++)
if(row==col || col==(num+1)-row)cout<<counter;
else cout<<" ";
}
cout<<endl;
}
return 0;
}

Can someone show me how to display a code like you guys..


But that example uses an if statement which you said you couldn't use?
With a simple if .. else ... you can replace the cout in my second example code and you have the result desired.

If you click the new topic button it shows you how to block your code.
I can't show you without executing the action making the [code] blocks statements invisible.
Last edited on
SOLVED IT!
You can do it with a single cout statement without using an if.. else... block.
HINT:Involves using the logical operators.
Last edited on
Pages: 12