The program from Homework #11 Rewrite the program from Homework #11, to count th
ID: 3823721 • Letter: T
Question
The program from Homework #11
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 o 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 FUNCTIONE 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.Explanation / Answer
Hi,
Please find below the code for user defined functions for the requested tasks-
#include<stdio.h>
#include<stdlib.h>
void Negative(float *arr,int len);
void Positive(float *arr,int len);
void zero(float *arr,int len);
int main()
{
int length,i;
printf("Please enter the length of array");
scanf("%d",&length);
if(length<=0)
{
printf(" Please enter positive value of array");
printf("Quiting the program");
return 0;
}
else
{
float *arr;
arr=(float *)malloc(length);
if(arr==NULL){
printf(" Error while creating array dynamically");
printf(" Please check your memory.Quiting!");
}
else
{
printf(" The Array allocation dynamically is successfull");
prinf(" Enter the values of the array");
}
}
for(i=0;i<length;i++)
{
scanf("%f",(arr+i));
}
int negativeNos=0,positiveNos=0,ZeroCount=0;
Positive(arr,length);
Negative (arr,length);
zero(arr,length);
return 0;
}
void Negative(float *arr,int len)
{
int negativeNos=0,i;
for(i=0;i<len;i++) {
if(arr[i]<0) {
negativeNos++;
}
}
printf("The number of negative numbers are :%d",negativeNos);
}
void Positive(float *arr,int len)
{
int positiveNos=0,i;
for(i=0;i<len;i++) {
if(arr[i]>0) {
positiveNos++;
}
}
printf("The number of positive numbers are :%d",positiveNos);
}
void Zero(float *arr,int len)
{
int zeros=0,i;
for(i=0;i<len;i++) {
if(arr[i]==0) {
zeros++;
}
}
printf("The number of zero numbers are :%d",zeros);
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.