
Patrick B. answered 05/11/21
Math and computer tutor/teacher
int addNegatives( int * A, int n)
{
int sum=0;
for (int iLoop=0; iLoop<n; iLoop++)
{
if (A[iLoop]<0)
{
sum += A[iLoop];
}
}
return(sum);
}
Kat H.
asked 05/10/21The existing code is trying to call a function addNegatives that should take an array of ints and its size and return the sum of JUST the negative numbers from the array. Your task is to write the missing function.
Example: given input of an array {-4 2 1 -3} and size 4 you should print -7
Code:
#include <iostream>
using namespace std;
//Do not modify anything on or above the line below this
//YOUR_CODE_BELOW
//YOUR_CODE
//YOUR_CODE_ABOVE
//Do not modify anything on or below the line above this
int main()
{
int NUM_VALUES; //cheating to make this flexible
cin >> NUM_VALUES;
int values[NUM_VALUES];
//Read in values
for(int i = 0; i < NUM_VALUES; i++) {
cin >> values[i];
}
int sum = addNegatives(values, NUM_VALUES);
cout << sum;
}
Patrick B. answered 05/11/21
Math and computer tutor/teacher
int addNegatives( int * A, int n)
{
int sum=0;
for (int iLoop=0; iLoop<n; iLoop++)
{
if (A[iLoop]<0)
{
sum += A[iLoop];
}
}
return(sum);
}
Get a free answer to a quick problem.
Most questions answered within 4 hours.
Choose an expert and meet online. No packages or subscriptions, pay only for the time you need.