# Lab exercise 9 Problems Solved

35.00 \$

Category:
Click Category Button to View Your Next Assignment | Homework

You'll get a download link with a: . ` zip` solution files instantly, after Payment

## Description

5/5 - (1 vote)
1. Write C++ programs
2. Compile C++ programs
3. Implement programs that use pointers and dynamically allocated memory

# Additional Reading

This lab exercise requires an understanding of some concepts to solve the problems. You are strongly encouraged to read the following tutorials to help you answer the problems.

# Instructions

Answer the programming problems sequentially (i.e., answer prob01 before prob02). If you have questions let your instructor or the lab assistant know. You can also consult your classmates.

When you answer two programming problems correctly, let your instructor know and wait for further instruction.

# Lab exercise guide

Here’s a link to theÂ Lab exercise guideÂ in case you need to review the lab exercise objectives, grading scheme, or evaluation process.

# Evens Array

Create a program that checks whether each value in a given array is even or not. The results will be stored in a separateÂ `bool`Â array.

We will implement this functionality using theÂ `find_evens`Â function.

### find_evens

`find_evens`Â should receive three parameters, anÂ `int*`Â that refers to an array, aÂ `bool*`Â that refers to another array, and anÂ `int`Â for the size of the array. We assume that the size of both arrays are the saame.

Kindly make use of pointer arithmetic and not arrays or array indexes (`[]`).

The function should check to see if the contents in theÂ `int*`Â array areÂ evenÂ orÂ odd. Store the valueÂ `true`Â into theÂ `bool`Â array at the corresponding index if the content in theÂ `int`Â array at that same index isÂ `even`, otherwise store the valueÂ `false`. For example:

int* array values: {2, 6, 3, 7, 4}

bool* array values: {true, true, false, false, true}

NOTE: In theÂ `main`Â function, you are given an array with initial values and an initial size. Complete the missing parts of the program by following the instructions inÂ `main.cpp`. Your function is expected to work with any other values aside from those used inÂ `main`.

## Sample Output

``````01011
``````

Place theÂ `find_even`‘s function prototype inÂ `evens_array.hpp`Â and it’s implementation inÂ `evens_array.cpp`. TheÂ `main`Â function already contains some code, but you need to complete the requirements that is described inside the file.

# Submission checklist

1. Created function prototype and stored inÂ `.hpp`Â file.
2. Created function implementation and stored inÂ `.cpp`Â file (seeÂ reference).
3. Call function in the driver
4. Compiled and ran the driver (`main`).
5. Manually checked for compilation and logical errors.
6. Ensured no errors on the unit test (`make test`).
7. Followed advice from the stylechecker (`make stylecheck`).
8. Followed advice from the formatchecker to improve code readbility (`make formatcheck`).

# Code evaluation

Open the terminal and navigate to the folder that contains this exercise. Assuming you have pulled the code inside ofÂ `/home/student/labex02-tuffy`Â and you are currently inÂ `/home/student`Â you can issue the following commands

``````cd labex02-tuffy
``````

You also need to navigate into the problem you want to answer. To access the files needed to answer problem 1, for example, you need to issue the following command.

``````cd prob01
``````

When you want to answer another problem, you need to go back up to the parent folder and navigate into the next problem. Assuming you are currently inÂ `prob01`, you can issue the following commands to go to the parent folder then go into another problem you want to answer;Â `prob02`Â for example.

``````cd ..
cd prob02
``````

Use theÂ `clang++`Â command to compile your code and theÂ `./`Â command to run it. The sample code below shows how you would compile code saved inÂ `evens_array.cpp`Â andÂ `main.cpp`, and into the executable fileÂ `main`. Make sure you use the correct filenames required in this problem. Take note that if you make any changes to your code, you will need to compile it first before you see changes when running it.

``````clang++ -std=c++17 main.cpp evens_array.cpp -o main
./main
``````

You can run one, two, or all the commands below toÂ `test`Â your code,Â `stylecheck`Â your code’s design, orÂ `formatcheck`Â your work. Kindly make sure that you have compiled and executed your code before issuing any of the commands below to avoid errors.

``````make test
make stylecheck
make formatcheck
``````

A faster way of running all these tests uses theÂ `all`Â parameter.

# Class Average

Create a program that dynamically creates an array whose size depends on the user’s preference. Pass the array to aÂ `calculate_avg`Â function that is responsible for computing the average GPA of the given array.Â Use pointer arithmetic throughout your program.

### calculate_avg

Create a function calledÂ `calculate_avg`Â that calculates the average of aÂ `double`Â array and returns that average.

