CS 0007 Exam 2 Name: _________________________


11/22/1999


Part 1: Program Segment Questions


1. (25 points)

Write a procedure called sort that sorts an array of 20 integers in ascending order using the selection sort algorithm. You’ll also need to write a swap procedure, defined within the sort procedure. Make sure you pass parameters correctly.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

2. (25 points)

Write a complete program that does nothing but continuously reads lines of characters (strings) from the user and writes each line to a text file called myfile after the user presses enter. The program must terminate when the user hits enter on a blank line.

 

3. (25 points)

Write a function called readintegers that reads all the integers from a text file called myfile (use readln()) and returns the sum of the integers as an integer. Note: (1) this function takes no parameters, (2) is responsible for opening the file for reading, and (3) assume myfile is in the program header line and declared globally as text.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 


  1. (25 points)

Write a function called findinteger that takes an array of 1000 unique integers by variable and a single integer by value and searches the array for the integer. If the integer is found, the function returns the index where it was found. If the integer is not found in the array, the function returns –1.