Write a C program that Allow the user to enter the size of t
Write a C++ program that:
Allow the user to enter the size of the matrix such as N.
Call a function to do the following:
Create a vector size n X n
Populate the vector with n2distinct randon integers.
Solution
Solution for problrm (CPP) :-
Program:-
#include<iostream>
#include<vector>
using namespace std;
int main()
{
int n ;
cout<<\"Enter size of the matrix : \";
cin>>n;
vector<vector<int> > vec(n,vector<int>(n));
// looping of the outer vector vec
for (int i = 0; i < n; i++)
{
// loopingof the inner vector vec[i]
for (int j = 0; j < n; j++)
{
vec[i][j] = i*n + j; //Filling the vector which had size n*n
}
}
cout<<\"Matrix values are\"<<endl;
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
cout<<vec[i][j]<<\"\\t\";
}
cout<<endl;
}
return 0;
}
Output :-
Enter size of the matrix : 4
Matrix values are :-
0 1 2 3
4 5 6 7
8 9 10 11
12 13 14 15
Thank you!