`calculate_avg()`Â will have two parameters:

1. aÂ `double*`Â referring to the array
2. anÂ `int`Â that contains the size of the given array.

When the array is size is greater than 0, the function should calculate the average GPA from the given array of grades.

However, when the size of the array is 0, then the function should return 0.

### main

TheÂ `main`Â function has mostly been built for you. It is your task to dynamically create aÂ `double`Â array, store users’ grades into the array, and pass the array toÂ `calculate_avg`Â to compute and then display the students’ average GPA. Read the instructions inÂ `main.cpp`Â for more details.

If the user happens to provide a class size ofÂ `0`, then the program should outputÂ `"You have no class!"`Â and then end the execution of the program without attempting to calculate the average.

Do not forget to deallocate memory that your code dynamically created.

Place theÂ `calculate_avg`‘s function prototype inÂ `calculate_avg.hpp`Â and it’s implementation inÂ `calculate_avg.cpp`.

# Sample Output

```How many students are in your class? 5
Enter the GPA for the students in your class (0.0 - 4.0)
Enter the GPA for student #1: 3.8
Enter the GPA for student #2: 2.5
Enter the GPA for student #3: 4.0
Enter the GPA for student #4: 1.9
Enter the GPA for student #5: 3.6
Class average: 3.16
```
```How many students are in your class? 0
You have no class!
```

Submission checklist

1. Created function prototype and stored inÂ `.hpp`Â file.
2. Created function implementation and stored inÂ `.cpp`Â file (seeÂ reference).
3. Call function in the driver
4. Compiled and ran the driver (`main`).
5. Manually checked for compilation and logical errors.
6. Ensured no errors on the unit test (`make test`).
7. Followed advice from the stylechecker (`make stylecheck`).
8. Followed advice from the formatchecker to improve code readbility (`make formatcheck`).

# Code evaluation

Open the terminal and navigate to the folder that contains this exercise. Assuming you have pulled the code inside ofÂ `/home/student/labex02-tuffy`Â and you are currently inÂ `/home/student`Â you can issue the following commands

``````cd labex02-tuffy
``````

You also need to navigate into the problem you want to answer. To access the files needed to answer problem 1, for example, you need to issue the following command.

``````cd prob01
``````

When you want to answer another problem, you need to go back up to the parent folder and navigate into the next problem. Assuming you are currently inÂ `prob01`, you can issue the following commands to go to the parent folder then go into another problem you want to answer;Â `prob02`Â for example.

``````cd ..
cd prob02
``````

Use theÂ `clang++`Â command to compile your code and theÂ `./`Â command to run it. The sample code below shows how you would compile code save inÂ `calculate_avg.cpp`Â andÂ `main.cpp`, and into the executable fileÂ `main`. Make sure you use the correct filenames required in this problem. Take note that if you make any changes to your code, you will need to compile it first before you see changes when running it.

``````clang++ -std=c++17 main.cpp calculate_avg.cpp -o main
./main
``````

You can run one, two, or all the commands below toÂ `test`Â your code,Â `stylecheck`Â your code’s design, orÂ `formatcheck`Â your work. Kindly make sure that you have compiled and executed your code before issuing any of the commands below to avoid errors.

``````make test
make stylecheck
make formatcheck
``````

A faster way of running all these tests uses theÂ `all`Â parameter.

# Add Array

Create a program that adds the contents of one array into another array using their corresponding indices.Â Use pointer arithmetic throughout your program.

### add_array

Create a function calledÂ `add_array`Â that takes two arrays of the same size and adds their contents then stores the result in the first array.

`add_array`Â will have three parameters in total:

1. anÂ `int*`Â pointing to the first array
2. anÂ `int*`Â pointing to the second array
3. theÂ `size`Â of the arrays (we assume that the two arrays always have the same size)

The goal of the function is to add all the values from the second array into the first array according to their position in the array. For example, the value in index 0 of the second array will be added to the current value in index 0 of the first array. The sum of both values will replace the old value in index 0 of the first array.

We expect that modifying the first array insideÂ `add_array`Â will also change the source array that was passed into this function as an argument because arrays point to the address of the array. Take a look at how the values in the array declared inÂ `main`Â change after you callÂ `add_array`.

### Output

All screen output (`std::cout`) should be placed in theÂ `main`Â function while the array manipulation will be in theÂ `add_array`Â function.

Use the 2 integer arrays of size 10 inÂ `main`Â as parameters to call yourÂ `add_array`Â function. The function will add the values in both arrays and store the result in the first array.

Most of the code has already been created for you inÂ `main.cpp`. You only need to fill in the missing parts. Read the instructions inside the file for more details.

