For each of the problems below you are to consider making a function to solve th
ID: 3595275 • Letter: F
Question
For each of the problems below you are to consider making a function to solve the problem. As before, state the purpose of the function in your own words. Next specify input that is needed for the function to complete the task, and state what input should be passed to the function as parameters and what would be acquired inside the function using the input function. Specify the expected output and state if the output will be sent back to the function call or be sent to the monitor. Finally ,give the step by step process that will obtain the output from the input (the algorithm). In addition to these 4 items also specify test data that can be used for each function. Remember to describe your steps in enough detail so someone other than you could follow your algorithm and solve the problem. Also do not write any actual code.
Problem 2: Describe a function (mini-progam) that would accept 7 values from the function call and determine the minimum of those 7 values. Design this function without using the idea of arrays. Send back to the function call the minimum value. Hint, you should use no more than 7 comparisons (relational operators) to find the minimum.
Program 3: Describe a function (mini-program) that will prompt the user to enter 50 whole numbers and count how many even and odd numbers were entered (0 should be considered an even number).
Explanation / Answer
1. Minimum of 7 numbers
Purpose :
Purpose of the function is to find minimum of the seven numbers provided.
It compares all the functions
Function :
function findMinOfSeven(num1,num2,num3,num4,num5,num6,num7)
Input Needed :
7 numbers from the user as input
Input Passed to function :
num1,num2,num3,num4,num5,num6,num7
Acquired inside function :
min
Expected output :
Min of seven numbers , to be returned by the function
Step by Step Algo :
-> min = num1 < num2 ? num1 : num2
-> min = num3 < min ? num3 : min
-> min = num4 < min ? num4 : min
-> min = num5 < min ? num5 : min
-> min = num6 < min ? num6 : min
-> min = num7 < min ? num7 : min
-> return min
Function findMinOfSeven(num1,num2,num3,num4,num5,num6,num7) {
min = num1 < num2 ? num1 : num2
min = num3 < min ? num3 : min
min = num4 < min ? num4 : min
min = num5 < min ? num5 : min
min = num6 < min ? num6 : min
min = num7 < min ? num7 : min
return min
}
Test Data :
-> Input : 1,2,3,4,5,6,7 Output : 7
-> Input : 1,1,1,1,1,1,1 Output : 1
-> Input : -2,-3,1,2,3,4,5 Output : -3
2.Count how many even and odd numbers
Purpose :
Purpose of the function is to take 50 whole number as input from user and count odd and even numbers.
0 is considered as even.
Function :
function countOddEnven()
Input Needed :
50 whole numbers from user
Input Passed to function :
none
Acquired inside function :
countOdd,countEven
Expected output :
none , print odd and even count
Step by Step Algo :
1. countOdd=0,countEven=0,counter=0
2. take user input , store it in input
3. if input%2==0 the countEven++
else
countOdd++
4.counter++;
5.if counter < 50 , go to step 2 else go to steo 6
6. print countOdd and countEven
function countOddEnven(){
odd=0,even=0,counter=0
for(i=0;i<50;i++){
input -> take input from user
if(input%2==0)
even++;
else
odd++;
}
print odd
print even
}
Test Data :
1,1,1,1,1,1,1,1,1,1,2,2,2,2,2,2,2,2,2,2,3,3,3,3,3,3,3,3,3,3,4,4,4,4,4,4,4,4,4,4,5,5,5,5,5,5,5,5,5,5
odd -> 30
even -> 20
**Feel free to comment for any query with details.
**Happy learning :)
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.