Assignment 8 consists of THREE (3) exercises:Solved

$24.99 $18.99

C1A8 General Information, continued 2 What is a “Working Directory”? Where Does a Program Look for Files When Attempting to Open Them? 6 Where Does a Program Create New Files? 7 Where Should You Put Instructor-Supplied Data Files? 8 9 What is a “Working Directory”? A program’s “Working Directory” is the directory it uses for…

5/5 – (2 votes)

You’ll get a: zip file solution

 

Description

5/5 – (2 votes)

  1. C1A8 General Information, continued

2

What is a “Working Directory”?

  1. Where Does a Program Look for Files When Attempting to Open Them?

6

Where Does a Program Create New Files?

7

Where Should You Put Instructor-Supplied Data Files?

8

9

  1. What is a “Working Directory”?

  1. A program’s “Working Directory” is the directory it uses for any files it opens or creates if their names are

  2. specified without a path, and you must place any instructor-supplied data file(s) (.txt or .bin extensions)

  3. your program needs in that directory. Its default location differs between IDEs and operating systems

  4. and it is important to know where it is and how to change it. For further information please refer to the

  5. Determining/Changing the “Working Directory” topic in the version of the course document titled “Using

  1. the Compiler’s IDE…” that is applicable to the IDE you are using.

17

18

19

20

Opening Files – Testing for Failure/Success

21

  1. Always check the success/failure status of opening a file before using it or opening another file.

26

Supplying Information to a Program via its “Command Line”

27

  1. It is often more appropriate to supply information to a program via “command line arguments” than by

  2. user prompts. Such arguments can be provided regardless of how a program is being run, whether it be

  1. from within an IDE, a system command window, a GUI icon, or a batch file. For this course I strongly

  2. recommend using an IDE for running all programs.

  1. If you are not familiar with using command line arguments first review note 8.3 for information on how to

  2. process them within any program, then review the appropriate version of the course document titled

  3. Using the Compiler’s IDE…”, which illustrates implementing an arbitrary command line in several ways

  4. including implementing command arguments containing spaces.

  5. It is important to note that command line redirection information (note 4.2), if any, is only visible to the

  6. operating system and will not be among the command line arguments available to the program being

  1. run.

Personalized C1A8 requirements exclusively for Jose Medrano (U09845800)

C/C++ Programming I (Section 174273)

C1A8E0 (6 points total – 1 point per question – No program required)

Assume language standards compliance and any necessary standard library support unless stated otherwise. These are not trick questions and there is only one correct answer. Basing an answer on actual testing is risky. Place your answers in a plain text “quiz file” named C1A8E0_Quiz.txt formatted as:

a “Non-Code” Title Block, an empty line, then the answers:

  1. A

  2. C etc.

Personalized C1A8 requirements exclusively for Jose Medrano (U09845800)

C/C++ Programming I (Section 174273)

  1. C1A8E1 (6 points – C++ Program)

  1. Exclude any existing source code files that may already be in your IDE project and add three new ones,

  2. naming them C1A8E1_SavingsAccount.h, C1A8E1_SavingsAccount.cpp, and C1A8E1_main.cpp. Do not

  3. use #include to include either of the two .cpp files in each other or in any other file. However, you may

  4. use it to include any appropriate header file(s) you need. You must include C1A8E1_SavingsAccount.h

  1. in any file that needs its contents.

7

  1. This exercise is not an example of rigorous banking practices but is only to familiarize you with simple C++

  2. class members. Assume the class below represents just the data members of a savings account:

10

class SavingsAccount // Do not change the access level (private) of the class’s data members.

11

12

{

// Do not add any additional data members.

  1. int type_;

  2. string ownerName_;

  3. long IDnbr_;

  4. double balance_, closurePenaltyPercent_;

  5. };

18

19

Arrays and loops are neither necessary nor allowed anywhere in this exercise.

  1. There must not be a SavingsAccount variable anywhere other than in function main.

21

Do not add any member functions other than those described below.

22

  1. File C1A8E1_SavingsAccount.h must contain the following:

  2. 1. The class SavingsAccount definition from above with the following added to its contents:

25

a. The prototype only for member function GetInitialValues.

26

b. The prototype only for member function DisplayValues.

27

c. The entire definition of member function CalculatePenalty.

  1. 2. The entire definition of function DisplayValues – placed outside class SavingsAccount.

  2. File C1A8E1_SavingsAccount.cpp must contain:

  3. 1. The entire definition of member function GetInitialValues.

  4. File C1A8E1_main.cpp must contain function main, which must:

  5. 1. Declare a variable of type SavingsAccount (do not allocate it dynamically).

  6. 2. Call functions GetInitialValues, DisplayValues, and CalculatePenalty in that order. You

  1. may respond to any prompts with whatever values you deem sufficient to test your program.

  2. 3. Display the value returned from CalculatePenalty using the following format, where the

  3. question mark represents the actual value:

