0

I wish to print a pattern like this in C++

1 2 3 4 5
10 9 8 7 6
11 12 13 14 15
20 19 18 17 16
21 22 23 24 25
30 29 28 27 26

I have tried everything but still not able to print this. Can you suggest what changes I should make to my code:

#include <iostream>
using namespace std;

int main() {
    int n;
    cin>>n;
    int odd;
    int j =0;
    
    for(int i = 1; i<=n;i++){
        
        if(i%2!=0){
            for( j; j<(5*i);j++){
                odd +=1;
                cout<<odd<<" ";
            }
             cout<<endl;
        }
        
         if(i%2==0){
            for( j =5*i; j>(5*(i-1));j--){
                odd =j;
                 cout<<odd<<" ";
            }
            
            
        }
    }

}
rawrex
  • 4,044
  • 2
  • 8
  • 24
Sina
  • 1
  • 1
  • You need `cout << setw(2) << " ";`. Set the field with to 2. Alternatively, you could `setw(3)` and avoid the extra space. You may need to `#include `. – Tim Roberts Jun 27 '21 at 03:37

4 Answers4

1

You should create the j variable in the for loop so you can control it's value. Try this one:

  • Loop i from 0 to n
  • if i % 2 == 0 then loop j from 1 to n and print n*i + j
  • if i % 2 == 1 then loop j from n to 1 and print n*i + j
#include <iostream>
using namespace std;

int main() {
    int n;
    cin >> n;    
    for(int i = 0; i <= n; i++)
    {
        if(i%2 == 0){
            for(int j = 1; j <= n; j++)
                cout << n*i + j << " ";
            cout << endl;
        }else{
            for(int j = n; j > 0; j--)
                cout << n*i + j << " ";
            cout << endl;
        }
    }
}

1

Although, with some changes, your code may works, but as you have hardcoded the constant 5 into your code (for instance, for( j =5*i; j>(5*(i-1));j--) has a lot of 5 inside the for-loop), it'll be likely that the program will only print correctly when n=5.

So, a simpler solution:

  • Calculate the starting and ending value of each line
  • If i%2==1 then print the line in normal order, else print it in reverse order

Not only this solution would be shorter, it'll be more versatile if n were to change.

Code:

#include <iostream>

void printRow(int l, int r, int mode) //function to print a line
{
    if (mode == 1) {for (int i = l; i <= r; i++) {std::cout << i << " ";} std::cout << '\n';} //normal order
    else if (mode == 2) {for (int i = r; i >= l; i--) {std::cout << i << " ";} std::cout << '\n';} //reverse order
}

int main()
{
    std::cout << "Input N : "; int n; std::cin >> n;
    for (int i = 1; i <= n; i++)
    {
        int l = n*(i-1)+1, r = l+n-1, mode = (i%2==0) ? 2 : 1; //calculate the starting and ending point of a line
        printRow(l, r, mode); //print that line
    }
}

Result:

Input N : 5
1 2 3 4 5
10 9 8 7 6
11 12 13 14 15
20 19 18 17 16
21 22 23 24 25

Result (2):

Input N : 10
1 2 3 4 5 6 7 8 9 10
20 19 18 17 16 15 14 13 12 11
21 22 23 24 25 26 27 28 29 30
40 39 38 37 36 35 34 33 32 31
41 42 43 44 45 46 47 48 49 50
60 59 58 57 56 55 54 53 52 51
61 62 63 64 65 66 67 68 69 70
80 79 78 77 76 75 74 73 72 71
81 82 83 84 85 86 87 88 89 90
100 99 98 97 96 95 94 93 92 91

Also, see Why is "using namespace std;" considered bad practice?

silverfox
  • 1,568
  • 10
  • 27
0

here is your own code with minute changes--

#include <iostream>
using namespace std;

int main() {
    int n=0;
    cin>>n;
    int odd=0;
    int j =0;
    
    for(int i = 1; i<=n+1;i++)//*
                                 
       { if(i%2!=0){
            for( j; j<(5*i);j++){
                odd+=1;
                cout<<odd<<" ";
            }
             cout<<endl;
        }
        
         if(i%2==0){
            for( j =5*i; j>(5*(i-1));j--){
                odd=j;
                 cout<<odd<<" ";
            }
            cout<<endl; // inserted endl 
            
            
        }
        odd=odd+4; //increment in value of odd
        j=j+5;  //increment in value of j
    }

}
  • if you want the above output for the input 6 then replace i<=n+1 with i<=n, else it is working fine for input 5.
sam2611
  • 455
  • 5
  • 15
-1

just chnaged some conditions

#include <iostream>
using namespace std;

int main() {
    // your code goes here
    int n;
    cin>>n;
    int odd =0;
    int j =0;
    int flag =0;
    
    for(int i = 1; i<=n;i++){
        
        if(i%2!=0){
            for( j; j<(5*i);j++){
                odd +=1;
                if(odd>5*flag && odd<=5*i)
                cout<<odd<<" ";
            }
             cout<<endl;
             flag++;
        }
        
         if(i%2==0){
            for( j =5*i; j>(5*(i-1));j--){
                odd =j;
                 cout<<odd<<" ";
            }
            cout<<endl;
            flag++;
            
        }
    }

}
Sina
  • 1
  • 1
  • Your code, while it works, depends heavily on the condition that `n=5`. Should `n` changed, that code will print the wrong output. – silverfox Jun 27 '21 at 04:07