Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Rewrite the program from Homework #11, to count the number of negative values, t

ID: 3824369 • Letter: R

Question

Rewrite the program from Homework #11, to count the number of negative values, the number of positive values and the number of zeros in a floating point array. Instead of performing these counts inside the main program, each count should be calculated by a USER-DEFINED FUNCTION. Specificially, the program should: • greet the user, • prompt for and input the length of the array; • idiotproof the array length; • dynamically allocate the array; • check that the allocation was successful; • prompt for and input the values in the array; • count the number of negative values by calling a USER-DEFINED FUNCTION; • count the number of positive values by calling a USER-DEFINED FUNCTION; • count the number of zeros by calling a USER-DEFINED FUNCTION; • output the numbers of negative, positive and zero values in the array; • deallocate the array. NOTE: You MUST calculate each of the three counts in ITS OWN user-defined function; you are ABSOLUTELY FORBIDDEN to calculate more than one of them in the same user-defined function. You DON’T have to use comments. Otherwise, all rules for Programming Projects ( using language C)

Explanation / Answer

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int countTheNegativeValues(float floatArray[],int length);
int countThePositiveValues(float floatArray[],int length);
int countTheZeroValues(float floatArray[],int length);

int main()
{
   printf("Welcome! ");
int length=0;
   while (length == 0)
   {
       printf("Please enter the length for the array: ");
       if(scanf("%d",&length) == EOF)
       {
           length = 0;
       }      
   }
float *floatArray = (float *)malloc(length * sizeof(float));
for (int i = 0; i < length; i++)
   {
       printf(" Please enter an element at position %d :",i);
       float tempValue;
       scanf("%f",&tempValue);
       floatArray[i] = tempValue;
   }
   int negativeCount = countTheNegativeValues(floatArray,length);
   int positiveCount = countThePositiveValues(floatArray,length);
   int zeroCount = countTheZeroValues(floatArray,length);
   printf(" Number of negative numbers are: %d",negativeCount);
   printf(" Number of positive numbers are: %d",positiveCount);
   printf(" Number of zeros are: %d",zeroCount);
free(floatArray);
   return 0;
}

int countTheNegativeValues(float floatArray[],int length)
{
   int count = 0;
   for (int i = 0; i < length; i++)
   {
       if (floatArray[i] < 0){
           count++;
       }
   }
   return count;
}

int countThePositiveValues(float floatArray[],int length)
{
   int count = 0;
   for (int i = 0; i < length; i++)
   {
       if (floatArray[i] > 0){
           count++;
       }
   }
   return count;
}

int countTheZeroValues(float floatArray[],int length)
{
   int count = 0;
   for (int i = 0; i < length; i++)
   {
       if (floatArray[i] == 0){
           count++;
       }
   }
   return count;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote