C++ Final Chapters: (7,8,9,10,13)

¡Supera tus tareas y exámenes ahora con Quizwiz!

7.2 How would you pronounce this? hours[0] = 20;

"hours sub zero is assigned twenty" or "hours sub zero gets twenty" p. 380

Include Guard

(#ifndef) Prevents the header file from accidentally being included more than once. (Helps to prevent multiple definitions of an entity by selectively enabling/disabling the code they surround)

array[index] is equivalent to

*( array + index )

If you add a value to a vector that is already full, the vector will automatically increase its size to accommodate the new value.

...

Its best to think of two-dimensional arrays as having rows and columns.

...

The values in an initialization list are stored in the array in the order they appear in the list.

...

To calculate the amount of memory used by an array, multiply the number of elements by the number of bytes each element uses.

...

To pass an array to a function, pass the name of the array

...

When an array is passed to a function, the function has access to the original array.

...

You can write programs that use invalid subscripts for an array.

...

7.5 When using the increment and decrement operators, be careful not to...

... confuse the subscript with the array element. amount[count --]; // decrements the variable count but does nothing to the value in amount. p. 394

7.5 Any time the name of an array is used without brackets and a subscript...

... it is seen as the array's beginning memory address. p. 396

7.5 Individual array elements are processed...

... like any other variable. p. 394

7.2 Inputting data into an array must normally be done ...

... one element at a time. cin >> hours; // will not input data to the hours array You must use multiple cin statements to read data into each array element or use a loop to step through the array, reading data into its elements. p. 384

7.2 Outputting an arrays elements must be done...

... one element at a time. cout << hours; // will not work p. 384

7.3 C++ does not prevent you from...

... overwriting an arrays bounds. C++ does not provide many of the common safeguards to prevent unsafe memory access found in other languages. p. 386

7.4 If an array is partially initialized, the uninitialized elements will be...

... set to zero. The uninitialized elements of a string array will contain an empty string. This is true even if the string is defined locally. p. 393

7.5 The only way to assign one array to another is...

... to assign the individual elements in the arrays. Usually, this is done best with a loop for(int count = 0; count < SIZE; count++0 newValues[count] = oldValues[count]; p. 396

7.4 If you leave an element uninitialized...

... you must leave all the elements that follow it uninitialized as well. C++ does not provide a way to skip elements in the initialization list. int array[6] = {2, 4, 8, , 12]; // Not legal! p.393

What ending should be added to header files?

.h

To determine the number of elements in a vector, use the ___ member function.

.size()

If an array is partially initialized, the uninitialized elements will be set to___.

0

Subscript numbering in C++ always starts at___.

0

A binary search function is searching for a value that is stored in the middle elements of an array. How many times will the function read an element in the array before finding the value?

1 time

Rules for function/class interaction

1) Class objects can be passed as parameters and returned as function values 2) Class objects can be passed by reference or by value 3) If passed by value, contents of member variables are copied into corresponding member variables of formal parameter.

3 things associated with ADT's

1) the name 2) domain of the ADT 3) operations that can be performed on the ADT

What advantages does a vector offer over an array?

1. You do not have to declare the number of elements that the vector will have. 2. If you had value to a full vector, it will automatically increase it size, rather than go over like an array. 3. A vector can report the number of elements it contains

What is the maximum number of comparisons that a binary search function will make when searching for a value in a 1,000 elements array?

10 comparisons

Look at the following array definition. int values[10]; How many elements does the array have? What is the subscript of the 1st element in the array? What is the subscript of the last element in the array? Assuming that an int uses four bytes of memory, how much memory does the array use?

10 elements Subscript of the 1st element is zero Subscript of the last element is 9 The array used 40 bytes of memory

If a linear search function is searching for a value that is stored in the last element of a 10,000- element array, how many elements will the search code have to read to locate the value?

10,000

how much memory storage does the above pointer take?

4 bytes

Look at the following code. What value will be stored in s after the code executes? char name[10]; int s; strcpy(name, "Jimmy"); s = strlen(name);

5

To define a vector in your program, you must #include the ____________ header file.

<Vector>

To define a vector in you program, you must #include the ____ header file.

<vector>

You cannot use the _________ operator to copy data from one array to another in a single statement.

=

Class Implementation File

A .cpp file with member function definitions for a class.

Default Constructor

A constructor that takes no arguments

Abstraction Data Type (ADT)

A data type that separates the logical properties from the implementation details

Class Specification File

A header file that contains a class declaration.

How does a member function interact with other members of the class?

A member function can access all member variables and functions without having to pass them as parameters. The only condition is that an identifier must be declared before it is used.

Accessor (Getter Function)

A member function that gets a value from a class's member variable but does not change it.

Constructor

A member function that is automatically called when a class object is created.

Destructor

A member function that is automatically called when a class object is destroyed.

Mutator (Setter Function)

A member function that stores a value in a member variable or changes the value of a member variable in some other way.

Difference between "public" and "private" members of a class

A private member is accessible only within the class, while a public member is available outside the class.

client

A program or software that uses and manipulates the objects of a class

Problem Domain

A set of real-world objects, parties and major events related to the problem.

Object

A software entity that contains both data and procedures.

Precondition

A statement specifying the conditions that must be true before the function is called.

Postcondition

A statement specifying what is true after the function call is completed.

7.2 What is a subscript?

A subscript is used as an index to pinpoint a specific element within an array. The first element is assigned subscript 0, the second element is assigned 1 and so on... short hours[6]; // Has subscripts 0-5 p. 379

How to declare members of a class as static members

Add the word "static" in front of the member name

arr2 = new int[20]; for (int i = 0; i < 10 ; i++) { *(arr2 + i) = *(arr1 + i); } for (int j = 10; j < 20; j++) { *(arr2 + j) = 0; }

