100% satisfaction guarantee Immediately available after payment Both online and in PDF No strings attached
logo-home
COS1512 Assignment 4 2023 (ANSWERS) R51,54   Add to cart

Exam (elaborations)

COS1512 Assignment 4 2023 (ANSWERS)

 14 views  0 purchase

COS1512 Assignment % TRUSTED workings with detailed Answers for A+ Grade.

Preview 3 out of 20  pages

  • September 6, 2023
  • 20
  • 2023/2024
  • Exam (elaborations)
  • Questions & answers
All documents for this subject (39)
avatar-seller
StudyStore
COS1512
ASSIGNMENT 4 SEMESTER 2 2023

, COS1512 ASSIGNMENT 4 2023




TUTORIAL MATTER: Chapters 14, 15 and 17 of the Study Guide
(Tutorial Letter 102 available under Additional
Resources on the COS1512 website on
myUnisa )
Chapters 14 (excluding section 14.3), 15
(excluding sections 15.2 and 15.3) and 17 of
Savitch

WEIGHT: 25%
QUIZ AVAILABLE: 26 August 2023
DUE DATE: 18 September 2022
CUT-OFF DATE 18 September 2022



NB: This assignment consists of two parts:

• a part where you write and implement program code (this part) and
• an MCQ part where you answer questions on the code you have written,
and the material covered in this assignment.
The MCQ part of the assignment will be available in the Assessment Shell for
Assignment 4 on the myModules site for COS1512.
You will not be able to do the MCQ part unless you have completed the
coding part.


Question 1
The program below contains an incomplete recursive functionraised_to_power().
The function returns the value of the first parameternumber of type float raised to
the value of the second parameterpower of type int for all values of power greater
than or equal to 0.


The algorithm used in this question to write a recursive function to raise a float
value number to a positive power uses repeated multiplication as follows:
numberpower =1 if power = 0

= number x numberpower-1 otherwise

In other words, number raised to power gives 1 if power is 0;




Open Rubric

, and otherwise numberpower can be calculated with the formula:
number x numberpower-1


1. #include <iostream>using namespace std;
2. float raised_to_power( )
3. {
4. if (power < 0)
5. {
6. cout << "\nError - can't raise to a negative power\n";
7. exit(1);
9. }
10. else if ( )
11. return ( );
12. else
13. return (number * raised_to_power(number, power - 1));
14. }
15. main()
16.
17. float answer = raised_to_power(4.0,3);
18. cout << answer;
19. return 0;
20.}

(a) Complete the function header in line 3.
(b) Using the fact that any value raised to the power of 0 is 1, complete the base
case in line 10 and 11.
(c) Why do we need a base case in a recursive function?
(d) What is the purpose of the general case?


Question 2
Examine the code fragment below and answer the questions that follow:


1: #include <iostream>
2: using namespace std;
3:
4: //
5:
6: class A
7: {
8: private:
9: int x;
10: protected:
11: int getX();
12: public:
13: void setX();
14: };

The benefits of buying summaries with Stuvia:

Guaranteed quality through customer reviews

Guaranteed quality through customer reviews

Stuvia customers have reviewed more than 700,000 summaries. This how you know that you are buying the best documents.

Quick and easy check-out

Quick and easy check-out

You can quickly pay through EFT, credit card or Stuvia-credit for the summaries. There is no membership needed.

Focus on what matters

Focus on what matters

Your fellow students write the study notes themselves, which is why the documents are always reliable and up-to-date. This ensures you quickly get to the core!

Frequently asked questions

What do I get when I buy this document?

You get a PDF, available immediately after your purchase. The purchased document is accessible anytime, anywhere and indefinitely through your profile.

Satisfaction guarantee: how does it work?

Our satisfaction guarantee ensures that you always find a study document that suits you well. You fill out a form, and our customer service team takes care of the rest.

Who am I buying this summary from?

Stuvia is a marketplace, so you are not buying this document from us, but from seller StudyStore. Stuvia facilitates payment to the seller.

Will I be stuck with a subscription?

No, you only buy this summary for R51,54. You're not tied to anything after your purchase.

Can Stuvia be trusted?

4.6 stars on Google & Trustpilot (+1000 reviews)

80461 documents were sold in the last 30 days

Founded in 2010, the go-to place to buy summaries for 14 years now

Start selling
R51,54
  • (0)
  Buy now