In: Computer Science
1. (50 pts) Write a C program that generates a 2D array-of-double and finds the indexes of the largest value stored in the 2D array. Specific requirements: (1) Your main function defines the 2D array a. You will need to prompt the user to specify the size of the 2D array b. You will need to prompt the user to put in numbers to initialize the array (2) Write a function to display the array that is visualized as rows and columns (3) Write a function that finds the indexes of the largest value stored in a 2D array-of double. (4) Print the resulting indexes in your main function.
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
#include <float.h>
#define length 100
//structure for returning row and column
struct result {
int row, column;
};
typedef struct result Struct;
//function for display 2D array of doubles
void Display_array(int rsize,int csize , double arr[length][length]) {
int i, j;
for(i=0;i<rsize;i++){
for(j=0;j<csize;j++){
printf("%lf ", arr[i][j]);
}
printf("\n");
}
}
//function for comaring double elements
int compares(double x, double y){
double xx=0.00000000;
if((x-y)<xx){
return 0;
}
return 1;
}
//function for returning index of largest element
Struct find(int rsize,int csize , double arr[length][length]){
int i, j;
Struct ans;
int l_element_row=-1, l_element_column=-1;
double largest=-DBL_MAX;
for(i=0;i<rsize;i++){
for(j=0;j<csize;j++){
if(compares(arr[i][j], largest)){
largest=arr[i][j];
l_element_row=i, l_element_column=j;
}
}
}
ans.row=l_element_row;
ans.column=l_element_column;
return ans;
}
int main() {
int row=0, column=0;
double arr[length][length];
//struct answer for returning row and column
Struct answer;
//input size of 2D array
scanf("%d %d",&row,&column);
//input element of 2D array
int i, j;
for(i=0;i<row;i++){
for(j=0;j<column;j++){
scanf("%lf",&arr[i][j]);
}
}
//Display 2D array with function Display_array();
Display_array(row, column, arr);
//calling functin find for largest element row and column
answer=find(row , column, arr);
//printing row and column of largest element in 2D array of Double indexing from 0;
printf("Largest Element \n Row=> %d \n Column=> %d", answer.row, answer.column);
return 0;
}