Your assignment must be submitted electronically by 11:59pm on Wednesday, February 3.
Compile and run the program analyse.c. Modify the program to replace the loop containing repeated getchar() calls with a single call to fgets(). Refer to P&M (pp. 235-236) for details on the fgets() function, but here is a quick tip on its usage:
fgets(text, MAX, stdin);
fgets stands for "get string from file", and it stores the string read into text, reading up to a maximum number of MAX characters, from the file specified in the third argument, which in our case is the "standard input" or stdin (meaning keyboard, or a file if input is redirected using "<").
Once you make this change, however, you will no longer have the length of the string in length, so you will have to use the C library function strlen() (see P&M pp. 176-177 for details) to compute the length of the string.
Note that the string read using fgets has a newline character at the end, and strlen includes this newline character in the length of the string, which is not what we want. So, adjust the string length accordingly.
Note that while there are several others ways to accomplish this exercise, you must do exactly as explained above, in particular: (i) use fgets() to read the line of input; and (ii) use strlen() to compute the length of the input line. Call your program ex1.c. Compile it and run it on a few inputs of your choice. Also, run it on the sample inputs provided and check that the output is correct.
Make a copy of the program of Exercise 1, and name it ex2.c. Modify the program so that it outputs the line entered by the user in the reversed order of characters. Then, if the line is a palindrome, i.e., it reads the same forward and reverse, then your program should also indicate that.
Specifically, your program should display, "Your input in reverse is:" (with newline), and the reversed input on the next line. And if the line were a palindrome, it should also print, "Found a palindrome!" (with newline).
NOTE: For this exercise, we will use a very strict definition of palindrome: A string is a palindrome if and only if it reads exactly the same backward as forward, including spaces and punctuation. Thus, for this exercise, "madam" is a palindrome, but "Madam" is not. Also, "nurses, run" is not a palindrome, but "nursesrun" is. We will use this highly restrictive definition for now just to make coding easier.
Compile and run the program on a few inputs of your choice. Also, run it on the sample inputs provided and check that the output is correct (i.e., identical to the output provided, no extra white space anywhere).
Understand the following program to multiply two 3x3 matrices: array.c. Modify it to read the values of matrices A and B at run-time instead of defining them in the program.
Specifically, use loops to read and write the matrices, instead of the long series of printf() calls. Refer to the sample inputs and outputs provided on exactly how to format your I/O.
Name the file with your program ex3.c. Compile and run your program on inputs of your choice, and also make sure it runs correctly on the sample inputs provided. The numbers in the input are assumed to be in decimal format.
Have a look at the program bubble.c. Do you understand how it works? Compile and run the program.
The number of swaps is not a very accurate measure of performance, as much time is inevitably spent comparing elements that do not require swapping. Modify the program to count the number of iterations of the do...while loop and display this number together with the number of swaps. Refer to the sample inputs and outputs provided on exactly how to format your I/O.
Name the file with your program ex4.c. Compile and run your program on inputs of your choice, and also make sure it runs correctly on the sample inputs provided.
Write a function called fibonacci() that computes the n-th Fibonacci number, given a non-negative number n. You can assume that n will be small enough that the result will fit within the range of an int in the C language.
The sequence of Fibonacci numbers is defined as:
Fibonacci(0) = 0 Fibonacci(1) = 1 Fibonacci(n) = Fibonacci(n-1) + Fibonacci(n-2)
Note: The function fibonacci() can be implemented either as a recursive function (one that calls itself), or as an iterative one (using a for loop). Recursion in C is very much like recursion in Java. Whether to use recursion or iteration is completely your choice.
Now write the main() function of your program so it does the following, repeatedly: (i) reads an integer from the keyboard; (ii) calls fibonacci() to compute the Fibonacci number; and (iii) displays the result. The program should do so repeatedly until the user enters 0, in which case it should still print the answer (fibonacci(0) = 0), and then terminate.
Refer to the sample inputs and outputs provided on exactly how to format your I/O. The numbers in the input are assumed to be in decimal format.
Name the file with your program ex5.c. Compile and run your program on inputs of your choice, and also make sure it runs correctly on the sample inputs provided.
Sample inputs and corresponding outputs are provided under /home/montek/comp411/samples/lab3. You should try running your program on the sample input files provided, and make sure the program's output is identical to that in the sample output files.
The sequence of steps to run your program and verify its output is as follows. First, copy all the sample input and output files to your lab3 folder:
% cd ~/comp411lab/lab3
% cp /home/montek/comp411/samples/lab3/* .
Then do the following:
% ./ex1 < ex1in1 > ex1result1
% diff ex1result1 ex1out1
... and so on. Before submitting your work, be sure that each of your compiled programs runs correctly on all of the sample inputs provided exactly, i.e., diff produces no output at all. You may receive zero credit if your program's output does not exactly match the sample outputs provided.
Your assignment must be submitted electronically by 11:59pm on Wednesday, February 3.
You will submit your work on Exercises 1-5. Specifically, the files ex1.c, ex2.c, ex3.c, ex4.c and ex5.c will be collected.
How to submit: Navigate to the folder where these files are stored (~/comp411lab/lab3), and run the following command for electronically submitting them:
% cd ~/comp411lab/lab3
% /home/montek/comp411/bin/submitlab3
When you invoke the submit script, it performs a quick check of your work (including compiling, running and comparing the output of your program with the expected output, for all sample input/output files), and provides a quick summary of any errors. Your work is submitted regardless of whether errors were found or not, but you are free to fix any errors and submit again.
If you would like to run the quick check script without submitting your work, you may run it as follows:
% cd ~/comp411lab/lab3
% /home/montek/comp411/bin/selfchecklab3
In case of any problems, please contact the instructor or the TAs.