0% found this document useful (0 votes)
444 views

Subject: PRF192-PFC Workshop 05

The document provides problems and solutions for a programming workshop. It includes 5 problems covering topics like dice throws, ball lottery, date validation, quadratic equations, and calculating greatest common divisor and least common multiple. For each problem, it provides the required output, algorithms, and full C code solutions. The problems involve using random numbers, loops, conditional statements, functions, and menus.
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)
444 views

Subject: PRF192-PFC Workshop 05

The document provides problems and solutions for a programming workshop. It includes 5 problems covering topics like dice throws, ball lottery, date validation, quadratic equations, and calculating greatest common divisor and least common multiple. For each problem, it provides the required output, algorithms, and full C code solutions. The problems involve using random numbers, loops, conditional statements, functions, and menus.
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/ 13

Subject: PRF192- PFC

Workshop 05
Objectives: Use functions in the library math.h for getting an integer at random
and stdio.h for formatting output

Grading: 6 problem, marks: 2, 2, 3, 3

Problem 1. Dice Throws ( 2 marks)

You are required to develop a program that will throw two dice until the top faces of the
two dice total to a specified number.

The output from your program looks something like: 

Dice Thrower
============
Total sought : 11
Result of throw 1 : 1 + 3
Result of throw 2 : 4 + 4
Result of throw 3 : 6 + 2
Result of throw 4 : 5 + 6
You got your total in 4 throws!

Algorithm should be as the following

/* Get a random integer between min and max randomly */


int intRandom(int min, int max)
{ /* Refer to the lecture to get algorithm for this task */
}
main()
Variable : int total, x,y, count
do
{ Accept total;
}
while (total<2 || total >12);
count =1;
do
{ x= intRandom(2,6);
y= intRandom(2,6);
Print out (“Result of throw %d “ %d + %d\n”, count, x, y)
count++;
}
while (x+y != total);
#include<stdio.h>
#include<time.h>
#include<stdlib.h>
int intrandom(int a,int b)
{
return(a+rand()%(b-a-1));
}
int main()
{
int total,x,y,count;
printf("Dice Thrower\n");
do {
printf("Total:");
scanf("%d",&total);

}
while (total<2||total>12);
count = 1;
do{
x = intrandom(1,6);
y = intrandom(1,6);
printf("Result of throw %d: %d+%d\n",count,x,y);
count++;

}
while (x+y!=total);
printf("You got your total in %d throws\n", count-1);
}

Problem 2. Ball Lottery ( 2 marks)

A basket contains ten balls.


Balls are numbered from 1 to 10.
User gets a pair of balls and he/she hopes that sum of numbers is equal to a known
expected total.
This problem is the same with the previous problem but the total is between 2 to 20.

The output from your program looks something like: 

Ball Lottery
============
Total sought : 11
Result of picks 1 and 2 : 1 + 3
Result of picks 3 and 4 : 4 + 5
Result of picks 5 and 6 : 6 + 3
Result of picks 7 and 8 : 5 + 6
You got your total in 8 picks!

