118k views
4 votes
You are responsible for a rail convoy of goods consisting of several boxcars. You start the train and after a few minutes you realize that some boxcars are overloaded and weigh too heavily on the rails while others are dangerously light. So you decide to stop the train and spread the weight more evenly so that all the boxcars have exactly the same weight (without changing the total weight). For that you write a program which helps you in the distribution of the weight.

Your program should first read the number of cars to be weighed (integer) followed by the weights of the cars (doubles). Then your program should calculate and display how much weight to add or subtract from each car such that every car has the same weight. The total weight of all of the cars should not change. These additions and subtractions of weights should be displayed with one decimal place. You may assume that there are no more than 50 boxcars.
Example 1
In this example, there are 5 boxcars with different weights summing to 110.0. The ouput shows that we are modifying all the boxcars so that they each carry a weight of 22.0 (which makes a total of 110.0 for the entire train). So we remove 18.0 for the first boxcar, we add 10.0 for the second, we add 2.0 for the third, etc.
Input
5
40.0
12.0
20.0
5. 33.
0
Output
- 18.0
10.0
2.0
17.0
-11.0

User Freshest
by
4.1k points

1 Answer

4 votes

Answer:

The program in C++ is as follows:

#include <iostream>

#include <iomanip>

using namespace std;

int main(){

int cars;

cin>>cars;

double weights[cars];

double total = 0;

for(int i = 0; i<cars;i++){

cin>>weights[i];

total+=weights[i]; }

double avg = total/cars;

for(int i = 0; i<cars;i++){

cout<<fixed<<setprecision(1)<<avg-weights[i]<<endl; }

return 0;

}

Step-by-step explanation:

This declares the number of cars as integers

int cars;

This gets input for the number of cars

cin>>cars;

This declares the weight of the cars as an array of double datatype

double weights[cars];

This initializes the total weights to 0

double total = 0;

This iterates through the number of cars

for(int i = 0; i<cars;i++){

This gets input for each weight

cin>>weights[i];

This adds up the total weight

total+=weights[i]; }

This calculates the average weights

double avg = total/cars;

This iterates through the number of cars

for(int i = 0; i<cars;i++){

This prints how much weight to be added or subtracted

cout<<fixed<<setprecision(1)<<avg-weights[i]<<endl; }

User HackAfro
by
4.3k points