so I want to ask the user for a matrix size
int x = rows
int y = columns
for int x = 3 and int y = 3
I want to print
1 2 3
4 5 6
7 8 9
and for each dimension I would print number the start from 1 to the end
so for int x = 4 and int y = 2
I would print
1 2
3 4
and such.
// C++ program to input matrix size and print the elements in the matrix starting from 1
#include <iostream>
using namespace std;
int main()
{
int rows, cols;
// input of rows and columns of the matrix
cout<<"Enter number of rows and columns of the matrix (separated by a space) : ";
cin>>rows>>cols;
int start = 1; // initialize the start element
// loop over the number of rows
for(int i=0;i<rows;i++)
{
// loop over the number of columns for each row
for(int j=0;j<cols;j++)
{
// print the start
cout<<start<<" ";
start++; // increment the start
}
cout<<endl; // add a new line after the columns of ith row are printed
}
return 0;
}
//end of program
Output:
Get Answers For Free
Most questions answered within 1 hours.