C++ I have two questions [closed]
Please h开发者_如何学Pythonelp me with my homework:
- There is an array a [20] of float numbers, Enter number in the array. Find the sum of negative number.
- Enter 10 integer numbers in the array a [10]. Find the sum of positive numbers.
General psuedo-code to get you in the right direction for finding the sum of positive numbers in the array:
float result = 0;
for(int i = 0; i < array_length; i++)
{
if(array[i] > 0)
result = result + array[i];
}
For the sum of negative numbers, just switch the if-statement. Of course, this isn't exactly C++ code, but I think you can make it work from here pretty easily enough. :)
You access a value with the []
operator, so for instance, accessing the first value from the array a
, you write a[0]
.
After that it is a matter of using for
to iterate over the numbers.
精彩评论