Programming True False C477B0
1. State whether each of the following is true or false. If false, explain why.
1. a. Function printf always begins printing at the beginning of a new line.
- False. printf prints output starting at the current cursor position, not necessarily a new line.
1. b. Comments cause the computer to display the text after // on the screen when the program is executed.
- False. Comments are ignored by the compiler and do not produce output.
1. c. The escape sequence \n in a printf format control string positions the output cursor to the beginning of the next line.
- True. \n moves the cursor to the start of the next line.
1. d. All variables must be given a type when they’re defined.
- True. In C, every variable must have a declared type.
1. e. C considers the variables number and NuMbEr to be identical.
- False. C is case-sensitive; these are different variables.
1. f. A switch expression can take integers, floats, and double as parameters.
- False. switch expressions must be integral types (int, char, etc.), not float or double.
1. g. All arguments following the format control string in a printf function must be preceded by an ampersand (&).
- False. Arguments are passed by value; ampersand is not used in printf arguments.
1. h. The remainder operator (%) can be used only with integer operands.
- True. % works only with integers.
1. i. The arithmetic operators *, /, %, +, -, and – all have the same precedence.
- False. *, /, % have higher precedence than + and -.
1. j. A program that prints three lines of output must contain three printfs.
- False. One printf can print multiple lines using \n.
2. Exchange money between Eva and Faith.
2.1 Problem: Write steps and C program to exchange two real values input from keyboard and print results.
2.2 Steps:
1. Read two real numbers Eva and Faith.
2. Store Eva's amount in a temporary variable.
3. Assign Faith's amount to Eva.
4. Assign temporary variable to Faith.
5. Print the exchanged values.
2.3 Explanation: Use a temporary variable to swap values without losing data.
2.4 C program snippet:
```c
#include
int main() {
float Eva, Faith, temp;
printf("Enter two real values to be exchange: ");
scanf("%f %f", &Eva, &Faith);
printf("Values entered are Eva = %.4f and Faith = %.4f\n", Eva, Faith);
temp = Eva;
Eva = Faith;
Faith = temp;
printf("Values after exchange are Eva = %.4f and Faith = %.4f\n", Eva, Faith);
return 0;
}
```
3. Correct the if statements.
3.1 a.
Original: if(x = 10);
Correction: if(x == 10) {
printf("x is equal to 10\n");
}
Explanation: Use == for comparison, remove semicolon after if.
3.2 b.
Original: if(x≤10);
Correction: if(x <= 10) {
printf("x is less than or equal to 10\n");
}
Explanation: Use <= operator, lowercase printf, remove semicolon.
3.3 c.
Original: printf("The value is %d\n", &number);
Correction: printf("The value is %d\n", number);
Explanation: Remove & to print value, not address.