Arrays and Functions in C


Exercise 1

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 K&R 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 function strlen() (see K&R 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.


Exercise 2

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 before displaying the statistics.

Specifically, it should display, "Your input in reverse is:" (with newline), and the reversed input on the next line, followed by all the statistics as in Exercise 1.

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.


Exercise 3

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.


Exercise 4

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.


Exercise 5

Write a function called factorial() that computes and returns the factorial of a positive integer n, which is the product of all the positive integers from 1 to 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.

Note: The function factorial() 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 factorial() to compute its factorial; 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 (factorial(0) = 1), and then terminate.

Refer to the sample inputs and outputs provided on exactly how to format your I/O.

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.


Revised: 25 September 2005, f.mokhtarian@surrey.ac.uk
Revised: 14 September 2012, Montek Singh, montek@cs.unc.edu