Write a program that counts the number of negative values, the number of positiv
ID: 3824176 • 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 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.
Please provide answer in C
Explanation / Answer
#include<stdio.h>
#include<stdlib.h>
int find_positive(float[] ,int );
int find_negative(float[] ,int );
int find_zero(float[] ,int );
int main()
{
int i,size,no_pos,no_neg,no_zero;
float arr[100];
printf("Enter the size of the array ");
scanf("%d",&size);
printf("Enter the values into the array ");
for(i=0;i<size;i++)
{
scanf("%f",&arr[i]);
}
no_pos=find_positive(arr,size);
no_neg=find_negative(arr,size);
no_zero=find_zeros(arr,size);
printf(" The number of positive numbers in the list = %d ",no_pos);
printf(" The number of negative numbers in the list = %d ",no_neg);
printf(" The number of zeros in the list = %d ",no_zero);
return 0;
}
int find_positive(float arr[100],int size)
{
int i,count=0;
for(i=0;i<size;i++)
{
if(arr[i]>0)
{
count++;
}
}
return count;
}
int find_negative(float arr[100],int size)
{
int i,count=0;
for(i=0;i<size;i++)
{
if(arr[i]<0)
{
count++;
}
}
return count;
}
int find_zeros(float arr[100],int size)
{
int i,count=0;
for(i=0;i<size;i++)
{
if(arr[i]==0)
{
count++;
}
}
return count;
}
OUTPUT:-
Enter the size of the array
6
Enter the values into the array
2.2
6.5
0
-5.8
0.00
-22.11
The number of positive numbers in the list = 2
The number of negative numbers in the list = 2
The number of zeros in the list = 2
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.