Question

You will write a C program, q1 sequence.c, that computes the value of the nth term...

You will write a C program, q1 sequence.c, that computes the value of the nth term in any recursive sequence with the following structure:

an = c1 · an−1 + c2 · an−2

a0 > 0

a1 > 0

c1 6= 0

c2 6= 0

Your C program will take 5 integer arguments on the command line: n, a0, a1, c1 and c2. n must be an integer greater than or equal to 0. If more or fewer arguments are provided, your program should print out an error message andcorrect usage information, and then exit. Use atoi() to verify that none of the arguments violate the restrictions above (e.g. n cannot be negative, but may be zero, while c1 can not be zero); if any arguments violate the restrictions, print out an error message and correct usage information, and then exit.

Your program, after checking arguments, should call a recursive function, check sequence(), which takes the five arguments as described above. If n == 0 or n == 1, your function should return the value of the base cases a0 and a1. Otherwise, it should recursively calculate an using the equation above. Your program should also establish a global variable, number iterations, which increments every time check sequence() is called. Once the result of check sequence() is returned, your program should print that result, as well as the number of iterations that check sequence() ran.

You will use good C development practices for this question. You are expected to have a q1 sequence.h header file defining check sequence(), as well as any other functions you may have used in your program, and include this in your .c file. You are also expected to turn in a makefile which has at least four rules in it:

• A rule for compiling q1 sequence.c into a q1 sequence.o file. Use the -c option for gcc to compile this file.

• A rule for creating an executable from your q1 sequence.o file, q1 sequence.

• A clean rule for removing your executable and .o file, as well as any other temporary files or output files you may have generated.

• A test rule which runs your program with test cases and checks them against the test output.

Your makefile may contain any additional rules that you feel would be useful.

Exactly how you format the output of your program and your tests is up to you, but it should clearly indicate which value is the calculated sequence value and which is the number of iterations. Each test case must report its results by echoing to the command line, such as "Test case n: PASSED". Exactly how you check the program’s output is also up to you, but you may wish to create a shell script which can be run to test the output. You may also wish to use the diff command to compare your results with the test cases, and an if block in the shell, using the test command. The basic syntax for this is:

i f ( t e s t \$VARIABLE −eq 0 ) ; then

echo ” Test was t r u e ” ;

e l s e echo ” Test was f a l s e ” ;

f i

Use comments within your makefile or testing script to justify each of the test cases you run: what are you trying to test? Why these test cases? You must cover at least three failure cases of your choice, and at least five test cases with valid parameters, including these three:

1. 10 1 1 1 1: returns a value of 89 and 177 iterations

2. 5 1 3 3 -2: returns a value of 63 and 15 iterations

3. 8 2 5 -3 6: returns a value of -52407 and 67 iterations

When you have completed this question, you must remove all object files and executables, and then use the tar command to compress your code and test cases files into a single archive, a2q1.tar. a2q1.tar should contain:

• q1 sequence.h

q1 sequence.c

• Makefile

0 0
Add a comment Improve this question Transcribed image text
Answer #1
  • I have pasted the code and screenshot of successful run below.
  • In case of any doubts just comment down.

-------------------------Screenshot-----------------------


-------------------------Code-----------------------

#include <stdio.h>
#include <stdlib.h>

int numIterations = 0;

int check_sequence(int n, int a0, int a1, int c1, int c2){
   numIterations++;
   if(n==0) return a0;
   if(n==1) return a1;
   return c1*check_sequence(n-1,a0,a1,c1,c2) + c2*check_sequence(n-2,a0,a1,c1,c2);
}

int main(int argc, char const *argv[]){
   if(argc<6) {printf("Fewer arguments provided\n"); return 0;}
   else if(argc>6) {printf("More arguments provided\n"); return 0;}
   int n = atoi(argv[1]);
   int a0 = atoi(argv[2]);
   int a1 = atoi(argv[3]);
   int c1 = atoi(argv[4]);
   int c2 = atoi(argv[5]);
   if(n<0){
       printf("n cannot be negative\n");
       return 0;
   }
   if(c1==0 || c2==0){
       printf("ci cannot be zero\n");
       return 0;
   }
   if(a0<=0 || a1<=0){
       printf("ai must be positive\n");
       return 0;
   }

   int result = check_sequence(n,a0,a1,c1,c2);
   printf("returns a value of %d and %d iterations\n", result, numIterations);

   return 0;
}

