Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

the goal of this code is to create a vector matrix userNum x userNum, and to fil

ID: 3791494 • Letter: T

Question

the goal of this code is to create a vector matrix userNum x userNum, and to fill it with distinct random variables. The code below gives me a segmentation error 11. how can i fix this?

do

   {

do

{

   cout << " Enter the size of the matrix: ";

   cin >> userNum;

   if (userNum < 2 && !isdigit(userNum))

cout << " Error *** Must be an integer greater than 2" << endl;

} while(userNum < 2);

vector<vector<int> > randMatrix;

cout << " The perfect matrix that is created for size " << userNum << ":" << $

srand(time(NULL));

for (int i = 0; i < userNum; i++){

   vector<int> row;

   for(int j = 0; j < userNum; j++){

while ((rand() % 10) != row[i]){

   row.push_back(rand() % 10);

}

   }

   randMatrix.push_back(row);

}

for(int i = 0; i < randMatrix.size(); i++){

   for(int j = 0; j < randMatrix[i].size(); j++){

cout << randMatrix[i][j] << " ";

   }

   cout << endl;

}

Explanation / Answer

#include <iostream>
#include <cstdlib>
#include<vector>

using namespace std;

int main()
{
int userNum;
int row;
  
cout << "Hello World" << endl;

cout << " Enter the size of the matrix: ";
cin >> userNum;
if (userNum < 2 && !isdigit(userNum)){
cout << " Error *** Must be an integer greater than 2" << endl;
}
else{
std::vector<vector<int>> randMatrix;
// cout << " The perfect matrix that is created for size " << userNum << ":" << $
srand(time(NULL));
for (int i = 0; i < userNum; i++){
std::vector<int> row;
for(int j = 0; j < userNum; j++){
while ((rand() % 10) != row[i]){
row.push_back(rand() % 10);
}
}
randMatrix.push_back(row);
  
for(int i = 0; i < randMatrix.size(); i++){
for(int j = 0; j < randMatrix[i].size(); j++){
cout << randMatrix[i][j] << " ";
}
cout << endl;
}

cout<<"goood"<<endl;
}
  
return 0;
}
}