How can I subtract from a multi-position array with the same number in C?

Asked

Viewed 109 times

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 the average.

  • Should your arrayWeight array actually be declared with 0 positions? Why don’t you use the for variable as the index of your array?

1 answer

2

As the comments have already said, your array has size 0, there is no way to store values in it. You need to set the array to a size large enough to store all the values, or else set the array size at runtime with malloc.

int boxCarsQuantity;
int *arrayWeight;

printf("Insira a quantidade de vagões\n");
scanf("%d", &boxCarsQuantity);
arrayWeight = (int*) malloc(sizeof(int) * boxCarsQuantity);

Other tips: declare Average as double type for floating point split results, and use for iterator to access array positions, there is no reason to declare numberPosition.

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int boxCarsQuantity;
    int readValue;
    int *arrayWeight;
    int sum = 0;
    double average = 0;

    printf("Insira a quantidade de vagões\n");
    scanf("%d", &boxCarsQuantity);
    arrayWeight = (int*) malloc(sizeof(int) * boxCarsQuantity);

    for(int i = 0; i < boxCarsQuantity; i++)
    {
        printf("Insert the weight of a Boxcar\n");
        scanf("%d", &readValue);

        arrayWeight[i] = readValue;
        sum += arrayWeight[i];
    }

    average = (double) sum/boxCarsQuantity;
    printf("A média é: %.2f\n", average);

    for(int j = 0; j < boxCarsQuantity; j++)
    {
        printf("%.2f\n", arrayWeight[j] - average);
    }

    return 0;
}
  • Don’t forget that for each malloc, there must be a free.

  • printf("Thank you :D");

Browser other questions tagged

You are not signed in. Login or sign up in order to post.