CS1101C Lab 3 (Even Week)

Number Manipulation Part II

The deadline for this lab question is Friday 23 March 2007, 23:59:59 hours.

The name of your C program file must be called manipulate2.c, files with any other name will not be marked.

Background

The purpose of this lab is to familiarize you with the notion of functions. There are three simple objectives:
  1. Make use of functions that are already defined.
  2. Write and make use of new functions.
  3. Write recursive functions.

Manipulate Number

An integer can be manipulated in various interesting ways. For example, in Lab 1 (Even Week), we tried to swap the digits of a four-digit number. In this lab, we are going to carry this idea further. The general idea is as follows:
  1. Create a random number of n digits. Let's call this number Num. The value of n will be given in a file.
  2. Swap two random positions of the Num.
  3. Delete part of the number Num from digit i to k. The values of i and k will also be given.
  4. Finally, print the digits of Num (after deletion) in pairs of two digits. The pairs should also appear in reverse order.
As mentioned in lecture 6 (Modular Programming), each of the steps above can form an individual module. Hence, we will use four modules (functions) in our program:
  1. Function rand_int(int a, int b) is given. This is the function discussed in Lecture 7, which generate a random number in the range of [a..b]. Figure out how to use this.
  2. Function swap_digit(int input, int pos1, int pos2) is given. If you look closely at the code, you'll see that it is essentially the same code as in the suggested solution for Lab 1 Even Week. This is an example of how to extract useful logic into an independent function. Again, you have to figure out how to use this to achieve Step 2. (Additional Note: For uniformity, position of a digit is redefined as follows. Position 0 is the zeroth digit, i.e. the Least significant digit and Position (N-1) is the most significant digit for a N digits number.)
  3. You need to write a new function for this step. Think about the necessary parameters and output.
  4. Another new function is required for this step. For extra challenge, write a recursive function to do the required task.

Additional Information for the Program

Other than the above, there are a couple of simplier requirements. Your program should use an input file called "input.txt" for some important information. This file contains at least two lines, the first line is always the random seed value (see Lecture 7 on random number), which is used to setup the random number generator. All subsequent lines in the file has the following format: n i k, where n is the number of digits in the Num (0 < n < 10). i and k specified the part of the Num to be extracted in Step 3. Note that i and k are integers in the range [0 .. n-1], where 0 is the least significant digit and (n-1) is the most significant digit. (You can assume that i <= k.) For each of these lines, the program will perform the mainpulations as mentioned above.

Your Task

Write a program manipulate2.c as specified. A skeleton code, with two given functions is given here here. A sample input file can be found here.

Sample Runs

Assuming that the executable is manipulate, and the input.txt contains the following lines:
12345
6 3 5
7 0 5
9 4 8
9 7 7
The sample output is as follows:
$ gcc -Wall mainpulate2.c -o manipulate2
$ ./manipulate2

Using Seed Value = 12345

Original Number (6 digits) : 673412
After swapping digit 2 with 3: 674312
Deleted from 3 to 5 : 312
Paired and Reversed : 12 3

Original Number (7 digits) : 8885210
After swapping digit 0 with 0: 8885210
Deleted from 0 to 5 : 8
Paired and Reversed : 8

Original Number (9 digits) : 419771231
After swapping digit 4 with 4: 419771231
Deleted from 4 to 8 : 1231
Paired and Reversed : 31 12

Original Number (9 digits) : 678736144
After swapping digit 6 with 3: 676738144
Deleted from 7 to 7 : 66738144
Paired and Reversed : 44 81 73 66

$

More to come

Your friends in the Odd Week has a very similar lab assignment (actually the instructions are almost identical :) ). Their assignments, however, has a few functions that manipulate the number in different ways. To appreciate the usefulness of modular programming, you can try to copy those functions into one program and have multiple ways of manipulate a number at your fingertip. This is also a good way to test whether you have designed the functions in an independent and general way. Enjoy!

Ponder

  1. The code of the rand_int seems complicated (though you should be able to utilize it without understanding it, which demonstrates the power of the abstraction principle). What is it doing? Why is it needed?
  2. The recursive function in Step 4 prints out the number in the required way without changing the original number. Can you derive a recursive function that changes the number in the same way instead of printing it?
  3. For Step 4, how can you change the recursive function such that the pairs are printed in the right order instead of reverse order? This is related to the "Windup" and "Unwind" phase of recursive functions (see Lecture 7).


This document, index.html, has been accessed 20 times since 25-Jun-24 11:57:13 +08. This is the 1st time it has been accessed today.

A total of 14 different hosts have accessed this document in the last 445 days; your host, nsrp-source.comp.nus.edu.sg, has accessed it 6 times.

If you're interested, complete statistics for this document are also available, including breakdowns by top-level domain, host name, and date.