How to subtract the same number from an array

Discussion in 'C' started by Guilherme Romero, Dec 28, 2021.

Tags:
  1. Guilherme Romero

    Guilherme Romero New Member

    Joined:
    Nov 19, 2021
    Messages:
    10
    Likes Received:
    0
    Trophy Points:
    1
    Gender:
    Male
    I'm doing an exercise of which statement is below:

    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.0 33.0

    Output -18.0 10.0 2.0 17.0 -11.0

    For that I have done so far:
    Code:
    #include <stdio.h>
    
    int main()
    {
       int num, cellNumber = 0, sum = 0, i;
       double w = 0, arr_w[50], obj, arr_new[50];
    
    
       scanf("%d", &num);
    
       for (i = 0; i<num;i++){
           scanf("%lf", &w);
           arr_w[cellNumber] = w;
           cellNumber = cellNumber + 1;
           sum = sum + arr_w;
       }
    
       obj = sum/num;
    
       arr_new = ???????
    
      
       return 0;
    }
    
    After creating an array with the inputs, I calculated the value of the distributed weight (obj). Now I need to create the output vector (new) by doing the subtraction (obj - w) (in the example: 22- w). Should I create a new vector only with values of obj and subtract each value of arr_w?

    How could I do that?
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice