C++ Functions and Arrays: A Comprehensive Guide

CIS-265 Test 1 Cheat Sheet


Chapter 6

Local variables lost their values between calls to the function in which they are defined because they are created in memory when the function begins execution and are destroyed when the function ends.


The difference between an argument and a parameter variable is that an argument is a value passed to a function and a parameter variable is a variable local to the function which receives the argument. The argument’s value is copied into the parameter variable.


You define parameter variables inside the parentheses of a function header.


If you are writing that accepts an argument and you want to make sure the function cannot change the value of the argument, nothing needs to be done when the argument is passed by value. The function cannot access the argument. If the argument is passed by reference, the parameter should be defined with the const key word.


When a function accepts multiple arguments, it does matter in what order the arguments are passed. The first argument is passed into the parameter variable that appears first inside the function’s header parentheses. Same goes for the second argument and so on.


You return a value from a function with the return statement. For example, the following statement returns the value in the variable x. return x;


The advantage of breaking your application’s code into several small procedures is that it makes the program easier to manage.


A static local variable would be useful when it enables a function to retain a value between function calls.


An example where passing an argument by reference would be useful is that a function such as the following could be written to get user input. The input is stored in the variables that are passed as arguments.

void getValues (int &x, int &y){          cout           cin >> x;          cout           cin >> y;}

The header is the part of a function definition that shows the function name, return type, and parameter.

If a function doesn’t return a value, the word void will appear as its return type.

Either a function’s definition or its prototype must precede all calls to the function.

Values that are sent into a function are called arguments.

Special variables that hold copies of function arguments are called parameters.

When only a copy of an argument is passed to a function, it is said to be passed by value.

A(n) prototype eliminates the need to place a function definition before all calls to the function.

A(n) local variable is defined inside a function and is not accessible outside the function.

Global variables are defined outside all functions and are accessible to any function within their scope.

Global variables provide an easy way to share large amounts of data among all the functions in a program.

Unless you explicitly initialize global variables, they are automatically initialized to 0.

If a function has a local variable with the same name as a global variable, only the local variable can be seen by the function.

Static local variables retain their value between function calls.

The return statement causes a function to end immediately.

Default arguments are passed to parameters automatically if no argument is provided in the function call.

When a function uses a mixture of parameters with and without default arguments, the parameters with default arguments must be defined last.

The value of a default argument must be a(n) constant.

When used as parameters, reference variables allow a function to access the parameter’s original argument.

Reference variables are defined like regular variables, except there is a(n) & in front of the name.

Reference variables allow arguments to be passed by reference.

The exit function causes a program to terminate.

Two or more functions may have the same name, as long as their parameter lists are different.

Functions should be given names that reflect their purpose.

Function headers are not terminated with a semicolon.

Function prototypes are terminated with a semicolon.

If other functions are defined before main, the program still starts executing at function main.

When a function does not terminate, it always branches back to main, regardless of where it was called from.

Arguments are passed to the function parameters in the order they appear in the function call.

The scope of a parameter is limited to the function which uses it.

Changes to a function parameter do not always affect the original argument as well.

In a function prototype, the names of the parameter variables may be left out.

Many functions may have local variables with the same name.

Overuse of global variables can lead to problems.

Static local variables are not destroyed when a function returns.

All static local variables are not initialized to -1 by default.

Initialization of static local variables only happens once, regardless of how many times the function in which they are defined is called.

When a function with default arguments is called and an argument is left out. all arguments that come after it must be left out as well.

It is possible for a function to have some parameters with default arguments and some without.

The exit function cannot only be called from main.

A stub is a dummy function that is called instead of the actual function it represents.

void showValue (int quantity)
showValue(x);
half function returns a value that is half that of the argument.

result = half(number);
double half (double num){          return num / 2;}
int cube (int num)
{
          return num * num * num;
}

statement that passes the value to 4 to this function and assigns its return value to the variable.
result = cube (4);
timesTen should display the product of its argument multiplied times 10.
void timesTen (int num){          cout }
void display (int arg1, double arg2, char arg3)
{
          cout

}

statement that calls the procedure and passes the following variables to it:

