Exam 1 Review Chapter 8

Lakukan tugas rumah & ujian kamu dengan baik sekarang menggunakan Quizwiz!

Write a statement that displays the contents of the last columns of the last row of the sales array defined in question 8.20

cout << sales[5][3];

Define a two-dimensional array of ints named grades. It should have 30 rows and 10 columns.

int grades[30][10];

Write a typedef statement that makes the name TenInts an alias for an array that holds 10 integers.

typedef int TenInts[10];

Define gators to be an empty vector of ints and snakes to be a 10-element vector of doubles. Then write a statement that stores the value 27 in gators and a statement that stores the value 12.897 in element 4 of snakes.

vector<int>gators; vector<doubles> snakes(10); gators.push_back(27); snakes[4] = 12.897;

Write a function called displayArray7. The function should accept a two-dimensional array as an argument and display its contents on the screen. The function should work with any of the following arrays: int hours[5][7]; int stamps[8][7]; int autos[12][7];

void displayArray7(int array[][7], int numRows) { for(int row = 0; row < numRows; row++) { for (int col = 0; col < 7; col++) { cout << array[row][col] << " "; } cout << endl; } }

Given the following array definition: int nums[5] = { 1, 2, 3}; What will the following statement display? cout << nums[3];

0

What would the valid subscript values be in a four-element array of doubles?

0 1 2 3

What is the output of the following code? int values[5], count; for (count = 0; count < 5; count++) values[count] = count +1; for (count = 0; count < 5; count++) cout << values[count] << endl;

1 2 3 4 5

What will the following program display on the screen? #include<iostream> using namespace std; class Tank { private: int gallons; public: Tank() { gallons = 50;} Tank(int gal) {gallons = gal;} int getGallons() {return gallons;} }; int main() { Tank storage[3] = { 10,20}; for(int index = 0; index<3; index++) cout<< storage[index].getGallons() << endl; return 0; }

10 20 50

What is the output of the following code? double balance[5] = {100.0, 250.0, 325.0, 500.0, 1100.0}; const double INT_RATE = 0.1; cout << fixed << showpoint << setprecision(2); for(int count = 0; count < 5; count++) { cout << (balance[count] * INT_RATE) << endl; }

10.00 25.00 32.50 50.00 110.00

Write a definition for an array named items that can hold 100 product structures.

Product items[100]

True or false: The default constructor is the only constructor that may be called for objects in an array of objects.

false

Write a statement that assigns the value 56893.12 to the first column of the first row of the sales array defined in question 8.20.

sales[0][0] = 56893.12;

Write a structure declaration called Destination, with the following members: city, a string object travelTime, a Measurement structure (declared in Checkpoint 8.39)

struct Destination { string city; Measurement travelTime; };

Write a structure declaration called Measurement that holds an int named miles and a double named hours.

struct Measurement { int miles; double hours; };

When an array name is passed to a function, what is actually being passed

the address of the array

Write definition statements for the following three vector objects: frogs (an empty vector of ints), lizards ( a vector of 20 doubles), and toads (a vector of 100 chars, with each element initialized to 'Z')

vector <int> frogs; vector <float> lizards(20); vector <char> toads(100, 'Z');

The following program segments, when completed, will ask the user to enter 10 integers, which are stored in an array. The function avgArray, which you must write, should calculate and return the average of the numbers entered. //Write the avgArray function prototype here. //It should have a const array parameter. int main() { const int SIZE = 10; int userNums[SIZE]; cout << "Enter 10 integers: "; for (int count = 0; count < SIZE; count++) { cout << "#" << (count +1) << " "; cin >> userNums[count]; } cout << "The average of those numbers is "; cout << avgArray(userNums, SIZE) << endl; return 0; } //Write the avgArray function here.

#include <iostream> using namespace std; //Function prototype double avgArray(const int[ ], int ); int main() { const int SIZE = 10; int userNums[SIZE]; cout << "Enter 10 numbers: "; for (int count = 0; count < SIZE; count++) { cout << "#" << (count + 1) << " "; cin >> avgArray(userNums, SIZE) << endl; return 0; } //Function avgArray double avgArray(const int array [ ], size) { double total = 0.0, average; for (int count = 0; count < size; count++) total+=array[count]; average = total/size; return average; }

