Write a program to find largest and smallest element in each row of a matrix
Answers
Answered by
2
de<stdio.h>
#include<conio.h>
int max(int a[][],int m,int n)
{
int i,j,max;
max=a[0][0];
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
if(a[i][j]>max)
max=a[i][j];
}
}
return max;
}
void main(void)
{
int a[25][25],m,n,i,j;
clrscr();
printf("\n Enter the order of matrix : ");
scanf("%dx%d",m,n);
printf("\n Enter the matrix row wise : ");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
scanf("%d",&a[i][j]);
}
printf("\n Largest number of matrix is : %d",max(a,m,n));
getch();
}
Output
Enter the order of matrix : 3x3
Enter the matrix row wise : 1 2 3 4 5 6 7 8 9
Largest number of matrix is : 9
Here, a function called 'max' is used to find the largest number in the matrix. A variable 'max' is considered and the value at position (0,0) is put in it. Then, it is compared with every number in the matrix and replaced if a larger number is found. Eventually, the largest number of the matrix gets into it.
To find the largest number in every row and every column of the matrix
Program
#include<stdio.h>
#include<conio.h>
int max(int a[],int n)
{
int i,max;
max=a[0];
for(i=0;i<n;i++)
{
if(a[i]>max)
max=a[i];
}
return max;
}
void main(void)
{
int a[25][25],b[25],m,n,i,j;
clrscr();
printf("\n Enter the order of matrix : ");
scanf("%dx%d",m,n);
printf("\n Enter the matrix row wise : ");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
scanf("%d",&a[i][j]);
}
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
b[j]=a[i][j];
printf("\n Largest number of row %d : %d",i+1,max(b,n));
}
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
b[j]=a[i][j];
printf("\n Largest number of column %d : %d",i+1,max(b,m));
}
getch();
}
Output
Enter the order of matrix : 3x3
Enter the matrix row wise : 1 2 3 4 5 6 7 8 9
Largest number of row 1 : 3
Largest number of row 2 : 6
Largest number of row 3 : 9
Largest number of column 1 : 7
Largest number of column 2 : 8
Largest number of column 3 : 9
Here, each row and each column of the matrix are copied to a single dimensional array and the largest number in that single dimensional array is found using the 'max' function. A variable 'max' is considered and the value at position 0 is put in it. Then, it is compared with every number in the single dimensional array and replaced if a larger number is found.
Hope this answer helps.#13949727 Oct 2016 03:05Mad MaxPoints:4 (Rs 4)This C program is awesome!Helped me a lot. Except one small thing:
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
for(i=0;i{
for(j=0;jb[j]=a[i][j];
printf("\n Largest number of row %d : %d",i+1,max(b,n));
}
for(i=0;i{
for(j=0;j-------------
b[j]=a[i][j];
^^^^^^^^^^
/*This part the same as rows.You need to change it for coloumn "b[j] = a[j][i]" so instead of 'i' 'j' use 'j' 'i' */
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""#14028928 Dec 2016 11:27Deepak Jayant DattawadkarPoints:10 (Rs 10)Below is the program to find out largest number from give matrix(i.e. from given rows and columns). In C or C++ there is a provision to represent the data in row and column format known as multidimensional array representation.
Program to find out greatest number from row and column of matrix.
please include stdio.h and conio.h header files respectively.
#include
#include
void main(void)
{
int arr[2][3],i,j,max;
clrscr();
printf("Enter any six elements one by one\n ");
for(i=0;i<2;i++)
{
for(j=0;j<3;j++)
{
scanf("%d",&arr[i][j]);
}
}
printf("Elements of the array are as follows\n");
for(i=0;i<2;i++)
{
for(j=0;j<3;j++)
{
printf("%d ",arr[i][j]);
}
printf("\n\n");
}
max=arr[0][0];
for(i=0;i<2;i++)
{
for(j=0;j<3;j++)
{
if(arr[i][j]>max)
{
max=arr[i][j];
}
}
}
printf("Maximum element of array is %d",max);
getch();
}
In the above program a matrix of 2 x 3 elements is represented by using double dimensional array. In the first part of program variable declaration is done. in next session by using two nested for loops, elements of array get inputted by user. Then after array elements will be displayed in matrix form by using two nested for loops. (Actually in memory array elements were stored in contiguous memory locations).
After that zeroth element of the array will be declared as maximum number and then comparing this maximum number with rest of the array elements gives actual maximum number from the matrix.#14462414 Aug 2017 19:08sachinPoints:2 (Rs 2)C-PROGRAM TO FIND THE BIGGEST&SMALLEST NUMBER OF THE GIVEN MATRIX.
PROGRAM:
#include
main()
{
int a[10][10],i,j,m,n,big,small;
printf("enter the values of m,n");
scanf("%d%d",&m,&n);
printf("enter the elemnts of matrix a");
for(i=0;ifor(j=0;jscanf("%d",&a[i][j]);
big=a[0][0];
small=a[0][0];
for(i=0;ifor(j=0;jif(a[i][j]>big)
big=a[i][j];
if(a[i][j]small=a[i][j];
printf("biggest element =%d",big);
printf("smallest element =%d",small);
}
OUTPUT:
enter the values of m,n
3,3
enter the elements of matrix a
1 2 3
3 4 5
5 6 7
biggest element 7
smallest element 1
#include<conio.h>
int max(int a[][],int m,int n)
{
int i,j,max;
max=a[0][0];
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
if(a[i][j]>max)
max=a[i][j];
}
}
return max;
}
void main(void)
{
int a[25][25],m,n,i,j;
clrscr();
printf("\n Enter the order of matrix : ");
scanf("%dx%d",m,n);
printf("\n Enter the matrix row wise : ");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
scanf("%d",&a[i][j]);
}
printf("\n Largest number of matrix is : %d",max(a,m,n));
getch();
}
Output
Enter the order of matrix : 3x3
Enter the matrix row wise : 1 2 3 4 5 6 7 8 9
Largest number of matrix is : 9
Here, a function called 'max' is used to find the largest number in the matrix. A variable 'max' is considered and the value at position (0,0) is put in it. Then, it is compared with every number in the matrix and replaced if a larger number is found. Eventually, the largest number of the matrix gets into it.
To find the largest number in every row and every column of the matrix
Program
#include<stdio.h>
#include<conio.h>
int max(int a[],int n)
{
int i,max;
max=a[0];
for(i=0;i<n;i++)
{
if(a[i]>max)
max=a[i];
}
return max;
}
void main(void)
{
int a[25][25],b[25],m,n,i,j;
clrscr();
printf("\n Enter the order of matrix : ");
scanf("%dx%d",m,n);
printf("\n Enter the matrix row wise : ");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
scanf("%d",&a[i][j]);
}
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
b[j]=a[i][j];
printf("\n Largest number of row %d : %d",i+1,max(b,n));
}
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
b[j]=a[i][j];
printf("\n Largest number of column %d : %d",i+1,max(b,m));
}
getch();
}
Output
Enter the order of matrix : 3x3
Enter the matrix row wise : 1 2 3 4 5 6 7 8 9
Largest number of row 1 : 3
Largest number of row 2 : 6
Largest number of row 3 : 9
Largest number of column 1 : 7
Largest number of column 2 : 8
Largest number of column 3 : 9
Here, each row and each column of the matrix are copied to a single dimensional array and the largest number in that single dimensional array is found using the 'max' function. A variable 'max' is considered and the value at position 0 is put in it. Then, it is compared with every number in the single dimensional array and replaced if a larger number is found.
Hope this answer helps.#13949727 Oct 2016 03:05Mad MaxPoints:4 (Rs 4)This C program is awesome!Helped me a lot. Except one small thing:
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
for(i=0;i{
for(j=0;jb[j]=a[i][j];
printf("\n Largest number of row %d : %d",i+1,max(b,n));
}
for(i=0;i{
for(j=0;j-------------
b[j]=a[i][j];
^^^^^^^^^^
/*This part the same as rows.You need to change it for coloumn "b[j] = a[j][i]" so instead of 'i' 'j' use 'j' 'i' */
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""#14028928 Dec 2016 11:27Deepak Jayant DattawadkarPoints:10 (Rs 10)Below is the program to find out largest number from give matrix(i.e. from given rows and columns). In C or C++ there is a provision to represent the data in row and column format known as multidimensional array representation.
Program to find out greatest number from row and column of matrix.
please include stdio.h and conio.h header files respectively.
#include
#include
void main(void)
{
int arr[2][3],i,j,max;
clrscr();
printf("Enter any six elements one by one\n ");
for(i=0;i<2;i++)
{
for(j=0;j<3;j++)
{
scanf("%d",&arr[i][j]);
}
}
printf("Elements of the array are as follows\n");
for(i=0;i<2;i++)
{
for(j=0;j<3;j++)
{
printf("%d ",arr[i][j]);
}
printf("\n\n");
}
max=arr[0][0];
for(i=0;i<2;i++)
{
for(j=0;j<3;j++)
{
if(arr[i][j]>max)
{
max=arr[i][j];
}
}
}
printf("Maximum element of array is %d",max);
getch();
}
In the above program a matrix of 2 x 3 elements is represented by using double dimensional array. In the first part of program variable declaration is done. in next session by using two nested for loops, elements of array get inputted by user. Then after array elements will be displayed in matrix form by using two nested for loops. (Actually in memory array elements were stored in contiguous memory locations).
After that zeroth element of the array will be declared as maximum number and then comparing this maximum number with rest of the array elements gives actual maximum number from the matrix.#14462414 Aug 2017 19:08sachinPoints:2 (Rs 2)C-PROGRAM TO FIND THE BIGGEST&SMALLEST NUMBER OF THE GIVEN MATRIX.
PROGRAM:
#include
main()
{
int a[10][10],i,j,m,n,big,small;
printf("enter the values of m,n");
scanf("%d%d",&m,&n);
printf("enter the elemnts of matrix a");
for(i=0;ifor(j=0;jscanf("%d",&a[i][j]);
big=a[0][0];
small=a[0][0];
for(i=0;ifor(j=0;jif(a[i][j]>big)
big=a[i][j];
if(a[i][j]small=a[i][j];
printf("biggest element =%d",big);
printf("smallest element =%d",small);
}
OUTPUT:
enter the values of m,n
3,3
enter the elements of matrix a
1 2 3
3 4 5
5 6 7
biggest element 7
smallest element 1
Similar questions