Place theÂ `add_array`‘s function prototype inÂ `add_array.hpp`Â and it’s implementation inÂ `add_array.cpp`.

# Sample Output

```Inputs for the first array:
Enter the integer for index 0: 5
Enter the integer for index 1: 8
Enter the integer for index 2: 2
Enter the integer for index 3: 7
Enter the integer for index 4: 21
Enter the integer for index 5: 67
Enter the integer for index 6: 12
Enter the integer for index 7: 0
Enter the integer for index 8: 1
Enter the integer for index 9: 6

Inputs for the second array:
Enter the integer for index 0: 5
Enter the integer for index 1: 2
Enter the integer for index 2: 7
Enter the integer for index 3: 2
Enter the integer for index 4: 3
Enter the integer for index 5: 12
Enter the integer for index 6: 7
Enter the integer for index 7: 15
Enter the integer for index 8: 16
Enter the integer for index 9: 100

The first array contains:
5 8 2 7 21 67 12 0 1 6

The second array contains:
5 2 7 2 3 12 7 15 16 100

Calling add_array using the first and second array ...

After calling add_array, the first array now contains:
10 10 9 9 24 79 19 15 17 106
```

Submission checklist

1. Created function prototype and stored inÂ `.hpp`Â file.
2. Created function implementation and stored inÂ `.cpp`Â file (seeÂ reference).
3. Call function in the driver
4. Compiled and ran the driver (`main`).
5. Manually checked for compilation and logical errors.
6. Ensured no errors on the unit test (`make test`).
7. Followed advice from the stylechecker (`make stylecheck`).
8. Followed advice from the formatchecker to improve code readbility (`make formatcheck`).

# Code evaluation

Open the terminal and navigate to the folder that contains this exercise. Assuming you have pulled the code inside ofÂ `/home/student/labex02-tuffy`Â and you are currently inÂ `/home/student`Â you can issue the following commands

``````cd labex02-tuffy
``````

You also need to navigate into the problem you want to answer. To access the files needed to answer problem 1, for example, you need to issue the following command.

``````cd prob01
``````

When you want to answer another problem, you need to go back up to the parent folder and navigate into the next problem. Assuming you are currently inÂ `prob01`, you can issue the following commands to go to the parent folder then go into another problem you want to answer;Â `prob02`Â for example.

``````cd ..
cd prob02
``````

Use theÂ `clang++`Â command to compile your code and theÂ `./`Â command to run it. The sample code below shows how you would compile code save inÂ `add_array.cpp`Â andÂ `main.cpp`, and into the executable fileÂ `main`. Make sure you use the correct filenames required in this problem. Take note that if you make any changes to your code, you will need to compile it first before you see changes when running it.

``````clang++ -std=c++17 main.cpp add_array.cpp -o main
./main
``````

You can run one, two, or all the commands below toÂ `test`Â your code,Â `stylecheck`Â your code’s design, orÂ `formatcheck`Â your work. Kindly make sure that you have compiled and executed your code before issuing any of the commands below to avoid errors.

``````make test
make stylecheck
make formatcheck
``````

A faster way of running all these tests uses theÂ `all`Â parameter.

# Pointer Min

Create a function calledÂ `min`Â that receives two parameters, anÂ `int*`Â andÂ `int`Â size. The function should return the minimum value in the array. Assume that the array passed toÂ `min`Â will always have at least one element.

Your code should only use pointer arithmetic. Do not useÂ `[]`Â notations.

# Sample Output:

``````Array: -1 22 54 33 -40 67 8 15
Min: -40
``````

Submission checklist

1. Created function prototype and stored inÂ `.hpp`Â file.
2. Created function implementation and stored inÂ `.cpp`Â file (seeÂ reference).
3. Call function in the driver
4. Compiled and ran the driver (`main`).
5. Manually checked for compilation and logical errors.
6. Ensured no errors on the unit test (`make test`).
7. Followed advice from the stylechecker (`make stylecheck`).
8. Followed advice from the formatchecker to improve code readbility (`make formatcheck`).

# Code evaluation

Open the terminal and navigate to the folder that contains this exercise. Assuming you have pulled the code inside ofÂ `/home/student/labex02-tuffy`Â and you are currently inÂ `/home/student`Â you can issue the following commands

``````cd labex02-tuffy
``````

You also need to navigate into the problem you want to answer. To access the files needed to answer problem 1, for example, you need to issue the following command.

``````cd prob01
``````

When you want to answer another problem, you need to go back up to the parent folder and navigate into the next problem. Assuming you are currently inÂ `prob01`, you can issue the following commands to go to the parent folder then go into another problem you want to answer;Â `prob02`Â for example.