Complete the following program so that it defines an array of 10 Yard objects. The program should use a loop to ask the user for the length and width of each yard. Then it should use a second loop to display the length and width of each yard. To do this, you will need to add two member functions to the Yard class. #include <iostream> using namespace std; class Yard { private: int length, width; public: Yard(){ length = 0; width = 0; } void setLength(int leng) { length = leng; } void setWidth(int wid) { width = wid; } }; int main() { // Finish the program };

#include <iostream> using namespace std; class Yard { private: int length, width; public: Yard(){ length = 0; width = 0; } void setLength(int leng){length = leng; } void setWidth(int wid) { width = wid;} int getLength(){return length; } int getWidth(){return width; } }; int main() { const int SIZE = 10; Yard lawns[SIZE]; cout << "Enter the length and width of " << "each yard. \n"; for(int count = 0; count < SIZE; count++) { int input; cout << "Yard " << (count + 1) << ":\n"; cout << "length: "; cin >> input; lawns[count].setWidth(input); cout << "width: "; cin >> input; lawns[count].setWidth(input); } cout << "\nHere are the yard dimensions.\n"; for(int yard =0; yard < SIZE; yard++) { cout << "Yard " << (yard +1) << " " << lawns[yard].getLength() << " X " << lawns[yard].getWidth() << endl; } return 0; }

What header file must you #include in order to define vector objects?

#include<vector>

What is the output of the following code? const int SIZE 5; int count; int time[SIZE] = {1, 2, 3, 4}; int speed[SIZE] = { 18, 4 , 27, 52, 100}; int dist[SIZE]; for(count = 0; count < SIZE; count++) { dist[count] = time[count] * speed[count]; } for(count = 0; count < SIZE; count++) { cout << time[count] << " "; cout << speed[count] << " "; cout << dist[count] << endl; }

1 18 18 // 1*18 = 18 2 4 8 3 27 81 4 52 208 5 100 500

How many elements are in the following array? double sales[6][4];

24 elements

Given the following array definition: int values[ ] = {2, 6, 10, 14}; What do each of the following display? A) cout << values[2]; B) cout << ++values[0]; C) cout << values[1]++; D) x = 2; cout << values[++x];

A) 10 B) 3 C) 6 D) 14

Assume a program includes the following two statements. They both use square brackets, but they mean different things. int score[15]; score[6]; A) Which number is the size declarator? B) Which number is a subscript?

A) The size declarator is 15 B) The subscript is 6 // Where in the array it is being pulled from(position array 6)

Indicate if each of the following array definitions is valid or invalid. (If a definition is invalid, explain why.) A) int numbers[10] = {0,0,1,0,0,1,0,0,1,1}; B) int matrix[5] = {1,2,3,4,5,6,7}; C) double radii[10] = {3.2,4.7}; D) int table[7] = {2, , ,27, , 45, 39}; E) char codes[ ] = {'A', 'X', '1', '2', 's'}; F) int blanks [ ]; G) string suit[4] = {"Clubs", "Diamonds", "Hearts", "Spades"};

A) Valid B) Invalid because there are too many values in the initialization list. C) Valid because elements 2 through 9 will be initialized to 0.0 D) Invalid because values can't be skipped in the initialization list E) Valid because the codes array will be allocated space for five characters. F) Invalid because an initialization list must be provided when an array is implicitly sized. G) Valid

Define the following arrays: A) ages, a 10- element array of ints initialized with the values 5, 7, 9, 14, 15, 17, 18, 19, 21, and 23 B) temps, a 7-element array of doubles initialized with the values 14.7, 16.3, 18.43, 21.09, 17.9, 18.76, and 26.7 C) alpha, an 8-element array of chars initialized with the values 'J', 'B', 'L', 'A', '*', '$', 'H', and 'M'.

A) int ages[10] = { {5}, {7}, {9}, {14}, {15}, {17}, {18}, {19}, {21}, {23} }; B) double temps[7] = { {14.7}, {16.3}, {18.43}, {21.09}, {17.9}, {18.76}, {26.7} }; C) char alpha[8] = {'J', 'B', 'L', 'A', '*', '$', 'H', 'M'};

