CSC 215

Fall '98

Section 701

Sample final solutions

Listed below are the solutions to the questions I posted for the sample final questions.

  1. int equal(int a, int b, int c)
    {
      if (a == b || a == c || b == c) {
        return 1;
      }
      else {
        return 0;
      }
    }
    

  2. int yearInCentury(int year)
    {
      return ((year - 1) % 100) + 1;
    }
    

  3. int numberOfDigits(int n)
    {
      int digits = 0;
      while (n > 0) {
        n = n / 10;
        digits++;
      }
      return digits;
    }
    

  4. void getMonthYear(int& month, int& year)
    {
      do {
        cout << "Enter the month: ";
        cin >> month;
      } while (month < 1 || month > 12);
    
      do {
        cout << "Enter the year: ";
        cin >> year;
      } while (year < 1960 || year > 2000);
    }
    

  5. void exceeds(ifstream& inputfile, int limit, ofstream& outputfile)
    {
      int number;
      while (inputfile >> number) {
        if (number >= limit) {
          outputfile << number;
        }
      }
    }
    

  6. 1
    1
    2
    3
    5
    8