100% tevredenheidsgarantie Direct beschikbaar na je betaling Lees online óf als PDF Geen vaste maandelijkse kosten 4.2 TrustPilot
logo-home
Tentamen (uitwerkingen)

COS1512 ASSIGNMENT 4 2023.

Beoordeling
-
Verkocht
15
Pagina's
23
Cijfer
A+
Geüpload op
05-08-2023
Geschreven in
2023/2024

COS1512 ASSIGNMENT 4 2023.Detailed working, solutions, memo, notes & explanations.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 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 powern"; 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: }; 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: } Answer the following questions based on the code fragment given above: (a) Is line 18 a valid access? Justify your answer. (b) Is line 32 a valid statement? Justify your answer. (c) Identify another invalid access statement in the code. (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. (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. Question 3 Consider the class definition below and answer the questions that follow: class InsurancePolicy { public: InsurancePolicy(); InsurancePolicy(int pNr, string pHolder, double aRate); ~InsurancePolicy(); void setPolicy(int pNr, string pHolder, double aRate); int get_pNr()const; string get_pHolder()const; double get_aRate()const; private: int policyNr; string policyHolder; double annualRate; }; (a) Implement the class InsurancePolicy. (b) Code the interface for a class CarInsurance derived from class InsurancePolicy (the base class). This class has an additional member variable, excess. Class InsurancePolicy also has member functions, get_excess() and set_excess()to return the value of member variable excess and update the value of member variable excess respectively. The class CarInsurance should override function showPolicy() in order to display the member variables of CarInsurance and also override member function setPolicy() in order to update the member variables of CarInsurance. (c) Implement the class CarInsurance and use the code below to implement setPolicy(): void CarInsurance:: setPolicy(int pNr, string pHolder, double aRate, double eValue) { policyNr = pNr; policyholder = pHolder; annualRate = aRate; excess = eValue; } You should obtain the following errors: Explain why setPolicy()is not a legal definition in the derived class CarInsurance? Suggest two ways to fix this problem. (d) Add a member function void showPolicy(ostream & out)const; to the class InsurancePolicy as well as to the class CarInsurance in order to display the member variables of InsurancePolicy and CarInsurance. (e) Use the following driver program to test your classes InsurancePolicy and CarInsurance: #include <iostream> #include <fstream> #include "Insurance.h" #include "CarInsurance.h" using namespace std; int main() { InsurancePolicy myPolicy(, "Peter Molema", 3450.67); CarInsurance yourPolicy(, "Wilson Ntemba", 5550.67, 15000.00); (ios::fixed); (ios::showpoint); sion(2); myPPolicy(cout); cout << endl; yourPPolicy(cout); cout << endl << "AFTER UPDATES:" << endl; myPPolicy(, "Peter Molema", 5450.67); yourPPolicy(,"WilsonNtemba",6650.67, 25000.00); myPPolicy(cout); cout << endl; yourPPolicy(cout); cout << endl; return 0; } Question 4 (a) Write a function called found() to determine whether a specific value occurs in a vector of integers. The function should receive two parameters: the vector to be searched (v) and the value to search for (val). The function found() should return a Boolean value to indicate whether or not val occurs in vector v. Test your function found() in a program by declaring a vector and initializing it, and then call function found() to determine whether a specific value occurs in the vector. (b) Write a template version of the function found() to determine whether a specific value occurs in a vector of any base type. Test your template function by declaring two or more vectors with different base types and determining whether specific values occurs in these two vectors. Question 5 Many application programs use a data structure called a dictionary in which one can use a key value to retrieve its associated data value. For example, we might want to associate automobile part numbers with the names of the corresponding parts: Key Value tire wheel distributor air filter The following class interface presents an approach to implementing the above scenario: class Dictionary { public: Dictionary(); void Add(int key, const string &value); string Find (int key) const; private: vector<int> Keys; vector<string> Values; }; The class Dictionary has the following operations (member functions): • Add() - adds a new key and value to the dictionary • Find()- retrieves the corresponding value for that particular key, for example Find() would return “distributor”. Consider the following implementation of the class Dictionary and convert it into a template class. In other words, re-design the Dictionary interface so that it may be used to create a Dictionary containing keys and values of any type. For instance, the value could be of type double, whereas the key could be of type char. Note the key and value may be most likely of different types hence we need two different template arguments to be supplied. Also test your template class by declaring two objects of template class Dictionary with different template arguments. Dictionary.h #ifndef DICTIONARY_H #define DICTIONARY_H #include <vector> #include <string> #include <iostream> using namespace std; class Dictionary { public: Dictionary(); void add(int key, const string &value); string find (int key) const; void display(); private: vector<int> keys; vector<string> values; }; #endif // DICTIONARY_H D #include "Dictionary.h" #include <vector> #include <iostream> using namespace std; Dictionary::Dictionary() { //nothing to do, vector member variables are empty on //declaration }; void Dictionary::add(int key, const string &value) { _back(key); _back(value); } string Dictionary::find (int key) const { string value = " "; for (unsigned int i = 0; i < (); i++) if (key == keys[i]) value = values[i]; if (value == " ") return "no such key can be found"; else return value; } void Dictionary::display() { for (unsigned int i = 0; i < (); i++) cout << keys[i] << ' ' << values[i] << endl; return; } M #include <iostream> #include <cstdlib> #include "Dictionary.h" #include <vector> using namespace std; int main() { Dictionary parts; string part; int key; //add 4 values to the parts dictionary for (int i = 0; i <= 3; i++) { cout << "Please enter a part name and a key to add " << "to the parts dictionary." << endl; cout << "Part name: "; getline(cin, part); cout << "Key for part name: "; cin >> key; (key, part); (); } cout << endl; ay(); cout << endl; //find the part for a key cout << "For which key do you want to find the part? "; cin >> key; cout << "The part for key " << key << " is "; cout << (key) << endl; return 0; }

