


Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
A c program that counts the number of positive, negative, and zero numbers in an array. The user is prompted to input the length of the array and its elements. The program then calculates and displays the number of negative, positive, and zero numbers in the array.
Typology: Exams
1 / 4
This page cannot be seen from the preview
Don't miss anything!



#include <stdio.h> #include <stdlib.h> int main() { /* main / /
**** Declaration Section ****
/ const float initial_sum = 0.0; const int first_element = 0; const int program_success_code = 0; const int program_failure_code = -1; /
/ 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***
*** 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; }