In: Computer Science
C Programming:
Problem 1:
Write a function that returns the index of the largest value stored in an array-of- double. Test the function in a simple program
Problem 2:
Write a function that returns the difference between the largest and
smallest elements of an array-of-double. Test the function in a
simple program
1.
Program:
#include <stdio.h>
int max_index(double arr[], int n);
int main(void) {
int n,index; // variable Declaration
double x[50];
printf("Enter number of elements in the array: ");
scanf("%d",&n); // Accept the number
printf("Enter array elements: \n");
for(int i=0; i<n; i++) // loop to accept array elements
scanf("%lf",&x[i]); // accept array elements
index = max_index(x, n); // calling function
printf("Max element index is : %d\n",index); // print max index
return 0;
}
int max_index(double arr[], int n){
int index; // variable Declaration
double max = arr[0]; // variable Declaration
for(int i=0; i<n; i++) { // loop to find max element index
if(max<arr[i]) {
max=arr[i];
index = i; // index contain max element index
}
}
return index; // return max index
}
Output:

2.
Program:
#include <stdio.h>
double diffLargeSmall(double arr[], int n);
int main(void) {
int n; // variable Declaration
double x[50];
printf("Enter number of elements in the array: ");
scanf("%d",&n); // Accept the number
printf("Enter array elements: \n");
for(int i=0; i<n; i++) // loop to accept array elements
scanf("%lf",&x[i]); // accept array elements
double difference = diffLargeSmall(x, n); // calling function
printf("Max element index is : %.2lf\n",difference); // print max index
return 0;
}
double diffLargeSmall(double arr[], int n){
double large = arr[0]; // variable Declaration
double small = arr[0]; // variable Declaration
for(int i=1; i<n; i++) { // loop to find largest and smallest
if(large<arr[i]) {
large=arr[i]; // calculate large
}
if(small>arr[i]) {
small=arr[i]; // calculate small
}
}
return large-small; // return difference of large and small
}
Output:
Any doubts leave a comment
Please Rate My Answer