Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Write an ANSI-C program that reads inputs from the user one integer, one floating point number, and a character operator. The program does a simple

image text in transcribed
image text in transcribed
image text in transcribed
image text in transcribed
Write an ANSI-C program that reads inputs from the user one integer, one floating point number, and a character operator. The program does a simple calculation based on the two input numbers and the operator. The program continues until both input integer and floating point number are-1. Implementation download partially implemented program lab3conv.c, compile and run it. Observe that o 9/2 gives 4, not 4.5. (When the result is converted to float, get 4.0). o In order to get 4.5, we need to convert 9 or 2 (or both) to float, before division. . One trick is to multiply 9 or 2 by 1.0. This forces the conversion from int to float. Note that this must be done before the division. The "official approach, is to explicitly cast 9 or 2 to float, using the cast operator (float). Note that this must be done before the division. Cast after the division does not work correctly. o When assigning a float value to an int variable, the int variable gets the integral part value. The floating point part is truncated (without any warning.) Also no rounding occur here. Note, the first two observations are the same in Java. For the last observation, when assigning a float value to an int variable, Java will give compilation error because "possible lossy conversion from float to int". In this case, an explicit cast is required. use scanf to read inputs (from Standard input), each of which contains an integer, a character ('+', '-!i*' or '/) and a floating point number (defined as float) separated by blanks. Assume all the inputs are valid. define a function float fun_IF (int, char, float) which conducts arithmetic calculation based on the inputs define another function float fun_II (int, char, int) which conducts arithmetic calculation based on the inputs define another function float fun_FF (float, char, float) which conducts arithmetic calculation based on the inputs note that these three functions should have the same code in the body. They only differ in the parameter type and return type. pass the integer and the float number to both the three functions directly, without explicit type conversion (casting). display prompts and outputs as shown below. Once the program is running, observe the output of the first 2 lines, where conversions happen in arithmetic and assignment operations. Convince yourself of the outputs (why three arithmetic operations have different results, why i and j both get 3?) Sample Inputs/Outputs: red 330 % a.out 9/2=4.000000 9+1.0/2=4.500000 9/2*1.0-4.000000 9/(2*1.0)-4.500000 (float) 9/2=4.500000 9/(float) 2=4.500000 (float) (9/2) -4.000000 3.0*9/2/4=3.375000 9/2*3.0/4=3.000000 9*3/2*3.0/4=3.000000 i: 3 j: 3 Enter operand_1 operator operand_2 separated by blanks> 12 + 22.3024 Your input '12 + 22.302401' result in 34.302399 (fun_IF) 34.000000 (fun_II) 34.302399 (fun FF) Enter operand_1 operator operand_2 separated by blanks> 12 * 2.331 Your input '12 * 2.331000' result in 27.972000 (fun_IF) 24.000000 (fun II) 27.972000 (fun_FF) Enter operand_1 operator operand_2 separated by blanks> 2 / 9.18 Your input '2 / 9.180000' result in 0.217865 (fun_IF) 0.000000 (fun II) 0.217865 (fun_FF) Enter operand 1 operator operand_2 separated by blanks> -1 + -1 red 331 % #include main() { int opl; float op2; char operator; float resu, resu2, resu3; float a = 9/2; float b = 9*1.0/2; // force 9 to be converted before / float c = 9/2*1.0; // force 2 to be converted before /. but not working float c2 = 9/(2*1.0); // force 2 to be converted before / float d = (float)9/2; 11 cast 9 before / float e = 9/(float)2; // cast 2 before / float f = (float) (9/2); // cast after 7. not working printf("9/2-4f991.0/2=*f 9/2*1.0=f 9/(2*1,0)=1f ", a, b, c, c2): printf("{float)9/2=4f9/(float)2-27 (float) (9/2)=4f ", d, e, f): // conversion in arithemetic printf("3.0*9/2/4=1f9/2*3.0/43f 943/2*3.0/4=2f ", 3.0*9/2/4, 9/2*3.0/4, 9/2*3.0/4); // conversion in assignment float f2 = 3.96; float f3 = 3.03; int i = f2; int j = f3; printf("i: dj: %d ", i, j): // - } float fun_IF(int opi, char operator, float op2) { } float fun_II (int opi, char operator, int op2) { } float fun_FF(float op1, char operator, float op2) { }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Beginning C# 2005 Databases

Authors: Karli Watson

1st Edition

0470044063, 978-0470044063

More Books

Students also viewed these Databases questions