Functions with Arrays (HELP PLEASE)

I'm having difficulty creating a function that adds all the positive numbers in an array and adding negative numbers too. SEE SAMPLE BELOW

SAMPLE DIALOGUE
Type 5 numbers for the array: 3 1 2 -4 -5

Sum of all positive values: 6

Sum of all negative values: -9
Last edited on
The first thing you should do when faced with a programming problem is to try and break it down into a series of steps. Then you can code and test each step in turn.

1. Read 5 numbers from the user
2. Iterate over each element of the array
3. If an element is positive, add it to the sum of positive values
4. If an element is negative, add it to the sum of negative values
5. Display the values of the two sums.
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include < iostream>


const unsigned int NVALUES = 5;
signed int values[NVALUES];

unsigned int i = 0;
signed int NegSum=0;
signed int PosSum = 0;
while(i < NVALUES)
{
    signed int value;
    std::cout << "Enter value " << i << std::endl;
    std::cin >> value;
    value < 0 ? NegSum += value : PosSum += value;
    values[i] = value;
    i++;
}

//print Neg and Pos sums here... 
Last edited on
Topic archived. No new replies allowed.