User inputs an integer N from the range from 1 to 30. Write a program to display the numbers from 1 to N*N in a table with N*N size in the following pattern
Input
3
Please note that this is not a homework site. We won't do your homework for you. The purpose of homework is that you learn by doing. However we are always willing to help solve problems you encountered, correct mistakes you made in your code and answer your questions.
We didn't see your attempts to solve this problem yourself and so we cannot correct mistakes you didn't made and answer questions you didn't ask. To get help you should do something yourself and get real problems with something. If your problem is "I don't understand a thing", then you should go back to basics and study again.
#include<iostream>
#include<conio.h>
using namespace std;
int main()
{
int tArr[3][3];
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][3]+1; //fetching the value of the last cell in the previous row
else
tArr[i][j]=tArr[i][j-1]+1; //fill subsequent cells
}
Your code has an array that has 3 rows and 3 columns. Your loops, however, iterate over 5 rows and 5 columns. Furthermore, you don't ask from user how many rows&columns you actually do need.
This task does not require any array.
1 2 3 4 5 6 7
// compute and print the array
for ( int row=0; row < N; ++row ) {
for ( int col=0; col < N; +col ) {
std::cout << '\t' << ????;
}
std::cout << '\n';
}
The ????, the value of number in that position, can be computed from 'row', 'col', and 'N'.