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

in C++ is this how I declare the 2d dynamic vector? vector>matrix; how do i popu

ID: 3834586 • Letter: I

Question

in C++

is this how I declare the 2d dynamic vector?

vector>matrix;

how do i populate the vector ,print it , find the rows and print the major diagnols with 1s

program prompts the user to enter the length if a square matrix, randomly fill in 0s and 1s into the matrix , print the matrix, and find the rows and major diagonal with all 1s


This is what i have so fare but it dosnet track the rows full of 1s or major diagnols with 1s

#include <iostream>

#include <vector>

#include <ctime>

using namespace std;

int main()

{

srand(time(0)); //this is for random generation

vector<vector<int> > matrix;

int m=0;

  

  

cout << " Please enter size of matrix : ";

cin>> m;

  

  

//adding elements to 2D vector

for(int i=0; i<m; i++)

{

vector<int> row;

for(int j=0; j<m; j++)

// this will full in random values of 0 and 1

row.push_back(rand()%2); // you can fill in the values here

matrix.push_back(row);

}

//printing the vector

for(int i=0; i<m; i++)

{

for(int j=0; j<m; j++)

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

cout<<endl;

}

// this will print the diagonal elements from left to right

cout<<"All Rows with 1s, and major diagnols with 1s";

for(int i=0; i<m; i++)

{

  

for(int j=0; j<m; j++)

{

if(i==j)

cout<<" :"<<matrix[i][j]<<" ";

  

}

}

cout << endl;

return 0;

}

in C++

Explanation / Answer

#include <iostream>
#include <vector>
#include <ctime>
using namespace std;

int main()
{
srand(time(0)); //this is for random generation
vector<vector<int> > matrix;

int m=0;
  
cout << " Please enter size of matrix : ";
cin>> m;
  
  
//adding elements to 2D vector
for(int i=0; i<m; i++)
{
vector<int> row;
for(int j=0; j<m; j++)
// this will full in random values of 0 and 1
row.push_back(rand()%2); // you can fill in the values here
matrix.push_back(row);
}
//printing the vector
for(int i=0; i<m; i++)
{
for(int j=0; j<m; j++)
cout<<matrix[i][j]<<" ";
cout<<endl;
}
   bool majorDiagnolHasAll1s = true;
   bool rowsWithAll1s[m];
   for(int i=0; i<m; i++)
{
       rowsWithAll1s[i]=true;
   }
// this will print the diagonal elements from left to right
cout<<"All Rows with 1s, and major diagnols with 1s";
for(int i=0; i<m; i++)
{
for(int j=0; j<m; j++)
{
           if(matrix[i][j]==0)
               rowsWithAll1s[i] = false;
if(i==j && matrix[i][j]==0)
majorDiagnolHasAll1s = false;
}
}
cout << endl;

   if(majorDiagnolHasAll1s){
       cout << "Major Diagonal ";
       for(int i=0; i<m; i++)
       {
           cout << ":"<<1;
       }
       cout << endl;
   }
   else{
       cout << "Major Diagonal Don't have all 1s ";
   }

   for(int i=0; i<m; i++)
{
       if(rowsWithAll1s[i]){
           cout << "Row["<<i<<"] ";
           for(int j=0; j<m; j++)
           {
               cout << ":"<<1;
           }
           cout << endl;
       }
       else{
           cout << "Row["<<i<<"] Don't have all 1s ";
       }
}
return 0;
}