CS35: Quiz 3

CS35: Programming and Problem Solving
Ray Ontko
Department of Computer Science
Earlham College

This quiz is meant to give you an idea of how well you understand the material regarding arrays and strings. Your performance on this quiz does not affect your grade in the course; it is meant only to provide feedback to you about how you're doing.


    Use the following code segment to answer questions 1 through 4.
    #include <stdio.h>
    #include <ctype.h>
    #include <string.h>
    
    #define MAX 100
    
    int ispalindrome( char s[] ) ;
    
    int main()
    {
      int i ;
      int j ;
      char s[MAX] ;
      char t[MAX] ;
    
      while( fgets( s , MAX , stdin ) != NULL )
      {
        j = 0 ;
        for( i = 0 ; i < strlen(s) ; i ++ )
          if( isalpha( s[i] ) )
            {
            t[j] = tolower( s[i] ) ;
            j++ ;
            }
        t[j] = '\0' ;
        if( ispalindrome( t ) )
          fputs( s , stdout ) ;
      }
    }
    
    int ispalindrome( char s[] )
    {
      int i ;
      int len ;
    
      len = strlen(s) ;
      for( i = 0 ; i < ( len / 2 ) ; i++ )
        if( s[i] != s[len-i-1] )
          return 0 ;
    
      return 1;
    }
    

    When run, given the following input:

    madam
    eat oatmeal
    A man, a plan, a canal, Panama!
    love your mother
    Able was I ere I saw Elba.
    Happy Thanksgiving!
    

    produces the following output:

    madam
    A man, a plan, a canal, Panama!
    Able was I ere I saw Elba.
    

  1. What is the purpose of the for loop in function main()?
    1. To convert the input line to lower case.
    2. To remove non-alphabetic characters from the input line.
    3. To copy the relevant information from s to t.
    4. All of the above.
    5. None of the above.

  2. What is the significance of t[j] = '\0' ; in the program?
    1. It divides the array by 0 and converts it to a character.
    2. It concatenates a backslash and the zero character to the end of the string.
    3. It puts the number 0 in the jth element of the array t.
    4. It terminates the character string t.
    5. None of the above.

  3. The program will not behave as intended if:
    1. More than 100 characters appear on an input line.
    2. If digits are entered.
    3. If an empty (blank) line is entered.
    4. A palindrome containing an even number of letters is entered.
    5. All of the above.
    6. None of the above.

  4. Suppose the declaration and definition of ispalindrome() were changed from:
    int ispalindrome( char s[] ) 
    
    to:
    int ispalindrome( char *s )
    
    What would likely happen?
    1. Syntax error.
    2. Segmentation fault.
    3. Bus error.
    4. Unpredictable behavior.
    5. No change in behavior.


    Use the following code segment to answer questions 5 and 6.
    /* mult.c */
    
    #include <stdio.h>
    
    int main( int argc , char *argv[] )
    {
      int i , j , w , h ;
    
      if( argc != 3 )
      {
        printf( "usage: %s width height\n" , argv[0] ) ;
        return 1 ;
      }
    
      w = atoi( argv[1] ) ;
      h = atoi( argv[2] ) ;
    
      for( i = 1 ; i <= h ; i ++ )
      {
        for( j = 1 ; j <= w ; j ++ )
          printf( " %4d" , i * j ) ;
        putchar( '\n' ) ;
      }
    
      return 0 ;
    }
    
  5. Suppose this program were executed using the command:
    ./mul x y z
    
    Which of the following would be the output from the program?
    1. usage: ./mult 0 0
    2. usage: ./mult x y
    3. usage: ./mult x y z
    4. usage: ./mult width height
    5. None of the above.

  6. Suppose this program were executed using the command:
    ./mul 15w 10h
    
    Which of the following statements are true about the output of the program?
    1. There is no output.
    2. One hundred fifty numbers are printed.
    3. The number at the bottom right of the printout is 150.
    4. The thirtieth number printed is 30.


    Use the following code segment to answer questions 7 through 10.
    #include 
    #include 
    #include 
    
    void rand_matrix( int m[] , int r , int c , int mod ) ;
    void swap_matrix( int *m , int size ) ;
    void print_matrix( int m[10][10] ) ;
    
    int main()
    {
      int m[10][10] = { 0 } ;
    
      srand( time ( NULL ) ) ;
    
      rand_matrix( (int *)m , 10 , 10 , 100 ) ;
      print_matrix( m ) ;
      putchar( '\n' ) ;
      swap_matrix( (int *)m , 10 ) ;
      print_matrix( m ) ;
    
      return 0;
    }
    
    void rand_matrix( int m[] , int r , int c , int mod )
    {
      int i , j ;
    
      for( i = 0 ; i < r ; i ++ )
        for( j = 0 ; j < c ; j ++ )
          m[i*c+j] = rand() % mod ;
    }
    
    void swap_matrix( int *m , int size )
    {
      int i , j , temp ;
    
      for( i = 0 ; i < size ; i ++ ) 
        for( j = i + 1 ; j < size ; j ++ )
        {
          temp = m[i*size+j] ;
          m[i*size+j] = m[j*size+i] ;
          m[j*size+i] = temp ;
        }
    }
    
    void print_matrix( int m[10][10] )
    {
      int i , j ;
    
      for( i = 0 ; i < 10 ; i++ )
      {
        for( j = 0 ; j < 10 ; j ++ )
          printf( " %4d" , m[i][j] ) ;
        putchar( '\n' ) ;
      }
    }
    
    When run, the program produces output similar to the following:
       71   28   44   71   88    1   21   20    8   54
       70   37   15   85   64   13   24   37   54   30
       41   12   43   32   86   10   75   97   81   55
       21   52   35   65   24   23   66   97   43   26
       52   65   15   19   51   79   32   27   17   38
       57   58   50   52   90   89   63   17   86   44
       24   59   49   11   24   73   34   42   22   29
       68   74   47   84   94   98   15   78   77   84
       17   87   95   19   39   37    8   54    7   46
       51   31    5    0   43   29   25   29   23   47
    
       71   70   41   21   52   57   24   68   17   51
       28   37   12   52   65   58   59   74   87   31
       44   15   43   35   15   50   49   47   95    5
       71   85   32   65   19   52   11   84   19    0
       88   64   86   24   51   90   24   94   39   43
        1   13   10   23   79   89   73   98   37   29
       21   24   75   66   32   63   34   15    8   25
       20   37   97   97   27   17   42   78   54   29
        8   54   81   43   17   86   22   77    7   23
       54   30   55   26   38   44   29   84   46   47
    
  7. In the declaration of rand_matrix(), what is m?
    1. an int
    2. a pointer to an int
    3. an array of pointers to ints
    4. a pointer to an array of one or more ints
    5. a pointer to a 10 by 10 array of int

  8. In the declaration of swap_matrix(), what is m?
    1. an int
    2. a pointer to an int
    3. an array of pointers to ints
    4. a pointer to an array of one or more ints
    5. a pointer to a 10 by 10 array of int

  9. In the declaration of print_matrix(), what is m?
    1. an int
    2. a pointer to an int
    3. an array of pointers to ints
    4. a pointer to an array of one or more ints
    5. a pointer to a 10 by 10 array of int

  10. Why do we use (int *) as a prefix to m in the calls to rand_matrix() and swap_matrix()?
    1. to convert the elements of m into pointers to ints.
    2. to tell the compiler that we intend to pass m as a pointer to an int.
    3. to convert m into a one-dimensional matrix.
    4. to allow the functions to modify the elements of m.


Copyright © 1999, Ray Ontko (rayo@ontko.com).