WRITE A PROGRAM that counts the number of negative values, the number of positiv
ID: 3775451 • Letter: W
Question
WRITE A PROGRAM that counts the number of negative values, the number of positive values and the number of zeros in a float array. Specifically, the program should: greet the user. prompt for and input the length of the array; idiot proof 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; count the number of positive values; count the number of zeros; output the numbers of negative, positive and zero values in the array; deallocate the array. FORBIDDEN to calculate more than one of them in the same for loop. You DON'T have to use comments. Otherwise, all rules for Programming Projects (through PP#5) apply. For this Homework question only, you may submit a script file of the standard kind, in place of writing the answer to this question by hand. You DON'T need to submit a cover or a summary, but the first page of the script file should be clearly labeled with your name and "Quiz #11" at the top. You MUST run the program using an array length of at least 10 and at least two of each kind of value. If you choose not to create, print and submit such a script file, then you w ill instead have to submit a handwritten source code during the quiz. (You CANNOT generate a script file if you're submitting a handwritten solution.) If you use ANY resources Other than Dr. Neeman, the TAs (Ivanov. Mirza. Narasimhan), the course textbook or the materials posted on the cou.se webpage, you MUST reference them on the quiz. THIS INCLUDES CLASSMATES, FRIENDS, PROFESSORS, ONLINE RESOURCES, ETC.Explanation / Answer
int main(void)
{
int num, i, countnegative ,countpositive,countzeros;
countnegative = countpositive = countzeros= 0;
float *ptr;
printf(" Enter number of elements: ");
scanf("%d", &num);
ptr = (float*) malloc(num * sizeof(float)); //dynamic memory allocated using malloc
if(ptr == NULL)
{
printf(" Error! memory not allocated."); //check if memory is allocated successfully
exit(0);
}
printf(" Enter elements of array: ");
for(i = 0; i < num; ++i)
{
scanf("%f", ptr + i); //input elements of array and check if they are positive,negative or zero and increment counts
if( *(ptr + i) < 0)
countnegative++;
else if( *(ptr + i) == 0)
countzeros++;
else if( *(ptr + i) > 0)
countpositive++;
}
printf(" Number of negative values in the array = %d", countnegative);
printf(" Number of positive values in the array = %d", countpositive);
printf(" Number of zero values in the array = %d", countzeros);
free(ptr); //deallocate memory
return 0;
}
output:
Enter number of elements: 5
Enter elements of array: 23.56 -78 0 34 56
Number of negative values in the array = 1
Number of positive values in the array = 3
Number of zero values in the array = 1
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.