Final Exam CS 141 Tuesday, May 2, 2000 This is a closed-book, closed-notes examination. There are 4 problems. Do not spend too much time on any problem. Read them all through first and solve them in the order that allows you to make the most progress. For each problem you need to write a function or class. You do not need to write comments, #include statements, or meaningful variable names. Assume that the type bool exists. Function prototypes are not necessary, even for classes. When you define a class, you only need to give the private part. For each problem on this test, you can (and you need to) use the functions and classes defined in some of the previous questions. You are not allowed to use any friend functions or friend classes on this test. -------------------------------------------------------------------------- 1A. Write a function called some_A which has an array of integers (grades for a test) and an integer (the size of the array) as parameters. The function should return true if at least one person received an A (90 or above) on the test. Otherwise it returns false. bool some_A(int A[], int size) { int i; for (i=0; i= 90) return true; } return false; } 1B. Write a function called all_A which has the same parameters as some_A. This function should return true if everybody got an A on the test. Otherwise it returns false. bool all_A(int A[], int size) { int i; for (i=0; i> grades[i]; if (good_test(grades,100)) cout << "It was a good test\n"; else cout << "It was a bad test\n"; } -------------------------------------------------------------------------- 2A. Define a class called person, which has a first name, a last name, a salary, and how many feet and inches tall the person is as private data fields. (See note about defining classes in the instructions) class person { private: char fname[20]; char lname[20]; int salary; int feet; int inches; public: int get_salary(); }; 2B. Write a member function for person called get_salary, which returns the salary of that person. int person:: get_salary() { return salary; } 2C. Define a class called department which has three private data fields: an array of persons and two integers. The array of persons contains all the people in the department. One integer is the number of persons currently in the array. The other integer is an index, representing the fact that the person at that position in the array is the boss of the department. class department { private: person A[100]; int filled; int boss_index; public: int highest_salary(); void new_boss(); }; 2D. Write a member function for department called highest_salary which returns the index position of the person in that department with the highest salary. (You may assume that everyone has a different salary) int department::highest_salary() { int index_best_salary = 0; int salary_of_person; int i; for (i=1; i A[index_best_salary].get_salary()) index_best_salary = i; } return index_best_salary; } 2E. Write a member function for department called new_boss which makes the person in the department with the highest salary be the boss of the department. void department::new_boss() { boss_index = highest_salary(); } -------------------------------------------------------------------------- -------------------------------------------------------------------------- CS141 Final Exam - Page 2 -------------------------------------------------------------------------- 3A. Write a member function for person called read, which takes an input stream as parameter. The function reads first name, last name, salary, and height in feet and inches from the file associated with the input stream and stores it in the person. (Note: the person class is defined in problem 2) void person::read(ifstream &in) { in >> fname >> lname >> salary >> feet >> inches; } 3B. Write a member function for person called shorter. This takes a person as parameter. It returns true if the (implicit) person is shorter than the person given in the parameter. Otherwise it returns false. void person::shorter(person p) { if (feet < p.feet) return true; if (feet > p.feet) return false; if (inches < p.inches) return true; return false; } 3C. Write a nonmember function called find_index, which takes a person, an array of persons, and the number of persons in that array as parameters. The function returns the index position of the first person in the array who is at least as tall as the person given as parameter. If everybody is shorter, the function returns the size of the array. int find_index(person p, person A[], int size) { int i; for (i=0; ii; j--) A[j] = A[j-1]; A[i] = p; return count+1; } 3E. Write a function called read_persons, which takes an input stream, an array, and the size of the array as parameters. Assume the input stream is already associated with a file that has been opened. The file contains persons (in other words, each line in the file contains a first name, last name, salary, and height in feet and inches). The function should read the persons from the file into the array so that the array is ordered from shortest person to tallest. The function should return the number of persons it has read in. (Hint: Use place_person) int read_persons(ifstream &in, person A[], int size) { person p; int count = 0; p.read(in); while (!in.eof()) { place_person(p,A,count); count++; } return count; } -------------------------------------------------------------------------- 4A. Write a function called match, which takes two strings and an integer as parameters. The function should return true if the second string occurs inside the first string beginning at the index position given by the integer. Otherwise it returns false. For example, If the first string is the word "bother" and the second string is the word "the", and the integer is 2, then the function will return true, because "the" occurs inside the word "bother" at index position 2 in the word "bother". bool match(char s[], char t[], int position) { int i; for (i=0; t[i] != `\0'; i++) { if (s[position+i] != t[i]) return false; } return true; } 4B. Write a function called find, which takes two strings as parameters. The function should return true if the second string occurs inside the first string. Otherwise it returns false. For example, "the" occurs inside the word "bother", but "the" does not occur inside the word "three". bool find(char s[], char t[]) { int i; for (i=0; s[i] != `\0`; i++) { if (find(s,t,i)) return true; } return false; } 4C. Write a function called count_the, which takes an input stream as parameter. The input stream is already associated with a file which has been opened. The function should return the number of words in the file which contain the word "the" inside them. int count_the(ifstream &in) { char word[20]; char counter = 0; in >> word; while(!in.eof()) { if (find(word,"the")) counter++; in >> word; } return counter; } --------------------------------------------------------------------------