MIME-Version: 1.0 Content-Type: multipart/related; boundary="----=_NextPart_01C46400.4D06B7C0" This document is a Single File Web Page, also known as a Web Archive file. If you are seeing this message, your browser or editor doesn't support Web Archive files. Please download a browser that supports Web Archive, such as Microsoft Internet Explorer. ------=_NextPart_01C46400.4D06B7C0 Content-Location: file:///C:/19BA1233/Quiz2Ans.htm Content-Transfer-Encoding: quoted-printable Content-Type: text/html; charset="us-ascii" Problem 1 (10 points): Short Answer

Problem 1 (10 points): Short Answer

1.&n= bsp;     Arrays (3 points)

a)      What type of data does the following array hold?

= Weather march[31];

__Weather______= _________________________

 

b)      Create a table called e= xams to hold all the exam grades for a class.&n= bsp; There are three exams and 5 students for this class.  Each row of the table should corre= spond to a single student. __int exams[5][3]_______= __________

2.      Search (2 points)

a)      Why is binary search better than linear search?

__It is faster.___________________________

b)      What has to be true of an array in order to use binary search?

__The array must be sorted._______________

= 3.      Objects (5 points)

a)What is one difference between a struct and a class?

__By default in a s= truct, everything is public, whereas in a class everything is private by default._= ______________________

b)      What is the purpose of the constructor?

_Initialize the val= ues of the data fields.___________________________________

c)Describe any problems with the following class definition and indicate how you would correct them.

class Rectangle{        =     class Rectangle {

public:        =              private:

  int height;        =           int height;

  int width;        =            int width;

  void Rectangle(h, w);     public:

};        =             &nb= sp;        Rectangle(int, int);

        =             =         };

_The data fields sh= ould be private.  The constructor h= as no return type, and types are needed for the parameters of the constructors.__= ___________________________


Problem 2 (10 points): What is the output of the following program?

 

#include <iostream>

using std::cout;

using std::endl;

#include <string>

 

const int ASIZE =3D 5;

 

void mystery(string[], int);

 

int main() {

 

  string animals[ASIZE] =3D {"giraffe", "elephant", "dolphin",

        =             &nb= sp;      "walrus", "monkey"};

  mystery(animals, ASIZE);

 

  cout << endl;

  for (int i =3D 0; i < ASIZE; i+= +) {

    cout << animals[= i] << ", ";

  }

  cout << endl;

}

 

void mystery(string names[], int size) {

  for (int i =3D 0; i < size-1; i= ++) {

    int j =3D i;

    int k =3D i+1;

    while (k < size) {<= o:p>

      if (names[= j] < names[k])[1]   

        j =3D k;

      k++;<= /o:p>

    }

 

    cout << i <&l= t; " . " << names[i] << " and " << j << ". " << names[j]

         << endl;

    string str =3D names[i= ];

    names[i] =3D names[j];=

    names[j] =3D str;=

  }

}

0. giraffe and 3. walrus   &nbs= p;            &= nbsp;  

1. elephant and 4. monkey   &nb= sp;            =   

2. dolphin 3. giraffe    &= nbsp;           &nbs= p;     

3. dolphin 4.elephant    &= nbsp;           &nbs= p;     

____________________________________________<= /p>

walrus, monkey, giraffe elephant, dolphin,__


 

Problem 3 (10 points): For this problem, you are given the files Card.h, Card.cpp, and main.cpp.  Card.h is given bel= ow and declares the class Card.  Modi= fy the Card.cpp file on the next page to define the constructor and the method isRedQueen.  Assume that the methods getRankName and= getSuitName are= also defined in that file.  Then wr= ite a main on the last page that creates 52 cards and prints the name of the card= if it is a red queen.

 

// file: Card.h

 

#include <string>

 

// Creates cards and provides a few method for the cards

 

#ifndef CARD_H

#define CARD_H

 

class Card {

private:

  // Data Fields

  int rank;         // Numeric value of the card

  int suit;         // Suit of the card

 

public:

  // Constants for names of ranks - example use Card::JACK

  const static int JACK;<= /span>

  const static int QUEEN;=

  const static int KING;<= /span>

  const static int ACE;

 

  // Constants for suits<= /span>

  const static int CLUBS;=

  const static int DIAMONDS;

  const static int HEARTS;

  const static int SPADES;

 

  // Creates a card

  // If rank is invalid creates a ca= rd of rank 2

  // If suit is invalid, creates a c= ard with a suit of CLUBS

  // @param r - rank of the card

  // @param s - suit of the card

  Card(int r, int s);

 

  // Returns true if and only if the= card is a red queen

  bool isRedQueen();

 

  // Returns the name of the rank of= the card

  string getRankName();

 

  // Returns the name of the suit of= the card

  string getSuitName();

 

};


// file: Card.cpp

 

#include   &#= 8220;Card.h”        =             &nb= sp;          // Necessary file

 

// Constants for names of ranks - example use Card::JACK

const int Card::JACK =3D 11;

const int Card::QUEEN =3D 12;

const int Card::KING =3D 13;

const int Card::ACE =3D 14;

 

// Constants for suits

const int Card::CLUBS =3D 0;

const int Card::DIAMONDS =3D 1;

const int Card::HEARTS =3D 2;

const int Card::SPADES =3D 3;

 

// Creates a card

// If rank is invalid creates a card of rank 2

// If suit is invalid, creates a card with a suit of CLUBS

// @param r - rank of the card

// @param s - suit of the card

_______Card::Card(  int r, int s            =     )

{

    rank =3D r;

    suit =3D s;

 

 

 

 

}

 

// Returns true if and only if the card is a red queen

_bool_Card::_isRedQueen()

{

 

 &nb= sp;  if (suit =3D=3D Card::DIAMONDS ||

        suit =3D=3D Card::HEARTS)

        return true;

    else<= o:p>

        return false;

 

 

 

 

}


// file: main.cpp

 

// Creates all 52 cards and determines if the card is a red queen. 

// If the card is a red queen, it outputs the card in the following

// format: “rank of suit”, so the output of main should be

//     Queen of Diamond= s

//     Queen of Hearts<= o:p>

// This method should use constants where appropriate

 

 

#include <iostream>

using std::cout;

using std::endl;

#include “Card.h”        =       // Necessary header file

 

 

int main()

{

 

  // iterate through the suits and the ranks<= /o:p>

  for (int= s =3D Card::Clubs; s <=3D Card::Spades; s++)

    for (int r =3D 2; r <=3D Card::ACE; r++)

    {

 

        =   Card c =3D Card(r, s);        =          // create a card

 

       // Tests if the card i= s a red queen

       if (= c.isRedQueen()        =             &nb= sp; )

         // outputs the card in the format “rank of suit”=

 

         cout << c.getRankName() << = 220; of “

        =       << c.getSuitName() << endl;

 

 

     }

 

  return 0;

}



[1] Given string a and b, comparison a < b<= /i> is true if a comes before b alphabetically

------=_NextPart_01C46400.4D06B7C0 Content-Location: file:///C:/19BA1233/Quiz2Ans_files/header.htm Content-Transfer-Encoding: quoted-printable Content-Type: text/html; charset="us-ascii"





Spring 2004        =             &nb= sp;            =             &nb= sp;           Name: ___________________________

CS630: Quiz= #2

------=_NextPart_01C46400.4D06B7C0 Content-Location: file:///C:/19BA1233/Quiz2Ans_files/filelist.xml Content-Transfer-Encoding: quoted-printable Content-Type: text/xml; charset="utf-8" ------=_NextPart_01C46400.4D06B7C0--