Meer zien Lees minder
Instelling
Vak








Oeps! We kunnen je document nu niet laden. Probeer het nog eens of neem contact op met support.

Geschreven voor

Instelling
Vak

Documentinformatie

Geüpload op
5 augustus 2023
Aantal pagina's
23
Geschreven in
2023/2024
Type
Tentamen (uitwerkingen)
Bevat
Vragen en antwoorden

Onderwerpen

Maak kennis met de verkoper

Seller avatar
De reputatie van een verkoper is gebaseerd op het aantal documenten dat iemand tegen betaling verkocht heeft en de beoordelingen die voor die items ontvangen zijn. Er zijn drie niveau’s te onderscheiden: brons, zilver en goud. Hoe beter de reputatie, hoe meer de kwaliteit van zijn of haar werk te vertrouwen is.
ExellentStudyResources Chamberlain College Of Nursing
Volgen Je moet ingelogd zijn om studenten of vakken te kunnen volgen
Verkocht
1091
Lid sinds
4 jaar
Aantal volgers
917
Documenten
2077
Laatst verkocht
2 dagen geleden

3,6

146 beoordelingen

5
66
4
19
3
30
2
4
1
27

Recent door jou bekeken

Waarom studenten kiezen voor Stuvia

Gemaakt door medestudenten, geverifieerd door reviews

Kwaliteit die je kunt vertrouwen: geschreven door studenten die slaagden en beoordeeld door anderen die dit document gebruikten.

Niet tevreden? Kies een ander document

Geen zorgen! Je kunt voor hetzelfde geld direct een ander document kiezen dat beter past bij wat je zoekt.

Betaal zoals je wilt, start meteen met leren

Geen abonnement, geen verplichtingen. Betaal zoals je gewend bent via Bancontact, iDeal of creditcard en download je PDF-document meteen.

Student with book image

“Gekocht, gedownload en geslaagd. Zo eenvoudig kan het zijn.”

Alisha Student

Veelgestelde vragen