The algorithm for this program is similar to those in the previous problem
#include<time.h>
#include<stdlib.h>
int intrandom(int a,int b)
{
return(a+rand()%(b-a-1));
}
int main()
{
int total,x,y,count;
printf("Ball Lottery\n");
do {
printf("Total
sought:");

scanf("%d",&total);
}
while (total<2||
total>12);
count = 1;
do{
x=
intrandom(1,10);
y=
intrandom(1,10);
printf("Result
of picks %d and %d: %d+%d\n",count++,count,x,y);
count++;
}

while (x+y!=total);
printf("You got your
total in %d throws\n", count-1);
}

Problem 3. Program using menu ( 2 marks)

Write a C program using the following simple menu:


1- Processing date data
2- Character data
3- Quit
Choose an operation:
- When user chooses 1: User will enter values of date, month, year then the
program will announce whether this date is valid or not.
- -When user chooses 2: User will enter two characters, then the program will print
out ASCII codes of characters between them using descending order. Examples:
Input: ca
Output:
c: 99, 63h
b: 98, 62h
a: 97, 61h

#include<stdio.h>
#include<time.h>
#include<stdlib.h>
#include<windows.h>
int date(int d,int m,int y)
{
int maxd = 31;
if(d<1||d>31||m<1||m>12) return 0;
if(m==4||m==6||m==9||m==11)
maxd = 30;
else if(m==2){
if(y%400==0||(y%4==0&&y%100!=0)) maxd=29;
else maxd=28;
}
return d<=maxd;

}
void cha(char c, char d)
{
char a,b;
printf("Input a character:");
scanf("%c", &a);
fflush(stdin);
a=getchar();
printf("Input a character:");
scanf("%c", &b);
fflush(stdin);
b=getchar();
printf("ASCI code:%d,%Xh\n",a,a);
printf("ASCI code:%d,%Xh\n",b,b);
system("pause");
system("cls");

}
int main(){
int choice;
int c;
do {
printf("1/Processing date data\n");
printf("2/Character data\n");
printf("3/Quit\n");
printf("Your choice:");
scanf("%d", &choice);
switch(choice){
case 1: int d,m,y;
printf("Nhap ngay:");
scanf("%d",&d);
printf("Nhap thang:");
scanf("%d",&m);
printf("Nhap nam:");
scanf("%d",&y);
if(date(d,m,y))
printf("Valid day\n");
else printf("Invalid day\n");
system("pause");
system("cls");
break;
case 2: cha(c,d);
break;
default: printf("Quit!");

}
} while (choice>0&choice<3);
}
Problem 4. Program using menu ( 2 marks)

Write a C program using the following simple menu:


1- Quadratic equation ( phương trình bậc 2)
2- Bank deposit problem
3- Quit
Choose an operation:
- When user chooses 1: User will enter values describing a quadratic equation
then the program will print out its solution if it exists.
- When user chooses 2: User will enter his/her deposit ( a positive number), yearly
rate ( a positive number but less than or equal to 0.1), number of years ( positive
integer), then the program will print out his/her amount after this duration.

Validations
- Deposit, d >0
- Yearly rate, r: > 0.0 to <1.0
- Number of year, n>0
- Amount at the n(th) year: P = d(1+r)n , Use the function pow(x,y) in Math.h for xy

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

void function1()
{
float a, b, c;
printf("Input a b c : a*x^2 + b*x +c = 0 \n");
scanf("%f%*c%f%*c%f", &a, &b, &c);
if (a == 0)
{

if ( (b == 0) && (c != 0) ) printf("Vo nghiem\n");


else if ( (c == 0) && (b == 0) )
printf("Phuong trinh co 1 nghiem duy nhat\n");
else
printf("X = %0.3f\n", -c/b);
}
else
{
int delta;
delta = b*b - 4*a*c;
if (delta < 0) printf("Vo nghiem\n");
else if (delta == 0)
{
printf("Phuong trinh co nghiem kep : \n");
printf("X = %0.3f\n", -b / 2 / a );
}
else
{
printf("Phuong trinh co 2 nghiem : \n");
printf("X1 = %0.3f \n", (- b - sqrt(delta) )/ 2 / a );

printf("X2 = %0.3f \n", (- b + sqrt(delta) )/ 2 / a );


}
}
printf("\n");
system("pause");
system("cls");
}

void function2()
{
printf("Bank deposit \n");
printf("============ \n");
double deposit, rate, amount;
int month, check;
do {
printf("Input your deposit , monthly rate and the number of months : ");
fflush(stdin);
check = scanf("%lf%*c%lf%*c%d", &deposit, &rate, &month);
if (check != 3) printf("Retype 3 value please!\n");
else {
if (deposit <= 0 ) printf("Deposit must be positive!\n");
if (month <= 0 ) printf("The number of months must be positive!\n");
if (rate <= 0 || rate > 0.1) printf("Monthly rate must be positive but less
than or equal to 0.1!\n");
}
} while ( deposit <= 0 || month <= 0 || rate <= 0 || rate > 0.1 || check != 3 );
amount =(float) deposit * pow( (1 + rate) , month );
printf("Your amount after duration : %f", amount);
printf("\n");
system("pause");
system("cls");
}

int main()
{
int n,s;
do
{
printf("1-Quadratic Equation\n");
printf("2-Bank deposit\n");
printf("3-Quit\n");
printf("Choice [1..3] : ");

scanf("%d", &s);

switch(s)
{
case 1:function1();break;
case 2:function2();break;

}
} while (s>0&s<3);
}

Problem 5 . Program using menu ( 1 marks)

Viết chương trình C sử dụng menu như sau:


1. Ước số chung lớn nhất (UCLN) của hai số a và b.
2. Bội chung nhỏ nhất (BCNN) của hai số a và b.
3. Thoát
#include<stdio.h>
#include<windows.h>
#include<algorithm>
#include<windows.h>
int bcnn(int a,int b){
printf("Nhap a:");
scanf("%d",&a);
printf("Nhap b:");
scanf("%d",&b);
int lcm;
int maxV = a*b;
int step = std::max(a, b);
for(int i = step; i <= maxV; i+= step){
if(i % a == 0 && i % b == 0){
lcm = i;
break;
}
}
printf("BCNN(%d, %d) = %d\n", a, b, lcm);
system("pause");
system("cls");
}
int ucln(int a,int b,int i,int hcf) {
printf("Nhap a:");
scanf("%d",&a);
printf("Nhap b:");
scanf("%d",&b);
for(i = 1; i <= a || i <= b; i++) {
if( a%i == 0 && b%i == 0 )
hcf = i;
}

printf("USCLN(%d, %d) = %d\n", a,b,hcf);


system("pause");
system("cls");
}
int main(){
int n,a,b,i,hcf;
do{
printf("1- UCLN cua 2 so\n");
printf("2- BCNN cua 2 so\n");
printf("3- Thoat\n");
printf("Chon: ");
scanf("%d",&n);
switch(n)
{
case 1:
ucln(a,b,i,hcf);
break;
case 2:
bcnn(a,b);
break;
default: printf("Thoat!");

}
} while (n>0&n<3);
return 0;
}

Problem 6 . Program using menu ( 1 marks)

Viết chương trình C sử dụng menu như sau:


1. Tính tổng 2 khung giờ nhập từ bàn phím
2. Hiệu của 2 khung giờ nhập vào từ bàn phím
3. Thoát

Ví dụ tính tổng 2 khung giờ nhập từ bàn phím


#include<stdio.h>
#include<windows.h>
int main () {
int h1,h2,m1,m2,s1,s2,n,t1,t2,t,h,m,s,p;
do{

printf("1. Tinh tong 2 khung gio \n");


printf("2. Hieu 2 khung gio\n");
printf("3. Thoat\n");
printf("Chon: ");
scanf("%d",&p);
switch(p) {
case 1:

printf("Input amount of time 1:(HH:MM:SS)");


scanf("%d:%d:%d",&h1,&m1,&s1);
printf("Input amount of time 2:(HH:MM:SS)");
scanf("%d:%d:%d",&h2,&m2,&s2);
t1=h1*3600 + m1*60 + s1;
t2=h2*3600 + m2*60 + s2;
t = t1 + t2;
h = t/3600;
m = (t - h*3600)/60;
s = (t - h*3600)%60;
printf("Output time: %d:%d:%d\n",h,m,s);
system("pause");
system("cls");
break;

case 2:
printf("Input amount of time 1:(HH:MM:SS)");
scanf("%d:%d:%d",&h1,&m1,&s1);
printf("Input amount of time 2:(HH:MM:SS)");
scanf("%d:%d:%d",&h2,&m2,&s2);
t1=h1*3600 + m1*60 + s1;
t2=h2*3600 + m2*60 + s2;

if (t1>t2) t=t1-t2; else t=t2-t1;


h = t/3600;
m = (t - h*3600)/60;
s = (t - h*3600)%60;
printf("Output time: %d:%d:%d\n",h,m,s);
system("pause");
system("cls");
break;

default : printf("Thoat");
}
} while (p>0&p<3);;
}

You might also like