I l @ ve RuBoard |
Determine which expressions are true and which are false.
100 > 3 && `a'>`c'
100 > 3 `a'>'c'
!(100>3)
Construct an expression to express the following conditions:
number is equal to or greater than 1 but smaller than 9.
ch is not a q or a k character.
number is between 1 and 9 but is not a 5.
number is not between 1 and 9.
The following program has unnecessarily complex relational expressions as well as some outright errors. Simplify and correct it.
#include <stdio.h> int main(void) /* 1 */ { /* 2 */ int weight, height; /* weight in lbs, height in inches */ /* 4 */ scanf("%d, weight, height); /* 5 */ if (weight < 100) /* 6 */ if (height >= 72) /* 7 */ printf("You are very tall for your weight.\n"); else if (height < 72 && > 64) /* 9 */ printf("You are tall for your weight.\n"); else if (weight > 300 && ! (weight <= 300)) /* 11 */ if (!(height >= 48) /* 12 */ printf(" You are quite short for your weight.\n"); else /* 14 */ printf("Your weight is ideaL.\n"); /* 15 */ /* 16 */ return 0; }
What is the numerical value of each of the following expressions?
5 > 2
3 + 4 > 2 && 3 < 2
x >= y y > x
d = 5 + ( 6 > 2 )
`X' > `T' ? 10 : 5
x > y ? y > x : x > y
What will the following program print?
#include <stdio.h> int main(void) { int num; for (num = 1; num <= 11; num++) { if (num % 3 == 0) putchar(`$'); else putchar(`*'); putchar(`#'); putchar(`%'); } putchar(`\n'); return 0; }
What would the following program print?
#include <stdio.h> int main(void) { int i = 0; while ( i < 3) { switch(i++) { case 0 : printf("Merry"); case 1 : printf("Merr"); case 2 : printf("Mer"); default: printf("Oh no!"); } putchar(`\n'); } return 0; }
What's wrong with this program?
#include <stdio.h> int main(void) { char ch; int lc = 0; /* lowercase char count int uc = 0; /* uppercase char count int oc = 0; /* other char count while ((ch = getchar()) != `#') { if (`a' <= ch >= `z') lc++; else if (!(ch < `A') !(ch > `Z') uc++; oc++; } printf(%d lowercase, %d uppercase, %d other, lc, uc, oc); return 0; }
What will the following program print?
/* retire.c */ #include <stdio.h> int main(void) { int age = 20; while (age++ <= 65) { if (( age % 20) == 0) /* is age divisible by 20? */ printf("You are %d. Here is a raise.\n", age); if (age = 65) printf("You are %d. Here is your gold watch.\n", age); } return 0; }
What will the following program print when given this input?
q c g b #include <stdio.h> int main(void) { char ch; while ((ch = getchar()) != `#') { if (ch == `\n') continue; printf("Step 1\n"); if (ch == `c') continue; else if (ch == `b') break; else if (ch == `g') goto laststep; printf("Step 2\n"); laststep: printf("Step 3\n"); } printf("Done\n"); return 0; }
Rewrite the program in Question 9 so that it exhibits the same behavior but does not use a continue or a goto .
I l @ ve RuBoard |