CSE142                                                        
29. Mar. 2001

Quiz #1(Version 1)

March 29, 2001

Sample Solution


QOTD 2.

Syntax Error and a Semantic Error Around You

Imagine a game or a sport that you like to play. Or, if you're a musician, use musical scores. Now, in that domain (game, sport, or music), describe in words what the difference between a syntax error and a semantic error would be. Give a concrete example of a syntax error.

Solution:

Possible Examples:

Suppose you are a composer, and you are invited to compose a song for a new couple's wedding, if the notes are not correctly written down, say, the wrong format, no musician could play your song successfully. This is a syntax error, just like a compiler could not compile a program, containing a syntax error.

Let's say your song's note format is correct, and the musician could successfully understand and play your song. If you just embody despair in your song by mistake, against the original intent of happiness, you have made a semantic error. Just like a compiler could successfully compile your program if it has no syntax error, but rather a semantic error, but you may nonetheless get the wrong result.  

In sports, the players can make technical offenses, which are syntax errors and are not allowed by the referee. But even if you make no technical offenses, you may still make strategic mistakes, which are semantic errors. Of course, the referee allows this unless you make a syntax error at the same time.


Question: Adapted from the materials covered in class.

Consider the following program:

1:         int main(void)
2:         {
3:                     int i, j;    /* input values typed by the user */
4:                     char ch;                        /* extra input character */
5:                    
6:                     /* Prompt for first integer and get it from the keyboard. */
7:                     printf("Please enter an integer: ");
8:                     scanf ("%d", &i);
9:        
10:                   /* Prompt for second integer and get it. */
11:                   printf("Please enter another integer: ");
12:                   scanf ("%d", &j);
13:      
14:                   /* Print both integers and their product */
15:                   printf("The value of the product %d * %d is %d.\n",  1, i, j, i*j);
16:                  
17:                   /* Wait until the user is ready to end the program */ 
18:                   printf ("Type another character and enter to end\n");           
19:                   scanf (" %c", &ch);
20:                   return 0;
21:       }

a. What line(s) contain statements?

    Solution: 7, 8, 11, 12, 15, 18, 19, 20

Note:

  1. { or } is not a statement in the strict sense, but if you include them here, we will not take off credit.
  2. Comments are not statements either.
  3. Variable declarations are not statements.
  4. Line 1 is not a statement. It is the function head (function declaration).

Hint: A simple way to distinguish the statements. Statements are those lines which could be executed at runtime. It is apparent that we could not execute the comments, {, }, variable declarations, function head, etc.

b. What line(s) contain variable declarations?

Solution: 3, 4

c. What line(s) contain functions?

            Solution: 7, 8, 11, 12, 15, 18, 19, 20

Note: If you specify line 1 is a function, we will not take credit off, since line 1 is the function head which declares a function. And the lines listed in solution contain the function "call sites".

d. What line(s) contain comments?

Solution: 3, 4, 6, 10, 14, 17