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
  • Write a MATLAB program that implements the algorithm designed in the Topic 1 "Non-Linear Flowchart" and...

    Write a MATLAB program that implements the algorithm designed in the Topic 1 "Non-Linear Flowchart" and "Creating a Flowchart" assignments previously implemented in C. Compare and contrast the C and MATLAB versions of your codes. convert this to a matlab program #include <stdio.h> int main() { int a; printf("Enter first number: "); scanf("%d", &a); int b; printf("Enter second number: "); scanf("%d", &b); int limit; printf("Enter limit: "); scanf("%d", &limit); printf("\nFirst number: %d\n", a); printf("Second number: %d\n", b); int c; c...

  • 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...

  • Can i get help with this C program requirement. Modify the program Figure 2.13 (letter/while). Create...

    Can i get help with this C program requirement. Modify the program Figure 2.13 (letter/while). Create a variable at the top of your code named power2 and assign it the value 8 Create a variable at the top of your code named result and assign it the value 0 Ask the user to enter FOUR 1's and 0's followed by a '*' In the loop, when the letter is '1' add power2 to result and store it back into result...

  • 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 =...

  • Write a C program that computes Pi with the approximation algorithm that I introduced in class....

    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 adjacent elements (remember...

  • 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...

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