Final: Math 120

11AM--1:30PM May 5, 1997.

  1. Write a C++ program which prints out the text Hello World!.

    Solution:

    #include<iostream.h>
    void main() { 
       cout << "Hello World!" ; 
    }
    
  2. Write a C++ program which inputs the temperature in Celsius and prints out the equivalent temperature in Fahrenheit. The conversion formula is:
        f = 32 + (9/5) c
    

    Solution:

    
    #include<iostream.h>
    void main() { 
       double c ;
       cin >> c ;
       cout << ( 32.0 + (9.0/5.0) * c ) << endl ;
    }
    
  3. Here is a C++ program which prints the letters a through z each on a single line:
       #include<iostream.h>
       void main() 
       {
          char c ;
          for (  c='a'; c<='z'; c++ )
          {
             cout << c << endl ;
          }
       }
    
    Write a C++ program which prints all the two letter combinations aa, ab, ac, etc., through zz each on a single line.

    Solution:

    #include<iostream.h>
    
    void main() {
    
       char c ;
       for (  c='a'; c<='z'; c++ ) {
    
          char d ;
          for (  d='a'; d<='z'; d++ )  {
             cout << c << d << endl ;
          }
       }
    }
    
  4. Write a C++ function called rvrStr which takes a string argument and reverses the order of the letters in the string. Example:
    #include<iostream.h>
    #include<strings.h>
    
    void rvrStr(char * s ) {
       // you write the code here
    }
    
    void main() {
       char * s =  "able was i" ;
       rvrStr(s) ;
       cout << s << endl ;
    }
    
    prints out i saw elba.

    Solution: This was presented in class as program SereverSenil.C and available with the usual class materials.

    Here is another possibility:

    void rvrStr(char * s ) {
       char * p ;
       p = s + strlen(s) - 1 ;
       while (p>s) {
          char t ;
          t = *s ;
          *s = *p ;
          *p = t ;
          p-- ;
          s++ ;
       }
    }
    
  5. Write a C++ function which takes three arguments: an integer value x, an integer array A[] and the array size n, and returns either:
    1. An integer i for which A[i]==x,
    2. or -1 in the case that the value x is not anywhere in the array A[].
    The function arguments and return type are:
       int findInt( int x, int A[], int n ) {
    
          // body of function, you do it.
    
       }
    

    Solution:

    int findInt( int x, int A[], int n )
    {
       int i ;
       for ( i=0; i<n; i++ ) {
          if ( x==A[i] ) return i ;
       }
       return -1 ;
    }
    
  6. Write a C++ function int sumLL(N * n) which takes a pointer to a linked list of integers and returns the sum of all the integers in the linked list. The linked list is defined using the structure:
       struct N {
          int theInt ;
          N * next ;
       }
    
    Example. if N * p points to the three element list containing integers 3, 4 and -1 it returns 6.

    Solution:

    int sumLL(N * n)
    {
       int sum = 0 ;
       while ( n!=NULL ) {
          sum += n->theInt ;
          n = n->next ;
       }
       return sum ;
    }
    
    
  7. Write a C++ function:
      N * bePos(N * l) {
      // your write the function
      }
    
    which takes a linked list of integers, using the definition of a linked list given in the previous problem, and returns the linked list remaining after removing all the nodes whose theInt values are negative.

    Solution:

    N * bePos(N * l) 
    {
       // assume dummy header 
       Node * p = l ;
       while ( p->next!=NULL ) {
          if ( p->next->theInt < 0 ) {
             // remove
             p->next = p->next->next ;
          }
          else {
             // keep
             p = p->next ;
          }
       return l ;
    }