39

Account closure penalty: ?

40

  1. GetInitialValues: a non-inline member function, which must:

  2. 1. Have a void parameter list and return void.

  3. 2. For each of the five data members in class SavingsAccount on this page and in the order

  4. shown there, prompt the user for a value and initialize the member with it. An owner’s name

  5. containing spaces must be supported.

46

  1. DisplayValues: a const inline member function, which must:

  2. 1. Have a void parameter list and return void.

  3. 2. Not prompt for anything.

  4. 3. Use one cout to display the values of all five data members of its SavingsAccount object in the

  5. order and format shown below, replacing the question marks with the members’ values:

52

Account type: ?

53

Owner name: ?

54

ID number: ?

55

Account balance: ?

……………………Continued

56

Account closure penalty percent: ?

1992-2023 Ray Mitchell

Page 1 of 2 of C1A8E1

Personalized C1A8 requirements exclusively for Jose Medrano (U09845800)

C/C++ Programming I (Section 174273)

  1. CalculatePenalty: a const inline member function, which must:

  2. 1. Have a void parameter list and return an appropriate type.

  3. 2. Calculate the account closure penalty and return it to the caller. It will be the percent specified

  4. by closurePenaltyPercent_ of the account balance specified by balance_. Do not use

  5. division.

  6. 3. Not prompt or display anything.

63

64

  1. Manually re-run your program several times, testing with different values.

  2. Submitting your solution

  1. `Send an empty-body email to the assignment checker with the subject line C1A8E1_174273_U09845800

  1. and with all three source code files attached.

  1. See the course document titled “How to Prepare and Submit Assignments” for additional exercise

  2. formatting, submission, and assignment checker requirements.

73

74

  1. Hints:

  1. 1. If you attempt to read the account owner’s name from user input using the >> operator, your

  2. program will fail if the name contains whitespace. This is because that operator stops reading a

  3. string when the first non-leading whitespace is encountered. The correct approach is to use the

  4. getline function (note 7.7).

80

  1. 2. See note 4.1. Some operations that read newline-terminated input leave the newline character

  2. in the input buffer, which then acts as leading whitespace for a subsequent read. If that

  3. character is still there when the getline function is called, only it will be read and the line will be

  4. considered empty. Use either cin >> ws or cin.ignore() before calling getline to remove

  5. and discard the newline character. ws is not a variable but is instead what is known as a

  1. manipulator”. It is in the std namespace and is available when you include standard C++

  2. header file iostream. It is not necessary to include standard C++ header file iomanip to use the

  1. ws manipulator.

89

  1. 3. 1.3% of 0.2 is not 0.26

  1. 4. Be sure to use “include guards” (note D.2) in header file C1A8E1_SavingsAccount.h. Use

  1. #include to include this file in files C1A8E1_SavingsAccount.cpp and C1A8E1_main.cpp.

  1. 5. If you get a “multiply defined” linker error regarding function DisplayValues, it is because it has

96 not been declared as “inline”.

1992-2023 Ray Mitchell Page 2 of 2 of C1A8E1

Personalized C1A8 requirements exclusively for Jose Medrano (U09845800)

C/C++ Programming I (Section 174273)

  1. C1A8E2 (8 points – C++ Program)

  1. Exclude any existing source code files that may already be in your IDE project and add a new one,

  2. naming it C1A8E2_main.cpp. Write a program in that file to perform a “Search and Replace All”

4 operation. This operation consists of performing a case-sensitive search for all occurrences of

  1. an arbitrary sequence of characters within a file and substituting another arbitrary sequence in place of

  2. them.

7

  1. One of the biggest problems some students have with this exercise occurs simply because they do not

  1. read the command line information provided in the appropriate version of the “Using the … IDE” course

  2. document. Aside from this, I recommend (but do not require) that you implement the algorithm

  1. provided in the hints.

12

  1. Your program:

  2. 1. May assume, for simplicity:

15

a. It will only be used with text files.

16

b. The input and output file names will be different.

17

c. No character sequence will be split across multiple lines.

  1. 2. Must support multiple occurrences of the “search” character sequence on a single line.

  2. 3. Must support cases where the length of the “search” character sequence is different from the

  3. length of the “replacement” character sequence.

  4. 4. Must get the following information from the command line in the order listed. Spaces must be

  5. allowed in all items:

23

a. The name of the file to search (the input file).

24

b. The name of the file to store the results in (the output file).

25

c. The sequence of characters to search for.

26

d. The sequence of characters to use as replacements.

  1. 5. Must verify that exactly the correct number of command arguments exist before accessing them

  2. and must exit with an error message and error code if they do not.

  3. 6. Must read one line at time from the input file into a character buffer using the getline function

  4. and process it completely before reading the next line.

  5. 7. Must not copy anything from the character buffer to any destination other than the output file.

  6. 8. Must not use data types list, queue, stack, string, or vector.

  7. 9. Must not use functions peek, seekp, seekg, tellp, tellg, fseek, ftell, fsetpos, or fgetpos.

  8. 10. Must not call the strlen function in any part of any loop.

  9. 11. Must not declare more than one array or use dynamic memory allocation.

  10. 12. Must not test for an empty input file or one that does not contain the search sequence.

  11. 13. Must not write any function other than main.

38

  1. Manually re-run your program several times, testing at least the following 2 cases with instructor-supplied

  2. data file TestFile1.txt, which must be placed in the program’s “working directory”. Choose any name

  1. you wish for the output file as long as it is different from the input file name:

42

43

Test#

Input File

Output File

Search For

Replace Each With

44

1

TestFile1.txt

your choice

the

John Galt?

45

2

TestFile1.txt

your choice

string literal

TESTING

46

  1. For example (Test #1), if the input file contained,

  2. These are the answers to neither of their questions!

  1. the output file would contain,

  2. These are John Galt? answers to neiJohn Galt?r of John Galt?ir questions!

  1. Note that there were three replacements on one line and that “the” was part of another word in

  2. two of those cases.

53

  1. Submitting your solution

  1. `Send an empty-body email to the assignment checker with the subject line C1A8E2_174273_U09845800

