MIME-Version: 1.0 Content-Type: multipart/related; boundary="----=_NextPart_01C46400.067F4C40" 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.067F4C40 Content-Location: file:///C:/CF4C3372/Quiz2.htm Content-Transfer-Encoding: quoted-printable Content-Type: text/html; charset="us-ascii"
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];
______________________=
___________________
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. _____________________=
_____________________________
2.
a) Why is binary search better than linear search?
________________________________________________=
____________________________________
b) What has to be true of an array in order to use binary search?
________________________________________________=
____________________________________
= 3. Objects (5 points)
a)What is one difference between a struct and a class?
______________________=
______________________________________________________________
b) What is the purpose of the constructor?
______________________=
______________________________________________________________
c)Describe any problems with the following class definition and indicate how you would correct them.
class Rectangle{
public:
int height;
int width;
void Rectangle(h, w);
};
______________________=
___________________________________________________________________________=
___________________________________________________________________________=
______________________________________
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++;
}
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;
}
}
________________________________________________=
___________________________________________________________________________=
___________________________________________________________________________=
___________________________________________________________________________=
___________________________________________________________________________=
____
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
//
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;
const static int QUEEN;
const static int KING;
const static int ACE;
// Constants for suits
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 =
&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( =
=
)
{
}
//
Returns true if and only if the card is a red queen
_____________________isRedQueen()
{
}
//
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 =
=
span>// Necessary header fi=
le
int
main()
{
// iterate through the suits and the ranks
for ( =
&nb=
sp;
) =
for ( =
&nb=
sp;
) =
{
=
&nb=
sp; =
// create a card
// Tests if the card i=
s a
red queen
if (=
=
&nb=
sp; =
)
//
outputs the card in the format “rank of suit”
cout <<
}
return 0;
}
Spring 2004 = &nb= sp; = &nb= sp; Name: ___________________________
CS630: Quiz= #2