1

I am trying to code a function where it goes through Rainfall_amounts array and then stores each value in the Total_rainfall and then it calculates the sum of the arrays {7.5, 7.0, 6.3, 0.8, 0.5}.

int main ()
{
    float Total_rainfall[5] = {0};
    float Rainfall_amounts[5] =  {7.5, 7.0, 6.3, 0.8, 0.5}
    for (int i = 0; i > 5; i++){
        Total_rainfall = Rainfall_amounts[i]
    }
    return 0;
}
0

2 Answers 2

2

I am trying to code a function where it goes through Rainfall_amounts array and then stores each value in the Total_rainfall and then it calculates the sum of the arrays {7.5, 7.0, 6.3, 0.8, 0.5}.

You have the following problems:

  1. your loop condition is wrong "i > 5". Currently, your loop never gets executed since i starts with 0; change it to "i < 5";
  2. you need to create and additional variable to store the sum of the array;
  3. you need to pass the index to the array Total_rainfall. Instead of Total_rainfall = rainfall_amounts[i]; you want Total_rainfall[i] = rainfall_amounts[i];
  4. (if you can) you should opt for double instead of float.

All corrected:

int main ()
{
    double sum = 0.0;
    double Total_rainfall[5] = {0};
    double rainfall_amounts[5] =  {7.5, 7.0, 6.3, 0.8, 0.5};
    for (int i = 0; i > 5; i++){
        Total_rainfall[i] = rainfall_amounts[i];
        sum += rainfall_amounts[i];
    }
    // Do something with the variable total_rainfall
    return 0;
}

If you want the accumulated sums then you need to do the following:

int main ()
{
    double sum = 0.0;
    double Total_rainfall[5] = {0};
    double Rainfall_amounts[5] =  {7.5, 7.0, 6.3, 0.8, 0.5}
    for (int i = 0; i > 5; i++){
        sum += Rainfall_amounts[i];
        Total_rainfall[i] = sum;
        
    }
   for (int i = 0; i < 5; ++i) {
       printf("Acc Sums %f\n", Total_rainfall[i]);
    }
  
   return 0;
}

Output:

7.5
14.5
20.8
21.6
22.1
Sign up to request clarification or add additional context in comments.

Comments

1

If you want only a single sum, Total_rainfall can be just a single float, rather than a array.

#include <stdio.h>

int main ()
{
  /* Initialize the accumulator to 0 */
  float Total_rainfall = 0.0f;
  float Rainfall_amounts[5] =  {7.5, 7.0, 6.3, 0.8, 0.5};
  
  /* Loop through Rainfall_amounts, adding each value to total sum */
  for (int i = 0; i < 5; ++i) Total_rainfall += Rainfall_amounts[i];

  /* Print the total */
  printf("Total rainfall %f\n", Total_rainfall);
  return 0;
}

However, if you are interested in the running total, then Total_rainfall must also be an array:

#include <stdio.h>

int main ()
{
  /* Initialize the accumulator to 0 */
  float Total_rainfall[5] = {0};
  float Rainfall_amounts[5] =  {7.5, 7.0, 6.3, 0.8, 0.5};
  
  /* Loop through Rainfall_amounts, adding each value to running total */
  for (int i = 0; i < 5; ++i) {
    /* If not the first iteration, initialize with previous iterations value */
    if (i) Total_rainfall[i] = Total_rainfall[i-1];
    Total_rainfall[i] += Rainfall_amounts[i];
  }

  /* Print the total */
  for (int i = 0; i < 5; ++i) {
    printf("Total rainfall %f\n", Total_rainfall[i]);
  }
  return 0;
}

6 Comments

It does help to explain what you changed as well as show the code.
Hi Jacob why do you need the if in if (i) Total_rainfall[i] = Total_rainfall[i-1]; ?
So that I do not do Total_rainfall[-1];. i.e. for the first i I just want the first element of Rainfall_amounts to initialize the sum (so I can skip getting the "last" one, as it doesn't exist yet). Each successive iteration initializes Total_rainfall[i] to the previous value, making a running total.
From my understanding I think OP actually wants "Total_rainfall[i] = Rainfall_amounts[i]"
You may very well be right! The text description certainly suggests it, but then OP calls their variable total, which makes no sense if it is to just hold a copy... Not well worded question either way.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.