0% found this document useful (0 votes)
30 views6 pages

Workshop 04

The document describes exercises and programs from a workshop on pointers and simple menus in C programming. It includes: 1) Exercises on explaining pointer outputs and walkthrough problems involving pointers. 2) Developing a program with a menu containing options to check if a number is prime, print minimum and maximum digits of a number, and quit. 3) Developing another program with a menu containing options to print the Fibonacci sequence up to a number, check if a date is valid, and quit. 4) The option to create additional programs picking functions from another workshop and associating them with a menu.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views6 pages

Workshop 04

The document describes exercises and programs from a workshop on pointers and simple menus in C programming. It includes: 1) Exercises on explaining pointer outputs and walkthrough problems involving pointers. 2) Developing a program with a menu containing options to check if a number is prime, print minimum and maximum digits of a number, and quit. 3) Developing another program with a menu containing options to print the Fibonacci sequence up to a number, check if a date is valid, and quit. 4) The option to create additional programs picking functions from another workshop and associating them with a menu.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Subject: PRF192- PFC

Workshop 04
(Lê Nhật Minh Khôi – HE180029)

Objectives:
(1) Managing data using pointers
(2) Developing programs using simple menus

Part 1: Use notebook

Exercise 1 (1 mark) : Explain outputs:


n = 7; m = 6;

*pn = address n; *pm = address m;

*pn = 6 + 2*6 – 3*7 = – 3

*pm = *pm – *pn = 6 –(–3) = 9

 m + n = –3 + 9 = 6

c1=’A’ =65

=> c1 = *p1 = *p1 + 3= c1 + 3=65 + 3 =


68

c2=’F’=70

=> c2 = *p2 = *p2 – 5 = 70 – 5 = 65

=> c1 – c2 = *p1 – *p2 = 68 – 65 = 3


X= *p1 += 3 – 2*(*p2)
= *p1 + 3 – 2*5.1 = 3.2 + 3 – 2*5.1 = – 4

Y= *p2 –= 3*(*p1)
=*p2 – 3*(*p1)= 5.1 – 3*(-4)= 17.1

=> x +y = – 4 + 17.1 = 13.1


Exercise 2: (1 marks) What are outputs
m=*p1 = *p1 + 12 – m + (*p2)
=*p1+12=7+12=19
int n=7,m=8; n=*p2 = m + n – 2*(*p1) =m-n=8-19=11
int* p1= &n, => m + n = 19 – 11 = 8
*p2=&m;
*p1 +=12-m+
(*p2);
*p2 = m + n-
2*(*p1);
printf(“%d”,
m+n);
What is the
output?
8
int n=7,m=8;
int* p1= &n,
*p2=&m;
*p1 +=12-m+
(*p2);
*p2 = m + n-
2*(*p1);
printf(“%d”,
m+n);
What is the
output?
n=260; ptr *p(kiểu int: 4byte) trỏ đến
n’s addr. (0000 0001 0000 0100)
Then, print out n’s value =260.
Then ptr *pp trỏ đến *p (*p bị ép kiểu
char:1byte nên *pp sẽ chỉ lấy 4bit
cuối : 0100 rồi chuyển thành 0000) . Ptr
*p chỉ còn (0000 0001 0000 0000 = 256)
Then , print out giá trị n : 256.

Exercise 3: (2 marks) Walkthroughs


L = 2 * 6 + 3 * 7 + 5 * 5 = 58 %13=6

t = 7 = *p = *q = t => b= 7 => a = 6 => (6, 7)

a = *p = 3; b = *q = 4; (*p)+(*q) = 3 +4 = 7 < 12 => t =6


=> return = (2*6)%5= 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 primes
2- Print min, max digit in an integer;
3- Quit
Select an operation:

1- When user selects the option 1, the program will accept a


positive integral number and print out a message about
whether the input number is a prime 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- The program will terminate when user selects the option 3.

Analysis Nouns:
- positive integral number  int n
- A number represents a choice of user  int choice;
Functions:
int prime( int n)  see above
void printMinMaxDigits( int n)  see above
Suggested algorithm Begin
(logical order of Do /* Print out the menu and get user choice*/
verbs) { Print out “1- Process primes\n”;
Print out “2- Print min, max digit in an integer \n”;
Print out “3- Quit\n”;
Print out “Select an operation:”;
switch(choice)
{ case 1: do
{ Input n;
}
while(n<0);
If ( prime(n)==1) Print “ It is a prime\n”;
Else Print “ It is not a prime\n”;
break;
case 2: do
{ Input n;
}
while(n<0);
printMinMaxDigits( int n) ;
break;
}
}
while ( choice >0 & choice<3);
End

Program 2(3 marks): ( refer to the workshop 2 for algorithms)


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 3 is selected, the program quits

More Programs

You can pick 2 or 3 functions in the workshop 2, associate them to a new program.

You might also like