Write C only not C Write a C program that generates a floati

(Write C only, not C++)

Write a C program that generates a floating-point array with 8 elements. The array elements will be initialized with the following initializer list: {2.6, 3.9, 4.1, 1.8, 5.7, 9.2, 4.6, 6.3}. Print the original array on the screen (6 spaces per value in total in that 1 for decimal point, i.e. -10.3). Find the max, min, total and average of the given array. Using functions. Output example: Original array: 2.6 3.9 4.1 1.8 5.7 9.2 4.6 6.3 Max value: 9.2 Min value: 1.8 Total: 38.2 Average: 4.8

Solution

code :

===========

#include <stdio.h>
float max(float a[],int n){
float m = a[0];
int i;
for(i=1;i<n;i++)
if(a[i]>m)
m=a[i];
return m;
}
float min(float a[],int n){
float m = a[0];
int i;
for(i=1;i<n;i++)
if(a[i]<m)
m=a[i];
return m;
}
float sum(float a[],int n){
float s = 0;
int i;
for(i=0;i<n;i++)
s += a[i];
return s;
}
int main(void) {
   float array[8] = {2.6,3.9,4.1,1.8,5.7,9.2,4.6,6.3};
   int i;
   printf(\"Original array:\");
   for(i = 0;i<8;i++)
   printf(\" %.1f\",array[i]);
printf(\"\ Max value: %.1f\",max(array,8));
printf(\"\ Min value: %.1f\",min(array,8));
printf(\"\ Total: %.1f\",sum(array,8));
printf(\"\ Average: %.1f\",sum(array,8)/8.0);
   return 0;
}

======================

output

======================

Original array: 2.6 3.9 4.1 1.8 5.7 9.2 4.6 6.3
Max value: 9.2
Min value: 1.8
Total: 38.2
Average: 4.8

(Write C only, not C++) Write a C program that generates a floating-point array with 8 elements. The array elements will be initialized with the following initi

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site