Occurrence of item in matrix in C++ by R4R Team

Matrix:
-Multidimensional Array
-Matrix consist of the rows and columns.

Syntax-
array[row][column]
//Matrix with numbers of rows and columns

Example-
Matrix is:
1 2 1
4 3 1
1 1 1
occurrence of 1 is 6
occurrence of 4 is 1
etc

program:

#include< iostream>
int main()
{
int a[10][10],r,c,i,j,count=0,num;
cout<<"Enter rows and columns in matrix"<< endl;
cin>>r>>c;
cout<<"Enter "<< r*c<<" number in matrix"<< endl;
//input in matrix
for(i=0;i< r;i++){
for(j=0;j< c;j++)
{
cin>>a[i][j];
}
}
cout<<"Matrix is "<< endl;
for(i=0;i< r;i++){
for(j=0;j< c;j++)
{
cout<< a[i][j];
}
cout<< endl;
}
cout<<"Enter number to search"<< endl;
cin>>num;
for(i=0;i< r;i++)
{
for(j=0;j< c;j++)
{
if(a[i][j]==num)
count++;
}
}
cout<<"Occurrence of "<< num<<" is "<< count;
}

output:

Enter rows and colums in matrix
3 3
Enter 9 number in matrix
1 2 3 1 4 5 1 6 7
Matrix is
1 2 3
1 4 5
1 6 7
Enter number to search
1
Occurrence of 1 in matrix is 3




Leave a Comment:
Search
Categories
R4R Team
R4Rin Top Tutorials are Core Java,Hibernate ,Spring,Sturts.The content on R4R.in website is done by expert team not only with the help of books but along with the strong professional knowledge in all context like coding,designing, marketing,etc!