SE17D04 - Bạch Gia Bảo DE180644 - Workshop4

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 14

Subject: PRF192- PFC

Workshop 04
BẠCH GIA BẢO – DE180644

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

Part 1: Use notebook

Exercise 1 (1 mark) : Explain outputs:


Exercise 2: (1 marks) What are outputs
int
n=7,m=8; int n=7,m=8;
int* p1= &n, int* p1= &n,
*p2=&m; *p2=&m;
*p1 +=12- *p1 +=12-m+
m+ (*p2); (*p2);
*p2 = m + n- *p2 = m + n-
2*(*p1); 2*(*p1);
printf(“%d”, printf(“%d”,
m+n); m+n);
What is the What is the
output? output?
Exercise 3: (2 marks) Walkthroughs
Part 2: Develop a program using simple menu

Program 1(1 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
#include <stdio.h>
#include <math.h>

void prime(){ //Ham kiem tra so nguyen to


int n;
printf("\nAccept the number: ");
scanf("%d",&n);

int m=sqrt(n);
int i;
if (n<2){
printf("The input number is not a prime\n");
return ;
}
for (i=2; i<=m; i++)
if (n%i==0){
printf("The input number is not a prime\n");
return ;
}
printf("The input number is a prime\n");
}

void printMinMaxDigits(){ //Ham tim chu so lon nhat va be nhat


int n;
printf("\nAccept the number: ");
scanf("%d",&n);

int digit;
int min=9,max=0;
while (n>0){
digit=n%10;
n=n/10;
if (digit<=min) min=digit; //tim chu so be nhat
if (digit>=max) max=digit; //tim chu so lon nhat
}
printf("The minimum digit in the input number: %d\n",min);
printf("The maximum digit in the input number: %d\n",max);
}

int main(){
int choice, n;
do{
printf("Menu\n");
printf("1- Process primes\n"); //Kiem tra so nguyen to
printf("2- Print min, max digit in an integer\n"); // In ra chu so be nhat va lon nhat cua
so nhap vao
printf("3- Quit\n"); //Thoat khoi chuong trinh
printf("\nPlease choose a calculation tool: ");
scanf("%d",&choice);

switch (choice){
case 1:
prime();
break;
case 2:
printMinMaxDigits();
break;
case 3:
printf("\nThank for using tool");
break;
default:
printf("\nInvalid choice. Please try again.\n");
break;
}
printf("\n=============================\n");
}
while (choice!=3);
return 0;
}

Program 2(2 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

#include <stdio.h>
#include <math.h>

void fibo(){ //Ham in ra n so fibonacci dau tien


int n,i;
do{
printf("Accept n: ");
scanf("%d",&n);
}
while (n<1);

if (n==1) printf("The first n Fibonacci numbers: 1");


else if (n==2) printf("The first n Fibonacci numbers: 1, 1");
else{
printf("The first n Fibonacci numbers: 1, 1");
double t1=1,t2=1,f;
for (i=3;i<=n;i++){
f=t1+t2;
printf(", %0.lf",f);
t1=t2;
t2=f;
}
}
}

void validDate(){ //Ham kiem tra ngay thang hop le


int d, m, y, check;
printf("Accept the date:\nDay: ");
scanf("%d",&d);
printf("Month: ");
scanf("%d",&m);
printf("Year: ");
scanf("%d",&y);

int maxd = 31; // Ngay toi da cua thang 1, 3, 5, 7, 8, 10, 12


if ( d<1 || d>31 || m<1 || m>12){
printf("The input data is not valid\n "); // Kiem tra ngay hop le
return;
}
if ( m==4 || m==6 || m==9 || m==11) maxd=30; //Ngay toi da cua thang 4, 6, 9, 11
else if (m==2){ // Kiem tra nam nhuan
if ( y%400==0 || ( y%4==0 && y%100!=0)) maxd=29;
else maxd=28;
}
if (d>maxd) printf("The input data is not valid\n ");
else printf("The input data is valid\n ");
}

int main(){
int choice, n;
do{
printf("Menu\n");
printf("1- Fibonacci sequence\n"); //In ra n so fibonacci
printf("2- Check a date\n"); // Kiem tra ngay thang hop le
printf("3- Quit\n"); //Thoat khoi chuong trinh
printf("\nPlease choose a calculation tool: ");
scanf("%d",&choice);

switch (choice){
case 1:
fibo();
break;
case 2:
validDate();
break;
case 3:
printf("\nThank for using tool");
break;
default:
printf("\nInvalid choice. Please try again.\n");
break;
}
printf("\n=============================\n");
}
while (choice!=3);
return 0;
}

Program 3 (3 marks): (viết 3 chương trình riêng cho 3 bài sau)


1. Viết chương trình in ra các số từ 1 đến 100 chia hết cho 3 hoặc chia hết cho 4.
2. Viết chương trình:
- Nhập số nguyên n.
- Tính tổng cho biểu thức sau:

Ví dụ : n = 10 => S = 1.855812

S= 1+
2! 3 ! 4 !
5
n!
+ 5 + 5 +…+ 5
2 4 6 2n

3. Viết chương trình nhập 1 số nguyên từ bàn phím và in ra các số hoàn hảo nhỏ hơn
n. (số hoàn hảo là số có tổng ước số = chính nó. Ví dụ số 6 là số hoàn hảo: 6 =
1+2+3

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

You might also like