``````cd ..
cd prob02
``````

Use theÂ `clang++`Â command to compile your code and theÂ `./`Â command to run it. The sample code below shows how you would compile code save inÂ `min.cpp`Â andÂ `main.cpp`, and into the executable fileÂ `main`. Make sure you use the correct filenames required in this problem. Take note that if you make any changes to your code, you will need to compile it first before you see changes when running it.

``````clang++ -std=c++17 main.cpp min.cpp -o main
./main
``````

You can run one, two, or all the commands below toÂ `test`Â your code,Â `stylecheck`Â your code’s design, orÂ `formatcheck`Â your work. Kindly make sure that you have compiled and executed your code before issuing any of the commands below to avoid errors.

``````make test
make stylecheck
make formatcheck
``````

A faster way of running all these tests uses theÂ `all`Â parameter.

# Print Array Reverse

Create a program that displays the contents of an array in reverse.

### print_array_reverse()

Create a function calledÂ `print_array_reverse`Â that prints out the contents of an integer array in reverse.

Within this function you are only allowed to use pointer arithmetic to access elements of the array.

You are not allowed to use theÂ `[]`Â operator to complete this question.

`print_array_reverse()`Â will have two parameters in total:

1. anÂ `int*`Â pointer that refers to an array
2. anÂ `int`Â that stores the number of elements in the array

Your function has two main scenarios it could encounter.

When you have an array with a size that is greater than 0, then it should behave normally and output the array’s contents in reverse.

But, if your function encounters an array with size of 0 or negative, then it should outputÂ `There are no contents in this array!`Â and go to the next line.

Refer to the sample output to see instances of both.

Complete the code inÂ `main.cpp`, provide the headers inÂ `print_array_reverse.hpp`Â and the implementation inÂ `print_array_reverse.cpp`.

# Sample Output

```Enter an integer for the size of the array(Must be less than or equal to 10): 10
Inputs for the array:
Enter the integer for index 0: 4
Enter the integer for index 1: 3
Enter the integer for index 2: 2
Enter the integer for index 3: 1
Enter the integer for index 4: 89
Enter the integer for index 5: 15
Enter the integer for index 6: 100
Enter the integer for index 7: 24
Enter the integer for index 8: 254
Enter the integer for index 9: 2
The contents of the array in reverse are:
2 254 24 100 15 89 1 2 3 4
```
```Enter an integer for the size of the array(Must be less than or equal to 10): 0
Inputs for the array:
There are no contents in this array!
```

Submission checklist

1. Created function prototype and stored inÂ `.hpp`Â file.
2. Created function implementation and stored inÂ `.cpp`Â file (seeÂ reference).
3. Call function in the driver
4. Compiled and ran the driver (`main`).
5. Manually checked for compilation and logical errors.
6. Ensured no errors on the unit test (`make test`).
7. Followed advice from the stylechecker (`make stylecheck`).
8. Followed advice from the formatchecker to improve code readbility (`make formatcheck`).

# Code evaluation

Open the terminal and navigate to the folder that contains this exercise. Assuming you have pulled the code inside ofÂ `/home/student/labex02-tuffy`Â and you are currently inÂ `/home/student`Â you can issue the following commands

``````cd labex02-tuffy
``````

You also need to navigate into the problem you want to answer. To access the files needed to answer problem 1, for example, you need to issue the following command.

``````cd prob01
``````

When you want to answer another problem, you need to go back up to the parent folder and navigate into the next problem. Assuming you are currently inÂ `prob01`, you can issue the following commands to go to the parent folder then go into another problem you want to answer;Â `prob02`Â for example.

``````cd ..
cd prob02
``````

Use theÂ `clang++`Â command to compile your code and theÂ `./`Â command to run it. The sample code below shows how you would compile code save inÂ `print_array_reverse.cpp`Â andÂ `main.cpp`, and into the executable fileÂ `main`. Make sure you use the correct filenames required in this problem. Take note that if you make any changes to your code, you will need to compile it first before you see changes when running it.

``````clang++ -std=c++17 main.cpp print_array_reverse.cpp -o main
./main
``````

You can run one, two, or all the commands below toÂ `test`Â your code,Â `stylecheck`Â your code’s design, orÂ `formatcheck`Â your work. Kindly make sure that you have compiled and executed your code before issuing any of the commands below to avoid errors.

``````make test
make stylecheck
make formatcheck
``````

A faster way of running all these tests uses theÂ `all`Â parameter.

``make all``
• Lab_09.zip