Write an EFFICIENT function that takes as its input Write an
Solution
#include<stdio.h>
 #include<malloc.h>
 //Returns 1 if the array contains k positive integers otherwise returns 0
 int kPositive(int *array, int n, int k)
 {
 //Initializes the counter to 0
 int x, c = 0;
 //Loops till end of the array
 for(x = 0; x < n; x++)
 {
 //Checks for the positive integer
 if(*(array + x) >= 0)
 //Increment the counter by 1 if true
 c++;
 }
 //If counter value is equal to the k which is number of positive integer required by the user
 if(c >= k)
 return 1;
 else
 return 0;
 }
int main()
 {
 //no for size of array
 //x for loop counter
 //noPos for number of positive numbers required by the user
 //res to store result returned by the function
 int no, x, noPos, res;
 //Accepts the size of array
 printf(\"\  Enter the size of array: \");
 scanf(\"%d\", &no);
 //Creates a pointer of size no
 int *p = (int *) malloc(sizeof(int) * no);
 //Accepts numbers
 printf(\"\  Enter %d numbers: \", no);
 for(x = 0; x < no; x++)
 scanf(\"%d\", (p + x));
 //Accepts number of positive numbers user looking for in the array
 printf(\"\  Enter how many positive number you want to check: \");
 scanf(\"%d\", &noPos);
 //Display the array contents
 printf(\"\  Entered Numbers: \");
 for(x = 0; x < no; x++)
 printf(\"%4d\", *(p + x));
 //Checks the return value of the function.
 if(kPositive(p, no, noPos) == 1)
 printf(\"\  Array contains at least %d positive integers\", noPos);
 else
 printf(\"\  Array does not contains at least %d positive integers\", noPos);
 }
Sample run 1:
Enter the size of array: 6
Enter 6 numbers: 12
 -9
 -6
 45
 -3
 -7
Enter how many positive number you want to check: 1
Entered Numbers: 12 -9 -6 45 -3 -7
 Array contains at least 1 positive integers
Sample run 2:
Enter the size of array: 7
Enter 7 numbers: 12
 -9
 -6
 23
 -5
 -7
 45
Enter how many positive number you want to check: 4
Entered Numbers: 12 -9 -6 23 -5 -7 45
 Array does not contains at least 4 positive integers


