Checkpoints
Checkpoints
Define a
three-dimensional array large enough to represent the store’s storage system. write program
#include <iostream>
#include <string>
using namespace std;
void addDVD(int rack, int shelf, int position, const string& dvdTitle) {
if (rack < 0 || rack >= RACKS || shelf < 0 || shelf >= SHELVES || position < 0 || position >=
DVDS_PER_SHELF) {
cout << "Invalid location!" << endl;
return;
}
dvdStore[rack][shelf][position] = dvdTitle;
cout << "DVD \"" << dvdTitle << "\" added at Rack " << rack << ", Shelf " << shelf << ", Position " <<
position << "." << endl;
}
int main() {
// Adding DVDs
addDVD(0, 0, 0, "The Matrix");
addDVD(1, 2, 5, "Inception");
addDVD(49, 9, 24, "Interstellar");
// Retrieving DVDs
getDVD(0, 0, 0);
getDVD(1, 2, 5);
getDVD(49, 9, 24);
getDVD(25, 5, 12); // Invalid location for testing
return 0;
}
7.8 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 floats 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’.
7.9 Is each of the following a valid or invalid array definition? (If a definition is invalid, explain why.)
int numbers[10] = {0, 0, 1, 0, 0, 1, 0, 0, 1, 1}; int matrix[5] = {1, 2, 3, 4, 5, 6, 7}; double radii[10] = {3.2,
4.7}; int table[7] = {2, , , 27, , 45, 39}; char codes[] = {'A', 'X', '1', '2', 's'}; int blanks[];
7.10 Given the following array definition: int values[] = {2, 6, 10, 14}; What does each of the following
display? A) cout << values[2]; B) cout << ++values[0]; C) cout << values[1]++; D) x = 2; cout <<
values[++x];
7.11 Given the following array definition: int nums[5] = {1, 2, 3}; What will the following statement
display? cout << nums[3];
7.12 What is the output of the following code? (You may need to use a calculator.) double balance[5]
= {100.0, 250.0, 325.0, 500.0, 1100.0}; const double INTRATE = 0.1; cout << fixed << showpoint <<
setprecision(2); for (int count = 0; count < 5; count++) cout << (balance[count] * INTRATE) << endl;
7.13 What is the output of the following code? (You may need to use a calculator.) const int SIZE = 5;
int time[SIZE] = {1, 2, 3, 4, 5}, speed[SIZE] = {18, 4, 27, 52, 100}, dist[SIZE]; for (int count = 0; count <
SIZE; count++) dist[count] = time[count] * speed[count]; for (int count = 0; count < SIZE; count++) {
cout << time[count] << " "; cout << speed[count] << " "; cout << dist[count] << endl;
#### B) `temps`, a 7-element array of `float`s initialized with the values 14.7, 16.3, 18.43, 21.09, 17.9,
18.76, and 26.7.
```cpp
float temps[7] = {14.7, 16.3, 18.43, 21.09, 17.9, 18.76, 26.7};
```
#### C) `alpha`, an 8-element array of `char`s initialized with the values ‘J’, ‘B’, ‘L’, ‘A’, ‘*’, ‘$’, ‘H’, and
‘M’.
```cpp
char alpha[8] = {'J', 'B', 'L', 'A', '*', '$', 'H', 'M'};
```
### 7.9 Valid or invalid array definitions:
#### A) `int numbers[10] = {0, 0, 1, 0, 0, 1, 0, 0, 1, 1};`
- **Valid**: This is a valid array definition.
#### D)
```cpp
x = 2;
cout << values[++x];
```
- **Output**: `14` (The value of `x` is incremented to 3, then `values[3]` is printed.)
int main() {
double balance[5] = {100.0, 250.0, 325.0, 500.0, 1100.0};
const double INTRATE = 0.1;
cout << fixed << showpoint << setprecision(2);
for (int count = 0; count < 5; count++)
cout << (balance[count] * INTRATE) << endl;
return 0;
}
```
- **Output**:
```
10.00
25.00
32.50
50.00
110.00
```
int main() {
const int SIZE = 5;
int time[SIZE] = {1, 2, 3, 4, 5};
int speed[SIZE] = {18, 4, 27, 52, 100};
int dist[SIZE];
for (int count = 0; count < SIZE; count++)
dist[count] = time[count] * speed[count];
for (int count = 0; count < SIZE; count++) {
cout << time[count] << " ";
cout << speed[count] << " ";
cout << dist[count] << endl;
}
return 0;
}
```
- **Output**:
```
1 18 18
248
3 27 81
4 52 208
5 100 500
```
These solutions provide the definitions for the arrays, explain whether the given array definitions are
valid or invalid, and calculate the outputs for the given code snippets.
```cpp
#include <iostream>
#include <algorithm> // for std::copy
int main() {
double array1[4] = {1.2, 3.2, 4.2, 5.2};
double array2[4];
return 0;
}
```
### 7.15 When an array name is passed to a function, what is actually being passed?
- When you pass an array name to a function in C++, what is actually being passed is the **address** of
the first element of the array. Therefore, the function receives a pointer to the first element of the
array.
### 7.16 When used as function arguments, are arrays passed by value?
- No, arrays are **not** passed by value when used as function arguments in C++. Instead, a pointer to
the first element of the array is passed (which can decay from array type to pointer type).
// Function prototypes
void fillArray(char [], int);
void showArray(const char [], int);
int main() {
const int SIZE = 8;
char prodCode[SIZE] = {'0', '0', '0', '0', '0', '0', '0', '0'};
fillArray(prodCode, SIZE);
showArray(prodCode, SIZE);
return 0;
}
### 7.18 Complete the program to calculate the average of 10 integers entered by the user:
```cpp
#include <iostream>
using namespace std;
// Function prototype
double avgArray(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 >> userNums[count];
}
cout << "The average of those numbers is ";
cout << avgArray(userNums, SIZE) << endl;
return 0;
}
These answers cover the correct usage of arrays, passing arrays to functions, and handling array
operations in C++. Let me know if you have further questions!
7.27 What header file must you #include in order to define vector objects?
7.28 Write a definition statement for a vector named frogs. frogs should be an empty vector of ints.
7.29 Write a definition statement for a vector named lizards. lizards should be a vector of 20 floats.
7.30 Write a definition statement for a vector named toads. toads should be a vector of 100 chars,
with each element initialized to 'Z'.
7.31 gators is an empty vector of ints. Write a statement that stores the value 27 in gators.
7.32 snakes is a vector of doubles, with 10 elements. Write a statement that stores the value 12.897
in element 4 of the snakes vector.
### 7.27 What header file must you #include in order to define vector objects?
To define vector objects in C++, you need to include the `<vector>` header file.
```cpp
#include <vector>
```
### 7.28 Write a definition statement for a vector named frogs. frogs should be an empty vector of ints.
```cpp
#include <vector>
### 7.29 Write a definition statement for a vector named lizards. lizards should be a vector of 20 floats.
```cpp
#include <vector>
### 7.30 Write a definition statement for a vector named toads. toads should be a vector of 100 chars,
with each element initialized to 'Z'.
```cpp
#include <vector>
std::vector<char> toads(100, 'Z'); // Define a vector of 100 chars initialized to 'Z' named toads
```
### 7.31 gators is an empty vector of ints. Write a statement that stores the value 27 in gators.
```cpp
#include <vector>
### 7.32 snakes is a vector of doubles, with 10 elements. Write a statement that stores the value 12.897
in element 4 of the snakes vector.
```cpp
#include <vector>
These statements demonstrate the basic operations and initialization of vectors in C++. Vectors provide
dynamic resizing and convenient operations compared to arrays in C++. If you have any more questions
or need further clarification, feel free to ask!