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

COS1511 Assignment 2 (COMPLETE ANSWERS) 2024 (735127) - DUE 13 May 2024

Rating
5,0
(3)
Sold
24
Pages
18
Grade
A+
Uploaded on
21-04-2024
Written in
2023/2024

COS1511 Assignment 2 (COMPLETE ANSWERS) 2024 (735127) - DUE 13 May 2024 ;100% TRUSTED workings, explanations and solutions. for assistance Whats-App.......0.6.7..1.7.1..1.7.3.9....... Question 1 Write function headers for the functions described below: (i) The function check has two parameters. The first parameter should be an integer number and the second parameter a floating point number. The function returns no value. (ii) The function mult has two floating point numbers as parameters and returns the result of multiplying them. (iii) The function time inputs seconds, minutes and hours and returns them as parameters to its calling function. (iv) The function countChar returns the number of occurrences of a character in a string, both provided as parameters. Question 2 Find the error(s) in each of the following program segments and explain how the error(s) can be corrected: (i) int function1() { cout << "Inside function function1 " << DUE DATE 13 May 2024 UNIQUE NUMBER EXTENSION There is no extension for this assignment TUTORIAL MATTER Study Guide, Lessons 17 – 23 CONTRIBUTION WEIGHT TO YEAR MARK QUESTIONS Practical exercises 735127 Downloaded by Corona Virus () lOMoARcPSD| COS1511/102/0/2024 3 endl; int function2() { cout << "Inside function function1 " << endl; } } (ii) int sum(int x, int y) { int result; result = x + y; } (iii) int computeProd(int n) { if (n == 0) return 0; else } n * computeProd(n – 1); (iv) void aFunction(float a) { float a; cout << a << endl; } (v) void theProduct() { int a; int b; int c; int result; cout << “Enter three integers “ << endl; cin >> a >> b >> c; result = a * b * c; cout << “Result is “ << result << endl; return result; } Downloaded by Corona Virus () lOMoARcPSD| COS1511/102/0/2024 (vi) float calculateSquare(float number) { return number * number; } Question 3 Write the functions as described below. You need not submit programs that use these functions. (i) Write a function that returns the cube of the integer passed to it. For example cube(2) will return 8 and cube(3) will return 27. (ii) Write a void function that receives four int parameters: the first two by value and the last two by reference. Name the formal parameters n1, n2, sum and diff. The function should calculate the sum of the two parameters passed by value and then store the result in the first variable passed by reference. It should calculate the difference between the two parameters passed by value and then store the result in the second parameter passed by reference. When calculating the difference, subtract the larger value from the smaller value. Name the function calcSumAndDiff. (iii) Write a function void rectangle(int w, int h) to print an open rectangle of asterisks (*). The parameters w and h are the width and the height of the rectangle, expressed in number of asterisks. (iv) Write a function, computePrice that receives two parameters: the first one is a character variable indicating the size of the pizza (S, M or L) and the second an integer variable indicating the number of toppings on the pizza. It then computes the cost of the pizza and returns the cost as a floating point number according to the rules: • Small pizza = R50 + R5.50 per topping • Medium pizza = R70 + R6.50 per topping • Large pizza = R90 + R7.50 per topping Question 4 The post office needs a program that reads in postal address data and then displays the data in a neat format. • Declare four string variables name, addr1, addr2 and postalCode. • First, the main function of the program must input the data by means of a function called inputData. • Second, the main function calls the function displayData to display the name and address as follows: Mr R.S. Bopape P.O. Box 50741 Sandton 2146 Submit the program and the output. Question 5 Write a program that calculates the average of a group of test scores, where the lowest score in the group is dropped. For example: if the user enters the values 65, 43, 78, 67 and 64, the output will be: Downloaded by Corona Virus () lOMoARcPSD| COS1511/102/0/2024 5 After dropping the lowest test score, the test average is 68.50 This average is calculated by dropping the lowest score which is 43 and dividing the sum of the remaining four values 65, 78, 67 and 64, which is 274, by 4. It should use the following functions: • An int function getScore with no parameters, to ask the user for a single test score, validate it and return the score. The function should be called by the main function once for each of the five scores to be entered. Do not accept test scores lower than 0 or higher than 100. • An int function findLowest that is passed the five test scores and then finds and returns the lowest of the five scores passed to it. It is called by function calcAverage, to determine which of the five scores to drop. • A float function calcAverage that is passed the five test scores and then calculates and returns the average of the four highest scores. • A void function displayOutput that is passed the average score; it then displays the average of the test scores. Display two digits after the decimal point. You must submit the four functions you have developed as well as output using the following 5 sets of input data: Submit the program and the output. Question 3 Write a program that will compute the volume of a room. User inputs are the height, width and length of a room. For example, if the user inputs the height as 3, the width as 4 and the length as 5 the volume will be 3 * 4 * 5 = 60. You have to declare three functions: one for input; one to do the calculation; and one for output. • The input function getData has to input the height, width and length into the variables theHeight, theWidth and theLength. • The calculation function, calculateVolume will receive three parameters that represent the height, width and length, do the calculation and return the volume of the room. • The output function displayOutput has to display the height, width and length entered by the user as well as the volume. • The program also has to indicate the size of the room as small, medium or large. If the volume is less than 100, the size is small, between 100 and 500 the size is medium and greater than 500 the size is large. For example: The volume of a room with height 3, width 4 and length 5 is 60. Size: Small The volume of a room with height 10, width 14 and length 12 is 1680. Size: Large The volume of a room with height 7, width 7 and length 7 is 343. Size: Medium • The main function should include a for loop that allows the user to repeat the calculation of the volume for new input values five times. Downloaded by Corona Virus () lOMoARcPSD| COS1511/102/0/2024 You must submit the three functions and the main function you have developed, as well as output produced by the following input data: 3 4 5 10 14 12 8 7 7 12 14 8 15 8 12 Question 7 Draw a series of variable diagrams for the program below. Use the conventions of the Study Guide. 1 // variable diagrams revisited 2 #include <iostream> 3 using namespace std; 4 const int C = 200; 5 int func1(int n, int n1) 6 { 7 n += 3; 8 n1 -= n; 9 return 2 + n + n1 * C; 10 } 11 void func2(int n, int & n1) 12 { 13 n = C * n1; 14 n1 = n – n1; 15 } 16 void func3(int & n, int & n1) 17 { 18 int k; 19 k = n1 + 3; 20 n = k * 30; 21 n1 = n + 2 * k; 22 } 23 int main( ) 24 { 25 int n, m, j; 26 n = 5; 27 m = 10; 28 j = func1(n, m); 29 n = 15; 30 m = 20; 31 func2(n, m); 32 n = 25; 33 m = 30; 34 func3(n, m); 35 36 return 0; 37 }

