Review problems#
Activity 64 (Suitable datatype for states of a state machine)
Which datatype/s can be used to define the state constants of an oven, e.g., HEATING, TARGET_TEMP_REACHED?
unsignedintdoublechar[](string)enum
Review resources
Section Data types
Section Enumerated type
Activity 65 (Typical layout of a C program)
Sketch the typical layout of a C program.
What is definition and initialization of a variable?
What does
#includedirective do?What does the following program do?
char tmp[4] = {'W', 'A', 'T', '?'}; int i; int main() { #include <stdio.h> for (i = 0; i < _Countof(tmp); ++i) putc(tmp[i], stdout); putc('\n', stdout); }
Do you have any improvement ideas on the code above?
Draw a flowchart for the code above.
Review resources
Section Including libraries
Section Object
Activity 66 (Flowchart to statement)
Which statement does the following flowchart correspond to?
flowchart LR
s( ) --> if{ } --> e( )
if --> body[ ] --> e
ifwhilerand()if-elsedo-while
Review resources
Section if
Section Enumerated type
Activity 67 (Flowchart to statement 2)
Which statement does the following flowchart correspond to?
flowchart LR
s( ) --> while{ } --> body[ ] --> while
while --> e( )
ifwhilerand()if-elsedo-while
Activity 68 (Implementable flowcharts)
Which of the following flowcharts can be implemented in C?
You are not allowed to use:
gotocontinuebreakmultiprocessing
flowchart LR s( ) --> if{ } --> e( ) if --> body[ ] --> p1[ ] --> e body[ ] --> p2[ ] --> eflowchart LR s( ) --> if1{ } --> if2{ } --> body2[ ] --> e( ) if1 --> e if2 --> if1flowchart LR s( ) --> p1[ ] --> p2[ ] --> e( ) p2 --> p1flowchart LR s( ) --> if{ } --> elsebody[ ]--> e( ) if --> ifbody[ ] --> eflowchart LR s( ) --> if{ } --> if2{ } --> if2body[ ] --> e( ) if --> ifbody[ ] --> e if2 --> e
Review resources
Section flowchart
Deitel Section 4.11
Activity 69 (Implementing flowcharts)
Sketch the implementation of the following flowcharts.
You can use a paper. If you prefer to use your editor, you can for example sketch an if-else like this:
int main() {
if (1)
;
else
;
}
And if you want to simulate different branches, you can use:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main() {
srand(time(0));
if (rand() % 2)
puts("if");
else
puts("else");
}
flowchart LR
s( ) --> if{ } --> if2{ } --> if2body[ ] --> e( )
if --> ifbody[ ] --> e
if2 --> else2body[ ] --> e
flowchart LR
s( ) --> init --> condition{ } --> body --> update
update --> condition -->e( )
flowchart LR
s( ) --> do[ ] --> while{ } --> e( )
while --> do
Activity 70 (Shuffling words)
You have an array of millions of random words. Create a program that selects n words from this array and returns an array representation of the selected words. Be as much memory-saving as possible.
Sketch your approach with a flowchart.
Implement it.
const char *WORDS[] = {"cat", "jumps", "red", "quickly", "moon"};
Activity 71 (Two dice simulation)
Write a program that simulates the distribution of the sum of two dice. Throw the dice 50000 times and store the occurrences of the possible sums (2-12) in an array.
The program must output the occurrences for every sum as follows:
2: 1389
3: 2778
..
12: 1389
Optional before simulation: Which sum do you think appears most often?
Activity 72
Exercise 11.12 implementation