Name:________________________ CS 210 ­ Fundamentals of Programming I Fall 2011 ­ In­class Exercise for 10/05/2011

advertisement
CS 210 ­ Fundamentals of Programming I
Fall 2011 ­ In­class Exercise for 10/05/2011
Name:________________________
(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 average 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. Then it displays the average test scores in reverse order, one on a line. The maximum number of students in a class is 15.
For the purposes of this exercise, the reading in of the data will be done with a function read_numbers_from_file and the displaying of the numbers will be done with a function print_array_in_reverse. Analysis & Design of Main Program
Analysis ­ what data is constant, input, computed, or output?
Objects
Type
Movement
Name
Capacity of array (15)
int
constant
MAX_STUDENTS
Array of average test scores double [ ]
input/output
avg_test_scores
Number of elements stored in array
int
computed
num_elements
Pointer to data file
FILE *
local
in_file
Design ­ what are the steps to solve this problem?
1. 2. 3. 4. 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
Display numbers in reverse order using print_array_in_reverse function
Function fill_array_from_file
This function is to read (real) numbers from a file, count them, and put them into an array. The analysis and design of this function was presented during lecture.
Function print_array_in_reverse
This function is to display, on the screen, the (real) numbers in an array in reverse order. The analysis and design of this function was presented during lecture.
10/05/2011
Page 1 of 2
D. Hwang
Assignment Answer the following questions about this program. a. What is the type of the elements of the array avg_test_scores? b. What is the maximum number of elements we can store in the array avg_test_scores? c. What will happen if the file scores.txt is an empty file (i.e., has no data in it)? d. What will happen if the file scores.txt has 100 test scores in it?
e. How could you modify the program so that it would handle up to 100 test scores? f. 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? g. What is the purpose of const in the parameter list of print_array_in_reverse? When you have completed this exercise, please put your name and "INCLASS for 10/5" in a comment at the top of your main.c file, zip up your main.c file and submit under assignment 10­IN as usual, and turn in this exercise sheet with your answers to the questions. 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.
10/05/2011
Page 2 of 2
D. Hwang
Download