Please answer 4 a and b in C Program please 4 a Write a C fu
Please answer 4 a and b in C Program please.
4 a. Write a C function that finds and displays the maximum value in a two-dimensional array of integers. The array should be declared as a 10-row-by-20-column array of integers in main ( ), and the starting address of the array should be passes to the function.
b. Modify the funtion written in Exercise 4a so that it also displays the row and column number of the element with the maximum value.
Solution
#include <stdio.h>
#include<stdlib.h>
void maximum(int arr[10][20])
{
int max_row,max_col,max_value = -1,i,j;
for(i=0;i<10;i++)
{
for(j=0;j<20;j++)
{
if(arr[i][j]>max_value)
{
max_value = arr[i][j];
max_row = i;
max_col = j;
}
}
}
printf(\"Max value in 2d arrays is %d at row %d and col %d\",max_value,max_row+1,max_col+1);
}
int main(void) {
// your code goes here
int arr[10][20];
int i,j;
for(i=0;i<10;i++)
{
for(j=0;j<20;j++)
{
arr[i][j] = rand()%100; //fill the array with random number between 0 to 99
}
}
printf(\"2d array is : \ \");
for(i=0;i<10;i++)
{
for(j=0;j<20;j++)
{
printf(\"%d \",arr[i][j]);
}
printf(\"\ \");
}
maximum(arr);
return 0;
}
OUTPUT:
2d array is :
83 86 77 15 93 35 86 92 49 21 62 27 90 59 63 26 40 26 72 36
11 68 67 29 82 30 62 23 67 35 29 2 22 58 69 67 93 56 11 42
29 73 21 19 84 37 98 24 15 70 13 26 91 80 56 73 62 70 96 81
5 25 84 27 36 5 46 29 13 57 24 95 82 45 14 67 34 64 43 50
87 8 76 78 88 84 3 51 54 99 32 60 76 68 39 12 26 86 94 39
95 70 34 78 67 1 97 2 17 92 52 56 1 80 86 41 65 89 44 19
40 29 31 17 97 71 81 75 9 27 67 56 97 53 86 65 6 83 19 24
28 71 32 29 3 19 70 68 8 15 40 49 96 23 18 45 46 51 21 55
79 88 64 28 41 50 93 0 34 64 24 14 87 56 43 91 27 65 59 36
32 51 37 28 75 7 74 21 58 95 29 37 35 93 18 28 43 11 28 29
Max value in 2d arrays is 99 at row 5 and col 10

