Exam 3 CS 143

advertisement
Exam 3
CS 143
(60 points closed book)
Name________________________________
Section_____
Circle the correct answer for each of the following 5 multiple-choice questions. (5)
1.
Given the declarations:
enum Days {YESTERDAY, TODAY, TOMORROW};
Days day = YESTERDAY;
what is the value of the expression int(day)?
a. TODAY
b. 1
c. TOMORROW
d. 2
e. none of the above
2.
Which of the following statements about C++ arrays is true?
a. An array component can be treated the same as a simple variable of its component type.
b. Array components cannot be of floating point types.
c. The index type of an array can be any data type.
d. b and c above
e. a, b, and c above
3.
What
int
int
for
4.
Which of the following cannot be used to store the string "Mary" into nameStr?
a. char nameStr[5];
nameStr = "Mary";
b. char nameStr[5];
strcpy(nameStr, "Mary");
c. char nameStr[5] = "Mary";
d. char nameStr[] = "Mary";
5.
Given the declarations
is the output of the following program fragment?
alpha[5] = {100, 200, 300, 400, 500};
i;
// a. 400 300 200 100
(i = 4; i >= 0; i--)
// b. 500 400 300 200 100
cout << alpha[i] << ' ';
// c. 500 400 300 200
// d. 400 300 200
// e. None of the above
class RecType1
//
{private:
//
int
length;
//
float width;
//
void DoIt(RecType1 myRec,
//
RecType1 yourRec);
};
a.
b.
c.
d.
e.
myRec.length = yourRec.length;
myRec = yourRec;
myRec.length = yourRec;
a and b above
none of the above
which of the above assignment statements that would be used in method DoIt, is valid (assume
myRec and yourRec have values)?
6.
Given that the following numbers are in an array:
29
8
12
45
(5 points)
89
Show the appearance of the array after each pass of the selection
sort given in class. Number your 4 passes starting at 0.
SORT IN DESCENDING ORDER.
7. All of the following sections of this problem are meant to fit into ONE program. YOU do NOT
need to write the entire program, just the part that is being asked for. This problem deals
with classes that describe playing cards. The spots on a card that represent its numeric
value are called “pips”. A playing card such as the three of spades has a pip value, 3, and
a suit value of spades. Pip values range from 1 to 13, where 1 corresponds to an Ace, 2 to
2, 3 to 3, … 10 to 10, 11 to jack, 12 to queen, and 13 to king.
a.
Declare an enumerated type, named SuitType, with the constants SPADES, HEARTS, DIAMONDS, and
CLUBS. (2)
________________________________________________________________________________________
b.
Declare 2 classes, one named CardType is used to represent one card. The following
information should be included for CardType; in addition write a default constructor and
GetSuit and GetPips methods:
(4)
pips
(integer)
suit
(SuitType)
The other class, named HandType, represents what one person might hold in a hand in a game
of poker, euchre, or bridge. Write a default constructor and copy constructor.
cards
(an array of up to 13 cards, each of type: CardType)
cardcount (integer) -- represents # of cards in this hand, e.g. 5 for poker, 13 for
bridge
c.
Complete the following private method of class CardType to read in a word (e.g. spades,
hearts, diamonds, clubs) and return the appropriate SuitType. Assume cstring is included,
as well as assume that StringToUpper can also be used. CLUBS is returned as the default
value.
(6)
SuitType ReadSuit()
{
char SuitName[10];
cin >> SuitName;
StringToUpper(SuitName);
return CLUBS;
}
d.
Complete the following public method of class CardType to read in data for a variable of
type CardType. This function should have NO parameters. The suit and pips will each be on
same line. The input data for such a variable would appear as follows: spades 3
You MUST use the function FROM PART c to receive any credit. (6)
void readCard( )
{
______________________________________________________________;
______________________________________________________________;
}
e.
5
spades
clubs
hearts
hearts
clubs
Complete the following public method of class HandType to read in input data into a variable
of type HandType, where the number of cards will be the first item in the input data.
Sample data is given below. It is equivalent to the cards: Jack of spades (spades 11), Queen
of clubs (clubs 12), King of Hearts (hearts 13), Ace of hearts (hearts 1), and the ten of
clubs (clubs 10). This function also has NO parameters. (10)
11
12
13
1
10
YOU MUST USE THE FUNCTION FROM PART d to receive any credit.
void readHand( )
{
cin >> ____________________________________________;
for ( int i = 0;______________________________________; i++ )
______________________________________________________________;
}
7 f.
Write a complete public Selection Sort method of class HandType, named SelSort, (including
heading and body) that has NO parameters. This function MUST use the Selection Sort
techniques discussed in class (NOT BUBBLE sort or any other sort techniques). Sort in
ascending order based on the CardType field. Two values in the CardType field should be
compared first on suit, such that spades < hearts < diamonds < clubs. For any ties (that is
two cards with the same suit), use the respective pips value to break the tie. For example,
the King of spades is < the Jack of clubs because spades < clubs. However the Queen of
spades is < King of Spades because the pips value of queen, 12, is < pips value of king
which is 13. (9)
8.
Given the following code declarations:
const int MAX = 10;
int alpha[MAX][MAX];
and assuming that alpha has been completely initialized.
Write a value returning function, named ProductAllElments, that returns the product of ALL
the elements in alpha. Just pass alpha as the ONLY parameter and assume that it is
completely filled in with values. (6)
9.
Given the following code segment, draw the array and its content after the code is executed.
Indicate any undefined values with the letter U. (7)
int example[5][5];
int row, col;
for (col = 0; col < 4; col++)
for (row = 0; row < 4; row++)
example[row][col] = (row + 1) * (col - 2);
Download