AP ClassJanuary 30, 2003

Exam 1 Review Questions

1. The class apstring has a function called find. What does this function do. Describe its input arguments and tell what it returns. What does this function return when it fails? Give an example of its usage.

2. The class apstring has a function called substr. What does this function do. Describe its input arguments and tell what it returns. Give an example of its usage.

3. Write a new function for apstring called remove which accepts an apstring as an argument and returns an apstring result. An example of how this function works is as follows:

apstring s1("Hello Mom! I like Jello.");

apstring s2("Mom");

apstring s3;

s3 = s1.Remove(s2);

cout < s3 < endl;

In this case the program will print the string "Hello! I like Jello."

4. Write a C++ program which consists of two parts. Part 1 creates a file which has 20 lines of random text and saves this file as "MyData.txt". Part 2 reads this file and creates a second file which contains only the 10 odd numbered lines from the first file.

5. Write a C++ program which creates an apvector of apstrings. Read the file Hgrades.txt from programming assignment 9 into this vector. Sort the strings by the 4-digit social ID number which is the first four digits immediately following the name field. Print the sorted vector to a new file called Hsorted.txt.

6. Given that the type Cell is a class that has been defined. Show how to declare of an 2-dimensional matrix of Cells using the apmatrix class. All the user to input two ints which will be used for the matrix dimensions.

7. Show how to declare a 2-dimensional matrix of strings. Use apstring for your string class and apmatrix for the 2-d array. Set the dimensions at 20x30.

8. Show how to write a prototype for a function called MyFunction which will accept two ints named row and col and the 2-d array created in the problem above.

9. Answer the questions below about the following program.

1. #include<iostream>

2. #include<fstream>

3. #include<cstdlib>

4. #include "apstring.h"

5. using namespace std;

6. int CountLetters(const apstring &xStr);

7. int main()

8. {int sumSpaces;

9. apstring xStr;

10. ifstream inStream;

11. inStream.open("blayer.txt");

12. sumSpaces = 0;

13. while(getline(inStream,xStr))

14. {cout < xStr < endl;

15. sumSpaces += CountLetters(xStr);

16. }

17. cout < "The number of non space characters is "

< sumSpaces < endl;

18. system("PAUSE");

19. return 0;

20. }

21. int CountLetters(const apstring &xStr)

22. {int i, sum = 0;

23. for(i=0;i<xStr.length();i++)

24. if(xStr[i] != ' ')

25. sum++;

26. return sum;

27. }

A) Lines 13 to 16 define a while loop. When does the while loop end?

B) In line 21, why is xStr passed by reference since it is not changed in the function.

C) What code could be added to this program between lines 11 and 12 to indicate an error in the event that blayer.txt does not exist or is unavailable.

D) Show how you would change this program to count spaces instead of nonspaces.

11. Why is the + operator overloaded three different times in the apstring class? What are the overloaded functions?

12. Given below is a set of 7 points which represent x,y coordinates in a plane. A triangle can be formed by connecting any three of the points. Write a C++ program to search through all possible combinations of three points to determine which three form a triangle with the largest area. Your program must use the apmatrix class to hold the point data.

To compute the area of a triangle use the formula:

where and a, b, and c are the three sides of the triangle.

To calculate the length of the sides you can use the distance formula:

where are the coordinates of one point and is the coodinates of the second point.

Your program should print the points that form the largest triangle and the corresponding area.

Your program must have at least one function to calculate the distance and a second to calculate the area.

DATA

1. 0,0

2. 5,4

3. -7, -4

4. -2, 6

5. 4, -3

6. 1, 10

7. 0, -6