Displaying Diagonal Lines that intersect using for-loops

Pages: 12
What!! no way!!Do you mind if i see, my tutor is asking to see it so he can explain it to me. And did u do it line by line?
I've got the X shape, just not sure how to do the reverse increments. This problem is really getting me too.
Couldn't you use a decrement in the for loop. That's what I would use.
I can show you my code, but I've tried to implement that and it comes back with different errors.
Yea show me and ill run it and see, because i have grown fond of for loops haha.
Glad you like them haha. I also have a program that increments, but not in the shape of an X just two lines parallel.

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


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;
}
return 0;
}
Do you mind explaining what r and c is? is it just your counter?
just rows and columns. it could be any letter or name really.
This is what i have so far now i am having trouble with the spacing.

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

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

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

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

for (int r=1; r<=number; r++){cout<<r;
for(int space=1;space<=number;space++){
    cout<<" "; 
  
}  cout << endl;
}

for (int c=1; c<=number; c++){cout<<c;
for(int space=1;space<=number;space++){
      cout<<" ";
      
}cout << endl;
}





return 0;

}
When I ran that it was just the number imputed incremented twice down a straight line?
Yea but it was in the opposite order, now do you think we can implement spacing into it? cause if so then thats the answer.

Another idea of mine is if we have it to where
X......Y
.X....Y
..X..Y
...X
..X..Y
.X....Y
X......Y

And we just replace the X's and Y's with the order.
Last edited on
This is another code I have for the opposite order and it's not in one straight line.

#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;
}
This is still extremely hard, i am trying to modify it to where it displays correctly but nothing seems to be working.
Topic archived. No new replies allowed.
Pages: 12