3
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 5.0 2 10.0 3 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.

Homework Assignment 6

Embed Size (px)

DESCRIPTION

Homework Assignment 6

Citation preview

Page 1: Homework Assignment 6

Lawrence Technological UniversityDepartment 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 5.02 10.03 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.

Page 2: Homework Assignment 6

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 don’t 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.

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!

Enjoy!