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

Answers

Answer 1

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;

}

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;    }


Related Questions

What are some examples and non-examples of digital security?

Answers

Answer:

Devices such as a smart card-based USB token, the SIM card in your cell phone, the secure chip in your contactless payment card or an ePassport are digital security devices

Other Questions
the ranas maintained relationship mainly with britainbefore 2007 bs.what might be the reason? Define what coal is, explain how it forms, and what it isused for today how many months are in 59 days??? What is the approximate length of the United States border with Canada? o 2,500 miles O 4,500 miles O 5,500 miles O 3,500 miles what is the value of 4/152/3 Grades on a science test are shown in the box plot below.Find the correct data that the box plot represents.A) 78, 81, 89, 75, 76, 91, 100, 63, 80, 87, 91, 95B) 55, 81, 89, 75, 76, 91, 100, 63, 80, 87, 91, 95C) 78, 81, 45, 75, 76, 91, 100, 63, 80, 87, 91, 95D) 78, 81, 89, 75, 76, 91, 100, 63, 80, 87, 91, 110 Can someone please help me I will mark u brilliant Jensen recorded the daily average temperature (in degrees Fahrenheit) at his home over two weeks' time. His results are shown below. 48, 56, 63, 67, 58, 60, 63, 72, 69, 59, 63, 68, 73, 64. What was the range of the temperatures? __________ degrees Fahrenheit Last month, Maria purchased a new cell phone for $500. The store manager told her that her new cell phone would depreciate by 70% every 6 months. Maria thinks she will want to replace her phone after a while. What will be the value (V) of her phone if she tries to trade it in after 2 years? Select all that apply. -6/5-2/3v+4/15+1/3v Combining like terms to create an equivalent expression boy in the striped pajamasWhich of the tasks was Mother NOT concerned about before having The Fury over for dinner?Group of answer choicesShe would have to order a new dress for the occasion.The windows would have to be washed.The maid's and butler's uniforms had to be washed and pressed.The crockery and glasses needed polishing. answer the question for 15 points and brainlest The development of the British cuisine described in the excerpted speech above is best seen as an example of which of the following? a The resistance to immigration by nativist groups b The global spread of western popular and consumer culture c The spread of culture through new communication technology d The effects of migration by former colonial subjects to imperial metropoles A prospective groom, who is not affected by cystic fibrosis (CF), has a sister with cystic fibrosis, an autosomal recessive disease. Their parents are also not affected. The brother plans to marry a woman who has no history of CF in her family. What is the probability that they will have a child with CF? They are both Caucasian and the overall frequency of CF in the Caucasian population is 1/2500 - that is, 1 affected child per 2500 (assume the population meets the Hardy-Weinberg assumptions). The floor plan for a house is shown.Vertical lengths and horizontal lengths are given.A drawing shows the floor plan for a house. The house is composed of three rectangles. The first rectangle is twenty feet long and twenty-four feet wide. The second rectangle is twenty-four feet long and eight plus twenty-four plus eight feet wide. The third rectangle is twenty-eight feet long and twenty-four feet wide.What is the floor area in square feet? What was the purpose of letting the transformed cells sit in LB for a few minutes before spreading them onto the plates? A. This allows time for the cells to express the antibiotic resistance gene B. This allows the cells to take up the plasmid after the heat shock procedure C. This allows time for the cells to warm up before plating D. This allows cells time to start glowing green PLZZZ HELP ME I WILL REPORT ANY LINKSWhich statement correctlycompares humidity in warm and cold air?A. Warm air holds less moisture than cold air.B. Warm air will always hold the same amount of moisture as cold air.C. Warm air can hold more moisture than cold air.D. Neither warm or cold air can contain water vapor. Paid $54,000 cash to replace a motor on equipment that extends its useful life by four years. Paid $270 cash per truck for the cost of their annual tune-ups. Paid $216 for the monthly cost of replacement filters on an air-conditioning system. Completed an addition to a building for $303,750 cash. 1. Classify the above transactions as either a revenue expenditure or a capital expenditure. 2. Prepare the journal entries to record the four transactions from part 1. laveUse the drop-down menus to identify the parts of awave.AB:CD Can someone help me with this ? it is relative frequency tables