Вы находитесь на странице: 1из 2

Lawrence Technological University

Department of Mathematics and Computer Science


MCS 1142 Introduction to C Fall 2006
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. But, rather than hard
code the radii of the circles in your program code, you'll need to read the circle number (1, 2, or 3)
and the radii for each (5, 10, 6.3) from an input file. The input file should have the structure
below:
1
2
3

5.0
10.0
6.3

To input this data you'll have to declare a file input stream pointer, assign it to your input file and
read the data from the file until you reach the EOF. You'll most likely want to use the fscanf
function for this read. The structure of this statement would look like below
fscanf(spInput, "%d %f", &loop_counter, &rad);

You will need to loop again to print the output but, rather than loop for a set number of times,
you'll need to loop over the number of rows of data in the input file. For example, for the input file
above you would still loop and print output for three circles, but that would be driven by the
number of rows in the input file rather than a hard coded counter variable.
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") == NULL)
printf("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:
// 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:
###.##
Please note that the circles dont have to be listed in this order. For example, you could print
circle with radius 6.3 first.
Try several test runs with the order of the radii changed. Start with order 5, 10, and 6.3 then
revise the order and run it again. This will help verify that your if else statements work for all
radii.
Dont forget the following program head for all programs submitted for grading.
// Program Author:
// Student ID:
// Email Address:
// Name of Program:
// Program Due Date:
// Objective:
assignment
// Program Description:
// Honor Code:
//
else's
//
Enjoy!

Your name
xx000012345
xx@ltu.edu
Name of Program.c
MM-DD-YY
The Instructors description of the programming
The Students description of how the program works
I have neither given nor received unauthorized aid in
completing this work, nor have I presented someone
work as my own!

Вам также может понравиться