set4 40-50 Flashcards
/* 40
Write a program to read a float representing a number of degrees Celsius, and print as a float the equivalent
temperature in degrees Fahrenheit. Print your results in a form such as
Enter a temperature is Celsius degrees: 100
100.0 degrees Celsius is equivalent to 212.0 degrees Fahrenheit
*/
include <stdio.h><br></br> int main(void) {</stdio.h>
float C, F;
printf(“Enter the temperature in Celsius: “);
scanf(“%f”, &C);
printf(“%5.1f degrees Celsius is %.1f degrees Fahrenheit\n”, C, F = C*2.12);
return 0;
}
/* 41
Write a program that asks the user to enter an integer number of seconds, then prints as output the equivalent time in
hours, minutes and seconds.
*/
int initTime, hours, minutes, seconds;
printf(“Enter the number of seconds: “);
scanf(“%d”, &initTime);
hours = initTime/3600; minutes = (initTime % 3600) / 60; seconds = ((initTime % 3600) % 60);
printf(“%d seconds is %dhr %dm %ds”, initTime, hours, minutes, seconds);
/* 42
Where possible, write equivalents for the following statements using compound assignment operators:
s = s / 5;
q = q * n + 4;
z = z - x * y;
t = t + (u % v);
*/
s /= 5;
q = q * n + 4;
z -= x * y
t += u % v;
/* 43
What values are assigned to n, m, and p, given these initial values?
i = 4; j = 8;
n = ++i * –j;
m = i + j–;
p = i + j;
*/
n = 35; m = 12; p = 11;
/* 44
What are the values of n, m, and p after the execution of this code fragment?
j = 5; k = 2;
n = j - ++k;
m = j– + k–;
p = k + j;
*/
n = 2; m = 8; p = 6;
/* 45
What arc the values of x, y, and z after the execution of this code fragment?
x = 3; y = 5; z = 2;
x *= y+ z;
y /= 2 * z + 1;
z += x;
*/
x = 21; y = 1; z = 23;
/* 46
Show the output produced by the following program fragment.
int i = 1; int j = 2;
i -= - – j;
printf(“%d %d\n”, i, j);
*/
2 1; i = 2, j = 1
/* 47
Show the output produced by the following program fragment.
int i = 1;
int j = 2;
i -= - - - j;
printf(“%d %d\n”, i, j);
*/
3 2; i = 3, j = 2;
/* 48
Show the output produced by the following program fragment.
int i = 1;
int j = 2;
i = + + + + + j;
j = - - - - - j;
printf(“%d %d\n”, i, j);
*/
2, -2; i = 2, j = -2
/* 49
Show the output produced by the following program fragment.
int i = 1;
int j = 2;
i = - + - + - + j;
printf(“%d %d\n”, i, j);
*/
-2, 2; i = -2, j = 2;
/* 50
Show the output produced by the following program fragment.
int i = 1;
int j = 2;
i = + - + - + - j;
printf(“%d %d\n”, i, j);
*/
-2, 2
NOTE: evidently unary + does not do anything