1
The activity asks me to report a number of wagons and then asks me to assign a weight to each of them. I calculate the average of these weights and then subtract the average of the weights of each of them to reach a common weight.
This is where the number of wagons and their respective weights are reported:
int boxCarsQuantity;
int readValue
int numberPosition = 0;
int arrayWeight[0];
int sum=0;
int average=0;
printf("Insira a quantidade de vagões\n");
scanf("%d",&boxCarsQuantity);
for(int i = 0; i<boxCarsQuantity; i++)
{
printf("Insert the weight of a Boxcar\n");
scanf("%d",&readValue);
arrayWeight[numberPosition] = readValue;
sum=sum+arrayWeight[numberPosition];
numberPosition++;
}
Here I calculate the average weights of the wagons:
average=sum/boxCarsQuantity;
And this is where I display the average, and this is where I’m supposed to display how much I’m supposed to subtract from each car to get a common weight between the wagons:
printf("A média é: %d\n",average);
for(int e=0; e<boxCarsQuantity; e++)
{
printf("%d\n",arrayWeight[numberPosition2]-average);
numberPosition2++;
}
However, the last part is not executing correctly as I am not able to subtract the positions of an array. How can I do this?
For this problem you will have to go through the array with a
for
and take the total weight of the wagon and subtract theaverage
.– bruno101
Should your arrayWeight array actually be declared with 0 positions? Why don’t you use the for variable as the index of your array?
– anonimo