124k views
4 votes
LAB:

Warm up:
People's weights Output each floating-point value with two digits after the decimal point, which can be achieved as follows:
printf("%0.2lf", yourValue);
(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:
Enter weight 1: 236.0
Enter weight 2: 89.5
Enter weight 3: 142.0
Enter weight 4: 166.3
Enter weight 5: 93.0
You entered: 236.00 89.50 142.00 166.30 93.00
(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)
Ex:
Enter weight 1: 236.0
Enter weight 2: 89.5
Enter weight 3: 142.0
Enter weight 4: 166.3
Enter weight 5: 93.0
You entered: 236.00

User Right Leg
by
4.6k points

2 Answers

4 votes

Final answer:

To solve this question, you will need to use arrays and perform calculations on the data entered by the user. Here are the steps to follow: Prompt the user, output the array's numbers, calculate the total weight, average, and find the maximum weight.

Step-by-step explanation:

To solve this question, you will need to use arrays and perform calculations on the data entered by the user. Here are the steps to follow:

Prompt the user to enter the weights of five people and store them in an array of doubles.

Output the array's numbers on one line, each number followed by one space.

Calculate the total weight by summing the array's elements.

Calculate the average of the array's elements.

Find the maximum element in the array.

For example, if the user enters weights of 236.0, 89.5, 142.0, 166.3, and 93.0, the program should output:

You entered: 236.00 89.50 142.00 166.30 93.00

The total weight would be 726.8, the average would be 145.36, and the maximum weight would be 236.0.

User Shine
by
4.6k points
4 votes

Answer:

Following is the program in the C language

#include <stdio.h> // header file

int main() // main function

{

float weight1[10]; // array declaration

float sum=0,max1,t; // variable declaration

for(int k = 0; k < 5; k++) //iterating the loop

{

printf("Enter weight %d: ",k+1);

scanf("%f",&weight1[k]); // Read the array by user

}

printf("\\");

printf("You entered: ");

max1=weight1[0];

for(int k = 0; k < 5 ; k++)

{

sum=sum+weight1[k];

if(max1 < weight1[k]) // check condition for highest element

{

max1=weight1[k];

}

printf("%.2lf ",weight1[k]);

}

t=sum/5.0; // find average

printf("\\Total weight: %.2lf\\",sum); // displat total

printf("Average weight: %.2lf\\",t); // display Average

printf("Max weight: %.2lf\\",max1); // display maximum value

return 0;

}

Output:

Enter weight 1: 1

Enter weight 2: 2

Enter weight 3:3

Enter weight 4:4

Enter weight 5: 5

You entered: 1.00 2.00 3.00 4.00 5.00

Total weight: 15.00

Average weight: 3.00

Max weight: 5.00

Step-by-step explanation:

Following are description of program :

  • Declared a array "weight1" of type "float" .
  • Declared a variable sum,t max1 of type "float ".
  • Iterating the loop for taking the input by the user by using scanf statement .
  • Iterating the loop for calculating the maximum value in "max1" variable ,total in "sum" variable .
  • In the "t" variable we calculated the average.
  • Finally we print the value of the maximum ,average ,total and respective array that are mention in the question .

User Pradeep Pansari
by
3.9k points