Write in C Language please: (1) Prompt the user to enter five numbers, being fiv
ID: 3589835 • Letter: W
Question
Write in C Language please:
(1) Prompt the user to enter five numbers, being five people's weights. Store the numbers in an array of doubles. Output the array's numbers on one line, each number followed by one space. (2 pts)
Ex:
(2) Also output the total weight, by summing the array's elements. (1 pt)
(3) Also output the average of the array's elements. (1 pt)
(4) Also output the max array element. (2 pts)
Remaining 4 points will consist of: formatting 2pts and comments 2pts
Ex:
#include <stdio.h>
int main(void) {
/* Type your code here. */
return 0;
}
Explanation / Answer
Program:
#include <stdio.h>
int main(void) {
// your code goes here
//create a double array with name weight and size 5
double weight[5];
//create a variable sum that stores the value of the sum of weights
//initialise it with 0
double sum = 0;
//create a variable to store maximum value of weight
//initialise it with -1
double max = -1;
for(int i=0;i<5;i++){
//taking the input
printf("Enter weight %d: ",i+1);
scanf("%lf",&weight[i]);
printf(" ");
//updating the sum variable or adding current weight to previous sum
sum += weight[i];
//checking if current weight is greater than max
//if it's greater put value of current weight in max
if( weight[i] > max ){
max = weight[i];
}
}
//printing the weights in single line
printf(" You entered: ");
for(int i=0;i<5;i++){
printf("%lf ",weight[i]);
}
printf(" ");
//printing the total weight
printf("Total weight: %lf ",sum);
//average weight = total weight / number of weights
double average_weight = sum / 5 ;
printf("Average weight: %lf ",average_weight);
//printing the maximum weight
printf("Max weight: %lf",max);
return 0;
}
Sample Output:
Enter weight 1: 236
Enter weight 2: 89.5
Enter weight 3: 142
Enter weight 4: 166.3
Enter weight 5: 93
You entered: 236.000000 89.500000 142.000000 166.300000 93.000000
Total weight: 726.800000
Average weight: 145.360000
Max weight: 236.000000
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.