Garantie de satisfaction à 100% Disponible immédiatement après paiement En ligne et en PDF Tu n'es attaché à rien
logo-home
COS1512 Assignment (DETAILED ANSWERS) 4 2023 - DUE 18 September 2022 €2,50   Ajouter au panier

Examen

COS1512 Assignment (DETAILED ANSWERS) 4 2023 - DUE 18 September 2022

 17 vues  2 achats
  • Cours
  • Établissement

COS1512 Assignment (DETAILED ANSWERS) 4 2023 - DUE 18 September 2022 100% TRUSTED workings, explanations and solutions. For assistance call or us on . Question 1 The program below contains an incomplete recursive function raised_to_power(). The function returns the value of the first paramet...

[Montrer plus]

Aperçu 3 sur 20  pages

  • 17 août 2023
  • 20
  • 2023/2024
  • Examen
  • Questions et réponses
avatar-seller
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: };

Les avantages d'acheter des résumés chez Stuvia:

Qualité garantie par les avis des clients

Qualité garantie par les avis des clients

Les clients de Stuvia ont évalués plus de 700 000 résumés. C'est comme ça que vous savez que vous achetez les meilleurs documents.

L’achat facile et rapide

L’achat facile et rapide

Vous pouvez payer rapidement avec iDeal, carte de crédit ou Stuvia-crédit pour les résumés. Il n'y a pas d'adhésion nécessaire.

Focus sur l’essentiel

Focus sur l’essentiel

Vos camarades écrivent eux-mêmes les notes d’étude, c’est pourquoi les documents sont toujours fiables et à jour. Cela garantit que vous arrivez rapidement au coeur du matériel.

Foire aux questions

Qu'est-ce que j'obtiens en achetant ce document ?

Vous obtenez un PDF, disponible immédiatement après votre achat. Le document acheté est accessible à tout moment, n'importe où et indéfiniment via votre profil.

Garantie de remboursement : comment ça marche ?

Notre garantie de satisfaction garantit que vous trouverez toujours un document d'étude qui vous convient. Vous remplissez un formulaire et notre équipe du service client s'occupe du reste.

Auprès de qui est-ce que j'achète ce résumé ?

Stuvia est une place de marché. Alors, vous n'achetez donc pas ce document chez nous, mais auprès du vendeur LIBRARYpro. Stuvia facilite les paiements au vendeur.

Est-ce que j'aurai un abonnement?

Non, vous n'achetez ce résumé que pour €2,50. Vous n'êtes lié à rien après votre achat.

Peut-on faire confiance à Stuvia ?

4.6 étoiles sur Google & Trustpilot (+1000 avis)

73314 résumés ont été vendus ces 30 derniers jours

Fondée en 2010, la référence pour acheter des résumés depuis déjà 14 ans

Commencez à vendre!
€2,50  2x  vendu
  • (0)
  Ajouter