Skip to content

Instantly share code, notes, and snippets.

@arrbxr
Created May 9, 2020 09:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save arrbxr/1956f2dc72026044e1bf503c3f22abdd to your computer and use it in GitHub Desktop.
Save arrbxr/1956f2dc72026044e1bf503c3f22abdd to your computer and use it in GitHub Desktop.
/* By using a two-dimensional array, write C++ program to display the exact table of numbers that is shown below:
##################
# 1 2 3 4 5 #
# 6 7 8 9 10 #
# 11 12 13 14 15 #
# 16 17 18 19 20 #
# 21 33 23 24 25 #
##################
*/
#include<iostream>
using namespace std;
int main() {
int arr[9][9];
int i, j;
int row, clm;
cout<<"Enter Row Size: ";
cin>>row;
cout<<"Enter Column Size: ";
cin>>clm;
for(i = 0; i < row; i++) { // Assigning values to the two dimensional array
for(j = 0; j < clm; j++) {
if(i == 0) {
arr[i][j] = j + 1; // fills the first row
}
if(i > 0 && j == 0) {
arr[i][j] = arr[i - 1][clm - 1] + 1; // fetching the value of the last cell in the previous row
} else {
arr[i][j] = arr[i][j - 1] + 1; // fills subsequent cells
}
}
}
for(i = 0; i < row; i++) { // print the array
for(j = 0; j < clm; j++) {
cout<<arr[i][j]<<" ";
}
cout<<endl;
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment