[SOLVED] Lab exercise 9 Problems

35.00 $

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

You will receive the following solution file(s) instantly after successful payment:

zip file icon Lab_09.zip (106.6 KB)
Assignment Instructions Updated Recently? Submit Below and we will provide new Solution!
Submit New Instructions
πŸ”’ Securely Powered by:
Secure Checkout
5/5 - (2 votes)
  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.

  1. Organizing C++ files: function prototypes, implementations, and drivers.
  2. Using objects as parameters and return values in functions
  3. Passing arrays as parameters to functions
  4. File reading and writing (also includes dealing with arrays)

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.

make all

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.

make all

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.

make all

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.

make all

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

1 file Β· 106.6 KB

  • Lab_09.zip 106.6 KB