100% satisfaction guarantee Immediately available after payment Both online and in PDF No strings attached 4.2 TrustPilot
logo-home
Other

Detailed Solution

Rating
-
Sold
-
Pages
21
Uploaded on
03-06-2025
Written in
2024/2025

This Word document provides complete, step-by-step solutions for “Assignment 2,” which focuses on fundamental C++ programming skills. Inside, you’ll find: Function Signatures (Question 1): Exact C++ headers for common functions, with explanations of their return types, parameter lists, and usage. Error Detection & Correction (Question 2): Six flawed code snippets are analyzed in detail—each error is identified, explained, and corrected so you can easily spot and fix similar mistakes in your own code. Full Function Implementations (Question 3): Four standalone functions are written from scratch (e.g., computing cubes, summing and finding differences by reference, printing an open rectangle, and calculating pizza prices), with clear commentary on why each line works. Post Office Address Program (Question 4): A complete C++ program that reads four lines of mailing information (name, two address lines, and postal code) and displays them neatly—ideal for learning how to use getline and pass strings by reference. Test-Score Averaging (Question 5): A robust program that prompts for five test scores, validates input, drops the lowest score, then computes and displays the average to two decimal places. Sample runs with multiple data sets are provided so you can verify your own results. Room Volume & Classification (Question 6): A modular C++ solution that reads height, width, and length five times; calculates each room’s volume; and categorizes it as Small, Medium, or Large. All three functions (input, calculation, output) are shown, along with sample outputs.

Show more Read less










Whoops! We can’t load your doc right now. Try again or contact support.

Document information

Uploaded on
June 3, 2025
Number of pages
21
Written in
2024/2025
Type
Other
Person
Unknown

Subjects

Content preview

Cos1511


Assignment 2: Detailed Solutions

Question 1: Writing Function Headers
You are asked to write the headers (signatures) for four functions. A “function header” in C+
+ includes the return type, function name, and parameter list (with types), but no body.



1. (i) Function check

Requirement:

- Two parameters:

1. an integer number

2. a floating-point number

- Returns no value.

Header:

void check(int number, float value);

Explanation:

- The return type is void because it “returns no value.”

- The first parameter is int number (an integer).

- The second parameter is float value (a floating-point number).

- The names number and value are arbitrary but descriptive.



2. (ii) Function mult

Requirement:

- Two parameters, both floating-point (float).

- Returns the result of multiplying them.

Header:

, float mult(float a, float b);

Explanation:

- The return type is float, since multiplying two floats yields a float.

- The two parameters are float a and float b.

- Inside the body (not shown), one would write return a * b;.



3. (iii) Function time (inputs and returns three values)

Requirement:

- Inputs seconds, minutes, and hours and returns them via parameters.

- In C++, to return multiple values, use reference parameters.

Header:

void getTime(int& seconds, int& minutes, int& hours);

Explanation:

- Return type is void, because values are returned via the reference parameters.

- Each parameter is int& (reference to an integer), so assignments inside the function
update the caller’s variables.

- Renamed from time to getTime to avoid conflict with std::time().

- If strictly required to name it time, you could write:

void time(int& seconds, int& minutes, int& hours);



4. (iv) Function countChar

Requirement:

- Returns the number of occurrences of a character in a string.

- Both the string and the character are provided as parameters.

Header:

int countChar(const std::string& text, char target);

Explanation:

, - Return type is int, because we count how many times target appears in text.

- The first parameter is const std::string& text: passed by constant reference to avoid
copying and ensure it is not modified.

- The second parameter is char target: a single character to count.

- Inside the function (not shown), iterate through text, increment a counter when a
character matches target, then return the counter.

Question 2: Finding and Correcting Errors
Below are six code segments. For each one, errors are identified and explained, followed by
a corrected version (or explanation of the fix).



(i)

Original:

int function1()

{

cout << "Inside function function1 " <<

endl; int function2()

{

cout << "Inside function function1 " << endl;

}

}

Errors:

- Nested function definition: You cannot define function2 inside function1 in C++.

- Missing return in function1 (should return int).

- The message in function2 mentions function1; likely meant to say function2.

Fix:

#include <iostream>

using namespace std;
R150,00
Get access to the full document:

100% satisfaction guarantee
Immediately available after payment
Both online and in PDF
No strings attached

Get to know the seller
Seller avatar
mukundigis

Get to know the seller

Seller avatar
mukundigis University of South Africa (Unisa)
View profile
Follow You need to be logged in order to follow users or courses
Sold
0
Member since
6 year
Number of followers
1
Documents
2
Last sold
-

0,0

0 reviews

5
0
4
0
3
0
2
0
1
0

Recently viewed by you

Why students choose Stuvia

Created by fellow students, verified by reviews

Quality you can trust: written by students who passed their exams and reviewed by others who've used these notes.

Didn't get what you expected? Choose another document

No worries! You can immediately select a different document that better matches what you need.

Pay how you prefer, start learning right away

No subscription, no commitments. Pay the way you're used to via credit card or EFT and download your PDF document instantly.

Student with book image

“Bought, downloaded, and aced it. It really can be that simple.”

Alisha Student

Frequently asked questions