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 7. Also store the input data from your input file into the array radii for later processing. For this exercise you’ll need to create another function, calcAndPrint(), that takes as input parameters the array, radii, and the number of radii stored in the array. For example calcAndPrint(3, radii); calls the function with 3 elements in the array. So, like in the last assignment, you should process the input file in two steps: 1) read the data from the input file and store the radius values in the array, radii 2) process the array, radii, with the if statement that determines what to print, based on the value of each radius The difference between this assignment and the last is that this time you need to use calcAndPrint to replace the second loop. The first loop should read the file and store the input in the array as before. But the second loop should be contained in the calcAndPrint function. Below is a PSEUDOCODE representation of how the main function should appear. Remember this is pseudocode – it cannot be placed directly in your programs and compiled. main() { if <input file CANNOT be opened> print error message; else {

Homework Assignment 8

Embed Size (px)

DESCRIPTION

Homework Assignment 8

Citation preview

Page 1: Homework Assignment 8

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 7. Also store the input data from your input file into the array radii for later processing.

For this exercise you’ll need to create another function, calcAndPrint(), that takes as input parameters the array, radii, and the number of radii stored in the array. For example

calcAndPrint(3, radii);

calls the function with 3 elements in the array.

So, like in the last assignment, you should process the input file in two steps:

1) read the data from the input file and store the radius values in the array, radii2) process the array, radii, with the if statement that determines what to print, based on

the value of each radius

The difference between this assignment and the last is that this time you need to use calcAndPrint to replace the second loop. The first loop should read the file and store the input in the array as before. But the second loop should be contained in the calcAndPrint function.

Below is a PSEUDOCODE representation of how the main function should appear. Remember this is pseudocode – it cannot be placed directly in your programs and compiled.

main(){

if <input file CANNOT be opened> print error message;

else{ while <input file not EOF> read radius values from input file

and store in radii;

calcAndPrint(numRadii, radii);

return();}

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

1 5.02 10.03 6.3

Page 2: Homework Assignment 8

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 – but this time this if statement will be inside calcAndPrint. Remember to 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: ###.##

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

Page 3: Homework Assignment 8

// completing this work, nor have I presented someone else's // work as my own!

Enjoy!

Homework Assignment 8 is Due 11-19-08