CPS 006, Spring 2000, Quiz Questions, March 20-March 24

Two or three of these questions will be used for an in-class quiz at the end of the week. It's possible that the questions may be modified slightly. These questions are good questions to study for the test as well.
  1. Write a function that reads words from a stream, and prints the words in the reverse order from how they were read. For example, if the stream is bound to a file:
    
      One fish two fish
      Red fish blue whale
    
    
    Then the function should print:
      whale
      blue
      fish
      red
      fish
      two
      fish
      One
    
    
    Here's the prototype for the function void PrintReverse(istream& input) // pre: input is open for reading // post: print all words in input but in reverse order

  2. Write a bool function InOrder that returns true if all the elements in the vector list are in order, and false otherwise. For example, true should be returned for (1, 5, 9, 20) and false should be returned for (1, 5, 9, 20, 7). The function should stop comparing elements as soon as possible. bool InOrder(const tvector<int>& list) // pre: list.size() is number of elements in list // post: returns true iff elements in list are in sorted order // i.e., list[0] <= list[1] <= ... <= list[list.size()-1]

  3. The sentence A man, a plan, a canal, Panama! is a palindrome, it reads the same forwards as it does backwards if only letters (no punctuation, no spaces) are considered.

    Write a function IsPalindrome that takes a string as a parameter and returns true if and only if the string is a palindrome when all the case (lower/upper) of letters is ignored, and only letters (no numbers, no spaces, no puncutation) are considered. The function should return true, for example, for the string

     A man, a plan, a cat, a ham, a yak, a yam, a hat, a canal...Panama!
    
    
    Hint: first create a string (or a vector) that consists only of the letters, all converted to lowercase. Then test this new string. You may want to use isalpha and tolower from the header file <cctype> -- see pages 401 and 402 in the book. bool IsPalindrome(const string& s) // post: returns true iff s is a palindrome where only letters of s // are considered and case (upper/lower) is ignored.

  4. You want to expand a vector by repeating each element a specific number of times. For example, the vector (2, 5, 8) expanded by three is (2,2,2,5,5,5,8,8,8): each element is replicated/repeated three times. Write the function Expand to do this expansion. void Expand(tvector<int> & list, int number) // pre: the number of integers in list is equal to list.size() // 1 < number // post: list contains "number" consecutive // copies of the original vector, list.size() is number // elements in list

  5. Assume that every player on a team has information stored in a struct Player declared in player.h. struct Player { string myLast; // last name string myFirst; // first name tvector<int> myMinutes; // minutes played in each game Player() { } Player(const string& last, const string& first) : myLast(last), myFirst(first) { } };

    For example, Fred Smith and Chris Jones might have information stored as shown below where Fred didn't play in the first two games and Chris played eight minutes in the first game and seven minutes in the last game.

     Smith                Jones
     Fred                 Chris
     (0,0,1,3,5,1)        (8,2,5,6,4,7)
    

    In this problem every player has the same number of entries in his/her myMinutes data field, though these entries can be zero.

    Write the function IronPlayer that prints the first and last name of the player with the most minutes of playing time on a team. Assume there will be no ties, there is always one iron player. (It may be helpful to write another function called by IronPlayer).

    void IronPlayer(const tvector<Player>& team) // pre: team contains team.size() elements each element // has the same number of entries in its myMinutes data field // post: print the first and last name of the player with the most // minutes

  6. Using the same struct Player, write the function ReadTeam below. Assume that the text file with team information being read is in the format below, the first line of the file is the number of games played. Then there are two lines for each player in the team, the first line contains first and last name and the second line contains minutes played in each game.
     6
     Fred Smith
     0 0 1 3 5 1
     Chris Jones
     8 2 5 6 4 7
    
    
    Here's the prototype: void ReadTeam(const string& filename, tvector<Player>& team) // pre: filename is the name of a file in the correct format // post: reads information and stores it in team, // team.size() is # of players in the file with filename