Sunday, January 24, 2016

C++ arrays: series of numbers

A  two-dimensional array  stores values in rows and columns. By using two-dimensional array, write C++ program to display a table of numbers... thumbnail 1 summary
two-dimensional array stores values in rows and columns. By using two-dimensional array, write C++ program to display a table of numbers as shown below:

12345 
67 910
1112131415
1617181920
2122232425

Solution:

#include<iostream> 
#include<conio.h> 
using namespace std; 
int main() 
    { 
      int tArr[5][5]; 
     int i,j;
     for(i=0;i<5;i++) //assign values to the two-dimensional array
        for(j=0;j<=5;j++){
           if(i==0) tArr[i][j]=j+1; //fill the first row
           if(i>0 && j==0)
              tArr[i][j]=tArr[i-1][4]+1; //fetching the value of the last cell in the previous row
           else
             tArr[i][j]=tArr[i][j-1]+1; //fill subsequent cells
             }    
         for(i=0;i<5;i++){ //print the array
        for(j=0;j<5;j++)
            cout<<tArr[i][j]<<"\t";
        cout<<endl;  
       }
     getch();
     return 0;
      } 

No comments

Post a Comment