0% found this document useful (0 votes)
6 views10 pages

Checkpoints

Uploaded by

sajalay17
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views10 pages

Checkpoints

Uploaded by

sajalay17
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

A video rental store keeps DVDs on 50 racks with 10 shelves each. Each shelf holds 25 DVDs.

Define a
three-dimensional array large enough to represent the store’s storage system. write program

#include <iostream>
#include <string>
using namespace std;

const int RACKS = 50;


const int SHELVES = 10;
const int DVDS_PER_SHELF = 25;

// Define the 3D array


string dvdStore[RACKS][SHELVES][DVDS_PER_SHELF];

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;
}

void getDVD(int rack, int shelf, int position) {


if (rack < 0 || rack >= RACKS || shelf < 0 || shelf >= SHELVES || position < 0 || position >=
DVDS_PER_SHELF) {
cout << "Invalid location!" << endl;
return;
}
if (dvdStore[rack][shelf][position].empty()) {
cout << "No DVD found at Rack " << rack << ", Shelf " << shelf << ", Position " << position << "." <<
endl;
} else {
cout << "DVD at Rack " << rack << ", Shelf " << shelf << ", Position " << position << " is \"" <<
dvdStore[rack][shelf][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;

Sure, let's go through each problem one by one.

### 7.8 Define the following arrays:


#### A) `ages`, a 10-element array of `int`s initialized with the values 5, 7, 9, 14, 15, 17, 18, 19, 21, and
23.
```cpp
int ages[10] = {5, 7, 9, 14, 15, 17, 18, 19, 21, 23};
```

#### 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.

#### B) `int matrix[5] = {1, 2, 3, 4, 5, 6, 7};`


- **Invalid**: This is invalid because the initializer list has more elements than the array size (7 elements
provided for an array of size 5).

#### C) `double radii[10] = {3.2, 4.7};`


- **Valid**: This is valid. The remaining elements will be initialized to 0.0.

#### D) `int table[7] = {2, , , 27, , 45, 39};`


- **Invalid**: This is invalid because there are missing values without proper initialization.

#### E) `char codes[] = {'A', 'X', '1', '2', 's'};`


- **Valid**: This is valid. The array size will be automatically determined to be 5.

#### F) `int blanks[];`


- **Invalid**: This is invalid because the size of the array must be specified.

### 7.10 Given the following array definition:


```cpp
int values[] = {2, 6, 10, 14};
```
#### A) `cout << values[2];`
- **Output**: `10`

#### B) `cout << ++values[0];`


- **Output**: `3` (The first element is incremented before printing.)

#### C) `cout << values[1]++;`


- **Output**: `6` (The original value of the second element is printed, then incremented.)

#### D)
```cpp
x = 2;
cout << values[++x];
```
- **Output**: `14` (The value of `x` is incremented to 3, then `values[3]` is printed.)

### 7.11 Given the following array definition:


```cpp
int nums[5] = {1, 2, 3};
```
#### What will the following statement display?
```cpp
cout << nums[3];
```
- **Output**: `0` (The uninitialized elements are set to 0 by default.)

### 7.12 What is the output of the following code?


```cpp
#include <iostream>
#include <iomanip>
using namespace std;

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
```

### 7.13 What is the output of the following code?


```cpp
#include <iostream>
using namespace std;

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.

7.14 Given the following array definitions


double array1[4] = {1.2, 3.2, 4.2, 5.2};
double array2[4];
will the following statement work? If not, why?
array2 = array1;
7.15 When an array name is passed to a function, what is actually being passed?
7.16 When used as function arguments, are arrays passed by value?
7.17 What is the output of the following program? (You may need to consult the
ASCII table in Appendix B.)
#include <iostream>
using namespace std;
// 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;
}
// Definition of function fillArray.
// (Hint: 65 is the ASCII code for 'A')
void fillArray(char arr[], int size)
{
char code = 65;
for (int k = 0; k < size; code++, k++)
arr[k] = code;
}
// Definition of function showArray.
void showArray(const char codes[], int size)
{
for (int k = 0; k < size; k++)
cout << codes[k];
cout << endl;
}
7.18 The following program skeleton, when completed, will ask the user to enter 10
integers, which are stored in an array. The function avgArray, which you must
write, is to calculate and return the average of the numbers entered.
#include <iostream>
using namespace std;
// Write your function prototype here
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;
}

Let's address each question and problem step by step:

### 7.14 Given the following array definitions:


```cpp
double array1[4] = {1.2, 3.2, 4.2, 5.2};
double array2[4];
```
#### Will the following statement work? If not, why?
```cpp
array2 = array1;
```
- **Explanation**:
- No, the statement `array2 = array1;` will not work.
- Arrays in C++ are not assignable as a whole after initialization. You cannot directly assign one array to
another using the assignment operator `=`. Each element of the array must be copied individually if you
want to copy the contents of one array to another.

#### Correct Approach to Copy Array Contents:


To copy the contents of `array1` to `array2`, you would need to use a loop or a library function like
`std::copy` from `<algorithm>`:

```cpp
#include <iostream>
#include <algorithm> // for std::copy

int main() {
double array1[4] = {1.2, 3.2, 4.2, 5.2};
double array2[4];

// Using std::copy to copy array1 to array2


std::copy(std::begin(array1), std::end(array1), std::begin(array2));

// Now array2 contains the same elements as array1


for (int i = 0; i < 4; ++i) {
std::cout << array2[i] << " ";
}
std::cout << std::endl;

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).

### 7.17 What is the output of the following program?


```cpp
#include <iostream>
using namespace std;

// 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;
}

// Definition of function fillArray.


// (Hint: 65 is the ASCII code for 'A')
void fillArray(char arr[], int size) {
char code = 65;
for (int k = 0; k < size; code++, k++)
arr[k] = code;
}

// Definition of function showArray.


void showArray(const char codes[], int size) {
for (int k = 0; k < size; k++)
cout << codes[k];
cout << endl;
}
```
- **Output**:
```
ABCDEFGH
```
- Explanation: The `fillArray` function fills the `prodCode` array with characters starting from 'A' (ASCII
65) to 'H' (ASCII 72). The `showArray` function then prints each character in `prodCode`, resulting in
`ABCDEFGH`.

### 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;
}

// Function to calculate the average of an array


double avgArray(int nums[], int size) {
int sum = 0;
for (int i = 0; i < size; ++i) {
sum += nums[i];
}
return static_cast<double>(sum) / size;
}
```
- **Explanation**:
- The `avgArray` function calculates the sum of all elements in the `nums` array passed to it and then
divides this sum by the `size` of the array to compute the average.
- In `main()`, the user inputs 10 numbers which are stored in `userNums`, and then `avgArray` is called
to calculate and print the average of these numbers.

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.

Let's address each question step by step:

### 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>

std::vector<int> frogs; // Define an empty vector of ints named frogs


```

### 7.29 Write a definition statement for a vector named lizards. lizards should be a vector of 20 floats.
```cpp
#include <vector>

std::vector<float> lizards(20); // Define a vector of 20 floats named lizards


```

### 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>

std::vector<int> gators; // Define an empty vector of ints named gators


gators.push_back(27); // Store the value 27 in gators
```
- **Explanation**:
- `push_back(27)` adds the value `27` to the end of the `gators` 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>

std::vector<double> snakes(10); // Define a vector of 10 doubles named snakes


snakes[4] = 12.897; // Store the value 12.897 in element 4 of the snakes vector
```
- **Explanation**:
- `snakes[4] = 12.897;` directly assigns the value `12.897` to the 5th element (index 4) of the `snakes`
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!

You might also like