Subject: PRF192-PFC Workshop 02: Objectives
Subject: PRF192-PFC Workshop 02: Objectives
Subject: PRF192-PFC Workshop 02: Objectives
Workshop 02
Objectives:
Practicing skills at analyzing and implementing simple programs
Contents: 7 programs
Program 1 2 3 4 5 6 7
Mark 2 2 1 1 2 1 1
Program 1 ( 2 marks)
Write a program that allows user inputting a simple expression containing one of four
operators +, -, *, / then the result is printed out to the monitor. Input format: num1
operator num2,
An example of user interface
Enter an expression (+ - * /): 4*5
Result: 20
Sample Analysis
Content Implementation
Nouns Expression, double num1, num2
format num1 operator num2 char op
double result
result
Verbs Begin
Accept num1, op, num2 scanf( “%lf%c%lf”, &num1, &op, &num2)
Calculate result switch (op)
Print out result { case ‘+’ : result = num1 + num2;
End print out result;
break;
case ‘-’ : result = num1 - num2;
print out result;
break;
case ‘*’ : result = num1 * num2;
print out result;
break;
case ‘/’ : if ( num2==0)
print out “Divide by 0 “
else
{ result = num1 / num2;
print out result;
}
break;
default: print out “Op is not
supported”
}
Suppose that:
In Viet Nam, each people has to pay for his/her yearly personal income tax as the
following description:
Rules:
Tax-free income:
Personal pending amount (tiền nuôi bản thân) pa= 9 000 000$/month
Alimony (tiền cấp dưỡng) for each his/her dependent pd= 3 600
000$/month/dependent
With n dependents, Yearly tax-free income: tf = 12*(pa + n*pd)
Based on taxable income, the employee has to pay his/her income tax with levels
pre-defined in the following table:
Write a program which will compute income tax of a people using the following
interface:
Case 1:
Case 1:
Program 3 (1 mark)
Program 4 (1 mark)
Program 5: (2 marks)
Program 6: (1 marks)
Related Each character will be stored as its ASCII code with value 0..255
knowledge
Problem Write a C program that will print out the ASCII code table.
Analysis Suggested algorithm (logical order of verbs)
ASCII code Begin
int code For each code = 0 to 255
{ Print out (“%c : %d, %o, %X\n”, code, code, code, code);
If (code !=0 && code %20==0) getchar(); /* code page of 20 lines */
}
End.
Program 7: (1 marks)
Problem Write a C program that will accept two characters then print
out ASCII code difference between them and characters
between them including code values in decimal, octal,
hexadecimal expansions in ascending order.
Analysis Suggested algorithm (logical order of verbs)
2 character Begin
char c1, c2 Accept c1 ;;
Difference Accept c2;
int d; If (c1 > c2 )
Character for swapping { t = c1; c1 = c2; c2= t;
operation }
char t d = c2 – c1;
Character for looping Print out d;
Char c For each c from c1 to c2
{ Print out (“%c : %d, %o, %X\n”, c, c, c, c);
}
End.
END