Positive and negative. (from Chapter 11) Write a function, named sums(), that ha
ID: 3826060 • Letter: P
Question
Positive and negative. (from Chapter 11)
Write a function, named sums(), that has two input parameters; an array, called Input of doubles; 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
SAMPLE INPUT:
-123.45
-234.56
576.1
-9.345
675.2
100
-10
1654.45
765.89
0 (NOT in computation)
SAMPLE OUTPUT:
YourName Program#8 CSCI1110
Input Read:
9999.9999
9999.9999
...
Statistics:
Desc Number Total: Average:
Positive 99 99999.9999 9999.9999
Negative 99 99999.9999 9999.9999
Overall 99 99999.9999 9999.9999
----PARTIAL SAMPLE OUTPUT:
Negative 4 -377.3550 -94.3388
Your function call will look something like sums(input[], n, &sumPos, &sumNeg, &countPos, &countNeg)
RUBRIC/CHECKLIST
Output Correct – including echo the input with implied decimal point aligned
Indentation
Comments
Used defines for all constants
Read into the array until a 0 is entered, only one set of data is used
Output numbers should aligned
Functions with prototypes ‘sums’ must be included. More functions can be used but NOT less.
Pointers MUST be used
Output copied into Input
Filename
Upload to Bb9
RUBRIC/CHECKLIST
Output Correct – including echo the input with implied decimal point aligned
Indentation
Comments
Used defines for all constants
Read into the array until a 0 is entered, only one set of data is used
Output numbers should aligned
Functions with prototypes ‘sums’ must be included. More functions can be used but NOT less.
Pointers MUST be used
Output copied into Input
Filename
Upload to Bb9
Explanation / Answer
#include <stdio.h>
void sums(double input[], int n, double *sumPos, double *sumNeg, int *countPos, int *countNeg)
{
*sumPos = 0;
*sumNeg = 0;
*countPos = 0;
*countNeg = 0;
int i;
for(i = 0; i < n; i++)
{
if(input[i] > 0)
{
*sumPos = *sumPos + input[i];
*countPos = *countPos + 1;
}
else
{
*sumNeg = *sumNeg + input[i];
*countNeg = *countNeg + 1;
}
}
}
int main()
{
int n = 10;
double input[n];
double sumPos;
double sumNeg;
int countPos;
int countNeg;
int i;
for(i = 0; i < n; i++)
{
double num;
scanf("%lf", &num);
if (num == 0)
break;
input[i] = num;
}
n = i;
sums(input, n, &sumPos, &sumNeg, &countPos, &countNeg);
printf("Input Read: ");;
for(i = 0; i < n; i++)
{
printf("%f ",input[i]);
}
printf(" ");
double posAvg = 0;
double negAvg = 0;
if(countPos != 0)
posAvg = sumPos/countPos;
if(countNeg != 0)
negAvg = sumNeg/countNeg;
double avg = 0;
if(n != 0)
avg = (sumPos + sumNeg)/n;
printf("Statistics: ");;
printf("Desc Number Total: Average: ");
printf("Positive %d %f %f ", countPos, sumPos, posAvg);
printf("Negative %d %f %f ", countNeg, sumNeg, negAvg);
printf("Overall %d %f %f ", n, sumNeg, (sumNeg + sumPos), avg);
return 0;
}