Lab exercise 9 Problems Solved

35.00 $

Category:

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.

  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
  • Lab_09.zip