- What values are true in C?
- What does the C statement "int q;" do inside the computer, as
discussed in class?
- Is #include a reserved word in C? Explain your answer very briefly.
- When would you use a sentinel-controlled loop?
- ¶6 Write two complete examples, one of each of the preprocessor directives
we have reviewed in class.
- What is the main issue, as discussed in class, that you must consider when choosing a sentinel?
- Structured programming says that all programs can be written using a
combination of three kinds of control structures. What are they?
- ¶4 When you load a C program, what physical part of the computer
does it come from?
- ¶4 When you load a C program, what physical part of the computer
does it go to?
- ¶6 What are the structures we have studied in class so far
for accomplishing selection of code in C?
- ¶12 Write the appropriate shell command next to each task:
- compile a C program
- list the contents of a file
- show the location of the current directory
- create a new directory
- make a copy of a file
- ¶4 Advocates of this philosophy proved that all programming
could be done using only sequence, selection, and repetition. What
is its name?
- 12 pts. Write the six stages of a C program in order. Next to
each, write the shell command that accomplishes it.
Short Answers
Consider the partial program below. Imagine that you are going to add
some functions to it. Make up function names if needed, and use parameters
from the existing code.
#include <stdio.h>
/* Terry Harvey CISC105-20 TA: Liric Waterchard*/
int main(){
int i, j;
double x;
... /* more code here */
- ¶4
Write a prototype for a function that can be
called from main() to print the value in i. Do not write the call or
definition.
- ¶4 Show how you could call the function from 14 in main().
- ¶4
The function sum will take an integer and a double parameter
and returns the sum of the two numbers. Write the prototype only for sum.
- ¶4 Show how you could call the function from 16 in main().
- ¶10 Write a definition for a function that takes a parameter
that represents the number of asterisks in a line. The function
prints that number of asterisks only.
- ¶6 Explain how C would go about evaluating the following expression: 4 < x < 9
For the next section, choose the letter that shows the value that
would be in x. Assume x and y are declared as integers. Choose
"error" if you think a fragment will not compile and run. The
fragments are unrelated (each question is separate from the
others). HINT: Calculate your answer before you look at the selections.
- ¶2
x = 2 - 3 * 2 - 7;
(a) 5 | (b) 11 (c) 2 (d) error (e) none of the above |
- ¶2
x = 2 + 3 || 2 < 3;
(a) 5 | (b) 0 (c) 1 (d) error (e) none of the above |
- ¶2
y = 3.6;
x = (double)y * 2;
(a) 7.2 | (b) 7 (c) 6 (d) error (e) none of the above |
- ¶2
x = 15 % 3;
(a) 5 | (b) 1 (c) 3 (d) error (e) none of the above |
- ¶2
x = 6 % 7;
(a) 1 | (b) 7 (c) 6 (d) error (e) none of the above |
- ¶2
x = 3 / 4 && 4 / 3;
(a) 0 | (b) 1 (c) 12 (d) error (e) none of the above |
- ¶2
y = 30 == 3;
x = !y;
(a) 1 | (b) 0 (c) 27 (d) -1 (e) error |
- ¶2
x = (8 > 5 > 4);
(a) false | (b) 1 (c) 0 (d) error (e) none of the above |
- ¶2
y = 0 >= 0;
x = !y;
(a) 1 | (b) -1 (c) 0 (d) error (e) none of the above |
- ¶2
x = 8;
x *= 2;
(a) 4 | (b) 8 (c) 16 (d) error (e) none of the above |
- ¶2
x = 0 / 10110110;
(a) 1 | (b) 6 (c) 0 (d) error (e) none of the above |
- ¶2
x = 11 / 3;
(a) 3 | (b) 2 (c) 3.333 (d) error (e) none of the above |
- ¶2
y = 15 / 16;
x = 16 / y;
(a) 15 | (b) 1 (c) pi (d) error (e) none of the above |
- ¶2
double z = 8;
x = 8 || z/12;
(a) 0 | (b) 1 (c) 0.66 (d) error (e) none of the above |
- ¶2
double z = 10;
x = z / 4.0;
(a) .5 | (b) 2.5 (c) 2 (d) error (e) none of the above |
- ¶2
x = 3 < 2;
x++;
(a) 0 | (b) 4 (c) 3 (d) error (e) none of the above |
- ¶2 Which of the following assigns the value of 2 times y to x?
(a)x == 2 * y; | (b) 2 * y = x; (c) x = 2 x y; (d) x = 2 * y; |
- ¶2 The load stage of a C program is accomplished by which command?
(a) emacs | (b) a.out (c) pwd (d)
gcc (e) none of the above |
- ¶2 The link stage of a C program is accomplished by which command?
(a) emacs | (b) a.out (c) pwd (d)
gcc (e) none of the above |
- ¶2 The preprocess stage of a C program is accomplished by which command?
(a) emacs | (b) a.out (c) pwd (d)
gcc (e) none of the above |
- ¶2 The execute stage of a C program is accomplished by which command?
(a) emacs | (b) a.out (c) pwd (d)
gcc (e) none of the above |
- ¶2 The edit stage of a C program is accomplished by which command?
(a) emacs | (b) a.out (c) pwd (d)
gcc (e) none of the above |
- ¶4 Which of the following correctly orders memory speed from slowest
to fastest?
- disk cache RAM registers
- disk RAM cache registers
- RAM disk cache registers
- cache RAM disk registers
- none of the above
- ¶3 Which kind of memory is built into the CPU?
- registers
- ALU
- cache
- RAM
- none of the above
Use these choices to relate classroom discussions to the next four questions:
- measured in Kilobytes
- extremely fast, very high cost per byte
- a chip connected to the CPU via the bus
- very slow, but lowest cost per byte
- none of the above
- ¶3 The best description to associate with disk is:
- ¶3 The best description to associate with cache is:
- ¶3 The best description to associate with ALU is:
- ¶3 The best description to associate with RAM is:
For the next section, choose the letter that shows the value that
would be in x. Assume x and y are declared as integers. Choose
"error" if you think a fragment will not compile and run. The
fragments are unrelated (each question is separate from the others).
- ¶2
x = 3 + 3 * 2 - 7;
(a) 5 | (b) 11 (c) 2 (d) error (e) none of the above |
- ¶2
x = -4 || 0;
(a) 0 | (b) -4 (c) -1 (d) error (e) none of the above |
- ¶2
y = 0;
x = y++;
(a) 1 | (b) 2 (c) 0 (d) error (e) none of the above |
- ¶2
x = 5 % 3;
(a) 2 | (b) 1 (c) 3 (d) error (e) none of the above |
- ¶2
x = 2 % 3;
(a) 1 | (b) 0 (c) 3 (d) error (e) none of the above |
- ¶2
x = 14 % 7;
(a) 0 | (b) 2 (c) 4 (d) error (e) none of the above |
- ¶2
y = 30 != 3;
x = -3 && !y;
(a) 1 | (b) 0 (c) 27 (d) -3 (e) error |
- ¶2
x = (8 < 15 < 4);
(a) false | (b) 1 (c) 0 (d) error (e) none of the above |
- ¶2
y = 0 >= 1;
x = !y;
(a) 1 | (b) -1 (c) 0 (d) error (e) none of the above |
- ¶2
x = 8;
x += 2;
(a) 2 | (b) 8 (c) 6 (d) error (e) none of the above |
- ¶2
x = 0 / 6;
(a) 1 | (b) 6 (c) 0 (d) error (e) none of the above |
- ¶2
x = 14 / 4;
(a) 3 | (b) 2 (c) 3 (d) error (e) none of the above |
- ¶2
y = 5 / 6;
x = 2 / y;
(a) 5 | (b) 11 (c) -8 (d) error (e) none of the above |
- ¶2
y = 4.4;
x = y * 2;
(a) 8 | (b) 8.8 (c) 2 (d) error (e) none of the above |
- ¶2
double z = 8;
x = z/12;
(a) 1.5 | (b) 1 (c) 0.75 (d) error (e) none of the above |
- ¶2
double z = 18;
x = z / 12.0;
(a) 1.5 | (b) 1 (c) 0.75 (d) error (e) none of the above |
- ¶2
x = 3 < 2;
x = 3 / x;
(a) 0 | (b) 1 (c) 3 (d) error (e) none of the above |
- 26 pts.
Write the value of x after each of the following code fragments, OR
write ERROR if you think a fragment will not compile and run. The fragments are
unrelated. Assume x and y are declared as integers. If you write
calculations, be sure the eventual answer is circled so that I can
find it.
-
x = 3 + 4 * 2 - 12;
-
x = 3 || 4;
-
y = 3 != 3;
x = 3 && y;
-
x = (3 < 5 < 2);
-
y = 0 >= 1;
x = !y;
-
x = 5 % 3;
-
x = 8;
x += 2;
-
x = 11 / 6;
-
y = 5 / 6;
x = 2 / y;
-
y = 2.5;
x = y * 2;
-
y = 8;
x = --y;
-
x = 5 + 2 * - 3;
-
x = 3 < 3 || 4 < 5;
-
2 * y = 8;
x = y == y;
-
x = (5 > 4 < 3);
-
x = 8 % 2;
-
x = 5 % 3;
-
x = 3 % 5;
-
x = 4;
x += 5;
-
x = 15 / 6;
-
x = 5 / 0;
-
x = 0 / 5;
- 40 pts.
Write code fragments that will generate exactly the output shown. Follow the
instructions carefully. DECLARE ANY VARIABLES YOU NEED. You may write
next to the output so that you have more space.
- Write a while loop to generate this output:
1
2
3
4
- Write a while loop to generate this output:
2 4 6 8
- Write a loop to generate this output:
-50 0 50 100 150
- Write a single loop to generate this output. Print only a
single asterisk at a time. You may use only two printfs total.
***
***
***
- Write a loop to generate this output:
15 10 5 0 -5
- 15 pts.
Write a whole program that prompts the user for a value, reads the
value into a variable, then prints the number out and stops.
- 20 pts. Write a switch statement that will print "green" for input
integers 0-3; "red" for 4-7; "yellow" for 8-11; and "purple" for any other
integer. You may not use more than four different cases. Assume the
input is already in the declared integer variable "input".
- 20 pts.
Write a whole program using if, else, and other C code so that it behaves as follows:
shell% a.out
Enter a positive integer: 7
7 is less than 10!
Enter a positive integer: 8
8 is less than 10!
Enter a positive integer: 11
11 is not less than 10.
Enter a positive integer: -1
bye!
shell%
- 30 pts. Write a whole program that behaves as follows: Prompt the user for
an integer, one thousand times. Then show the user the average of those
numbers, the highest number, and the lowest number.
- 15 pts.
Write a main() function that
- declares two variables
- initializes them to the numbers 4.7 and 7.8
- adds them together
- prints the sum of the two
- stops.
- 30 pts. Write a whole program that behaves as follows: Prompt the user for
an integer, one hundred times. Each time through the loop, show the
highest number so far, the lowest number so far, and the average of
all input so far.
- 20 pts. Write a switch statement with no more than five cases
that behaves as follows. READ THE OUTPUT VERY CAREFULLY! The results
are not an obvious pattern. All possible cases are shown in the
output.
Assume the input is already in the declared integer variable "input".
strauss% a.out
Enter a positive integer, or -1 to stop:
5
5 is less than 20
Enter a positive integer, or -1 to stop:
35
35 is less than 40
Enter a positive integer, or -1 to stop:
55
55 is less than 60
Enter a positive integer, or -1 to stop:
61
61 is less than 100
Enter a positive integer, or -1 to stop:
85
85 is less than 100
Enter a positive integer, or -1 to stop:
106
106 is equal to or more than 100
Enter a positive integer, or -1 to stop:
-1
strauss%
- 15 pts.
Write a whole program that prompts the user for a value, reads the
value into a variable, then prints the number out and stops.
- 15 pts.
Write a main() function that
- declares two variables
- initializes them to the numbers 4.7 and 7.8
- adds them together
- prints the sum of the two
- stops.
- 24 pts. Short answer. Use only the space provided.
- What values are false in C?
- What values are true in C?
- Write a complete example of a preprocessor statement.
- What are the structures we have studied in class so far for accomplishing
repetition of code in C?
- Structured programming says that all programs can be written using a
combination of three kinds of control structures. What are they?
- Of the six stages of a C program discussed in class, which stages are
accomplished by the shell command "gcc lab1.c"?
- When you load a C program as discussed in class, what physical part of the computer
does it go to?
- 20 pts.
Write the value of x after each of the following code fragments, OR
write ERROR if you think a fragment will not compile and run. The fragments are
unrelated. Assume x and y are declared as integers.
-
y = 5;
x = y++;
-
x = 3 + 4 * 2 - 12;
-
x = 3 || 4;
-
y = 3 != 3;
x = 3 && y;
-
x = (3 < 5 < 2);
-
y = 0 >= 1;
x = !y;
-
x = 5 % 3;
-
x = 8;
x += 2;
-
x = 11 / 6;
-
y = 5 / 6;
x = 2 / y;
- 32 pts.
Write code fragments that will generate exactly the output shown. Follow the
instructions carefully. DECLARE ANY VARIABLES YOU NEED. You may write
next to the output so that you have more space.
- Write a loop to generate this output:
1 2 3 4
- Write a loop to generate this output:
3
2
1
0
- Write a single loop to generate this output:
*****
*****
*****
*****
*****
- Write a loop to generate this output:
-50 0 50 100 150
- 20 pts. Write a switch statement that will print "green" for input
integers 0-3; "red" for 4-7; "yellow" for 8-11; and "purple" for any other
integer. You may not use more than four different cases. Assume the
input is already in the declared integer variable "input".
- 20 pts. Write a whole program that behaves as follows: Prompt the user for
an integer, one thousand times. Then show the user the average of those
numbers, the highest number, and the lowest number.
- Suppose you wanted to create a directory called "labs" to put
all of your lab code in. Show how to create this directory.
60 pts. Multiple Choice: use the scan sheet for your answers.
- ¶2 Which of the following assigns the value of 2 times y to x?
(a)x == 2 * y; | (b) 2 * y = x; (c) x = 2 x y; (d) x = 2 * y; |
- ¶2 The load stage of a C program is accomplished by which command?
(a) emacs | (b) a.out (c) pwd (d)
gcc (e) none of the above |
- ¶2 The link stage of a C program is accomplished by which command?
(a) emacs | (b) a.out (c) pwd (d)
gcc (e) none of the above |
- ¶2 The preprocess stage of a C program is accomplished by which command?
(a) emacs | (b) a.out (c) pwd (d)
gcc (e) none of the above |
- ¶2 The execute stage of a C program is accomplished by which command?
(a) emacs | (b) a.out (c) pwd (d)
gcc (e) none of the above |
- ¶2 The edit stage of a C program is accomplished by which command?
(a) emacs | (b) a.out (c) pwd (d)
gcc (e) none of the above |
For the next section, choose the letter that shows the value that
would be in x. Assume x and y are declared as integers. Choose
"error" if you think a fragment will not compile and run. The
fragments are unrelated (each question is separate from the others).
- ¶2
x = 3 + 3 * 2 - 7;
(a) 5 | (b) 11 (c) 2 (d) error (e) none of the above |
- ¶2
x = -4 || 0;
(a) 0 | (b) -4 (c) -1 (d) error (e) none of the above |
- ¶2
y = 0;
x = y++;
(a) 1 | (b) 2 (c) 0 (d) error (e) none of the above |
- ¶2
x = 5 % 3;
(a) 2 | (b) 1 (c) 3 (d) error (e) none of the above |
- ¶2
x = 2 % 3;
(a) 1 | (b) 0 (c) 3 (d) error (e) none of the above |
- ¶2
x = 14 % 7;
(a) 0 | (b) 2 (c) 4 (d) error (e) none of the above |
- ¶2
y = 30 != 3;
x = -3 && !y;
(a) 1 | (b) 0 (c) 27 (d) -3 (e) error |
- ¶2
x = (8 < 15 < 4);
(a) false | (b) 1 (c) 0 (d) error (e) none of the above |
- ¶2
y = 0 >= 1;
x = !y;
(a) 1 | (b) -1 (c) 0 (d) error (e) none of the above |
- ¶2
x = 8;
x += 2;
(a) 2 | (b) 8 (c) 6 (d) error (e) none of the above |
- ¶2
x = 0 / 6;
(a) 1 | (b) 6 (c) 0 (d) error (e) none of the above |
- ¶2
x = 14 / 4;
(a) 3 | (b) 2 (c) 3 (d) error (e) none of the above |
- ¶2
y = 5 / 6;
x = 2 / y;
(a) 5 | (b) 11 (c) -8 (d) error (e) none of the above |
- ¶2
y = 4.4;
x = y * 2;
(a) 8 | (b) 8.8 (c) 2 (d) error (e) none of the above |
- ¶2
double z = 8;
x = z/12;
(a) 1.5 | (b) 1 (c) 0.75 (d) error (e) none of the above |
- ¶2
double z = 18;
x = z / 12.0;
(a) 1.5 | (b) 1 (c) 0.75 (d) error (e) none of the above |
- ¶2
x = 3 < 2;
x = 3 / x;
(a) 0 | (b) 1 (c) 3 (d) error (e) none of the above |
- ¶18
Write code fragments that will generate exactly the output shown. Follow the
instructions carefully. DECLARE ANY VARIABLES YOU NEED. You may write
next to the output so that you have more space.
- Write a loop to generate this output:
1
2
3
4
- ¶15 Write a function named "min" in the space provided
above main(). "Min" finds the smaller of its arguments, and does
nothing else. It should be designed so that it works properly with
this main(). Do not modify main().
int main(){
printf("The smallest of these two is: %lf", min(4.5, 7.6));
return 0;
}
- ¶20 Write a whole program that behaves as follows: Prompt the user for
two integers and print the average of the two integers.
- ¶15 Write a switch statement that will print "green" for input
integers 0,3,6; "red" for 1,4,7; "mauve" for 2,5,8; and "blue" for any other
positive integer. You may not use more than four different cases. Assume the
input is already in the declared integer variable "input".
- Extra Credit:
- What is the emacs keyboard shortcut to open a file?
- To save a
file?
- To indent a whole region of code at once?