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 RANDOM integers.
#include <iostream>
#include <vector>
#include <cstdlib>
#include <ctime>
#include <iomanip>
using namespace std;
int main() {
//Declaring variable
int n;
srand(time(0));
//getting the number enterd by the user
cout<<"Enter the size of the matrix :";
cin>>n;
//Declaring vector of 'n' rows and 'n' columns
vector< vector<int>
>matrix(n,vector<int>(n));
/* generating the random numbers and
* populating those numbers into a vector
*/
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
matrix[i][j]=rand()%1000;
}
}
//Displaying the elements inside the vector
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
cout<<matrix[i][j]<<" ";
}
cout<<endl;
}
return 0;
}
______________________
output:
Get Answers For Free
Most questions answered within 1 hours.