int age;
double income;
char initial;
display (age, income, initial);
getNumber should use a reference parameter variable to accept an integer argument. should prompt the user to enter a number in the range 1-100. input should be validated and stored in the parameter variable.
void getNumber (int &num){          cout           cin >> num;          while (num 100)          {                    cout                     cout                     cin >> num;          }}
void total (int value1, value2, value3)
{
          return value1 + value2 + value3;
}
The data type of value2 and value3 must be defined.The function is declared void but returns a value.
double average (int value1, int value2, int value3)
{
          double average;
          average = value1 + value2 + value3 /3;
}
The assignment statement should read: average = (value1 + value2 + value3) / 3.0;
void area (int length = 30, int width)
{
          return length * width;
}
Width should have a default argument value.The function is defined as a float but returns no value.
void getValue (int value&)
{
          cout
          cin >> value&;
]
The parameter should be defined as: int &valueThe cin statement should read: cin >> value;
Overloaded Function
int getValue()
{
          int inputValue;
          cout
          cin >> inputValue;
          return inputValue;
The functions must have different parameter lists.

Chapter 7
The difference between a size declarator and a subscript is that the size declarator is used in a definition of an array to indicate the number of elements the array will have and a subscript is used to access a specific element in an array.

int values [10];
10 elementssubscript of first element is 0subscript of last element is 9Using four-byte integers, uses 40 bytes of memory
A function that accepts an array as an argument, and processes that array also accepts an argument specifying the array’s size because with the array alone, the function has no way of determining the number of elements it has.

int values [5] = {4, 7, 6, 8, 2};
cout 2
cout 14
cout 8

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.

int numbers [5] = {1, 2, 3};
numbers [2] 3
numbers [4] 0

Assuming that array1 and array2 are both arrays, it is 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.

Assuming the numbers is an array of doubles, the following statement: cout
An array is passed to a function by reference.

When you pass an array name as an argument to a function, the array’s beginning memory address is actually being passed.

You establish a parallel relationship between two or more arrays by using the same subscript value for each array.

double sales [8][10]
8 rows10 columns80 elementsstatement that stores a number in the last column of the last row in the array: sales [7][9] = 123.45;
When writing a function that accepts a two-dimensional array as an argument, the size declarator you provide in the parameter for the array is the second size declarator, which is for the number of columns.

The advantages that a vector offers over an array are:
You do not have to declare the number of elements that a vector will have.If you add a value to a vector that is already full, the vector will automatically increase its size to accommodate the new value.A vector can report the number of elements it contains.
The size declarator indicates the number of elements, or values, an array can hold.

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

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

Subscript numbering in C++ always starts at 0.

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

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

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

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

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

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

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

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

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

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

It’s best to think of a two-dimensional as having rows and columns.

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

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

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

The Standard Template Library (STL) is a collection of programmer-defined data types and algorithms that you may use in your programs.

The two types of containers defined by the STL are sequence and associative.

The vector data type is a(n) sequence container.

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

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

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

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

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

An array’s size declarator cannot be either a literal, a named constant, or a variable.

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

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

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

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

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

Subscript numbers may be stored in variables.

You can write programs that use invalid subscripts for array.

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

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

C++ allows you to partially initialize an array.

If an array is not partially initialized, the uninitialized elements will contain “garbage.”

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

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

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

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

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

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

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

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

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

It’s best to think of two-dimensional arrays as having rows an columns.

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

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

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

A vector is a sequence container.

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

Vectors can report the number of elements they contain.

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

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

names is an integer array with 20 elements. for loop, as well as a range- based for loop that prints each element.
const int SIZE = 20;for (int i = 0; i           cout
numberArray1 and numberArray1 have 100 elements. need code that copies the values in numberArray1 to numberArray2.
const int SIZE = 10;for (int i = 0; i           numberArray2 [i] = numberArray1 [i];
store identification numbers of 10 employees (as ints) and weekly gross pay (as doubles)

Define two arrays that may be used in parallel to store the 10 employee identification numbers and gross pay amounts.
Write a loop that uses these arrays to print each employee’s identification number and weekly gross pay.

const int SIZE = 10;int id [SIZE];double weeklyPay [SIZE];
for (int i = 0; i {          cout


}
two-dimensional array of integers named grades. should have 30 rows and 10 columns.
const int ROWS = 30, COLS = 10;int grades [ROWS] [COLS];
store the population of 12 countries.

Define two arrays that may be used in parallel to store the names of the countries and their populations.
Write a loop that uses these arrays to print each country’s name and its population.

const int SIZE = 12;const int NAME_SIZE – 25;// A 2D array to hold the country nameschar countries [SIZE] [NAME_SIZE];// An array to hold populationslong populations [SIZE];
// Display each country’s name and populationfor (int i = 0; i {          cout
}
numberArray1 and numberArray2 each have 25 elements. Will the code print the correct sum of values for both arrays? Why or why not?

int total = 0;                                // Accumulator
int count ;                                   // Loop counter
// Calculate and display the total of the first array.
for (count = 0; count
          total += numberArray1 [count];
cout
// Calculate and display the total of the second array.
for (count = 0; count
          total += numberArray2 [count];
cout
No, the correct sum will not be printed for numberArray2 because the accumulator (total) is not set back to zero before the second loop executes.
int numberArray [9][11];

statement that assigns 145 to the first column of the first row
statement that assigns 18 to the last column of the last row
numberArray [0][0] = 145;numberArray [8][10] = 18;
values is a two-dimensional array of floats with 10 rows and 20 columns. need code that sums all the elements in the arrays and stores the sum in the variable total.

const int ROWS = 10;const int COLS = 20;int row, col;float total = 0.0;// Sum the values in the array.for (row = 0; row {          for (col = 0; col                     total += values [row] [col];}