Comp128 Final Exam Review

Total Page:16

File Type:pdf, Size:1020Kb

Comp128 Final Exam Review

Comp128 Final Exam Review

1) Functions – Know how to set up function headers following a problem specification, including return type and parameter lists. Know when to use the & and when not to.

2) Decisions – Know how to use if and switch statements. You should be able to convert from a nested if statement to a switch and vice versa. Know how to use comparative operators such as ==, !=, <, etc. to do various tests, including testing to see if a number is in a specified range. Know how to use the logical operators &&, || and !.

3) Loops – Know the 3 basic loop constructs: while, do .. while and for. You should be able to switch from one to the other.

4) Streams – Know how to use standard input and output, namely cin and cout. Understand how to open a file and stream to/from it. Understand how to use manipulators and stream flags to control the formatting of output.

5) Arrays – Know how to declare and initialize 1-D arrays. Be able to write functions, which are passed arrays as parameters and need to do some processing at each cell of the array. Understand how an array variable can also be thought of as a pointer.

6) Strings – Know how to declare and use C-Strings, including use of functions in the cstring library such as strcmp() strlen(),strcpy(). Understand how to use the C++ string class.

7) Classes – Know how to declare a class including public and private member variables and functions. Be able to implement member functions given their specification. Know how to declare a variable whose type is a class, then use the variable to invoke member functions.

8) Program Design – Know how to break down a large problem into smaller tasks using top-down structured design techniques. Be able to draw a structure chart showing functions as boxes, and using directed arrows to indicate parameter passing. Also, know how to draw a simple class diagram, namely for each class a rectangle divided into 3 compartments for the class name, member variables and member functions.

9) Testing – be able to construct a driver function (usually main() ) to exercise functions you have written and see if they are working correctly. Sample Questions: For each of the function descriptions below:

a) Write the function definition code. b) Write a driver to test the function. It obtains test inputs from the user, calls the function sending necessary parameters, and then prints the answers.

1. Write a function named smallest, which is passed an array of int, and its size, and returns the smallest int in the array.

2. Write a function named total, which is passed an int n and returns the sum: 1 + 2 + 3 + … + n.

3. Write a function named find, which is passed an array of char, its size, and a char, named target to look for. It returns the index value of the first cell which contains target, or –1 if target is not found in the array.

4. Write a function named rotate, which is passed an array of int, and its size. It rotates the array by moving each int up 1, except for the last int, which is moved to the first position.

5. Write a function named breakup. It is passed a whole number of inches, and breaks it up into yards, feet and inches. i.e. breakup(100) => 2 yards, 2 feet, 4 inches.

6. Write a function named honor_roll. It is passed an array of test scores, and the size of the array. It returns the number of students who got A (90 – 100) and the number who got B (80 – 89).

7. Write a function named circle, which is passed the radius of a circle and returns its circumference.

8. Write a function named average, which is passed an array of double, and its size, and returns its average.

9. Write a function named box, which is passed an int n, and prints out an n by n box made of asterisks.

10. Write a function named hypotenuse, which is passed the two sides of a right triangle and returns the hypotenuse.

11. Write a function named reverse, which is passed an array of char, and its size, and prints out the array in reverse order.

12. Write a function named capitalize, which is passed a char, and returns a char. If the char passed is a lower case letter, it returns capitalized.

13. Write a function named is_vowel, which is passed a char, and returns 1 if it is a vowel, 0, otherwise.

14. Write a function count_vowels, which is passed an array of char, and its size, and returns the number of vowels in the array. You can use the function is_vowel to help. 15. Write a function named sum_square, which is passed 2 integers, known as lo and hi, where lo < hi, and returns the sum: lo2 + (lo+1)2 + …. + hi2.

16. Write a function named analyze, which is passed an array of char, and its length, and calculates the following statistics:

a) The number of vowels b) The number of consonants c) The number of digits

17. Predict the output of this program fragment: int x = 46; switch(x%3){ case 0: printf(“Yellow\n”); case 1: printf(“Blue\n”); case 2: printf(“Green”); default: printf(“Black\n”); }

18. Write a function named passFail, which is passed an array of test scores, and its size, and passes back the number of passing(>= 60), and the number of failing scores.

19. Write a function named harmonic, which is passed an int n, and returns the sum:

1 + ½ + 1/3 + ¼ + … + 1/n as a decimal number.

20. Write a function named careful, which is passed a char c, and returns 1 if c is a letter in the word “careful”, 0 otherwise.

21. Predict the output:

printf(“%d”, 5 > 6 ); printf(“%c”, ‘b’+4); int x[5] = {1,3,2,4,0};

22. Write a function named uncap, which is passes 2 already opened streams, one for input and one for output. It reads chars from the input and writes them to the output changing all lower case letters to upper and all upper case letters to lower.

23. Write your own versions of these c-string functions:

a) strcmp() b) strlen() c) strcpy()

24. Write a function named hash. It is passes a c-string, i.e. an array of char terminated by ‘\0’. It returns the sum of the ascii values of the chars. i.e. hash(“AB”) should return 131 since A is ascii 65 and B is ascii 66. 25. Consider the class definition below: class Date { private: int month; int day; int year; public: Date(void); //default constructor Date(int m, int d, int y);// initializing constructor void print(ostream & out);// prints date to ostream in //form mm/dd/yyyy ~Date(void); //destructor bool scan(istream & in);// scans a date in the form mm/dd/yyyy void setMonth(int m);// sets the month if it is valid int getMonth(void);// returns the month void euroPrint(ostream & out);// prints date in from dd-mmm-yyyy bool isValidDate(void); // returns true if the month,day,year //constitute a legal date bool isLeapYear(void); // returns true if year is a leap year- //i.e divisible by 4 and not by 100, or by 400 };

Write the implementations of all the functions. Be sure to include the class and scope resoulution operator. i.e. Date::Date(int m, int d, int y){.. }

Recommended publications