Write a function, named sums(), that has two input parameters; an array, called
ID: 3657743 • Letter: W
Question
Write a function, named sums(), that has two input parameters;
an array, called Input of floats; and an integer which is the number of values stored in the array.
Compute the sum of the positive values in the array and the sum of the negative values. Also count the number of values in each category. Return these four answers through output parameters.
Write a main program that reads no more than 10 real numbers and stores them in an array. Stop reading numbers when a 0 is entered.
Call the sums() function and print the answers it returns. Also compute and print the average values of the positive and negative sets.
Align decimal points on numbers
Explanation / Answer
void sum(
// two input parameters; an array, called Input of floats; and an integer which is the number of values stored in the array.
float Input[], int size,
// Return these four answers through output parameters.
float *sum_of_positive, int *count_of_positive, float *sum_of_negative, int *count_of_negative
)
{
// local variables for output
float sum_pos = 0;
int count_pos = 0;
float sum_neg = 0;
int count_neg = 0;
// loop through array
int i;
for(i = 0; i < size; i++)
{
// check if positive
if(Input[i] > 0)
{
sum_pos += Input[i];
count_pos++;
}
// check if negative
else if(Input[i] < 0)
{
sum_neg += Input[i];
count_neg++;
}
}
// return output through output parameters
*sum_of_positive = sum_pos;
*count_of_positive = count_pos;
*sum_of_negative = sum_neg;
*count_of_negative = count_neg;
}
I believe at this point you can handle the test code.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.