Functions with prototypes ‘sums’ must be included. More functions can be used bu
ID: 3825921 • Letter: F
Question
Functions with prototypes ‘sums’ must be included. More functions can be used but NOT less.
Pointers MUST be used.
Using Codeblocks.
Functions with prototypes ‘sums’ must be included. More functions can be used but NOT less.
Pointers MUST be used.
SUMS Positive and negative. (from Chapter 11) Write a function, named sum 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 sum 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: Your Name Program 8 CSCI1110 Input Read: 9999.9999 9999.9999 Statistics: Total Average Desc Number 99999.9999 Positive 99 9999.9999 99999.9999 9999.9999 Negative 99 Overall 99 99999.9999 9999.9999 PARTIAL SAMPLE OUTPUT: -377.3550 94.3388 Negative Your f call will look something like sums (input nr &sumPos;, sumNeg & countPos, & countNeg) unctionExplanation / 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;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.