Define a function named locateLargest() that finds the location of the largest e
ID: 3683393 • Letter: D
Question
Define a function named locateLargest() that finds the location of the
largest element in a two-dimensional array of type double .
void locateLargest(const double arr[][4], int location[])
The location of the largest element will be stored in the one-dimensional
array location[] that contains two elements . These two elements represent the
row and column indices of the largest element in the two-dimensional array
arr[][4].
Write a test program that prompts the user to enter a 3 by 4
two-dimensional array and uses the locateLargest() function to find and
display the location of the largest element in the array .
Explanation / Answer
#include <stdio.h>
int location[5];
int n;
void locateLargest(const double arr[4][4], int location[]){
int i=0,j=0,k=0;
double max;
for(i=0;i<n;i++){
max = arr[i][0];
for(j=0;j<4;j++){
if(max <= arr[i][j]){
max = arr[i][j];
location[k]=j;
}
}
k++;
}
}
int main(void) {
// your code goes here
double arr[5][5];
int i=0,j=0;
printf("Enter n:");
scanf("%d",&n);
printf("Enter elements");
for(i=0;i<n;i++){
for(j=0;j<4;j++){
scanf("%d",&arr[i][j]);
}
}
locateLargest(arr,location);
for(i=0;i<n;i++){
printf("%d ",location[i]);
}
return 0;
}
output:
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.