Garantie de satisfaction à 100% Disponible immédiatement après paiement En ligne et en PDF Tu n'es attaché à rien
logo-home
COS1512 Assignment 4 memo 2024 MCQ and code 6,82 €   Ajouter au panier

Autre

COS1512 Assignment 4 memo 2024 MCQ and code

 6 vues  0 achat
  • Cours
  • Établissement

COS1512 Assignment 4 memo 2024 MCQ and code 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 t...

[Montrer plus]

Aperçu 5 sur 69  pages

  • 13 septembre 2024
  • 69
  • 2024/2025
  • Autre
  • Inconnu
avatar-seller
COS1512 2024 Assignment 4 memo

Crystal Indigo!
Crystal Indigo!
Providing all solutions you need anytime
+27 76 626 8187




All the code and answered MCQ ready to submit and ready
for MCQ
NB: This assignment consists of two parts:
• a part where you write and implement program code, which is provided in this
document in full and
• an MCQ part where you answer questions on the code you have written,
which is provided in this document in full

,Question 1
The program below contains an incomplete recursive function raised_to_power(). The
function returns the value of the first parameter number of type float raised to the value of the
second parameter power 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; and otherwise numberpower can
be calculated with the formula: number x numberpower -1

(a) Complete the function header in line 2. //see in the code below.

(b) Using the fact that any value raised to the power of 0 is 1, complete the base case in line 10
and 11. //see in the code below.

(c) Why do we need a base case in a recursive function?
Because, the base case provides a stopping condition for the recursion. Without it, the
function would keep calling itself indefinitely, leading to a stack overflow. The base case is
the simplest scenario where we can directly return a result without further recursive calls.
(d) What is the purpose of the general case?
The general case generate the recursive calls which should eventually result in a call that
cause the base case to execute and end the recursion. It handles all other scenarios not
covered by the base case. It breaks down the problem into smaller subproblems and makes
recursive calls to solve these subproblems. In this case, it multiplies the number by itself
raised to power - 1, gradually reducing the problem until it reaches the base case.
1 #include <iostream>
2
3 using namespace std;
4
5 //(a)Complete the function header in line 2
6 float raised_to_power(float number, int power) {
7 if (power < 0) {
8 cout << "\nError - can't raise to a negative power\n";
9 exit(1);
10 } else if (power == 0) {//(b)
11 return 1.0;
12 } else {
13 return number * raised_to_power(number, power - 1);
14 }
15 }
16
17 int main() {
18 float answer = raised_to_power(4.0, 3);
19 cout << answer<<endl;
20 return 0;

,Question 2
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: };
15:
16: int A::getX()
17: {
18: return x;
19: }
20:
21: void A::setX()
22: {
23: x=10;
24: }
25:
26://
27: class B
28: {
29: private:
30: int y;

,31: protected:
32: A objA;
33: int getY();
34: public:
35: void setY();
37: };
38:
39: void B::setY()
40: {
41: y=24;
42: int a = objA.getX();
43: }
44:
45://
46:
47: class C: public A
48: {
49: protected:
50: int z;
51: public:
52: int getZ();
53: void setZ();
54: };
55:
56: int C::getZ()
57: {
58: return z;
59: }
60:
61: void C::setZ()
62: {
63: z=65;

, 64: }
(a) Is line 18 a valid access? Justify your answer.
It's inside the getX() member function of class A, which has access to the private member
x. x is a private data member of the class A and therefore it can only be accessed by other
member functions and operators of the class A.
(b) Is line 32 a valid statement? Justify your answer.
An object of class A is created in class B as objA. Since class B has access to protected
members of class A, it is able to use them.

(c) Identify another invalid access statement in the code.
Another invalid access statement in the code is on line 42: int a = objA.getX();
This is invalid because getX() is a protected member of class A, and class B is not derived
from A, so it cannot access getX().

(d) Class C has public inheritance with the class A. Identify and list class C’s private, protected
and public member variables resulting from the inheritance.
With public inheritance, the public and protected members of the base class A are inherited
as public and protected members of the derived class C.
Private data members or member functions resulting from the inheritance: None
Protected data members or member functions resulting from the inheritance: getX()
Public data members or member functions resulting from the inheritance: setX()
(e) If class C had protected inheritance with the class A, identify and list class C’s private,
protected and public members variables resulting from the inheritance.
With protected inheritance, public and protected members of the base class become
protected members of the derived class.
Private data members or member functions resulting from the inheritance: None
Protected data members or member functions resulting from the inheritance: setX() and
getX()
Public data members or member functions resulting from the inheritance: None



Question 3
(a) Implement the class InsurancePolicy.
insurancepolicy.h
#ifndef INSURANCE_H
#define INSURANCE_H
#include <iostream>

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 CrystalIndigo. Stuvia facilite les paiements au vendeur.

Est-ce que j'aurai un abonnement?

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

Peut-on faire confiance à Stuvia ?

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

74534 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!
6,82 €
  • (0)
  Ajouter