The variables arr1 and arr2 have been declared as pointers to integers. An array of 10 elements has been allocated, its pointer assigned to arr1, and the elements initialized to some values. Allocate an array of 20 elements, assign its pointer to arr2, copy the 10 elements from arr1 to the first 10 elements of arr2, and initialize the remainder of the elements of arr2 to 0.

int *temp; temp = xp; xp = yp; yp = temp;

The variables xp and yp have both been declared as pointers to integers, and have been assigned values (i.e., they are each pointing to an integer value). Write the code to exchange the values of these two variables (so that after the swap xp points to what yp originally pointed to and vice-versa-- in other words, in this exercise, you are swapping the pointers). Declare any necessary variables.

int x = 0; int *temp = &x; *temp = *xp; *xp = *yp; *yp = *temp;

The variables xp and yp have both been declared as pointers to integers, and have been assigned values. Write the code to exchange the two integers (so that after the swap xp still points at the same location, but it now contains the integer value originally contained in the location pointed to by y; and vice versa-- in other words, in this exercise you are swapping the integers, not the pointers). Declare any necessary variables.

Meaning of +, -, and # in UML Class Diagram

These symbols are placed in front of member variables and functions: + means public - means private # means protected

In which order must private/public members be declared?

They can be declared in any order. However, it is customary to declare all public members first, then private members.

void minMax(int x, int y, int z, int * big, int * small);

Write a statement that declares a prototype for a function minMax , which has five parameters. The first three parameters are integers. The last two are parameters that are set by the function to the largest and smallest of the values of the first three parameters. The function does not return a value.

void tripleIt (int *x);

Write a statement that declares a prototype for a function tripleIt , which can be used as follows: int x=5; tripleIt(&x); /* x is now equal to 15 */

void zeroIt (int *x);

Write a statement that declares a prototype for a function zeroIt , which can be used as follows: int x=5; zeroIt(&x); /* x is now equal to 0 */

void divide (int x, int y, int* q, int* r) { *q = x / y; *r = x % y; }

Write the definition of a function divide that takes four arguments and returns no value. The first two arguments are of type int . The last two arguments arguments are pointers to int and are set by the function to the quotient and remainder of dividing the first argument by the second argument. The function does not return a value. The function can be used as follows:

void doubleIt (int * x) { *x = *x * 2; }

Write the definition of a function doubleIt , which doubles the value of its argument but returns nothing so that it can be used as follows: int x=5; doubleIt(&x); /* x is now equal to 10 */

