Starting from:

$25

COMP1400-Lab 4 Solved

Objective: Programming with conditions 

 

To evaluate a simple expression, you need to have two numbers and an operator. Assume that the normal operators of addition, subtraction, multiplication, and division should be recognized. So, 

 

If operator is ‘+’ Æ number1 + number2 will be evaluated and displayed.

If operator is ‘-’  Æ number1 - number2  will be evaluated and displayed.

If operator is ‘*’  Æ number1 * number2  will be evaluated and displayed.

If operator is ‘/’  Æ number1 / number2  will be evaluated and displayed. Any other operators Æ Display “Unknown operator!” message.

 

Note: For division operations, divisor should not be zero. 

 

Part A: RAPTOR Exercise

a)    Watch the full video clip about how to work with variable.

b)    Watch the first half of the video clip about how to make selections.

c)     Start RAPTOR and create a flowchart that asks the user to enter two numbers (num1 and num2) and an operator (op) and displays the result of expression after evaluation.

d)    Your flowchart should check for division by zero. 

e)    Save the flowchart to a file named “exprEval.rap” in the working directory on the PC you are using.

f)      Demonstrate the exprEval.rap file to GA/TAs and run with different input values. 

 

Part B: C Programming Exercise

a)    Write a program, represented by “exprEval.rap” flowchart, that allows the user to type in simple expressions of the form

 number operator number

 

b)    The program evaluates the expression and displays the results at the terminal, to two decimal places of accuracy.

c)     The program, however, is allowed to call the scanf function only once. 

d)    Remember to have the program check for division by zero.

e)    Save your program to a file named “exprEval.c” in the working directory on the PC you are using. 

f)      Demonstrate the exprEval.c file to GA/TAs and run with different input values.

 

A sample interaction is shown below:

 

 

Enter a simple expression: 10 + 230

Output: 10.00 + 230.00 = 240.00

 

Enter a simple expression: 13 * 2.5

Output: 13.00 * 2.50 = 32.5

 

Enter a simple expression: 23.5 / 0 Output: Division by zero!

 

Enter a simple expression: 100 X 25.5 Output: Unknown operator!
Hint: Because Raptor has only two simple data types of number and string, the selection of an operator has to be done by comparing the operator input within double quotation marks, e.g., op == “+”. In C implementation, it is comparisons of two characters in the way as op == ‘+’, where op is a char type and the conversion specification for it is “%c”.

More products