Define the following arrays: A) empNum, a 100-element array of ints B) payRate, a 25-element array of doubles C) miles, a 14- element array of longs D) stateCapital, a 50-element array of string objects. E) lightYears, a 1,000- element array of doubles

A) int empNum[100]; B) double payRate[25]; C) long miles[14]; D) string stateCapital[50]; E) double lightYears[1000];

What is "array bounds checking"? Does C++ perform it?

Array bounds checking is a safeguard provided in some languages. It prevents a program from using a subscript that is beyond the boundaries of an array. C++ does not perform array bounds checking.

Define an array of 20 Destination structures. Write statements that store the following information in the fifth array element: City: Tupelo Miles: 375 Hours: 7.5

Destination places[20]; places[4].city="Tupelo"; places[4].travelTime.miles=375; places[4].travelTime.hours=7.5;

True or false: All elements in an array of objects must use the same constructor.

False, No need to use the same constructor for all the objects in the array of objects.

Write the definition for an array of five Product structures, initializing the first three elements with the following information: Description Part Number Cost Screwdriver 621 $1.72 Socket set 892 18.97 Claw Hammer 547 8.29

Product items[5] = { Product ("Screwdriver", 621, 1.72), Product ("Socket set", 892, 18.97), Product ("Claw Hammer", 547, 8.29)};

Add two constructors to the Product structure declaration. The first should be a default constructor that sets the description member to the null string and the partNum and cost members to zero. The second constructor should have three parameters: a string, an int, and a double. It should copy the values of the arguments into the description, partNum, and cost members.

Product( ) //Default constructor { description = " "; partNum = cost = 0; } Product(string d, int p, double c) //constructor { description = d; partNum = p; cost = c; }

Given the following array definitions: double double array1[4] = { 1.2, 3.2, 4.2, 5.2}; double array2[4]; will the following statement work? If not, why? array2 = array1;

The given array statement will not work because an entire array cannot be copied in a single statement with the = operator. The array can be copied element-by-element;

Write a loop that displays the contents of the entire items array you created in question 8.35

for (int i = 0; i < 100; i++) { cout << items[i].decription<<endl; cout << items[i].partNum<<endl; cout << items[i].cost<<endl;

Complete the following program skeleton so it will have a 10-element array of int values called fish. When completed, the program should ask how many fish were caught by fisherman 1 through 10, and store this information in the array. Then it should display the data. #include<iostream> using namespace std; int main() { const int NUM_MEN = 10; //Define an array named fish that can hold 10 int values //You must finish this program so it works as described above return 0;

int main() { const int NUM_MEN = 10; int fish[NUM_MEN]; for( int i = 0; i < NUM_MEN; i++) { cout << "Enter fish caught by fishermen" << i+1 << endl; cin >> fish[i]; } cout << "fish caught by each fisherman are" << endl; for (int i = 0; i < NUm_MEN; i++) { cout << "Fisher man" << i+1 << "=" << fish[i] << endl; return 0; }

What's wrong with the following array definitions? int readings[-1]; double measurements[4.5]; int size; string name[size];

int readings array cannot be negative. Measurements even though double array can't be a decimal. String name[size] does not work because int size is not set to an array value.

A DVD rental store keeps DVDs on 50 racks with 10 shelves each. Each shelf holds 25 DVDs. Define a 3D array to represent this storage system.

int vidNum[50][10][25];

Write statements that store the following information in the first element of the items array you defined in question 8.35 Description: Claw Hammer Part Number: 547 Part Cost: $8.29

items[0].description = "Claw Hammer"; items[0].partNum = 547; items[0].partCost = 8.29;


Set pelajaran terkait

Functional Anatomy Exam 1 Questions

View Set

ECON chapter 3 guided reading (lesson 1)

View Set

Data and Computer Communications - Final

View Set

Science Test (Last one for the year!!!) (the defs. are from the book)

View Set

Intro to Sociology: Ch.13-16(Exam 3)

View Set

Envir. Sci. Ch 8 Understanding Populations Test (with potential answers) Tolbert

View Set

Cosmetology pivot point chapter 14

View Set

Weather Maps and Cloud Formation

View Set