void minMax(int x, int y, int z, int* big, int* small) { int min; int max; min = x; if (min > y) min = y; if (min > z) min = z;

Write the definition of a function minMax that has five parameters. The first three parameters are integers. The last two are set by the function to the largest and smallest of the values of the first three parameters. The function does not return a value. The function can be used as follows: int a=31, b=5, c=19 big, small; minMax(a,b,c,&big,&small); /* big is now 31 */ /* small is now 5 */

void tripleIt (int * x) { *x = *x * 3; }

Write the definition of a function tripleIt , which triples its argument but returns nothing so that it can be used as follows: int x=5; tripleIt(&x); /* x is now equal to 15 */

void zeroIt (int * x) { *x = 0; }

Write the definition of a function zeroIt , which is used to zero out a variable. The function is used as follows: int x=5; zeroIt(&x); /* x is now equal to 0 */

The __ file must be included in a program that uses character testing functions

cctype

What header file must you include in a program using the character conversion functions toupper and tolower

cctype

7.1 Typical array sizes

char - 1 byte short - 2 bytes int - 4 bytes float - 4 bytes double 8 bytes p. 379

Syntax for defining a class

class classIdentifier { classMembersList };

syntax for accessing class members

classObjectName.memberName

To completely clear the contents of a vector, use the ___ member function.

clear

objects

components that combine data and their operations into a single unit; also refers to instances of classes that have been created in a program

To __ two strings means to append one to the other

concatenate

In a program you need to store the identification numbers of 10 employees (as ints) and their weekly gross pay (as doubles). Display their weekly pay.

const int SIZE = 10; int id[SIZE]; // To hold ID numbers double weeklyPay[SIZE]; // To hold weekly pay for (int i = 0; i < SIZE; i++) { cout << "The pay for employee " << id[i] << " is $" << fixed << showpoint << setprecision(2) << weeklyPay[i] << endl; }

The arrays numberArray1 and numberArray2 have 100 elements. Write code that copies the values in numberArray1 to numberArray2.

const int size = 100; for(int i = 0; i < size; i++) numberArray1[i] = numberArray2[i];

names is an integer array with 20 elements. Write a for loop that prints each element of the array.

const int size = 20; for(int i = 0; i < size; i++) cout << names[i] << endl;

implementation file

contains the implementation details of a class; contained in a separate file from the header file for information hiding

What header file must you include in a program using character testing functions such as isalpha and isdigit?

cstring

Allocate memory to the pointer and assign 1.123 to it.

dblPtr = New double; *dblPtr=1.123;

show 2 ways to make the pointer point to nothing.

dblPtr=0; dblPtr=Null;

Allocate 500 elements to your pointer.

dblPtr=New double[500];

class object

declared variable of a given class type

de-allocate the memory space.

delete dblPtr;...

Now deallocate the memory of the above pointer ptrs.

delete[ ] dblPtr; delete Ptrarry;

constructor

device used in C++ to initialize members of a class

divide(x, y, &q, &r);

divide is a function that takes four arguments and returns no value. The first two arguments are of type int . The last two arguments arguments are pointers to int that are set by the function to the quotient and remainder of dividing the first argument by the second argument. The function does not return a value. x and y are two int variables that have been declared and initialized. q and r are two int variables that have been declared. Write a statement that sets the value of q to the quotient of x divided by y , and sets the value of r to the remainder of x divided by y , by calling divide .

When working with classes, most of C++'s built-in operations...

do not apply. You cannot use arithmetic or relational operators with classes.

Create an array of pointers and place the address in it.

double *Ptrarry[500]; for(int i = 0; i < 500; i++) Ptrarry[i] = &dblPtr[i];

create another pointer and make a copy of the above pointer's contents.

double *dblPtr2; dblPtr=New double [500] for(int i = 0; i < 500; i++) dblPtr2[i] = dblPtr[i];

declare a pointer of type double.

double *dblPtr;

Look at the following array definition. double sales[8][10]; How many rows does the array have? How many columns does the array have? How many elements does the array have? Write a statement that stores a number in the last column of the last row in the array

double sales[8][10]; How many rows does the array have? 8 Rows How many columns does the array have? 10 columns How many elements does the array have? 80 elements Sales[7][9] = 53.1; stores double in the last column of the last row in the array

doubleIt(&savings);

doubleIt is a function that takes one argument and returns no value. The argument is a pointer to int . The function doubles the value that the argument points to and stores it back. savings is an int variable that has been declared and initialized. Write a statement that doubles the value stored in savings by invoking the function doubleIt . For example, if the value in savings was 7 before your statement, after your statement its value would be 14.

7.1 Valid array definitions

float temperatures[100); // An array of 100 floats string names[10]; // Array of 10 string objects long units[50]; // Array of 50 long integers double sizes[1200]; // Array of 1200 doubles p. 378

Initialize all elements to 1 using pointer notation.

for (int i = 0; i <500; i++) *(dblPtr + i ) = 1;

destructor

function without a type that executes automatically when a program exits a constructor scope

How to invoke a particular constructor

include a list of appropriate parameters after the class object name. Example: clockType myClock(5, 12, 40); or clockType myClock;

Starting values for an array may be specified with a(n) _________ list.

initialization

If the size declarator of an array definition is omitted, C++ counts the number of items in the _________ to determine how large the array should be.

initialization list

If the size declaratory of an array definition is omitted, C++ counts the number of items in the ____ to determine how large the array should be.

initialization list

7.4 You must provide an _______ if you leave out an array's size declarator.

initialization list. p. 394

Simplest way of declaring a constructor

initialize all the formal parameters. If no parameters are given in the class declaration, then the initialized values are the default values.

In C++11, what is an alternative to writing a default constructor?

inline initialization of the member variables. If no arguments are given, then the initialized values are used.

Define two arrays that may be used in parallel to store the 10 ID numbers and gross pay amounts.

int ID[10]; double Emp[10];

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

int grades[30][10];

int numberArray[9][11]; a statement that assigns 145 to the first column of the first row of this array. a statement that assigns 18 to the last column of the last row of this array

int numberArray[0][0] = 145; int numberArray[8][10] = 18;

Look at the following array definition. int numbers[5] = { 1, 2, 3 }; What value is stored in numbers[2]? What value is stored in numbers[4]?

int numbers[5] = { 1, 2, 3 }; What value is stored in numbers[2]? 3 What value is stored in numbers[4]? 0, its value is not initialized

Write code that sums all the elements in the array and stores the sum in the variable total

int values[10][20]; int row, col; // loop counter float total = 0.0 // Accumlator for(row = 0, col = 0; row < 10, col < 20; row++, col++) total += values[row][col]

Consider the following array definition: int values[5] = { 4, 7, 6, 8, 2 }; What does each of the following statements display? cout << values[4] << endl; __________ cout << (values[2] + values[3]) << endl; __________ cout << ++values[1] << endl; __________

int values[5] = { 4, 7, 6, 8, 2 }; cout << values[4] << endl; 2 cout << (values[2] + values[3]) << endl; 14 cout << ++values[1] << endl; 8

The __ function returns true if the character argument is a letter of the alphabet

isalpha

The __ function returns true if the character argument is a digit

isdigit

The __ function returns true if the character argument is a whitespace character

isspace

The __ function returns true if the character argument is uppercase

isupper

7.4 If a local array is completely uninitialized, its elements will contain "garbage", like all other local variables. p. 393

left blank intentionally

scope of class members

local to the class.

constant function

member function with the reserved word "const" at the end of the heading. Such functions cannot modify the member variables of a class.

Acessor functions

member functions of a class that uses the member variables of the class but does not change them.

Mutator functions

member functions that access and change the value of member variables in a class

minMax (x, y, z, &big, &small);

minMax is a function that takes five arguments and returns no value. The first three arguments are of type int . The last two arguments are pointers to int that are set by the function to the largest and smallest of the values of the first three parameters, respectively. x , y and z are three int variables that have been declared and initialized. big and small are two int variables that have been declared. Write a statement that sets the value of big to the largest of x , y , z and sets the value of small to the smallest of x , y , z by calling minMax .

instance variables

non-static data members of a class

Use the ________________ member function to remove the last element from a vector.

pop_back

use the ____ member functino to remove the last element from a vector.

pop_back

By default, all members of a class are...

private.

abstraction

process of separating the design details from the use.

client

program or software that uses and manipulates the objects of a class

member access specifiers

public, private, and protected.

Syntax for making a public member

public: identifier1 identifier2

To store a value in a vector that does not have a starting size, or that is already full, use the ________________ member function.

push_back

Is an array passed to a function by value or by reference?

reference

Its best to think of a two-dimensional array as having _________ and _________.

rows and columns

scope of a class object

same as any other variable. Can be automatic/static. Can declare a class array

The vector data type is a(n) ______________ container

sequence

To determine the number of elements in a vector, use the _____________ member function.

size

The _________ indicates the number of elements, or values, an array can hold.

size declarator

The number inside the brackets of an array definition is the _________, but the number inside an array s brackets in an assignment statement, or any other statement that works with the contents of the array, is the _________.

size declarator, subscript

What header file must you include in a program using string/numeric conversion functions such as atoi and atof?

stdlib

The __ function concatenates two strings

strcat

The __ function compares two strings

strcmp

The __ function copies one string to another

strcpy

What header file must you include in a program using string class objects?

string

What header file must you include in a program using string functions such as strlen and strcpy?

string

The __ function returns the length of a string

strlen

The __ function copies, at most, n number of characters from one string to another

strncpy

The __ function searches for a string inside of another one

strstr

Each element of an array is accessed and indexed by a number known as a(n)_________.

subscript

members

the components of a class

member access operator

the dot (.) Used with a class object to access class members

member access operator

the dot (period); used to access a member variable or function

writing "const" at the end of a member function indicates...

the function cannot modify the class's member variables

instance variables

the member variables of a class

type name

the name of an ADT

type name

the name of the ADT

information hiding

the process of hiding the details of the operations on the data

abstraction

the process of separating the design details from the implementation

domain

the set of values that belong to the ADT

The __ function converts an integer to a string

to_string

The __ function returns the lowercase equivalent of its character argument

tolower

The __ function returns the uppercase equivalent of its character argument

toupper

tripleIt(&penalty);

tripleIt is a function that takes one argument and returns no value. The argument is a pointer to int . The function triples the value that the argument points to and stores it back. penalty is an int variable that has been declared and initialized. Write a statement that triples the value stored in penalty by invoking the function tripleIt . For example, if the value in penalty was 7 before your statement, after your statement its value would be 21.

A___ ____ array is like several arrays of the same type put together.

two- dimensional

A constructor is a function that has no...

type

7.5 How do you decrements a value stored in an array?

use the following statement. amount[count]--; p. 394

scope resolution operator (::)

used to reference identifiers in a class

Write a definition statement for a vector named frogs . frogs should be an empty vector of int s.

vector <int> frogs;

gators is an empty vector of int s. Statement that stores the value 27 in gators .

vector <int> gators; gators.push_back(27);

Write a prototype to show how to pass a pointer to an array and an array to Ptrs: intPtr,intArryPtr.

void myFunct(int*,int*[])

build, rebuild, or make

when applied to a project, commands that tell the system to automatically compile and link all files required to create the executable code

In C++, when you add a value to a pointer,

you are actually adding that value times the size of the data type being referenced by the pointer

zeroIt(&x);

zeroIt is a function that takes one argument and returns no value. The argument is a pointer to int . The function stores the value 0 back into the variable pointed to by the argument. x is an int variable that has been declared. Write a statement that sets the value stored in x to zero by invoking the function zeroIt .

Not all arithmetic operations may be performed on pointers. For example, you cannot multiply or divide a pointer. The following operations are allowable:

• The ++ and −− operators may be used to increment or decrement a pointer variable. • An integer may be added to or subtracted from a pointer variable. This may be performed with the + and − operators, or the += and −= operators. • A pointer may be subtracted from another pointer.

Any time the name of an array is used without brackets and a subscript, it is seen as _________.

Array's beginning memory address

Any time the name of an array is used without brackets and a subscript, it is seen as the____

Array's beginning memory address

7.4 Talk about array definitions.

Arrays may be initialized when they are defined. p. 389

If an array is sorted in__order, the values are stored from lowest to highest.

Ascending

class assignment definition

Assignment statements perform memberwise copy. Each variable is assigned to corresponding member variable

You cannot use the ___ operator to copy data from one array to another in a single statement.

Assignment(=)

counterPointer = &counter;

Assume that an int variable counter has already been declared. Assume further a variable counterPointer of type "pointer to int " has also already been declared. Write a statement that makes counterPointer "point" to counter .

diffPointer = &diff;

Assume that an int variable diff has already been declared. Assume further a variable diffPointer of type "pointer to int " has also already been declared. Write a statement that assigns the address of diff to diffPointer .

tp = jp; jp = ip; ip = tp;

Assume that ip , jp , and tp have all been declared to be pointers to int and that result has been declared to be an array of 100 elements. Assume further that ip has been initialized to point to an element in the first half of the array and that jp has been initialized to point to an element in the second half of the array.

jp = ip + 5;

Assume that ip and jp have both been declared to be pointers to int and that result has been declared to be an array of 100 elements. Assume further that ip has been initialized to point to an element in the first half of the array. Write a statement that makes jp point to the element in the array 5 positions after the one that ip points to.

jp = ip + 1;

Assume that ip and jp have both been declared to be pointers to int and that result has been declared to be an array of 100 elements. Assume further that ip has been initialized to point to an element in the first half of the array. Write a statement that makes jp point to the element in the array just after the one that ip points to.

ip = enrollment;

Assume that ip has been declared to be a pointer to int and that enrollment has been declared to be an array of 20 elements. Write a statement that makes ip point to the first element in the array.

ip = &enrollment[19];

Assume that ip has been declared to be a pointer to int and that enrollment has been declared to be an array of 20 elements. Write a statement that makes ip point to the last element in the array.

ip = &enrollment[section];

Assume that ip has been declared to be a pointer to int and that enrollment has been declared to be an array of 20 elements. Assume also that section has been declared to be an int and has been initialized to some value between 5 and 10. Write a statement that makes ip point to the element in the array indexed by section .

ip ++;

Assume that ip has been declared to be a pointer to int and that result has been declared to be an array of 100 elements. Assume further that ip has been initialized to point to an element in the first half of the array. Write a statement that makes ip point to the next element in the array.

*(ip + 1)

Assume that ip has been declared to be a pointer to int and that result has been declared to be an array of 100 elements. Assume further that ip has been initialized to point to an element in the first half of the array. Write an expression whose value is the element in the array after the element that ip points to.

*ip + (*ip + 1) + (*ip + 2)

Assume that ip has been declared to be a pointer to int and that result has been declared to be an array of 100 elements. Assume further that ip has been initialized to point to an element in the first half of the array. Write an expression whose value is the sum of the element that ip points to plus the next two elements.

*ip3 = *ip1 + *ip2;

Assume that ip1 , ip2 , and ip3 have already been declared to be of type "pointer to int ". Assume further that each of these pointer variables have been initialized -- each points to some int variable. Write a statement that computes the sum of the variables that ip1 and ip2 point to, and assigns that value (the sum) to the variable that ip3 points to.

*strikeCounter += 22;

Assume that strikeCounter has already been declared to be a "pointer to int ". Assume further that strikeCounter has been initialized -- its value is the address of some int variable. Write a statement that adds 22 to the value of the variable that strikeCounter is pointing to.

*strikeCounter = 27;

Assume that strikeCounter has already been declared to be a "pointer to int ". Assume further that strikeCounter has been initialized -- its value is the address of some int variable. Write a statement that assigns the value 27 to the variable that strikeCounter is pointing to.

*strikeCounter * 4

Assume that strikeCounter has already been declared to be a "pointer to int ". Assume further that strikeCounter has been initialized -- its value is the address of some int variable. Write an expression whose value is four (4) times the value of the variable that strikeCounter is pointing to.

&diff

Assume the variable diff has already been declared. Write an expression whose value is the address of diff .

If you include constructors with parameters, why is it a good idea to include a default constructor?

Because C++ does not automatically provide a default. If the class object is declared without parameters, an error will occur.

Assuming that array1 and array2 are both arrays, why is it not possible to assign the contents of array2 to array1 with the following statement? array1 = array2;

Because an array name without brackets and a subscript represents the array's beginning memory address. The statement shown attempts to assign the address of array2 to array1, which is not permitted.

True

T/F The address operator is not needed to assign an address to a pointer.

False

T/F The indirection * is used to get the address of a variable.

True

T/F The new operator dynamically allocates the memory.

True

T/F The pointer variables are designed to hold addresses.

True

T/F When the indirection operator is used with a pointer variable, you are actually working with the value the pointer is pointing to.

True

T/F When used as function parameters, reference variables are much easier to work with that pointer.s

True

T/F When you add a value to a pointer, you are actually adding that number times the size of the data type referenced by the pointer.

False

T/F You cannot change the address that an array name points to.

You can write programs that use invalid subscripts for an array.

T; C++ has no bound checks, can accidentally cause the program to write pas the end of an array.

The contents of an array element cannot be displayed with cout.

T; must use a loop

The individual elements of an array are accessed and indexed by unique numbers.

T; subscripts

To define a two-dimensional array, __ size declaratory are required.

TWO

To define a two-dimensional array, _________ size declarators are required

TWO

The parentheses are critical when adding values to pointers.

The * operator has precedence over the + operator, so the expression *number + 1 is not equivalent to *(number + 1). *number + 1 adds one to the contents of the first element of the array, while *(number + 1) adds one to the address in number, then dereferences it.

When writing a function that accepts a two-dimensional array as an argument, which size declarator must you provide in the parameter for the array?

The second size declarator, which is for the number of columns.

int collection[-20];

The size declarator cannot be negative.

What is the difference between a size declarator and a subscript?

The size declarator is used in a definition of an array to indicate the number of elements the array will have. A subscript is used to access a specific element in an array.

7.1 What is an array?

An array allows you to store and work with multiple value of the same data type. An array works like a variable that can store a group of values, all of the same type. The values are stored together in consecutive memory locations p. 377

7.1 What is an arrays size declarator?

An arrays size declarator indicates the number of elements, or values, that an array can hold. The size declarator must be a constant integer expression with a value greater than zero. It can be either a literal or a named constant. const int NUM_DAYS = 6; <-- Size declarator int days[NUM_DAYS];

Data hiding

An object's ability to hide its data from code that is outside the object.

7.2 What do question marks mean with regard to arrays?

Because values have not been assigned to other elements of the array, question marks will be used to indicate that the contents of those elements are unknown. If an array is defined globally, all of its elements are initialized to zero, by default. Local arrays, however, have no default initialization value. P. 380

Why should a function that accepts an array as an argument, and processes that array, also accept an argument specifying the arrays size?

Because, with the array alone the function has no way of determining the number of elements it has.

The__ search algorithm repeatedly divides the portion of an array being searched in half.

Binary

The___search algorithm requires that the array's contents be sorted.

Binary

C++ has no array _________ checking, which means you can inadvertently store data past the end of an array.

Bounds

In C++, what is the difference between a struct and a class?

By default, struct members are public, while class members are private.

How do you define an array without providing a size declarator?

By providing an initialization list. The array is sized to hold the number of values in the list.

How can a programmer hide specification details in C++?

By storing all the specifications in a header file.

How do you establish a parallel relationship between two or more arrays?

By using the same subscript value for each array.

how must you name a constructor?

Constructors must have the same name as their class

To completely clear the contents of a vector, use the ___________ member function.

Clear

Class

Code that specifies the attributes and member functions that a particular type of object may have. (A "blueprint"...a description of an object).

When a two-dimensional array is passed to a function the ___ size must be specified.

Column

When a two-dimensional array is passed to a function the _________ size must be specified.

Column

The size declaratory must be a_____with a value greater than____.

Constant integer expression; zero

char *cp;

Declare a variable cp that can be assigned the address of an char variable. In other words, declare cp to be of type "pointer to char ".

double *dp;

Declare a variable dp that can be assigned the address of a double variable. In other words, declare dp to be of type "pointer to double ".

int *ip;

Declare a variable ip that can be assigned the address of an int variable. In other words, declare ip to be of type "pointer to int ".

bool* bp; bp = new bool; *bp = true;

Declare a variable, bp, as a pointer to bool, dynamically allocate memory for a single boolean value, assign the resulting pointer to bp and initialize the value to true.

customary way of passing an object by value

Declare the class object as a reference variable, then declare it constant.

How to deal with variables in class definitions

Declare variables just like any other variable. However, you cannot initialize variables when declaring them.

Instantiation

Defining a class object.

If an array is sorted in __order, the values are stored from highest to lowest.

Descending

Instance (of a Class)

Each object that is created from a class.

In an average case involving an array of N elements, how many times will a linear search function have to read the array to locate a specific value?

N/2

To pass an array to a function, pass the__of the array.

Name

for(int i = 0; i < 20; i++) { ip_arr[i] = new int; }

The variable ip_arr has been declared as an array of 20 pointers to integer (i.e., each element of ip_arr is a pointer to an integer). Allocate 20 integer values and assign their pointers to the elements of ip_arr.

A vector is an associative container.

F

Character testing functions, such as isupper, accept strings as arguments and test each character in the string

F

If tolower's argument is already lowercase, it will be inadvertently converted to uppercase

F

The strcat function checks to make sure the first string is large enough to hold both strings before performing the concatenation

F

There is no difference between "847" and 847

F

When defining a parameter variable to hold a single-dimensional array argument, you do not have to include the size declarator.

F

The strlen function returns the size of the array containing a string

F. It returns the number of characters up to but not including the null terminator

If data are sorted in descending order, it means they are ordered from lowest value to highest value.

F; Descending is from highest to lowest.

The maximum number of comparisons performed by the linear search on an array of N elements is N/2(assuming values are consistently found).

F; The maximum is N because it would go through all the numbers.

Arrays cannot be initialized when they are defined. A loop or other means must be used.

F; can be defined

The first size declarator (in the declaration of a two-dimensional array) represents the number of columns. The second size definition represents the number of rows.

F; first is rows then column.

When an array name is used without brackets and a subscript, it is seen as the value of the first element in the array.

F; it is passed by as a reference to the address of the array.

Two-dimensional arrays may be passed to functions, but the row size must be specified in the definition of the parameter variable

F; it is the column that must be specified.

IF an array is partially initialized, the uninitialized elements will contain "garbage".

F; only if it is not initialized at all will it be with garbage.

The uninitialized elements of a string array will automatically be set to the value "0".

F; the are set to empty strings.

The first element in an array is accessed by the subscript 1.

F; the subscript is 0

If you leave an element uninitialized, you do not have to leave all the ones that follow it uninitialized.

F; you cannot skip numbers in initialization stage.

If you leave out the size declaratory of an array definition, you do not have to include an initialization list.

F; you have to include it or else the program wont know how big the array is.

Subscript numbers may be stored in variables.

F;value must be stored at the subscript.

7.4 True or False: C++ does not allow you to spread the initialization list across multiple lines.

False. C++ does all you to spread the initialization list across multiple lines. p. 391

Comparing two pointers is not the same as comparing the values the two pointers point to.

For example, the following if statement compares the addresses stored in the pointer variables ptr1 and ptr2 : if (ptr1 < ptr2) The following statement, however, compares the values that ptr1 and ptr2 point to: if (*ptr1 < *ptr2)

float ratings[];

For the array to be implicitly sized there must be an initialization list.

for(int i = 0; i < 26; i++) { cp_arr[i] = new char; *(cp_arr[i]) = (char)(65 + i); }

The variable cp_arr has been declared as an array of 26 pointers to char. Allocate 26 character values, initialized to the letters 'A' through 'Z' and assign their pointers to the elements of cp_arr (in that order).

ip = new int; *ip = 27;

Given the variable ip, already declared as a pointer to an integer, write the code to dynamically allocate memory for a single integer value, assign the resulting pointer to ip, and initialize the integer value to 27.

Scope Resolution Operator

Helps to identify the function as a member of a specific class.

#ifndef

If Not Defined. (See Include Guard for further definition).

A pointer can be used as a function parameter. It gives the function access to the original argument, much like a reference parameter does.

In Chapter 6you were introduced to the concept of reference variables being used as function parameters. A reference variable acts as an alias to the original variable used as an argument. This gives the function access to the original argument variable, allowing it to change the variable's contents. When a variable is passed into a reference parameter, the argument is said to be passed by reference.

The asterisk in front of the variable name indicates that ptr is a pointer variable. The int data type indicates that ptr can be used to hold the address of an integer variable. The definition statement above would read " ptr is a pointer to an int."

In this definition, the word int does not mean that ptr is an integer variable. It means that ptr can hold the address of an integer variable. Remember, pointers only hold one kind of value: an address.

7.3 What are off-by-one errors?

In working with arrays, a common type of mistake is the off-by-one error. This is an easy mistake to make because array subscripts start at zero, rather than one. p. 388

Starting values for an array may be specified with an ____ list.

Initiialization

Why is the section sort more efficient than the bubble sort on large arrays?

It takes the smallest value of the array then places it in its final position. Then moves to the next element and does the same thing again until all the items are in the proper place.

Why is the linear search also called "sequential search"?

It uses a loop to sequentially step through an array, starting from the first element. IT compares each elements of the array and stops when either the number is found or the end of the array is encountered.

The size declarator must be a(n) _________ with a value greater than _________.

Integer with a value greater than zero

The class members that a class object can access depend on what?

It depends on where an object is being declared 1) declared in a member function: can access public and private members 2) declared outside of class definition: can only access public members

