CS1101C Lab 3 (Odd Week)
Number Manipulation Part I
The deadline for this lab question is Friday 16 March 2007,
23:59:59 hours.
The name of your C program file must be called manipulate.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:
- Make use of functions that are already defined.
- Write and make use of new functions.
- 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:
- Create a random number of n digits. Let's call this
number Num. The value of n will be given in a file.
- Swap two random positions of the Num.
- Extract part of the number Num from digit i to
k. The values of i and k will also be given.
- Finally, print the digits in the extracted part of Num
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:
- 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.
- 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.)
- You need to write a new function for this step. Think about the
necessary parameters and output.
- 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 manipulate.c as specified. A skeleton code,
with two given functions is given 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:
24078
7 3 5
7 0 5
9 1 4
The sample output is as follows:
$ gcc -Wall mainpulate.c -o manipulate
$ ./manipulate
Using Seed Value = 24078
Original Number (7 digits): 5189427
After swapping digit 1 with 4: 5129487
Extracted from 3 to 5: 129
Reversed: 921
Original Number (7 digits): 6720238
After swapping digit 0 with 3: 6728230
Extracted from 0 to 5: 728230
Reversed: 032827
Original Number (9 digits): 599646163
After swapping digit 6 with 3: 596649163
Extracted from 1 to 4: 4916
Reversed: 6194
$
More to come
The upcoming Lab 3 (Even Week) has a similar flavor,
which introduce more
ways to play around with a number. To appreciate the usefulness of
modular programming, after your friend has completed their Lab 3 Even Week
assignment (or you can attempt them if you want),
you can copy those additional 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
-
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?
-
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?
-
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 17 times since 25-Jun-24 11:57:13 +08.
This is the 1st time it has been accessed today.
A total of 10 different hosts have accessed this document in the
last 445 days; your host, nsrp-source.comp.nus.edu.sg, has accessed it 8 times.
If you're interested, complete statistics for
this document are also available, including breakdowns by top-level
domain, host name, and date.