Show more Read less








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

Document information

Uploaded on
April 21, 2024
Number of pages
18
Written in
2023/2024
Type
Exam (elaborations)
Contains
Questions & answers

Subjects

Content preview

COS1511
Assignment 2 2024
Detailed Solutions, References & Explanations

Unique number: 735127

Due Date: 13 May 2024
QUESTION 1

i. void check(int num1, float num2);

ii. float mult(float num1, float num2);

iii. void time (int &s, int &m, int &h);

iv. int countlets(string theString, char theChar);




Terms of use
By making use of this document you agree to:
• Use this document as a guide for learning, comparison and reference purpose,
• Not to duplicate, reproduce and/or misrepresent the contents of this document as your own work,
• Fully accept the consequences should you plagiarise or misuse this document.

Disclaimer
Extreme care has been used to create this document, however the contents are provided “as is” without
any representations or warranties, express or implied. The author assumes no liability as a result of
reliance and use of the contents of this document. This document is to be used for comparison, research
and reference purposes ONLY. No part of this document may be reproduced, resold or transmitted in any
form or by any means.
R50,00
Get access to the full document:
Purchased by 24 students

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

Reviews from verified buyers

Showing all 3 reviews
1 year ago

10 months ago

1 year ago

5,0

3 reviews

5
3
4
0
3
0
2
0
1
0
Trustworthy reviews on Stuvia

All reviews are made by real Stuvia users after verified purchases.

Get to know the seller

Seller avatar
Reputation scores are based on the amount of documents a seller has sold for a fee and the reviews they have received for those documents. There are three levels: Bronze, Silver and Gold. The better the reputation, the more your can rely on the quality of the sellers work.
EduPal University of South Africa (Unisa)
View profile
Follow You need to be logged in order to follow users or courses
Sold
149163
Member since
7 year
Number of followers
35995
Documents
4310
Last sold
1 day ago

4,2

13555 reviews

5
7803
4
2688
3
1790
2
455
1
819

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