If a member is not preceded by private/public...

It is automatically made a private member

7.4 What is implicit array sizing?

It's possible to define an array without specifying an initialization list. C++ automatically makes the array large enough to hold all initialization values. double ratings [ ] = {1.0, 1.5, 2.0, 2.5, 3.0}; p. 393

Why is the bubble sort inefficient for large array?

Items only move by one element at a time.

The ____ search algorithm steps sequentially through an array, comparing each item with the search value.

Linear (Sequential)

The_____ search algorithm is adequate for small arrays but not large arrays.

Linear (Sequential)

The individual elements of an array are accessed and indexed by ______.

Literal or named constant

Private class members

May NOT be accessed by code outside the class

Public class members

May be access by code outside the class

UML Class Diagram

Notation for describing a class graphically. contains 3 parts: 1) class name at top 2) member variables and their data types 3) member functions and their parameters

The address operator (&) returns the memory address of a variable. Every variable is allocated a section of memory large enough to hold a value of the variable's data type.

On a PC, for instance, it's common for one byte to be allocated for chars, two bytes for shorts, four bytes for ints, longs, and floats, and eight bytes for doubles.

& address

Operator to display a variable's address

* indirection

Operator used to get what a pointer points to.

When you pass an array name as an argument to a function, what is actually being passed?