1992-2023 Ray Mitchell Page 1 of 2 of C1A8E2

Personalized C1A8 requirements exclusively for Jose Medrano (U09845800)

C/C++ Programming I (Section 174273)

  1. and with your source code file attached.

  1. See the course document titled “How to Prepare and Submit Assignments” for additional exercise

  2. formatting, submission, and assignment checker requirements.

59

  1. Hints:

  1. 1. The value of EOF cannot be correctly stored or detected using data type char or unsigned char;

  1. use type int instead (Notes 4.3A & 4.3B). Since the end of file condition only occurs when a read

  1. (or write) is attempted, testing for it before such an attempt is meaningless and inappropriate.

  2. Only test after such an attempt before the value obtained from reading is used (Note 4.3B).

  1. 2. Not testing files for a successful opening is bad programming (Notes 10.3 & 10.4B).

  1. 3. It is not necessary to determine the length of the replacement string.

68

69 You may use any algorithm you wish to complete this exercise as long as none of the

  1. requirements/restrictions are violated. The following describes the algorithm I used and recommend.

  2. Drawing a diagram of memory as the algorithm progresses always helps:

72

  1. General Algorithm Description:

  1. For each line read into a character buffer from the input file

  2. {

  3. Set a pointer to the first character in the line.

  4. For each “search” sequence found in the line using the strstr library function:

  5. {

  1. Copy characters into the output file from the pointer to where the “search” sequence starts.

  2. Write the replacement string into the output file.

  1. Move the pointer to the next character in the input line after the “search” sequence ends.

  2. }

  3. Copy the remainder of the line into the output file.

  4. }

85

  1. Algorithm Step-by-Step Implementation Details:

  1. 1. Do all the standard things, including declaring needed variables and opening the input and

  2. output files.

  3. 2. Implement a loop statement that contains the following 3 statements (a, b, and c) in order:

90

a. a statement that gets the next line from the input file and stores it into a character buffer.

91

(The newline character must be discarded.) If the end of file condition occurs, terminate

92

the loop. Otherwise, proceed to step b below.

93

b. a “for” statement that does everything in steps i and ii below (in order):

94

i. does the following in its “control” section. The “control” section is the portion of

95

the “for” statement that is in parentheses just after the keyword for:

96

1)

The “initial expression” (Note 3.5) initializes a character pointer (I’ll call it

97

cp1) to point to the beginning of the character buffer you are reading

98

each line into.

99

2)

The “controlling expression” (Note 3.5) assigns the return value of the

100

strstr function to a different character pointer (I’ll call it cp2). The first

101

argument of strstr will be cp1 and the second argument will be a

102

pointer to the first character of the string you are looking for in the file.

103

3)

The “loop expression” (Note 3.5) is empty.

104

ii. does the following in the loop “body”:

105

1)

Uses the write function to write to the output file. The first argument of

106

write will be cp1 and the second will be cp2-cp1.

107

2)

Uses the << operator to write the replacement string to the output file.

108

3)

Updates cp1 by assigning to it the sum of cp2 and the length of the string

109

you are searching for in the file.

110

c. a statement that writes the string in cp1 and a newline character to the output file.

1992-2023 Ray Mitchell

Page 2 of 2 of C1A8E2

Page 7 (7/11/2023)

Assignment 8 consists of THREE (3) exercises:Solved
$24.99 $18.99