Please write the program in C language. It should first declare the numbers in t
ID: 3850335 • Letter: P
Question
Please write the program in C language. It should first declare the numbers in the array named rates. It shouldn't ask the user to enter the number. The numbers should be declared in the program. Then it should display the numbers and locate the largest and smallest value of the array.
Write a C program that uses a declaration statement to store the following numbers in an array named rates:15.75,22.00,18.75,20.45,6.25,12.52. Your program should display the contents of the array and then locate and display the largest value and the smallest value of the array.
Explanation / Answer
Below is the simple C code , which displays the all the numbers from array and then also displays largest and smallest elements from the array.
I have printed the numbers with two decimal digits precision.
#include <stdio.h>
int main()
{
int n =6;
double rates[6] = {15.75,22.00,18.75,20.45,6.25,12.52};
double largest = rates[0];
double smallest = rates[0];
int i=0;
for(i=0;i<n;i++){
printf("%.2f", rates[i]);
printf(" ");
if(rates[i] < smallest){
smallest = rates[i];
}
if(rates[i] > largest){
largest = rates[i];
}
}
printf("Smallest %.2f", smallest);
printf(" ");
printf("Largest %.2f", largest);
return 0;
}
Output:
15.75
22.00
18.75
20.45
6.25
12.52