Passing array name to function will pass address of array of integers

To store a value in a vector that does not have a starting size, or that is already full, use the___ member function.

Push_back

7.2 How do you read data from a file to an array?

Reading the contents of a file into an array is straight forward. - Open the file and use a loop to read each item from the file, storing each item in an array element. - The loop should iterate until either the array is filled or the end of the file has been reached. p. 384

You cannot use the assignment operator to copy ones array's contents to another in a single statement.

T

IT's best to think of two-dimensional arrays as having rows and columns.

T. damn straight

False

T/F A pointer variable that has not been initialized is called a null pointer.

False

T/F Any mathematical operation, , including multiplication and division, may be performed on a pointer.

How do you compare string class objects?

Relational Operators (<, ==, etc)

False

T/F Array names cannot be dereferenced with the indirection operator.

False

T/F In using a pointer with the delete operator, it is not necessary for the pointer to have been previously used with the new operator.

True

T/F Pointers may be compared using the relational operators.

It's best to think of a two-dimensional array as having___and ___.

Row's; Columns

::

Scope Resolution Operator

The vector data type is a ____ container

Sequence

The two types of containers defined by the STL are ___________ and______________.

Sequence container and associative container

The two types of containers defined by the STL are _____ and ____

Sequence; associative

When initialization a two-dimensional array, i helps to enclose each row's initialization list in a _____

