Sunteți pe pagina 1din 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
called calcAndPrint() with 3 input parameters 2 of which were arrays. One array contained
the 5 format strings used in the printf() statements in calcAndPrint().
For this assignment I want you add a new function, modifyFormats(), that will be called on the
array of format strings like this:
modifyFormats(formats[], 5);
where formats is the array of format strings and 5 is the integer number of entries in the array.
You should call modifyFormats() from the main() function right after you copy the format
strings into the array but before you call calcAndPrint().
In modifyFormats() I want you to modify the format strings (formats[]) so that the words
radius, area, and circum are in capital letters. To do this use the string manipulation function
strstr() to pick out the beginning of the substring in question (either radius, area, or circum).
Next remember you can modify individual characters in a string by using a pointer to the
character like this
*subStr = R;
Also remember you can advance a substring pointer by simply incrementing it like this:
subStr++;
Therefore you can combine these commands to replace a lower case word with upper case. For
example, to replace the word radius you would do the following:
*subStr
*subStr
*subStr
*subStr
*subStr
*subStr

=
=
=
=
=
=

'R';
'A';
'D';
'I';
'U';
'S';

subStr++;
subStr++;
subStr++;
subStr++;
subStr++;

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
formats[0] = "Circle
formats[1] = "Circle
formats[2] = "Circle
formats[3] = "Circle
formats[4] = "Circle

strings.
%d radius:
%d circum:
%d area:
%d circum:
%d area:

\t\t%6.2f\n";
\t\t%6.2f\n";
\t\t%6.2f\n\n";
\t\tNot Calculated\n";
\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
2
3

5.0
10.0
6.3

The output should have the following format (Please note the capital letters!):
Circle 1 RADIUS:
Circle 1 CIRCUM:
Circle 1 AREA:

###.##
###.##
Not Calculated

Circle 2 RADIUS:
Circle 2 CIRCUM:
Circle 2 AREA:

###.##
Not Calculated
###.##

Circle 3 RADIUS:
Circle 3 CIRCUM:
Circle 3 AREA:

###.##
Not Calculated
Not Calculated

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!

S-ar putea să vă placă și