Goals: Practicing loop/repetition structures and functions create a program that
ID: 3862684 • Letter: G
Question
Goals: Practicing loop/repetition structures and functions create a program that will implement the 3rd problem in Algorithm 5. Your main function should be a driver program that will call the function, then output the values sent back to the function call. Your function should prompt the user to enter the 50 numbers one at a time, determine how many are even and how many are odd (zero should be determined as even), and send back to the function call how many were even numbers and how many are odd numbers were entered. Your function definition should occur after main. Do not use any concepts beyond Chapter 6 (e.g. arrays) of your textbook. Remember that in addition to the introductory comments for the program your code should contain comments for each function other than main that states the purpose of the function, input to the function (both input from the function call and interactive input), output from the function (both information sent back to the function call and interactive output), and processing performed in the function. Follow the Assignment Guidelines posted on Canvas.Explanation / Answer
/*
* Method1 By using return value from function call
* since only value could be returned from a function, se we returned even values
* Remaining values will be odd
*/
#include<stdio.h>
#include<stdlib.h>
#define TOTALNUMBER 50
int findEvenOdd();
int main()
{
int even = 0;
// value returned by findEvenOdd will be even numbers
even = findEvenOdd();
printf("Count of even numbers are: %d & odd number are: %d ", even, (TOTALNUMBER-even));
return 0;
}
/*
* function to find count of even and odd numbers enetered
* Return: int
* parameters: void
*/
int findEvenOdd()
{
int even = 0;
int var = 0;
printf("Enter 50 digits ");
for(int i = 0; i<TOTALNUMBER; i++)
{
// read input from user in var
scanf("%d", &var);
// if entered value is 0 or remainder after division by 2 is 0, number is even
if(var==0 || var%2==0)
even++;
}
return even;
}
/*
* Method2 - By passing reference of variables in function calls
* even and odd values are filled in passed reference and thus no need to return any variables
*/
#include<stdio.h>
#include<stdlib.h>
#define TOTALNUMBER 50
void findEvenOdd(int *even, int *odd);
int main()
{
int even = 0, odd = 0;
findEvenOdd(&even, &odd);
printf("Count of even numbers are: %d & odd number are: %d ", even, odd);
return 0;
}
/*
* function to find count of even and odd numbers enetered
* Return: two int pointers
* parameters: void
*/
void findEvenOdd(int *even, int *odd)
{
int var;
printf("Enter 50 digits ");
for(int i = 0; i<TOTALNUMBER; i++)
{
// read input from user in var
scanf("%d", &var);
// if entered value is 0 or remainder after division by 2 is 0, number is even
if(var==0 || var%2==0)
{
(*even)++;
}
// else consitions says number is odd, so increament odd count
else
{
(*odd)++;
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.