Docsity

C Program to Count Positive, Negative and Zero Numbers in an Array - Prof. Henry Neeman, Exams of Computer Science

Toggle sidebar
bg1
#include <stdio.h>
#include <stdlib.h>
int main()
{ /* main */
/*
*****************************
**** Declaration Section ****
*****************************
*
*****************************
* Named Constant Subsection *
*****************************
*/
const float initial_sum = 0.0;
const int first_element = 0;
const int program_success_code = 0;
const int program_failure_code = -1;
/*
*****************************
* Local Variable Subsection *
*****************************
*
*/
float* values = (float*)NULL;
int elements;
int number_of_elements;
int sum_zero;
int sum_negative;
int sum_positive;
float zero(float* array, int number_of_elements);
float positive (float* array, int number_of_elements);
float negative (float* array, int number_of_elements);
/*
*************************
*** Execution Section ***
*************************
*
**************************
*** Greeting Subsection***
*************************
*
* Describe what the program does.
*/
printf("Hello my friend, This program will count the number of
each");
printf(" of the negative, positive, and zeroes number you'll choose\
n");
/*
pf3
pf4

Partial preview of the text

Download C Program to Count Positive, Negative and Zero Numbers in an Array - Prof. Henry Neeman and more Exams Computer Science in PDF only on Docsity!

#include <stdio.h> #include <stdlib.h> int main() { /* main / /


**** Declaration Section ****



  • Named Constant Subsection *

/ const float initial_sum = 0.0; const int first_element = 0; const int program_success_code = 0; const int program_failure_code = -1; /


  • Local Variable Subsection *

/ float values = (float)NULL; int elements; int number_of_elements; int sum_zero; int sum_negative; int sum_positive; float zero(float array, int number_of_elements); float positive (float* array, int number_of_elements); float negative (float* array, int number_of_elements); /*


*** Execution Section ***



*** Greeting Subsection***


  • Describe what the program does. / printf("Hello my friend, This program will count the number of each"); printf(" of the negative, positive, and zeroes number you'll choose
    n"); /

*** Input Subsection ***


/ printf("Inter the length of the array you want to count\n"); scanf("%d", &number_of_elements); if (number_of_elements < first_element) { printf("ERROR: The number you entered is not valid \n"); exit(program_failure_code); } values = (float)malloc(sizeof(float) * number_of_elements); if (values == (float)NULL) { printf("ERROR: the attempt to allocate\n"); printf(" array a failed.\n"); exit(program_failure_code); } if (number_of_elements == first_element ) { printf("ERROR: the attempt to allocate \n"); exit(program_failure_code); } printf("Inter the values in the array you want to count\n"); for(elements = first_element ; elements < number_of_elements; elements++){ scanf("%f", &values[elements]); } /


Calculation Section


/ sum_negative = negative ( values, number_of_elements); sum_positive = positive ( values, number_of_elements); sum_zero = zero (values, number_of_elements); /


*** Output Subsection ***


/ printf("The Negative count : %d\n", sum_negative); printf("The Positive count : %d\n", sum_positive); printf("The Zeros count : %d\n", sum_zero); free(values); values = (float)NULL;

sum_zero = initial_sum; for(elements = first_element; elements < number_of_elements; elements++) { if ( array[elements] == first_element ) { sum_zero++; } /* for elements */ } return sum_zero; }