Set of Braces({})

The_indicates the number of elements, or values, an array can hold

Size Declarator

The number inside the brackets of an array definition is the _____, but the number inside an array's bracket is an assignment statement, or any other statement that works with the contents of the array, is the___.

Size declaratory; subscript

False

T/F The & operator dereferences a pointer.

False

T/F The & symbol is called the indirection operator.

True

T/F The address 0 is generally considered unusable.

Access Specifiers

Specify how class members may be accessed.

The ____________________ is a collection of programmer-defined data types and algorithms that you may use in your programs

Standard Template Library (or STL)

The____is a collection of programmer-defined data types and algorithms that you may use in your programs.

Standard template library (STL)

By using the same _________ for multiple arrays, you can build relationships between the data stored in the arrays.

Subscript

By using the same___ for multiple arrays, you can build relationships between the data stored in the arrays.

Subscript

Each element of an array is accessed and indexed by a number known as a____.

Subscript

A two-dimensional array is like several identical arrays put together.

T

An array's size declaratory can either be a literal, a named constant, or a variable.

T

C++ allows you to create arrays with three or more dimensions.

T

C++ allows you to partially initialize an array.

T

C-string handling functions accept as arguments pointers to strings (array names or pointer variables), or literal strings

T

IF data is sorted in ascending order, it means they are going from lowest to highest.

