Write a function named percentOfEvenPositiveNumbers which has two input paramete
ID: 3880379 • Letter: W
Question
Write a function named percentOfEvenPositiveNumbers which has two input parameters: numbers (an array of integers) and size (an integer representing the actual number of element used in numbers). The function returns the percentage of even positive integers out of all positive integers in the array numbers. If there is no positive integer stored in the array, it returns the value of -1; otherwise, it returns the percentage of even positive integers out of all positive integers in the array. There is no precondition. The postcondition of the function has two possible outcomes (1) the percentage of positive integers out of all positive numbers is returned if there exists at least one positive integer in numbers, or (2) the value of -1 is returned if numbers does not contain any positive integer.
Below is the function prototype of this function.
int percentOfEvenPositive(int numbers[], int size);
Explanation / Answer
int percentOfEvenPositive(int numbers[], int size){
int count_positive = 0; //this will store the no of positive no
int count_even_positive = 0; //this will store the no of even positive no
/*
Now iterate through all the nos
*/
for (int i = 0; i < size; i++){
//Check whether the no is positive or not.
if( numbers[i] > 0){
count_positive++; //Since the no is positive, Increase the count_positive value
//Now check whether this positive no is even or not.
if(numbers[i] % 2 == 0){
count_even_positive++; //Since the no is even positive, Increase the count_positive value
}
}
}
//Check if the no of positive is zero, then return -1
if(count_positive == 0) return -1;
/*
Calculate the percentage
*/
int percent = count_even_positive*100/count_positive;
return percent; //return the percent value
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.