Subject: PRF192- PFC
Workshop 04
Objectives:
(1) Managing data using pointers
(2) Developing programs using simple menus
Part 1: Use notebook
Exercise 1 (1 mark) : Explain outputs:
The code initializes two variables `n = 7` and `m = 6`, with pointers `pn` and `pm`
pointing to their addresses. The expression `*pn = *pm + 2 * m - 3 * n` modifies
`n` by calculating `6 + 12 - 21 = -3`, so `n` becomes `-3`. Then, the statement `*pm
-= *pn` updates `m` by subtracting the new value of `n` from `m`, resulting in `m =
6 - (-3) = 9`. Finally, the program prints the sum of `m` and `n`, which is `9 + (-3) =
6`. Therefore, the output is `6`.
The program starts by setting `c1` to the character `'A'` (which has an ASCII value
of 65) and `c2` to the character `'F'` (ASCII value 70). It then uses pointers to
modify these values. The line `*p1 += 3` adds 3 to `c1`, changing it from `'A'` (65)
to `'D'` (68). The line `*p2 -= 5` subtracts 5 from `c2`, changing it from `'F'` (70) to
`'A'` (65). Finally, the program calculates the difference between the new values
of `c1` and `c2`, which is `68 - 65 = 3`, and prints `3` as the output.
The program starts with two variables: `x = 3.2` and `y = 5.1`. Two pointers `p1`
and `p2` are created to point to `x` and `y` respectively. The line `*p1 += 3 -
2*(*p2)` modifies `x` by calculating `3 - 2 * 5.1 = 3 - 10.2 = -7.2` and adding that to
`x`, making `x = 3.2 + (-7.2) = -4.0`. Next, the line `*p2 -= 3 * (*p1)` modifies `y` by
calculating `3 * (-4.0) = -12.0` and subtracting that from `y`, making `y = 5.1 - (-
12.0) = 5.1 + 12.0 = 17.1`. Finally, the program prints the sum of `x + y`, which is `-
4.0 + 17.1 = 13.1`, so the output is `13.100000`.
Exercise 2: (1 marks) What are outputs
int
n=17,m=8;
int* p1= &n,
*p2=&m;
*p1 +=12m+
(*p2);
*p2 = m + n-
2*(*p1);
printf(“%d”,
m+n); What
is the
output?
The program begins by setting `n = 17` and `m = 8`, with pointers `p1` pointing to
`n` and `p2` pointing to `m`. The line `*p1 += 12 - m + (*p2)` modifies `n` by
calculating `12 - 8 + 8 = 12`, and adding this to `n`, making `n = 17 + 12 = 29`.
Then, the line `*p2 = m + n - 2*(*p1)` modifies `m` by calculating `8 + 29 - 2*29 =
37 - 58 = -21`, so `m` becomes `-21`. Finally, the program prints the sum of `m +
n`, which is `-21 + 29 = 8`. Therefore, the output is `8`.
The program begins by initializing an integer variable `n` with a value of
`260` and creates a pointer `p` that points to `n`. It first prints the value of
`n`, which is `260`. Then, a character pointer `pp` is created, pointing to the
same address as `p`, effectively pointing to `n`. The line `*pp = 0;` changes
the first byte of `n` to `0`, which alters its value because an integer is
stored in multiple bytes. Since `260` is represented in memory as `0x01
0x04` (in hexadecimal), setting the first byte (least significant byte) to `0`
changes `n` to `0x00 0x04`, which is `4` in decimal. The program then
prints the modified value of `n`, which is now `4`. Therefore, the final output
is:
```
n=260
n=4
```
Exercise 3: (2 marks) Walkthroughs
The program defines a function `t` that takes three integer parameters: `x`,
`y`, and `z`. Inside this function, it calculates a value `k` using the formula
`k = 2 * x + 3 * y + 5 * z`, and then returns the remainder of `k` when
divided by `13`. In the `main` function, three integers `a`, `b`, and `c` are
initialized to `7`, `6`, and `5`, respectively. The program calls the function `t`
with `b`, `a`, and `c` as arguments (which correspond to `6`, `7`, and `5`).
The calculation inside the function becomes `k = 2 * 6 + 3 * 7 + 5 * 5 = 12 +
21 + 25 = 58`, and `58 % 13` equals `6`. The result, `6`, is stored in the
variable `L`, and then the program prints "The value of L is: 6" before
pausing to allow the user to see the output. Therefore, the final output is:
```
The value of L is: 6
The program defines a function `T` that swaps the values of two integers using
pointers. In the `main` function, two integers, `a` and `b`, are initialized to `7` and
`6`, respectively. The program first prints the values of `a` and `b` before
swapping, displaying "Before swapping: a = 7, b = 6". When the function `T` is
called with the addresses of `a` and `b`, it stores the value of `a` (which is `7`) in a
temporary variable `t`, then assigns the value of `b` (which is `6`) to `a`, and finally
assigns the value of `t` (which is `7`) to `b`. After the swap, the values of `a` and
`b` are now `6` and `7`, respectively. The program then prints "After swapping: a =
6, b = 7" to show the new values. Therefore, the output will be:
```
Before swapping: a = 7, b = 6
After swapping: a = 6, b = 7
```
The program defines a function `T` that takes two integer pointers as arguments
and calculates a value based on the sum of the integers they point to. In the
`main` function, two integers `a` and `b` are initialized to `3` and `4`, respectively.
The function `T` is called with the addresses of `a` and `b`, so it receives the
values `3` and `4`. Inside `T`, it first calculates the sum of these values: `3 + 4 = 7`.
Since `7` is not greater than `12`, the conditional operator sets `t` to `6`. The
function then computes `2 * t % 5`, which is `2 * 6 % 5 = 12 % 5 = 2`. This value `2`
is returned to the `main` function and stored in the variable `c`. Finally, the
program prints "The value of c is: 2", showing the result. Therefore, the output
will be:
```
The value of c is: 2
```
Part 2: Develop a program using simple menu
Program 1(3 marks):
Objectives Practice implementing a program with simple menu.
Related knowledge None
Problem Write a C program that will execute repetitively using a simple menu
as following:
1- Process PerfectSquareNumber
2- Print min, max digit in an integer
3- Character data
4- Quit
Select an operation:
1- When user selects the option 1, the program will accept a
non-negative integer and print out a message about whether
the input number is a PerfectSquareNumber or not.
2- When user selects the option 2, the program will accept a
positive integral number and print out the minimum and
maximum digit in this number.
3- When user selects the option 3, user will enter two
characters, then the program will print out ASCII codes
of characters between them using descending order.
4- The program will terminate when user selects the option 4.
Analysis Nouns:
- non-negative integer int n
- A number represents a choice of user int choice;
Functions: int PerfectSquareNumber ( int n) see above
void printMinMaxDigits( int n) see above
Suggested Begin
algorithm (logical Do /* Print out the menu and get user choice*/
order of verbs) { Print out “1- Process PerfectSquareNumber \n”;
Print out “2- Print min, max digit in an integer \n”;
Print out “3- Print character descending order \n”;
Print out “4- Quit\n”;
Print out “Select an operation:”;
switch(choice) { case 1: do
{ Input n;
}
while(n<0);
If (PerfectSquareNumber (n)==1) Print “ It is a
PerfectSquareNumber\n”;
Else Print “ It is not a PerfectSquareNumber\n”;
break; case 2: do
{ Input n;
}
while(n<0);
printMinMaxDigits( int n) ;
break;
case 3:// you add code yourself
}
}
while ( choice >0 & choice<4); End
Program 2(3 marks):
Write a C program that will execute repetitively using a simple menu as following: 1-Fibonacci
sequence
2-Check a date
3-Quit
Choose an operation:
1- When the option 1 is selected, the program will accept a positive integral number,
called as n, then the first n Fibonacci numbers will be printed out
2-When the option 2 is selected, the program will accept a date then the program will tell
that whether this data is valid or not.
3- If the option 4 is selected, the program quits