Program in C main program • declare variables, N, f, count • do call getInt (“ e
ID: 3769409 • Letter: P
Question
Program in C
main program
• declare variables, N, f, count
• do
call getInt (“ enter number from 1 – 15 inclusive: “ )
store returned value in N
Print the return value of sum with the label “sum of %d is %d“ , N, sum(N)
call fi( N )
call getInt to ask user to enter 1 to quit
• while (user does not enter 1)
• print count of how may numbers processed and “good by, press any key to close window: “
getInt:
• Receives a prompt as char *
• use while loop until number is in bounds
print prompt
read integer from key board
• while ( number <1 or number <15)
print prompt
read integer from key board
print error message
• return number
sum: THIS MUST BE A RECURSIVE FUNCTION
• receives an integer
• returns the sum of adding all the numbers from 1-N including N
• if < 1 return 0
• else return N + sum( N – 1)
fi: THIS MUST BE A RECURSIVE FUNCTION
The info below is not the code just the plan you need to use if else if and only one return statement in your code. Print out the fib sequence from first to Nth.
• receives an integer
• returns integer
• Prints each fib value from 1 to integer argument so you cannot just return a value
• if N < 1 return 0
• if N == 1 return 1
• if N == 2 return 1
• else return fi(N-2) + fi(N-1)
Explanation / Answer
#include <stdio.h>
#include <conio.h>
int getSum(int numberCount);
int main(){
int numberCount=0, number, counter, sum=0;
printf("Enter the number of integers to add: ");
scanf("%d",&numberCount);
printf("Enter %d numbers seperated by space ", numberCount);
sum = getSum(numberCount);
printf("SUM = %d", sum);
getch();
return 0;
}
/*
* Function to return sum of N numbers
*/
int getSum(int numberCount){
int sum=0;
/* exit condition */
if(0 == numberCount)
return 0;
scanf("%d", &sum);
/* Recursively call getSum by reducing numberCount by 1 */
return sum + getSum(numberCount - 1);
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.