2
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 a call to a new function, calcAndPrint(), print the radius, circumference and area of 3 circles. In this assignment you still need to use calcAndPrint() to print the output for the circles. calcAndPrint() should still take an integer parameter that signifies how many circles for which to print output and an array of floats that contains the radius of each circle. But, for this assignment, calcAndPrint() should also take a third parameter – an array of pointers to character strings. The name of the array should be formats and it’s declaration should look like this: char* formats[5]; The five elements in formats should be each of the five different format strings used in the printf() function calls in calcAndPrint(). These strings should be defined in the main function like this: // Create the format strings. formats[0] = "Circle %d radius: \t\t%6.2f\n"; formats[1] = "Circle %d circum: \t\t%6.2f\n"; formats[2] = "Circle %d area: \t\t%6.2f\n\n"; formats[3] = "Circle %d circum: \t\tNot Calculated\n"; formats[4] = "Circle %d area: \t\tNot Calculated\n\n Once these character strings are created and stored in formats, calcAndPrint() can be called with the three input parameters like this: calcAndPrint(3, radii, formats); Inside calcAndPrint(), the printf() calls should now use the entries in formats for the format strings rather than string literals. Below is an example of how the printf() call to print a circle radius would look: // Calculate and print circumf and area for the circle. printf(formats[0], loop_counter+1, radii[loop_counter]); You can use the input file you used for assignment 6. It should have the structure below: 1 5.0

Homework Assignment 9

Embed Size (px)

DESCRIPTION

Homework Assignment 9

Citation preview

Page 1: Homework Assignment 9

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 a call to a new function, calcAndPrint(), print the radius, circumference and area of 3 circles.

In this assignment you still need to use calcAndPrint() to print the output for the circles. calcAndPrint() should still take an integer parameter that signifies how many circles for which to print output and an array of floats that contains the radius of each circle. But, for this assignment, calcAndPrint() should also take a third parameter – an array of pointers to character strings. The name of the array should be formats and it’s declaration should look like this:

char* formats[5];

The five elements in formats should be each of the five different format strings used in the printf() function calls in calcAndPrint(). These strings should be defined in the main function like this:

// Create the format strings.formats[0] = "Circle %d radius: \t\t%6.2f\n";formats[1] = "Circle %d circum: \t\t%6.2f\n";formats[2] = "Circle %d area: \t\t%6.2f\n\n";formats[3] = "Circle %d circum: \t\tNot Calculated\n";formats[4] = "Circle %d area: \t\tNot Calculated\n\n

Once these character strings are created and stored in formats, calcAndPrint() can be called with the three input parameters like this:

calcAndPrint(3, radii, formats);

Inside calcAndPrint(), the printf() calls should now use the entries in formats for the format strings rather than string literals. Below is an example of how the printf() call to print a circle radius would look:

// Calculate and print circumf and area for the circle.printf(formats[0], loop_counter+1, radii[loop_counter]);

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 9

The output should have the following format:

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

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

Circle 3 radius: ###.##Circle 3 circumference: Not CalculatedCircle 3 area: Not Calculated

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!