Introduction to Programming II COS1512 Assignment Number 4 Student Number: 38711443 Question 1 (COS1512_Assignment4_38711443_Question1_Original.cpp) C++ Code: //A recursive function is a function that calls itself until a certain condition is met. The //program below contains a recursive function count(). Run the program and answer //the questions below: #include <iostream> using namespace std; void count(int counter) { if (counter == 0) return; else { cout << "Calling counter(" <<counter << ")" << endl; count(--counter); cout << "counter(" << counter << ") processed " <<endl; return; } } int main() { int i = 3; count(i); return 0; } Questions: (a) What is the output displayed on the console when the main program is executed? (b) What is the purpose of the base case? This is the case or statements that do not have any recursive calls, and are easily solved. (c) What is the purpose of the general case? This is the case or statements where the recursion happens, and the recursion continues until the base case is resolved. (d) Identify the base case in the recursive function count(). (counter == 0) return; (e) Identify the general case in the recursive function count(). cout << "Calling counter(" <<counter << ")" << endl; count(--counter); cout << "counter(" << counter << ") processed " <<endl; return; (f) Modify the recursive function count()so that it displays a countdown to 0 from the parameter sent to the function. I.e. modify the function count() so that for the call (COS1512_Assignment4_38711443_Question1.cpp) void count(int counter) { if (counter == 0) { cout << counter << endl; return; } else { cout << counter << endl; count(--counter); return; } } Question 2 (COS1512_Assignment4_38711443_Question2.cpp) C++ Code: #include <iostream> using namespace std; //-----------------------------------------class A { private: int x; protected: int getX(); public: void setX(); }; int A::getX() { return x; } void A::setX() { x=10; } //---------------------------------------------class B { private: int y; protected: A objA; int getY(); public: void setY(); }; void B::setY() { y=24; int a = objA.getX(); } //---------------------------------------------class C: public A { protected: int z; public: int getZ(); void setZ(); }; int C::getZ() { return z; } void C::setZ() { z=65; } Questions: //Examine the code fragment below and answer the questions that follow: //Answer the following questions based on the code fragment given above: (a) Is line 18 a valid access? Justify your answer. Yes that is valid but will return the value of x from Class A as defined. (b) Is line 32 a valid statement? Justify your answer. No as Class B objects cant access the protected member function in parent class. (c) Identify another invalid access statement in the code. void C::setZ() { z=65; } (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. Public int getZ(); void setZ(); void setX(); Protected: int getX(); int z; Private: (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. Public int getZ(); void setZ(); Protected: void setX(); int getX(); int z; Private: Question 3 (COS1512_Assignment4_38711443_Question3_Original.cpp) C++ Code: //Question 3 //Consider the class definition below and answer the questions that follow: //Note: The class definition is also called the class specification or the interface for the class. class Marks { public: Marks(); Marks (string name, string number, int asg1, int asg2, int asg3, double test); double calcMark() const; private: string stdtName; string stdNumber; int[3] assignments; double testMark; } int FinalMark::calcMark() const { return int (0.2 * testMark + (0.1 * (assignments[1] + assignments[2] + assignments[3])/3) + 0.7 * examMark); } C++ Code: Updated and Final Code Question 3 //Question 3 //Consider the class definition below and answer the questions that follow: //Note: The class definition is also called the class specification or the interface for the class. //(a) Implement the class Marks and test it in a driver program. //(b) Derive a class FinalMark from class Marks. This class has an additional member variable examMark that holds the examination mark. The class should override member function calcMark() to calculate a final mark that includes the three assignments, the test mark and the examination mark. //(c) Implement the overloaded constructor for the class FinalMark by invoking the base class constructor. //(d) Implement the member function calcMark() for the derived class FinalMark. The test mark contributes 20% to the final mark, the average of the three assignments contributes 10% and the examination mark contributes 70% to the final mark. Note that function calcMark() must return an integer result. //(e) Test your class FinalMark in a driver program that include the following instantiation: //FinalMark myMark; //Include a statement to invoke the version of calcMark()provided in class Marks for object myMark and display the name and student number with the mark. //Use another statement to invoke the version of calcMark() defined in the derived class FinalMark and again display the name and student number with the mark. //(f) Indicate whether the following statement is valid or invalid. Give a reason for your answer: //If testmark was a protected member variable of Marks, the following statements embedded in a complete main program would be legal: //Marks PetersMarks; //cin >> PetersMarks.testMark; #include <iostream> using namespace std; class Marks { public: //constructors Marks(); Marks (string name, string number, int asg1, int asg2, int asg3, double test); //Constant functions double calcMark() const; double getAssignmentMarks() const; string getName() const; string getStudentNumber()const; private: //member variables string stdtName; string stdNumber; int assignments[3]; protected: double testMark; }; //Default Constructor initializing variables Marks :: Marks() { stdtName = "Unknown"; stdNumber = "NA123"; assignments[0] = 0; assignments[1]= 0; assignments[2] = 0; testMark = 0; } //Parameterized Constructor initializing variables Marks :: Marks(string name, string number, int asg1, int asg2, int asg3, double test) { stdtName = name; stdNumber = number; assignments[0] = asg1; assignments[1]= asg2; assignments[2] = asg3; testMark = test; } //Function provides the net assignment marks double Marks :: getAssignmentMarks() const{ return assignments[0] + assignments[1] + assignments[2]; }; //Function provides the test marks value double Marks :: calcMark() const{ return testMark; } //Function provides the student name string Marks::getName() const{ return stdtName; } //Function provides the student number string Marks::getStudentNumber() const{ return stdNumber; } //Dervied class Final Mark, Public inheritance from Base Class Marks //Question 3b class FinalMark : public Marks { double examMark; public: FinalMark(): Marks{} { examMark = 0; } //Parameterized Constructor calling Marks constructor as part of initialisation //Question 3c FinalMark(string name, string number, int asg1, int asg2, int asg3, double test, double eMark) :Marks{name, number, asg1, asg2, asg3, test} { examMark = eMark; } //Override Function in derived class //Question 3d int calcMark() { double testValue = 0.2 * Marks::calcMark(); double avgassignment = (Marks::getAssignmentMarks())/3.0; double value = testValue + avgassignment * 0.1 + 0.7 * examMark; //Converting result value to integer return int(value); } }; //Question 3e int main() { // Marks class object Marks marks; cout << "Marks object initialised with the default constructor" << endl; cout<<marks.getName() <<" has scored "<<marks.calcMark() << " marks\n"; cout << endl; //FinalMark class object FinalMark myMark; cout << "Marks object initialised with the default constructor" << endl; cout << "FinalMark object using the base class CalcMark() Double Function" << endl; cout<<myMark.getName() <<" "<< myMark.getStudentNumber() <<" has scored "<< myMark.Marks::calcMark() << " marks\n"; cout << endl; //parameterized FinalMark object cout << "Marks object initialised with the default constructor" << endl; FinalMark newMark("Mogamad Allie Saal", "38711443", 60, 87, 98, 83.1, 87); cout << "FinalMark object using the base class CalcMark() Double Function" << endl; cout<<newMark.getName() <<" "<< newMark.getStudentNumber() <<" has scored "<< newMark.Marks::calcMark() << " marks\n"; cout << "FinalMark object using the derived class CalcMark() Int Function" << endl; cout<<newMark.getName()<< " "<< newMark.getStudentNumber() << " has net score of "<<newMark.calcMark() << " marks"; cout << endl; //Question 3f is not allows as testmark is protected within this context // Marks PetersMarks; // cin >> PetersMarks.testMark; // cout<<PetersMarks.getName() <<" "<< PetersMarks.getStudentNumber()<<" has scored "<<PetersMarks.calcMark() << " marks\n"; return 0; } Output: Question 4 (COS1512_Assignment4_38711443_Question4.cpp) C++ Code: //COS1512_Assignment4_38711443_Question4.cpp //(a) Write a function called count() to determine the number of times a specific value occurs in a vector of integers. The function should receive two value parameters: the vector //to be searched (v) and the value to search for (val). The function count() should return the number of times val occurs in vector v. //Test your function count in a program by declaring a vector and initializing it, and then call function count to determine how many times a specific value occurs in the vector. // //(b) Write a template version of the function count to determine the number of times a specific value occurs in a vector of any base type and test it by declaring two //or more vectors with different base types and determining how many times a specific value occurs in these two vectors. #include <iostream> #include <algorithm> #include <vector> using namespace std; //Function declaration //Standard count function int count(vector<int> vi1, int s) { int n = 0; int l = vi1.size(); for(int i = 0; i < l; i++) { if (s == vi1[i]) { n++; } } return n; }; //Template function //Using count function but for any variable type template<typename T> int countT(vector<T> vi1, T s) { int n = 0; int l = vi1.size(); for(int i = 0; i < l; i++) { if (s == vi1[i]) { n++; } } return n; }; int main() { vector<int> vec_int = {1,4,7,8,2,3,1,5,6,21,1,35,1,3}; vector<char> vec_char = {'a','b','a','c','a','d','a','e','a','f','a','g','a','h'}; vector<string> vec_string = {"Hi","Hill","Hi","Hello","Hi","High"}; //Using standard vector count function from C++ int num = 1; int count_vec_int = count(vec_int.begin(), vec_int.end(), num); cout << "Number of " << num << ": " << count_vec_int << endl; //Using user-defined count function from C++ count_vec_int=0; count_vec_int= count(vec_int,num); cout << "Number of " << num << ": " << count_vec_int << endl; //Using user-defined template count function for int from C++ count_vec_int=0; count_vec_int= countT<int>(vec_int,num); cout << "Number of " << num << ": " << count_vec_int << endl; //Using user-defined template count function for char from C++ count_vec_int=0; char searchc='a'; count_vec_int= countT<char>(vec_char,searchc); cout << "Number of " << searchc << ": " << count_vec_int << endl; //Using user-defined template count function for string from C++ count_vec_int=0; string searchs = "Hi"; count_vec_int= countT<string>(vec_string,searchs); cout << "Number of " << searchs << ": " << count_vec_int << endl; return 0; } Output: Question 5 (COS1512_Assignment4_38711443_Question5) C++ Code: Dictionary.h #ifndef DICTIONARY_H #define DICTIONARY_H #include <vector> #include <string> #include <iostream> #include <string.h> using namespace std; template <class T, class S> class Dictionary { public: Dictionary() { vector<T> keys={}; vector<S> values ={}; }; Dictionary(vector<T> k,vector<S> v) { keys = k; values = v; }; void add(T key, const S &value) { keys.push_back(key); values.push_back(value); }; S find (T key) const { string value = " "; for (unsigned int i = 0; i < keys.size(); i++) { //if (key == keys[i]) if (key.compare(keys[i]) == 0) { value = values[i]; } if (value == " ") value= "no such key can be found"; } return value; }; void display() { for (unsigned int i = 0; i < keys.size(); i++) cout << keys[i] << ' ' << values[i] << endl; return; }; private: vector<T> keys; vector<S> values; }; #endif // DICTIONARY_H Dictionary.cpp #include "Dictionary.h" #include <vector> #include <iostream> using namespace std; main.cpp #include <iostream> #include <cstdlib> #include "Dictionary.h" #include <vector> using namespace std; int main() { //Made both strings are this caters for anything the user enters. string part; string key; //add 4 values to the parts dictionary Dictionary <string,string> parts; 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: "; cin.clear(); cin.sync(); getline(cin, part); cout << "Key for part name: "; cin.clear(); cin.sync(); getline(cin, key); parts.add(key, part); } cout << endl; parts.display(); cout << endl; //find the part for a key cout << "For which key do you want to find the part? "; cin.clear(); cin.sync(); getline(cin, key); cout << "The part for key " << key << " is "; cout << parts.find(key) << endl; return 0; } Output: Question 5 Please see attached PDF document. COS1512: Reflection on doing Assignment 4 (2022) This questionnaire requires you to reflect on your learning experiences while doing this assignment. It is designed to help you take an active role in monitoring your learning. The purpose of reflection on your learning is to answer the question “What would I change to make my work better?” Please complete this questionnaire after now that you have done Assignment 4. It will take about 10 minutes to complete the questionnaire. Thank you! 1. What is your student number? * 38711443 2. How challenging did you find this assignment? * Very easy Easy It was okay Difficult Very difficult 3. How much time did you spent in thinking or planning while doing this assignment? * Less than an hour 1 to 5 hours 6 to 8 hours 9 to 12 hours Other 4. How much time did you spent programming while doing this assignment? * 3 to 5 hours 6 to 8 hours 9 to 12 hours 13 to 15 hours Other 5. How much time did you spent in total on doing this assignment? * 1 to 10hours 11 to 20 hours 21 to 30 hours 31 to 40 hours 41 to 50 hours Other 6. What surprised you while doing this assignment, and why? * The new method of processing the assignment 7. What is the most important thing you learned doing this assignment, and why do you think so? * Inheritance and Templates 8. What do you wish you knew before starting the assignment? * Templates and Vectors 9. What do you want to learn more about, and why? * Templates and Vectors 10. When were you at your best doing this assignment, and why? * Inheritance 11. When were you the most creative, and why do you think that is? * Templates 12. What made you curious doing this assignment? How does learning feel different when you’re curious? * For me it was more frustration than curious 13. What would you change if you had to do this assignment again? * Spending more time on assignment and less on work. 14. Please provide feedback on the format of the online assessment for Assignment 4. * Dont like the moodle format. 15. Was the time allowed for Assignment 4 * Too litte time, I could not complete the assessment in time Enough time, I could comfortably complete the assessment in the time allowed More than enough time, I completed the assessment long before the time was up 16. Anything else you want to tell the lecturer about Assignment 4? * Just wish the e-tutors can be more helpful, when doing the reviews and provide examples tha This content is created by the owner of the form. The data you submit will be sent to the form owner. Microsoft is not responsible for the privacy or security practices of its customers, including those of this form owner. Never give out your password. Powered by Microsoft Forms | Privacy and cookies | Terms of use