c++ - A program to print numbers from 1 to n in triangular wave form without using extra space -


i want print numbers 1 n(say 10) in triangular wave form.here code -

#include <iostream> using namespace std;  int main() {     //code     int n;     cin>>n;     int arr[3][10];     int r=1,c=0,dir=-1;     int i,j;     for(i=0;i<3;i++){         for(int j=0;j<n;j++){             arr[i][j]=0;         }     }      for(i=1;i<=n;i++){         arr[r][c]=i;         if(r==2 || r==0)dir = -dir;         if(dir==1)r++;         else r--;         c++;      }      for(i=0;i<3;i++){         for(j=0;j<n;j++){             if(arr[i][j]==0)               cout<<"  ";             else cout<<arr[i][j]<<" ";         }         cout<<endl;     }      return 0; } 

the output of code is

    2       6       10   1   3   5   7   9         4       8 

is possible solve problem using array?

replace body of third cycle with

if (arr[i][j] == 0)     cout << ' '; else     cout << arr[i][j]; cout << ' '; 

indeed, want print number, if 0, want print space


Comments

Popular posts from this blog

javascript - Laravel datatable invalid JSON response -

java - Exception in thread "main" org.springframework.context.ApplicationContextException: Unable to start embedded container; -

sql server 2008 - My Sql Code Get An Error Of Msg 245, Level 16, State 1, Line 1 Conversion failed when converting the varchar value '8:45 AM' to data type int -