SEHS2042 18-19 sem 2 exam

cpp

School

The University of Hong Kong *

*We aren’t endorsed by this school

Course

3414

Subject

Computer Science

Date

Nov 24, 2024

Type

cpp

Pages

7

Uploaded by zqweo

Report
#include <iostream> #include<cstdlib> #include<ctime> #include<iomanip> #include<cstring> #include<cmath> #define _CRT_SECURE_NO_WARNINGS using namespace std; void QB1() { //case1 } void QB2a() { //case2 int m = 5; for (int x = 100; x > 50; x -= 10) { cout << x << " "; do { m += 10; } while (m < x); } } void QB2b() { //case3 int n1, n2, lcm; cout << "Input 2 numbers: "; cin >> n1 >> n2; //Your codes for B2(b) should be inserted there // Ensure that nl is not larger than n2 if (n1 > n2) { int temp = n1; n1 = n2; n2 = temp; } // Calculate the LCM of nl and n2 for (int i = n2; i <= n1 * n2; i += n2) { if (i % n1 == 0 && i % n2 == 0) { lcm = i; break; } } cout << "LCM of " << n1 << " and " << n2 << " is " << lcm; } //QB3a void fn(int x) { if (x < 10) cout << x; else if (x < 16) cout << (char)('A' + (x - 10)); else {
fn(x / 16); fn(x % 16); } } void QB3a() { // case 4 fn(271); } //QB3b // Your codes for B3(b) should be inserted here void QB3b() { //case5 int a = 3, b = 5; cout << b / a; } //QB4 // Your codes for Question B4 should be inserted here class Triangle { private: int s1, s2, s3; // Length of the three sides public: // Constructor Triangle(int side1, int side2, int side3) { s1 = side1; s2 = side2; s3 = side3; } // Member function to check if the triangle is valid bool valid() { if (s1 + s2 > s3 && s1 + s3 > s2 && s2 + s3 > s1) return true; else return false; } // Member function to calculate the area of the triangle double area() { double p = double (s1 + s2 + s3) / 2.0; double area = sqrt(p * (p - s1) * (p - s2) * (p - s3)); return area; } }; void QB4() { //case6; } //QB5 void countFreq(int freqTbl[]) { // Your codes for B5(a) should be inserted here int input;
while (cin >> input && input != 0) { if (input >= 1 && input <= 99) { int index = (input - 1) / 10; freqTbl[index]++; } } } void printTable(int freqTbl[]){ // Your codes for B5(b) should be inserted here for (int i = 0; i < 10; i++) { if (freqTbl[i] > 0) { cout << "[ " << i * 10 << "-" << (i + 1) * 10 - 1 << ") | "; for (int j = 0; j < freqTbl[i]; j++) { cout << "*"; } cout << endl; } } } void QB5() { //case 7 int freqTbl[10] = {}; cout << "Input data(end with 0) :\n"; countFreq(freqTbl); cout << "Frequency Table:\n"; printTable(freqTbl); } //QB6 int common(char* sl, char* s2) { int count = 0; // Your codes for B6(a) should be inserted here while (*sl != '\0' && *s2 != '\0' && *sl == *s2) { count++; sl++; s2++; } return count; } char* longestCommon(char* sl, char* s2) { char* match = new char[100]; *match = '\0'; // Your codes for B6(b) should be inserted here int maxLength = 0; int slLength = strlen(sl); int s2Length = strlen(s2); for (int i = 0; i < slLength; i++) { for (int j = 0; j < s2Length; j++) { int commonLength = common(sl + i, s2 + j);
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help
if (commonLength > maxLength) { maxLength = commonLength; *(match + maxLength) = '\0'; } } } return match; } void QB6() { //case8 char* sl = new char[100], * s2=new char[100], * s3; cout << "Input 2 strings:\n"; cin.ignore(); cin.getline(sl, 100, '\n'); cin.ignore(); cin.getline(s2, 100, '\n'); s3 = longestCommon(sl, s2); cout << "Longest Common Substr is \";" << s3 << "\""; } //QC1 const int MAX_SIZE = 50; class Student { public: // Default constructor Student() { id = 0; name = ""; age = 0; } // Constructor with arguments Student(int studentId, string studentName, int studentAge) { id = studentId; name = studentName; age = studentAge; } // Display student's info void display() { cout << id << ":" << name << " "; } // Check if students is the same as the current object bool equal(Student s) { return (id == s.id && name == s.name && age == s.age); } // Compare the age of students with the current object int compare(Student s) { if (age < s.age) { return -1; } else if (age > s.age) { return 1;
} else { return 0; } } private: int id; string name; int age; }; class List { public: List() { size = 0; } // Display the list of student's info void display() { cout << size << " student(s): "; for (int i = 0; i < size; i++) { list[i].display(); } cout << endl; } // Search the students in the list int find_student(Student s) { for (int i = 0; i < size; i++) { if (list[i].equal(s)) { return i; } } return -1; } // Add students at the end of the list void add_student(Student s) { if (size == MAX_SIZE - 1) { cout << "List is full. No student is added.\n"; } else if (find_student(s) != -1) { cout << "Already in the list. No student is added.\n"; } else { list[size++] = s; } } // Remove students from the list void remove_student(Student s) { int index = find_student(s); if (index != -1) { for (int i = index; i < size - 1; i++) { list[i] = list[i + 1]; } size--; }
else { cout << "Student not found.\n"; } } // Sort the list by student's age void sort_age() { } // Reverse the list void reverse() { int start = 0; int end = size - 1; while (start < end) { Student temp = list[start]; list[start] = list[end]; list[end] = temp; start++; end--; } } private: Student list[MAX_SIZE]; int size; }; void QC1() { // case 9 // Student Objects Student alice(12, "Alice", 18), bob(21, "Bob", 20), cathy(34, "Cathy", 21), david(46, "David", 16), emily(52, "Emily", 15), fed(63, "Fed", 17), george(78, "George", 19); // Setup the list List list; list.add_student(alice); list.add_student(bob); list.add_student(cathy); list.add_student(david); list.add_student(emily); // List operation cout << "The list is:\n"; list.display(); cout << "\nAdd Fed. The list becomes:\n"; list.add_student(fed); list.display(); cout << "\nTry to remove George. "; list.remove_student(george); cout << "\nRemove David. The list becomes:\n"; list.remove_student(david); list.display();
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help
cout << "\nSort by age. The list becomes: \n"; list.sort_age(); list.display(); cout << "\nReverse the list:\n"; list.reverse(); list.display(); } void QC2() { //case10 } // IMPORTANT: DO NOT MODIFY main() int main() { char prog_choice; do { cout << "\n\n"; cout << "Program Selection Menu" << endl; cout << "---------------------------------------" << endl; cout << "Enter question number ('q' to quit): "; cin >> prog_choice; cout << "\n\n"; switch (prog_choice) { case '1': QB1(); break; case '2': QB2a(); break; case '3': QB2b(); break; case '4': QB3a(); break; case '5': QB3b(); break; case '6': QB4(); break; case '7': QB5(); break; case '8': QB6(); break; case '9': QC1(); break; case '10': QC2(); break; case 'q': break; default: cout << "No such question " << prog_choice << endl; break; } } while (prog_choice != 'q'); cout << "Program terminates. Good bye!" << endl; return 0; } // END