Problem 1.1 Compute division
Fix the program below such that it prints the correct result. Why is the result 0.000? Write your answer and explanations within comments.
#include <stdio.h>
int main() { double result; / The result of our calculation / result = (3 + 1) / 5;
printf(The value of 4/5 is %lf
, result); return 0;
}
Problem 1.2 Wrong output
Fix the program below such that it prints the correct value. Why does the program print The result is -1073745604? (Values will vary). Write your answer and explanations within comments.
#include <stdio.h>
int main() { int result; / The result of our calculation / result = (2 + 7) 9 / 3;
printf(The result is %d
); return 0;
}
Problem 1.3 A compile error)
You will get compiler errors, when you try to compile the example code given below.
Read the error messages that the compiler produces and fix the errors such that your source code compiles successfully. Then fix the program to print the correct result. Explain within comments the reason of the errors and describe your fixes. include <stdio.h>
int main() { float result; / The result of the division / int a = 5; int b = 13.5; result = a / b;
printf(The result is %d
, result); return 0; }
Problem 1.4 Simple arithmetics |
Write a program which does the following:
- assigns 17 to x and 4 to y,
- prints the values of x and y,
- computes the sum of x and y and prints the result,
- computes the product of x and y and prints the result,
- computes the difference of x and y (x minus y) and prints the result,
- computes the division of x and y (x divided by y) and prints the result (the result should be a float),
- computes the remainder of the division of x and y in this order and prints the result.
Your solution has to satisfy the requirements from the problem description and has to pass the following testcase and potentially other testcases which are uploaded. All characters are relevant for passing testcases including newlines and spaces.
Testcase 1.4: inputTestcase 1.4: output
x=17 y=4 sum=21 product=68 difference=13 division=4.250000 remainder of division=1
Problem 1.5 Using printf for multiple data types and conversions
Write a program which:
- declares and initializes an integer variable x with 2138, and prints the value of x over 9 places,
- declares and initializes a float variable y with 52.358925, and prints the value of y over 11 places and with a floating point precision of 5,
- declares and initializes a char variable z with G, and prints the character on the screen,
- declares and initializes a double variable u with 61.295339687, and prints the value of u with a floating point precision of 7.
Your solution has to satisfy the requirements from the problem description and has to pass the following testcase and potentially other testcases which are uploaded. All characters are relevant for passing testcases including newlines and spaces.
Testcase 1.5: inputTestcase 1.5: output
x= 2138
y= -52.35892 z=G u=61.2953397
Problem 1.6 Printing a char as character and as decimal value
Write a program which declares and initializes a char variable c with F and prints on the screen the third character (within the alphabet) after c as a character and as the corresponding ASCII code using only arithmetic operations.
Reviews
There are no reviews yet.