Answer to Question #333703 in C++ for Ali

Question #333703

Write a program that randomly fills in 0s and 1s into a

6 X 6 matrix, prints the matrix and finds the first row and the first column with the

least 1s.


1
Expert's answer
2022-04-25T15:15:26-0400


#include <iomanip> 
#include <iostream> 
#include <ctime>


using namespace std;
void genRandom(int a[6][6])
{
  for(int i=0;i<6;i++)
    {
      for(int j=0;j<6;j++)
        a[i][j]=rand()%2;
    }
}
int getRow1s(int a[6][6])
{
  int mxRow=0;
  int mxcnt=6;
  for(int i=0;i<6;i++)
    {
      int cnt=0;
      for(int j=0;j<6;j++)
        if(a[i][j])
          cnt++;
      if(cnt<mxcnt)
      {
        mxcnt=cnt;
        mxRow=i;
      }
    }
  return  mxRow;
}


int getCol1s(int a[6][6])
{
  int mxCol=0;
  int mxcnt=6;
  for(int i=0;i<6;i++)
    {
      int cnt=0;
      for(int j=0;j<6;j++)
        if(a[j][i])
          cnt++;
      if(cnt<mxcnt)
      {
        mxcnt=cnt;
        mxCol=i;
      }
    }
  return  mxCol;
}
void Print(int a[6][6])
{
  for(int i=0;i<6;i++)
    {
      int cnt=0;
      for(int j=0;j<6;j++)
        cout<<setw(5)<<a[i][j]<<" ";
      cout<<endl;
    }
}


int main() { 
  int a[6][6];
  genRandom(a);
  Print(a);
  int rws=getRow1s(a);
  int cls=getCol1s(a);
  cout<<"The Row the least 1s: "<<rws<<endl;
  cout<<"The Col the least 1s: "<<cls<<endl;
  return 0;
} 

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS