Question

Working in C, modify the algorithm created in the Topic 1 assignment "Non-Linear Flow Chart" to...

Working in C, modify the algorithm created in the Topic 1 assignment "Non-Linear Flow Chart" to instead compute the nth term in the series by calling a recursive function. Note that the value of "n" will need to be an input argument, along with the two starting values of the series. Discuss the efficiency of this method versus the iterative logic that you wrote previously.

Previous Code:

//main.c

#include<stdio.h>

#include<limits.h>

int main()

{

   //declare a integer data type variales

   int first;

   int second;

   int result=0;

   //Set INT_MAX as max value

   int max=INT_MAX;

   printf("Enter first value :");

   scanf("%d",&first);

   printf("Enter second value :");

   scanf("%d",&second);

   printf("%d,",first);

   printf("%d,",second);

   //Add the two values and store sum in result variable

   result=first+second;

   /*Loop till result is less than max value*/

   while(result<=20000)

   {

       //print result value

       printf("%d,",result);

       //Assign second value to first

       first=second;

       //Assign the result to second

       second=result;

       //Add two values and assign sum to result

       result=first+second;

       //Break the loop if result is less than 0

       if(result<0)

           break;

   }

   return 0;

}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include<stdio.h>

#include<limits.h>
//recursive function
//which returns the nth term in the series
int nthTerm(int s1,int s2,int n)//input:n value and two starting values
{
   if(n==1)
   return s1;
   if(s1+s2 == 0)
   return -1;//means proper posivite value are not given //so stopping here
   return nthTerm(s2,s1+s2,n-1);//recursive call
}
//complexity of both iterative and recursive methods is same : O(n)
int main()

{

//declare a integer data type variales

int first;

int second;

int result=0,n;

//Set INT_MAX as max value

int max=INT_MAX;

printf("Enter first value :");

scanf("%d",&first);

printf("Enter second value :");

scanf("%d",&second);

printf("Enter n value to find nth term in the series:");
scanf("%d",&n);
result = nthTerm(first,second,n);
printf("%dth term in the series is :%d\n",n,result);
return 0;

}

output:

Enter first value :1
Enter second value :2
Enter n value to find nth term in the series:5

5th term in the series is :8

Add a comment
Know the answer?
Add Answer to:
Working in C, modify the algorithm created in the Topic 1 assignment "Non-Linear Flow Chart" to...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • What to do Write a C program that computes Pi with the approximation algorithm that I...

    What to do Write a C program that computes Pi with the approximation algorithm that I introduced in class. I want you to write the program in two different ways: The first version will add up the first 28284277 elements of the approximation series in a simple for loop. I ask you to put the code into a function with this signature: float get_pi_for(void); The second version will break out of a while loop depending on whether a pair of...

  • Solve using C programming 3. lf you are given this code. #include <stdio.h> int main() int...

    Solve using C programming 3. lf you are given this code. #include <stdio.h> int main() int var1, var2; int sum; printf("Enter number 1:\n "); scanf("%d",&var1); printf("Enter number 2:In ); scanf("%d",&var2); sum-var1+var2; printf ("Vnsum of two entered numbers : %d", //printf ("Output: %d", res); sum); return e; Modify this code by creating a function called "addition". Make the arguments of the functions numberl and number 2. Add the two values in the function. Return a value called "result". Print "result" in...

  • In the Source Folder (src) of this project create a new C source file called "gcd.c"....

    In the Source Folder (src) of this project create a new C source file called "gcd.c". Once again, copy the contents of the "main.c" file above into the "gcd.c" source file. Modify this program to NOT ask the user to input the second Positive Integer, if the user has entered a program terminating value for the "first" input Postitive Integer. #include <stdlib.h> #include <stdio.h> int main(int argc, char *argv[]) { //============================== setbuf(stdout, NULL); // turns standard output buffering off int...

  • C Programming write two functions, similar to what you see in the sample program. The first will ask the user to enter some information (I have included the type in parentheses) First Name (char[]) L...

    C Programming write two functions, similar to what you see in the sample program. The first will ask the user to enter some information (I have included the type in parentheses) First Name (char[]) Last Name (char[]) Age (int) Height in Inches (double) Weight in Pounds (double) You will use pass-by-reference to modify the values of the arguments passed in from the main(). Remember that arrays require no special notation, as they are passed by reference automatically, but the other...

  • Step 4: Write a Sum Function Since we can write, compile and run simple c files,...

    Step 4: Write a Sum Function Since we can write, compile and run simple c files, lets add a bit to make a program that will sum an entire array of integers. To do this we are going to simply overwrite the main.c file to include the new function. The sum function below is not complete and must be finished before the program will execute properly. %%file main.c #include <stdio.h> int sum(int array[], int arrayLength) {     int i =...

  • Please I need help in C language, I am trying to modify the code per the...

    Please I need help in C language, I am trying to modify the code per the below instructions, but I am getting errors. Can't fgure it out. The attempted code modification and data file is privided below, after the instructions. Thank you. Instructions:                  1.      First, add a function named printMsg that displays a message, a greeting, or an introduction to the program for the user. Add a statement that calls that function from the main function when your program starts....

  • C program help: Write a C program that uses three functions to perform the following tasks:...

    C program help: Write a C program that uses three functions to perform the following tasks: – The first function should read the price of an item sold at a grocery store together with the quantity from the user and return all the values to main(). example: #include void getInput(int *pNum1, int *pNum2); // note: *pNum1 & *pNum2 are pointers to ints int main () {    int numDucks,numCats;    getInput(&numDucks, &numCats); // passing addresses of num1 & num2 to...

  • C program. Using do-while loop for this question. Question: write a program to calculate the average...

    C program. Using do-while loop for this question. Question: write a program to calculate the average of first n numbers. output: Enter the value of n : 18 The sum of first 18 numbers =171 The average of first 18 numbers = 9.00 my code couldn't give me the right avg and sum. Please help. #include<stdio.h> int main() {     int num;     int count =0;     int sum=0;     float avg;      printf("Enter the value of n: ");    ...

  • Digit to WordsWrite a C program that asks the user for a two-digit number, then prints...

    Digit to WordsWrite a C program that asks the user for a two-digit number, then prints the English wordfor the number. Your program should loop until the user wants to quit.Example:Enter a two-digit number: 45You entered the number forty-five.Hint: Break the number into two digits. Use one switch statement to print the word for the firstdigit (“twenty,” “thirty,” and so forth). Use a second switch statement to print the word for thesecond digit. Don’t forget that the numbers between 11...

  • In C Code provided: #include <stdio.h> int recursive_sequence(int n) { //type your code here } int...

    In C Code provided: #include <stdio.h> int recursive_sequence(int n) { //type your code here } int main() { int number; scanf("%d", &number); printf("recursive_sequence(%d) = %d",number, recursive_sequence(number)); return 0; } The nth term in a sequence of numbers called recursive_sequence can be defined as follows: • recursive_sequence(0) = 0 • recursive_sequence(1) = 1 If n is greater than 1, then recursive_sequence(n) = 2* recursive_sequence(n - 2) + recursive_sequence(n-1) The first 6 terms in this sequence are: 0,1, 1, 3, 5, 11...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT