What is wrong with my code?

I have an issue with my code. When R1>R2 and C1>C2 it doesn't work, but if R1<R2 and C1<C2 it runs correctly. 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
44
#include <iostream>
#include <iomanip>

using namespace std;

int main()
{
    int R1, R2, C1, C2;
    int col;

    cout << "Please Enter Four Integers R1 R2 C1 C2: ";
    cin>>R1>>R2>>C1>>C2;
    cout<<R1<<" "<<R2<<" "<<C1<<" "<<C2<<endl;
    
    if(R1<R2 & C1<C2)
    {
        
    }
    
    cout<< endl << "        ";
    for(col = C1; col <= C2; col++)
   
    cout << left << setw(6) << col;
    cout << endl;
    cout << "-----";

    for(col = C1; col <= C2; col++)
    cout << "|-----";
    cout << "|" << endl;

    for(; R1<=R2; R1++)
    {
        cout << left << setw(7) << R1;
        for(col = C1; col <= C2; col++)
        cout << left << setw(6) << R1*col;
        cout << endl << "-----";
        for(col = C1; col <= C2; col++)
        cout << "|-----";
        cout << "|" << endl;
    }


    return 0;
}



My output

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
Please Enter Four Integers R1 R2 C1 C2: 0 8 5 -3

        
0      
-----|
1      
-----|
2      
-----|
3      
-----|
4      
-----|
5      
-----|
6      
-----|
7      
-----|
8      
-----|




what it is suposed to look like:
 
Please Enter Four Integers R1 R2 C1 C2: 0 8 5 -3 


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
          5     4     3     2     1     0    -1    -2    -3
0       0     0     0     0     0     0     0     0     0     
-----|-----|-----|-----|-----|-----|-----|-----|-----|-----|
1       5     4     3     2     1     0     -1    -2    -3    
-----|-----|-----|-----|-----|-----|-----|-----|-----|-----|
2       10    8     6     4     2     0     -2    -4    -6    
-----|-----|-----|-----|-----|-----|-----|-----|-----|-----|
3         15    12    9     6     3     0     -3    -6    -9    
-----|-----|-----|-----|-----|-----|-----|-----|-----|-----|
4        20    16    12    8     4     0     -4    -8    -12   
-----|-----|-----|-----|-----|-----|-----|-----|-----|-----|
5         25    20    15    10    5     0     -5    -10   -15   
-----|-----|-----|-----|-----|-----|-----|-----|-----|-----|
6         30    24    18    12    6     0     -6    -12   -18   
-----|-----|-----|-----|-----|-----|-----|-----|-----|-----|
7         35    28    21    14    7     0     -7    -14   -21   
-----|-----|-----|-----|-----|-----|-----|-----|-----|-----|
8         40    32    24    16    8     0     -8    -16   -24   
-----|-----|-----|-----|-----|-----|-----|-----|-----|-----|



can someone tell me what I am doing wrong?
Last edited on
If R1 > R2 then the loop condition R1 <= R2 will be false right away.

If C1 > C2 and col starts as C1 then the loop condition col <= C2 will also be false.

This means the loops won't run.
Last edited on
What you can do is swap(...) the values:

1
2
3
4
    if(R2<R1) // The same for C
    {
        std::swap(R2, R1);
    }
But that depends on how you want to use them.

Another approach would be using the distance:
1
2
3
4
5
    const int r_dist = std::max(R1, R2) - std::min(R1, R2);
    for(int ri = 0; ri < r_dist; ++ri)
    {
        cout << left << setw(7) << R1 + ri;
    }
This would keep the order.
> if(R1<R2 & C1<C2)
Also note that & is the bitwise operator and && is the boolean operator.
Topic archived. No new replies allowed.