CS 210 ­ Fundamentals of Programming I Spring 2012 ­ In­class Exercise for 02/20/2012 & 02/21/2012

advertisement
CS 210 ­ Fundamentals of Programming I
Spring 2012 ­ In­class Exercise for 02/20/2012 & 02/21/2012
(10 points) This exercise consists of both a coding part to completed during the lecture and a written part. The purpose of this exercise is to work with arrays and files. Create a new console project in CodeBlocks. Here is a problem statement, analysis, and design for today's program.
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 (marked by sentinel value ­99) or the array is full. Then it finds and displays the highest test score in the array. The maximum number of students in a class is 20.
For the purposes of this exercise, the reading in of the data will be done with a function fill_array_from_file and the search for the highest score will be done with a function find_maximum_element. Analysis & Design of Main Program
Analysis
Objects
Design
1. 2. 3. 4. 5. Type
Movement
Name
Capacity of array (20)
int
constant
MAX_STUDENTS
Sentinel value (­99)
double
constant
SENTINEL
Array of test scores double [ ]
input
scores
Highest test score
double
output
max_score
Number of elements stored in array
int
local
num_elements
Pointer to data file
FILE *
local
in_file
Display what program does
Open file "scores.txt" for reading as in_file
Read numbers from file into the array using read_numbers_from_file function
Find the highest score in the array using find_maximum_element
Display the highest score
Function fill_array_from_file
This function is to read (real) numbers from a file, count them, and put them into an array. The end of the data is indicated by ­99. The analysis and design of this function was presented during lecture.
Function find_maximum_element
This function is to find and return the largest element in an array. The analysis and design of this function was presented during lecture.
02/19/2012
Page 1 of 2
D. Hwang
Assignment Name:________________________
Answer the following questions about this program. a. What is the type of the elements of the array scores? b. What is the maximum number of elements we can store in the array scores? c. What will happen if the file scores.txt has 100 test scores in it?
d. How could you modify the program so that it would handle up to 100 test scores? e. Why doesn't the array parameter an_array of fill_array_from_file have an & even though it is passed back like the integer parameter count? f. What is the purpose of const in the parameter list of find_maximum_element? When you have completed this exercise, please put your name and "INCLASS for 2/20 & 2/21" in a comment at the top of your main.c file, zip up your main.c file and submit under assignment 10­
IN6 as usual, and turn in this exercise sheet with your answers to the questions. The submission system will compile, but not run, the program. Reminder: if you get done before the end of the class period, you are expected to begin working on Programming Project 1 and/or Homework 5.
02/19/2012
Page 2 of 2
D. Hwang
Download