Write a program IN C, that counts the number of negative values, the number of p
ID: 3765105 • Letter: W
Question
Write a program IN C, 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.
Explanation / Answer
int main()
{
float p_nArr[20]={0};
float nCount_Pos = 0;
float nCount_Neg = 0;
float num=0;
float SIZEOFARRAY= 0;
float index;
printf("nEnter the Size of the array");
scanf("%d",&SIZEOFARRAY);
printf("nEnter the elements ::n");
for( index = 0; index < SIZEOFARRAY; index++ )
{
scanf("%d",&p_nArr[index]);
if (p_nArr[index] > 0 )
nCount_Pos++ ;
else if (p_nArr[index] < 0 )
nCount_Neg++;
}
if ( nCount_Neg > 0)
{
printf("nNumber of negative numbers in the array are :: %dn", nCount_Neg);
for( index = 0; index < SIZEOFARRAY; index++ )
{
if (p_nArr[index] < 0 )
printf("%d ",p_nArr[index]);
}
}
if ( nCount_Pos > 0 )
{
printf("nNumber of Positive numbers in the array are :: %dn",nCount_Pos);
for( index = 0; index < SIZEOFARRAY; index++ )
{
if (p_nArr[index] > 0 )
printf("%d ",p_nArr[index]);
}
}
getch();
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.