T

If the starting address of a C-string is passed into a pointer parameter, it can be assumed that all the characters, from that address up to the byte that holds the null terminator, are part of the string.

T

If toupper's argument is already uppercase, it is returned as is, with no changes

T

If you add a value to a vector that is already full, the vector will automatically increase its size to accommodate the new value.

T

The average number of comparisons performed by the linear search of an array of N elements is N/2(assuming values are consistently found)

T

The strcpy function performs no bounds checking on the first argument

T

The strcpy function will overwrite the contents of its first string argument

T

The subscript of the last element in a single-dimensional array is one less than the total number of elements in the array.

T

The values in an initialization list are stored in the array in the order they appear in the list.

T

To calculate the amount of memory used by an array, multiply the number of elements by the number of bytes each element uses.

T

To pass an array to a function, pass the name of the array.

T

To use a vector, you must include the vector header file.

T

Vectors can report the number of elements they contain.

T

When an array is passed to a function, the function has access to the original array.

T

You can use the [] operator to insert a value into a vector that has no elements.

T

int array1[4], array2[4] = {3, 6, 9, 12}; array1 = array2;

The assignment operator cannot be used to assign the contents of one array to another, in a single statement

Encapsulation

The combining of data and code into a single object.

default constructor

The constructor that has no parameters

Attributes

The data that are contained in an object.

What happens if you declare a class object without including any parameters?

The default constructor is used to initialize the object.

Include File Directory

The directory or folder where all of the standard C++ header files are located.

members

The individual components that make up a class

7.2 How do you access array elements?