Add a comment
Know the answer?
Add Answer to:
You will write a C program, q1 sequence.c, that computes the value of the nth term...
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
  • //I NEED THE PROGRAM IN C LANGUAGE!// QUESTION: I need you to write a program which...

    //I NEED THE PROGRAM IN C LANGUAGE!// QUESTION: I need you to write a program which manipulates text from an input file using the string library. Your program will accept command line arguments for the input and output file names as well as a list of blacklisted words. There are two major features in this programming: 1. Given an input file with text and a list of words, find and replace every use of these blacklisted words with the string...

  • . . In this programming assignment, you need to write a CH+ program that serves as...

    . . In this programming assignment, you need to write a CH+ program that serves as a very basic word processor. The program should read lines from a file, perform some transformations, and generate formatted output on the screen. For this assignment, use the following definitions: A "word" is a sequence of non-whitespace characters. An "empty line" is a line with no characters in it at all. A "blank line" is a line containing only one or more whitespace characters....

  • The program is written in c. How to implement the following code without using printf basically...

    The program is written in c. How to implement the following code without using printf basically without stdio library? You are NOT allowed to use any functions available in <stdio.h> . This means you cannot use printf() to produce output. (For example: to print output in the terminal, you will need to write to standard output directly, using appropriate file system calls.) 1. Opens a file named logfle.txt in the current working directory. 2. Outputs (to standard output usually the...

  • Write a c++ program in that file to perform a “Search and Replace All” operation. This...

    Write a c++ program in that file to perform a “Search and Replace All” operation. This operation consists of performing a case-sensitive search for all occurrences of an arbitrary sequence of characters within a file and substituting another arbitrary sequence in place of them. Please note: One of the biggest problems some students have with this exercise occurs simply because they don’t read the command line information in the course document titled “Using the Compiler’s IDE”. Your program: 1. must...

  • Objective: Use input/output files, strings, and command line arguments. Write a program that processes a text...

    Objective: Use input/output files, strings, and command line arguments. Write a program that processes a text file by removing all blank lines (including lines that only contain white spaces), all spaces/tabs before the beginning of the line, and all spaces/tabs at the end of the line. The file must be saved under a different name with all the lines numbered and a single blank line added at the end of the file. For example, if the input file is given...

  • Write a program in MIPs Assembly Language to compute nth number of a fibonacci number sequence....

    Write a program in MIPs Assembly Language to compute nth number of a fibonacci number sequence. Your program should prompt for an integer input n from the user. The program should call a recursive function to compute the nth fibonacci number. Your program must follow programming convention. You should submit program and screenshot of output in a single word/pdf file. You should use following recursive definition of fibonacci function: fib(0) = 0 fib(1) = 1 fib(n) = fib(n-1) +fib(n-2)

  • Round 1: sequence.c This program should read and execute a list of commands from stdin. Each comm...

    Round 1: sequence.c This program should read and execute a list of commands from stdin. Each command and its arguments (if any) will appear on a separate line. For example, if the file "cmdfile" contains the lines: whoami cal 4 2019 echo The time is: date then running 1 /sequence< cmdfile should output your username, a calendar of the month of April, the string "The time is:", and the current date/time, to standard output. Suggested approach: first, make sure you...

  • After reading pages 330 - 336, write a program that takes two command line arguments which...

    After reading pages 330 - 336, write a program that takes two command line arguments which are file names. The program should read the first file line by line and write each line, in reverse order, into the second file. The program should include a "usage" method that displays the correct command line syntax if two file names are not provided. Example: if my input file says Hello, World! then my output file will contain !dlroW ,olleH Hints: Use CaesarCipher...

  • Write a C program for a program to implement a recursive main. Include a static local...

    Write a C program for a program to implement a recursive main. Include a static local variable count initialized to 1. Post increment and print the value of count each time the main is called. The loopcount included in the usage is a positive integer number that will indicate how deep the recursion should go. Usage: lab3 loopcount Code should be nicely indented and commented. Create a simple Makefile to compile your program into an executable called lab3. You should...

  • In this assignment, you will develop a C program to construct a red and black tree....

    In this assignment, you will develop a C program to construct a red and black tree. For a given input sequence the tree is unique by using RB-INSERT on one number at a time. Below is an example: The input is a sequence of numbers separated by comma, e.g. 1,8,11,2, … You can enter the numbers using an input file and output the tree, or through a command line with one number at a time (with “X” to stop entering...

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