0

I'm learning c++ by myself and I'm currently having trouble making the sum between array values (if >0 else <0).

This is what I've been doing (bilIniziale is my array):

for (int i=0;i<10;i++){
    int sum = 0;
    if(bilIniziale[i] > 0) {
        sum+=bilIniziale[i];
    }else{
       sum+=bilIniziale[i];
    }
}

I also tried with sum = sum + bilIniziale[i]; but no difference.

In the terminal at the moment its only shows the element >0 or <0 but without summing between them; how can I do it?

8
  • What do you mean? You're just summing any value that is non-zero, which is the same as summing all values. Did you want to sum the absolute value or something? Of course, the main problem is that sum is a local variable in your loop, so it is reset to zero every time and not visible outside the loop. Commented Sep 19, 2022 at 8:14
  • 2
    Your test doesn't make sense, because you're doing the same thing whether it's true or false. Commented Sep 19, 2022 at 8:16
  • What I need is to find in the array the values that are <0 and sum them; viceversa for >0. Commented Sep 19, 2022 at 8:37
  • the result of the IF is correct because it's only showing the elements of array >0 and else viceversa so the cycle and ifelse statement seems right, but I need to sum them Commented Sep 19, 2022 at 8:38
  • If you want two sums, you can't store them in just one variable, can you? And you can't declare a variable inside a loop if you need its value after the loop. Commented Sep 19, 2022 at 8:41

2 Answers 2

2

Since you want the variable sum to persist outside of the loop you need to move the declaration outside of the loop

int sum = 0;
for (int i=0;i<10;i++){
    if(bilIniziale[i] > 0) {
        sum+=bilIniziale[i];
    }else{
       sum+=bilIniziale[i];
    }
}

In your version a new sum variable gets created and set to zero each time round the loop.

Also as stated in the comment above. The if statement makes no sense because you do the same thing whether it's true or false. So the above can be simplified

int sum = 0;
for (int i = 0; i < 10; i++) {
    sum += bilIniziale[i];
}

EDIT

So it seems the requirement is to make two sums, one of the negative elements the other of the positive elements. So we need two variables, one for each sum. Something like this

int positive_sum = 0;
int negative_sum = 0;
for (int i = 0; i < 10; i++){
    if (bilIniziale[i] > 0) {
        positive_sum += bilIniziale[i];
    } else {
        negative_sum += bilIniziale[i];
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

Okay. The solution u gave me it's correct, to have the result of all the elements of the Array. But I was asking how to sum the elements <0 and >0, differentiate between them.
@Natao OK I'll edit my answer.
0

With std::accumulate, you might do:

const int positive_sum =
    std::accumulate(std::begin(bilIniziale), std::end(bilIniziale), 0,
                    [](int acc, int value){ return value > 0 ? acc + value : acc; });
const int negative_sum =
    std::accumulate(std::begin(bilIniziale), std::end(bilIniziale), 0,
                    [](int acc, int value){ return value < 0 ? acc + value : acc; });

Demo

With regular for-range loop, it would be:

int positive_sum = 0;
int negative_sum = 0;
for (auto value : bilIniziale) {
    if (value < 0) {
        negative_sum += value;
    } else { // NO special case for value == 0 as += 0 is noop
        positive_sum += value;
    }
}

Comments

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.