Final Exam Self--Check Exercises

(not graded...just for practice!)

 

CS 161: Introduction to Computer Science 1

 

These are not turned in...and not graded.

We recommend working in study groups to work through these!

 

Self-Check Exercises

1.  Short answer:

 

a) How do you close a file that you have just written to?

 

b) When reading data from a file, how can you tell when you have read a line of data?

 

c) Why can't you use cout <<  to write data to a file?

 

d) List 3 advantages to using files instead of standard in/out.

 

 

2. Before you can write data to a text file, what three things need to be done? Explain what each is ... and show the corresponding C++ code

 

 

3. Write the code to initialize array c so that each element has a value of zero:

 

int main()

{

float c[101];

 

 

 

 

}

 

 

4.Write a function to read in exactly 6 letters from the keyboard and store them in an array. Then, output the array in the reverse order.

 

 

 

5. Explain the difference between call by reference versus call by value. When would you use one over the other.

 

 

 

6. What is the output for the following program?

 

#include <iostream.h>

void numbers(int  x, int & y);

 

int main() {

int a,b,c;

 

a = 22;

b = 90;

c = 14;

 

numbers(a,a);

numbers(a,b);

numbers(b,a);

cout <<a <<"  " <<b  <<"  " <<c <<endl;

return 0;

}

 

void numbers (int x, int & y) {

int b;

x +=6;

y += 11;

b = 55;

cout <<b <<"  "  <<x <<"  " <<y <<endl;

}

 

 

 

 

 

7. Write a program that allows the user to type in up to 10 positive numbers and then echoes back the numbers typed in but in reverse order. Have the user terminate the list with a control D.

 

 

 

 

 

 

8. Show how you would read in an array of characters from the keyboard. Make sure that you don’t read more characters than there is room for in the array:

 

char name[20];

 

 

 

 

 

 

9. What is the difference between the following two statements:

 

given:

char ch;         

 

cin.get(ch);

versus

cin >>ch;

 

 

10. Show how you would copy one array of characters into another, given:

 

char original_name[20];

char copied_name[20];

 

 

11. Write a function to concatenate one string of characters onto the end of another string of characters (without using the string.h library). Use the following function prototype:

 

void concatenate(char combined_string[], char to_append[]);

 

 

12. Write a function that sums together all the values supplied in an array of integers. Use the following function prototype:

 

result sum_all(int values[], int count);

 

 

13. Write a function that will compare the contents of two arrays and return a true if each of the elements matches (i.e., the first element in both arrays matches, the second element in both arrays matches with one another, etc.). If one of the elements doesn’t match in both arrays, return a false. Use the following function prototype:

 

int match_array_elements(int array_1[], int array_2[], int size_1, int       size_2);

 

14. Write the prototype for a function named pass that has two integer arguments. The first argument should be a value argument and the second a reference argument.

 

 

15. Write a program to simulate a state police radar gun. The program should read an automobile speed and print the message “speeding” if the speed exceeds 55 mph.

 

16. Write a loop that reads up to 10 integer values from a data file and displays them on the screen. If there are not 10 integers in the file, the message “That’s all folks” should be displayed after the last number.