Review problems

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?

  1. unsigned

  2. int

  3. double

  4. char[] (string)

  5. enum

Review resources

Activity 65 (Typical layout of a C program)

  1. Sketch the typical layout of a C program.

  2. What is definition and initialization of a variable?

  3. What does #include directive do?

  4. 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);
    }
    
  5. Do you have any improvement ideas on the code above?

  6. Draw a flowchart for the code above.

Review resources

Activity 66 (Flowchart to statement)

Which statement does the following flowchart correspond to?

        flowchart LR
s( ) --> if{ } --> e( )
if --> body[ ] --> e
    
  1. if

  2. while

  3. rand()

  4. if-else

  5. do-while

Review resources

Activity 67 (Flowchart to statement 2)

Which statement does the following flowchart correspond to?

        flowchart LR
s( ) --> while{ } --> body[ ] --> while
while --> e( )
    
  1. if

  2. while

  3. rand()

  4. if-else

  5. do-while

Activity 68 (Implementable flowcharts)

Which of the following flowcharts can be implemented in C?

You are not allowed to use:

  • goto

  • continue

  • break

  • multiprocessing

  1.         flowchart LR
    s( ) --> if{ } --> e( )
    if --> body[ ] --> p1[ ] --> e
    body[ ] --> p2[ ] --> e
        
  2.         flowchart LR
    s( ) --> if1{ } --> if2{ } --> body2[ ] --> e( )
    if1 --> e
    if2 --> if1
        
  3.         flowchart LR
    s( ) --> p1[ ] --> p2[ ] --> e( )
    p2 --> p1
        
  4.         flowchart LR
    s( ) --> if{ } --> elsebody[ ]--> e( )
    if --> ifbody[ ] --> e
        
  5.         flowchart 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
    
Review resources

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.

  1. Sketch your approach with a flowchart.

  2. 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