homework assignment 10

4
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 = 'R'; subStr++; *subStr = 'A'; subStr++; *subStr = 'D'; subStr++; *subStr = 'I'; subStr++; *subStr = 'U'; subStr++; *subStr = 'S';

Upload: rockondog0879739178

Post on 28-Jan-2016

214 views

Category:

Documents


0 download

DESCRIPTION

Homework Assignment 10

TRANSCRIPT

Page 1: Homework Assignment 10

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 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 = 'R'; subStr++;*subStr = 'A'; subStr++;*subStr = 'D'; subStr++;*subStr = 'I'; subStr++;*subStr = 'U'; subStr++;*subStr = 'S';

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

Page 2: Homework Assignment 10

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 3: Homework Assignment 10

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: Not CalculatedCircle 2 AREA: ###.##

Circle 3 RADIUS: ###.##Circle 3 CIRCUM: 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!