The individual elements of an array are assigned unique subscripts. These subscripts are used to access the elements. Even though an entire array has only one name, the elements may be accessed and used as individual variables. p. 379

int *squares; int n = 0; for (int i = first; i <= last; i++) { n++; } squares = new int[n]; for (int j = first; j <= last; j++) squares[j - first] = j*j;

The integer variables first and last have been declared and assigned values (with last >= first ). Allocate an integer array that can hold the squares of the numbers between first and last (inclusively), assign the resulting pointer to the variable squares (which you must declare), and initialize the array to the squares of the numbers from first to last (in that order).

7.2 What is the difference between the array size declarator and a subscript?

The number inside the brackets of an array definition is the size declarator. The number inside the brackets of an assignment statement, or any statement that works with the contents is a subscript. p. 380

void showValues(int nums[4][]) { for (rows = 0; rows < 4; rows++) for (cols = 0; cols < 5; cols++) cout << nums[rows][cols]; }

The parameter must specify the number of columns, not the number of rows.

How does a program know which constructor to execute?

The parameters passed will determine which constructor the program executes

Member Functions

The procedures that an object performs.

Methods

The procedures that an object performs. (This definition is used in other programming languages),

double *dp; dp = new double[n];

The variable dp is to refer to an array of double. Assuming the integer variable n has been assigned a value, declare dp appropriately, allocate an array of n doubles and assign the resulting pointer to dp.

7.5 True or False: Array elements may also be used in relational expressions.

True p. 396

A(n) _________ array is like several arrays of the same type put together.

Two-dimensional

UML

Unified Modeling Language

7.2 How do you write the contents of an array into a file?

Use a loop to step through each element of the array, writing its contents to a file. p. 385

Inline Function

When a member function is defined in the declaration of a class.

Pointers may be initialized with the address of an existing object. Remember that a pointer is designed to point to an object of a specific data type.

When a pointer is initialized with an address, it must be the address of an object the pointer can point to. int myValue; int *pint = &myValue;

When are structs usually used?

When all the member variables of a class are public and the class has no member functions, then a struct is used instead of a class.

Pointer variables, which are often just called pointers, are designed to hold memory addresses.

With pointer variables you can indirectly manipulate data stored in other variables.

How do you use a user-defined header file in a program?

With the preprocessor directive: # include "nameOfFile.h"

void divide (int x, int y, int *q, int *r);

Write a statement that declares a prototype for a function divide that takes four arguments and returns no value. The first two arguments are of type int . The last two arguments arguments are pointers to int that are set by the function to the quotient and remainder of dividing the first argument by the second argument. The function does not return a value.

void doubleIt(int *x);

Write a statement that declares a prototype for a function doubleIt , which has a single parameter of type pointer to int .

Can a class have more than one constructor?

Yes, but all constructors must have the same name

7.2 You can use any array integer expression as...

You can use any integer expression as an array subscript. for (int count = 1; count <= NUM_EMPLOYEES; count++) { cout << "Enter the hours worked by the employee " << count <<": "; cin >> hours[count - 1]; } In this expression the cin statement uses the expression, count - 1 , as a subscript p. 384

Array names can be used as constant pointers, and pointers can be used as array names.

You learned in Chapter 7that an array name, without brackets and a subscript, actually represents the starting address of the array. This means that an array name is really a pointer.

Another way to pass an argument by reference is to use a pointer variable as the parameter. Admittedly, reference variables are much easier to work with than pointers. Reference variables hide all the "mechanics" of dereferencing and indirection.

You should still learn to use pointers as function arguments, however, because some tasks, especially when you are dealing with strings, are best done with pointers.*Also, the C++ library has many functions that use pointers as parameters.

If an array is partially initialized, the uninitialized elements will be set to _________.

Zero

Subscript numbering in C++ always starts at _________.

Zero

class object (class instance, object)

a class variable

class

a collection of a fixed number of components

class

a collection of a fixed number of components with the operations you can perform on those components

default constructor

a constructor without parameters, or a constructor that has default values for all parameters; it is called when a class object comes into scope in a program

abstract data type (ADT)

a data type that specifies the logical properties without the implementation details

header file (interface file)

a file that contains the specification details of a class

struct

a fixed collection of components, wherein the components can be of different types

Unified Modeling Language (UML)

a graphical notation to describe a class and its members

mutator function

a member function of a class that modifies the value(s) of the member variable(s)

accessor function

a member function of a class that only accesses (that is, does not modify) the value(s) of the member variable(s)

constant function

a member function that cannot modify member variables of the class; its heading includes the reserved word const at the end

object-oriented design (OOD)

a programming methodology involving the use of objects that contain data members and their operations

operations

a set of operations on the data associated with an ADT

domain

a set of values belonging to the ADT

precondition

a statement specifying the condition(s) that must be true before a function is called

postcondition

a statement specifying what is true after a function call is completed

If the ; after the final brace is omitted...

a syntax error will occur

To pass an array to a function, pass the _________ of the array.

address, or name

class instance

another name for a class object

The __ function returns the value of a string converted to a float

atof

The __ function returns the value of a string converted to an integer

atoi

The __ function returns the value of a string converted to a long integer

atol

C++ has no array___ checking, which means you can inadvertently store data past the end of an array.

boundary

When initializing a two-dimensional array, it helps to enclose each row s initialization list in _________.

braces []

How can you access a class member outside of the class?

by using the class object name and the dot (member access operator)


Conjuntos de estudio relacionados

3.1.4 TRANSPORT INTO AND OUT OF CELLS

View Set

Ancient China Review (through Qin Dynasty)

View Set

Algebra Properties and Proportions

View Set

Ch 9: investment recommendations; risks and returns

View Set