CS 210 ­ Fundamentals of Programming I Fall 2015 ­ In­class Exercise 7 for 10/8/2015

advertisement
CS 210 ­ Fundamentals of Programming I
Fall 2015 ­ In­class Exercise 7 for 10/8/2015
This exercise consists of both a coding part to completed during the lecture and a written part. The purpose of this exercise is to continue working with arrays by searching and sorting them. Write the answers to the written part in this assignment sheet. Hand in this sheet and submit the program electronically when you are done. An empty CodeBlocks project should be created for this exercise and the program file sorting.c and the appropriate datafile (scores-win.txt or scores-unix.txt) should be downloaded from the course website to the project folder, then added to the project.
Problem Statement
Write a program that will read student test scores for a class from a data file into an array until the end of the data is reached or the array is full. The maximum number of students in a class is 20. After the array is filled, the program should repeatedly ask the user for a value to search for and searches the array for that value until the user
types in a negative number. Afterwards the program will sort the array, displaying the elements before and after sorting.
Analysis & Design of Main Program
Function get_file
Function fill_array_from_file
Function print_array
The A&D's for the main program and these functions are in the sorting.c file. These functions and most of the main program also are implemented.
Function search_array
This function is to search an array for a target value, returning the index of the target if the value is in the array or ­1 if the target is not in the array. The analysis and design, and implementation of this function will be presented during lecture.
Function select_sort
This function is to sort an array into ascending order using the Selection Sort algorithm. The analysis and design, and implementation of this function will be presented during lecture.
Function find_index_of_smallest_in_range
This function is to return the index of the smallest value of an array in range [start index .. number of elements in array) and is used by the select_sort function. Note that the range is closed on the low end, but open on the high end. The analysis and design, and implementation of this function will be presented during lecture.
10/08/2015
Page 1 of 2
D. Hwang
CS 210 ­ Fundamentals of Programming I
Fall 2015 ­ In­class Exercise 7 for 10/8/2015
Name:__________________________
Assignment Answer the following questions about this program. 1. (2 points) In the function print_array, why is the loop condition in the for loop i < num rather than i <= num? 2. (2 points) Why is the array parameter of find_index_of_smallest_in_range declared const, but the array parameter of select_sort is not?
d. (4 points) Suppose select_sort is called with the array pictured below. Show the contents of the array at the end of each loop iteration in the function select_sort. Circle the smallest element in each range and
draw arrows indicating which two elements are exchanged, if any, during each iteration of the loop.
[0] 14
[1]
2
[2]
7
[3]
3
[4] 11
Note you can check your answer by putting these values into a text file and calling print_array at the end of the loop iteration in select_sort.
d. (2 points) When you have completed this exercise, zip up your sorting.c file and submit under assignment 11­IN7 as usual, and turn in this exercise sheet with your answers to the questions. The submission system will compile, but not run, your program.
Reminder: if you get done before the end of the class period, you are expected to finish Project 4, work on Homework 5, or start on Project 5.
10/08/2015
Page 2 of 2
D. Hwang
Download