3
Lawrence Technological University Department of Mathematics and Computer Science MCS 1142 Introduction to C – Fall 2008 This homework assignment builds on the previous assignments. In the last assignment you used function calls and looping constructs to print the radius, circumference and area for three circles. For this assignment use the same code for looping, calculation and printing and reading the input data from a file as you did for assignment 6. But break down the processing of the data into 2 loops. The first loop should loop over all the input lines from the input file and read the data from the file. This is just like you did in the last assignment. But, rather than storing the input data in variables, store them in an array. The array should be declared as follows: float radii [10]; Notice that the array radii is declared to store data values of type float. This is where you will store the radius for each circle as you read it from the input file. Also notice that we are declaring the array to be 10 elements in length. That doesn’t mean you have to use all 10 elements. Up to now we’ve worked with only three circles and that is still acceptable. In that case you would only use the first three elements of the array for storing radii. Also notice that you are only storing the radius for each circle in the array. The number of the circle doesn’t need to be stored at all. When you process the input radii, you can get the circle number from the index of the array. You can use the input file you used for assignment 6. It should have the structure below: 1 5.0 2 10.0 3 6.3 The second loop of the program should loop over all the radii stored in the array and process them in the same manner as we have up to now. But please follow these rules: Make sure you use 2 loops for the program – the first for reading and storing input and the second for processing the array.

Homework Assignment 7

Embed Size (px)

DESCRIPTION

Homework Assignment 7

Citation preview

Page 1: Homework Assignment 7

Lawrence Technological UniversityDepartment of Mathematics and Computer Science

MCS 1142 Introduction to C – Fall 2008

This homework assignment builds on the previous assignments. In the last assignment you used function calls and looping constructs to print the radius, circumference and area for three circles.

For this assignment use the same code for looping, calculation and printing and reading the input data from a file as you did for assignment 6. But break down the processing of the data into 2 loops.

The first loop should loop over all the input lines from the input file and read the data from the file. This is just like you did in the last assignment. But, rather than storing the input data in variables, store them in an array. The array should be declared as follows:

float radii [10];

Notice that the array radii is declared to store data values of type float. This is where you will store the radius for each circle as you read it from the input file. Also notice that we are declaring the array to be 10 elements in length. That doesn’t mean you have to use all 10 elements. Up to now we’ve worked with only three circles and that is still acceptable. In that case you would only use the first three elements of the array for storing radii.

Also notice that you are only storing the radius for each circle in the array. The number of the circle doesn’t need to be stored at all. When you process the input radii, you can get the circle number from the index of the array.

You can use the input file you used for assignment 6. It should have the structure below:

1 5.02 10.03 6.3

The second loop of the program should loop over all the radii stored in the array and process them in the same manner as we have up to now. But please follow these rules:

Make sure you use 2 loops for the program – the first for reading and storing input and the second for processing the array.

Make sure the first loop is open ended meaning it will loop until all the input is read from the input file.

Make sure the second loop only runs over the number of elements actually stored in the array. That means if we only use 3 elements of the 10 element array, the second loop should only loop 3 times.

When you open the input file make sure you check that the file opened correctly and the stream point is properly created. You might use a statement like the following to accomplish this:

if ((spInput = fopen("input.txt", "r") == NULLprintf("Could not open input file!\n");

Also notice that we are opening this file in read mode ("r").

Continue using the if … else statement from your last assignment inside the loop. However generalize the statement so that it will work for all radii. It should have the following form:

Page 2: Homework Assignment 7

// Calculate and print circumference and area for the circle.printf("Circle %d radius: \t\t%6.2f\n", loop_counter, rad);if (rad == 5){

printf("Circle %d circum: \t\t%6.2f\n", loop_counter, calcCircum(rad));printf("Circle %d area: \t\tNot Calculated\n\n", loop_counter);

}else if (rad == 10){

printf("Circle %d circum: \t\tNot Calculated\n", loop_counter);printf("Circle %d area: \t\t%6.2f\n\n", loop_counter, calcArea(rad));

}else{

printf("Circle %d circum: \t\tNot Calculated\n", loop_counter);printf("Circle %d area: \t\tNot Calculated\n\n", loop_counter);

}

Please note you will only need to include one such if statement because it is generalized to work for all loops.

The output should have the following format:

Circle 1 radius: ###.##Circle 1 circumference: ###.##Circle 1 area: ###.##

Circle 2 radius: ###.##Circle 2 circumference: ###.##Circle 2 area: ###.##

Circle 3 radius: ###.##Circle 3 circumference: ###.##Circle 3 area: ###.##

Don’t forget the following program head for all programs submitted for grading.

// Program Author: Your name// Student ID: xx000012345// Email Address: [email protected]// Name of Program: Name of Program.c// Program Due Date: MM-DD-YY// Objective: The Instructors description of the programming

assignment// Program Description: The Students description of how the program works// Honor Code: I have neither given nor received unauthorized aid in // completing this work, nor have I presented someone else's // work as my own!

You can submit this program to me as a hardcopy in class (preferred option) or in the digital drop box. But please remember it is due by Tuesday Oct. 24. I will check dates in the digital drop box to make sure it was submitted on time.

Enjoy!