Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Question 3: For, While, Do-while Loops (20 pts) Compete the following function t

ID: 3648922 • Letter: Q

Question

Question 3: For, While, Do-while Loops (20 pts)

Compete the following function that counts the number of positive (greater than zero) integers in an array, using three types of loops. See the comments for more details. The three versions should behave exactly the same.
// Count the number of positive integers in an array
// X: the input array.
// N: the number of elements in the array, N > 0
// return: the counted number.
int count_positive(int X[], int N)

a. [10 pts] Complete the function using a for loop.


b. [5 pts] Complete the function using a while loop.


c. [5 pts] Complete the function using a do-while loop.


Explanation / Answer

please rate - thanks

main program

#include <stdio.h>
#include <conio.h>
int main(void)
{
int a[]={5,10,12,13,11,-4,-3,-6};
printf ( "array has %d positive numbers ",count_positive(a,8));
getch();
return 0;
}

a. [10 pts] Complete the function using a for loop.
int count_positive(int X[], int N)
{int i,count=0;
for(i=0;i<N;i++)
    if(X[i]>0)
         count++;
return count;
}


b. [5 pts] Complete the function using a while loop.
int count_positive(int X[], int N)
{int i=0,count=0;
while(i<N)
    {if(X[i]>0)
         count++;
         i++;
         }
return count;
}


c. [5 pts] Complete the function using a do-while loop.

int count_positive(int X[], int N)
{int i=0,count=0;
do
    {if(X[i]>0)
         count++;
         i++;
         }while(i<N);
return count;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote