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.
#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;
}
Comments
Leave a comment