Lab exercise 6 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

Securely Powered by: Secure Checkout

Description

5/5 - (2 votes)
  1. Write C++ programs
  2. Compile C++ programs
  3. Implement programs that use functions

Additional Reading

This lab exercise will require you to store function prototypes and implementations in separate files. Kindly go through this tutorial before starting the lab exercise.

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.

 

Military to Regular time

Create a function called mil_to_reg that converts time in the military time format into the regular format. For example, convert 2249 to 10:49 pm.

The function should receive a single unsigned short int parameter that represents the military time. It should return a std::string that contains the regular time counterpart of the given military time.

Please see the sample output below to guide the design of your program.

Note: Consider possible edge cases in user input to ensure your program works correctly.

Sample output:

Please enter the time in military time: 1433
The equivalent regular time is: 2:33 pm

Make sure that you write the mil_to_reg function prototype in time.hpp and its implementation in time.cpp, and call it from inside of main.cpp. You’ll find that skeleton code has already been provided and you simply need to call the function and include the necessary header files.

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

Header maker

Create a function called display_header that displays text surrounded by a rectangle composed of asterisks. The function receives a std::string text as input and does not return anything as an output, but displays the header. For example, given the text Hello World, the function should display this on screen:

****************
* Hello World! *
****************

Create another function called within_width that helps make sure that the header does not exceed the width of the screen. The function accepts two parameters: a std::string text that is the text input used for displaying the header, and an unsigned short int which is the maximum width of the screen. Typically, this is 80 characters, but this parameter makes it more flexible for the user. The function returns a bool value indicating whether the text will fit on the screen or not.

Note: Make sure you account for the added * and when the header is created. The function should return true if the length of the text together with the * and fit within the width.

A very important member function you will need to use is the std::string object’s length function. This will allow you to get the length of the string that you need for your function implementations. This is a good reference for the member function. Don’t forget to include the string header.

Make sure that you write the display_header and within_width function prototypes in header.hpp and their implementations in header.cpp, and call them from inside of main.cpp. You’ll find that skeleton code has already been provided and you simply need to call the function and include the necessary header files.

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 codes saved in header.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 header.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

 

Power

Create the power function that computes the value of a base value raised to a power. The function should accept a double and an int parameter to represent the base and the power values, respectively. The function should return a double value after performing the operation.

Hint: Be careful with your parameter names. If you use the same name for the function and parameters, the compiler won’t be able to distinguish between them. Your function’s name should be power, but you can use something else for the parameter names.

Sample input/output:

Please enter the base number: 5
Please enter the power: 3
5 ^ 3 = 125
Please enter the base number: 2.5
Please enter the power: 3
2.5 ^ 3 = 15.625
Please enter the base number: 4
Please enter the power: -4
4 ^ -2 = 0.06

Make sure that you write the power function prototype in power.hpp and its implementation in power.cpp, and call them from inside of main.cpp. You’ll find that skeleton code has already been provided and you simply need to call the function and include the necessary header files.

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 codes saved in power.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 power.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

Draw Rectangle Function

Create the draw_rectangle function that draws a rectangle on the screen given the user-defined length and width. The function should accept two unsigned short int parameters. The first refers to the length, and the second refers to the width. The function should not return any value, but display the rectangle on the screen.

In cases when the user provides invalid length or width values (i.e., zero or negative values), your function should display an error message.

Take a look at the sample input/output below.

Sample input/output:

Please enter the length: 2
Please enter the width: 2
**
**
Please enter the length: 3
Please enter the width: 5
*****
*****
*****
Please enter the length: 2
Please enter the width: 7
*******
*******
Please enter the length: -2
Please enter the width: 3
Invalid length and/or width values.

Make sure that you write the draw_rectangle function prototype in rectangle.hpp and its implementation in rectangle.cpp, and call them from inside of main.cpp. You’ll find that skeleton code has already been provided and you simply need to call the function and include the necessary header files.

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 main.cpp and rectangle.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 rectangle.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

Most significant digit

Make a program that utilizes functions and loops to determine the most significant digit of a given number

The function you make should be named: most_significant_digit

The parameters should be an int for the number you want to find the most significant digit of.

This function should return an int, the most significant digit.

Make sure that you write the most_significant_digit function prototype in most_sig_digit.hpp and its implementation in most_sig_digit.cpp, then and call it from inside of main.cpp. You’ll find that skeleton code has already been provided and you simply need to call the function.

Sample input/output:

Please enter an integer, this program will produce it's more significant digit: 4582
The most significant digit is 4
Please enter an integer, this program will produce it's more significant digit: 0
The most significant digit is 0
Please enter an integer, this program will produce it's more significant digit: -8382
The most significant digit is -8

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 main.cpp and most_sig_digit.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 most_sig_digit.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_06.zip