Assignment 6 consists of FOUR (4) exercises: Solved

$24.99 $18.99

2 What is size_t ? 3 size_t (note 2.12) is an implementation-defined data type typically used to count things related to memory, such as the amount of storage needed for an object or the number of objects available or to be processed. The actual type that size_t represents is at the discretion of the compiler…

5/5 – (2 votes)

You’ll get a: zip file solution

 

Description

5/5 – (2 votes)

2

What is size_t ?

3

  1. size_t (note 2.12) is an implementation-defined data type typically used to count things related to

  1. memory, such as the amount of storage needed for an object or the number of objects available or to

  2. be processed. The actual type that size_t represents is at the discretion of the compiler designer and

  3. may be any of unsigned char, unsigned short, unsigned int, unsigned long, or unsigned long long. Any

  1. assumption on the part of the applications programmer about which one of these types it is, is not

  2. portable.

10

11

Lexicographical” String Comparison

12

A string (C-string) is defined as a sequence of characters ending with a null character, ‘\0’. Two strings

13

are considered equal only if they are the same length and their corresponding characters are equal.

14

The term “corresponding characters” refers to the first character in one string compared to the first

15

character in the other string, the second character in one string compared to the second character in

16

the other string, etc.

17

The longer string is not necessarily the greater. Instead, the greater is determined entirely by the relative

18

numeric values of the first two non-equal corresponding characters. For example, the greatest of strings

19

“Heat” and “Hi” is “Hi” because the second two corresponding characters (‘e’ and ‘i’) are not

20

equal and the value of ‘i’ is greater than that of ‘e’ (ASCII character set assumed). This method of

21

comparing strings is known as “lexicographical” (dictionary) comparison.

22

23

24

Inputting an Entire (Possibly Empty) User Line in C

25

Eliminating the fgets newline Character

  1. All “C-style” strings end with the null character, ‘\0’. An “empty” string contains only that character.

  2. Because of the sometimes-inconsistent behavior of the scanf function between different compilers, I

  1. recommend against using it to read empty user input lines. Instead, both empty and non-empty lines

  2. can be read reliably using the fgets function in C and the getline function in C++.

  1. getline discards the newline character that terminates an input line but fgets keeps it. The simplest

  1. way to eliminate an unwanted newline character is to overwrite it with a null character using the

  2. standard library strcspn function technique illustrated below:

  1. char buffer[BUF_SIZE];

  2. fgets(buffer, BUF_SIZE, stdin);

  3. buffer[strcspn(buffer, “\n”)] = ‘\0’;

  1. How it works: The strcspn function searches the string in its first argument for the first of any characters

  2. in the string in its second argument, including each string’s terminating null character. It then returns the

  3. index in the first string of the first character it found from the second string. For example,

  1. strcspn(buffer, “\n”)

  1. searches buffer for the newline and null characters and returns the index in buffer of which

  2. character it found first. Thus,

  1. buffer[strcspn(buffer, “\n”)] = ‘\0’;

  1. will overwrite the first newline or null character in buffer with a null character.

Personalized C1A6 requirements exclusively for Jose Medrano (U09845800)

C/C++ Programming I (Section 174273)

C1A6E0 (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 C1A6E0_Quiz.txt formatted as:

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

  1. A

  2. C etc.

Personalized C1A6 requirements exclusively for Jose Medrano (U09845800)

C/C++ Programming I (Section 174273)

  1. C1A6E1 (4 points – C Program)

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

  2. naming them C1A6E1_MyStrlen.c and C1A6E1_main.c. Do not use #include to include either of these

  3. files in each other or in any other file. However, you may use it to include any appropriate header file(s)

  4. you need.

6

  1. File C1A6E1_MyStrlen.c must contain a function named MyStrlen that has the same syntax and

  2. functionality as the standard library strlen function. If you are not familiar with strlen look it up in

  3. your IDE’s help, the course book, any good C textbook, or online. MyStrlen must:

  4. 1. have the syntax (prototype): size_t MyStrlen(const char *s1);

  5. 2. return a count of the number of characters in the string in s1, not including the null terminator.

  6. 3. use only one variable other than its formal parameter s1. That variable must be of type “const

13

and must be initialized to the value of formal parameter s1 when

pointer to const char

14

declared, for example:

15

const char * const START = s1;

  1. 4. not assign anything to s1 (do not do: s1 = something), but you may increment it.

  2. 5. not call any functions, use any macros, or display anything.

  3. 6. not use the sizeof operator (it would not help anyway).

19

  1. File C1A6E1_main.c must contain function main, which must:

  2. 1. prompt the user to enter a string (which may be empty or contain spaces).

  3. 2. call strlen to determine that string’s length.

  4. 3. call MyStrlen to determine that string’s length.

  5. 4. display the string and its length as determined by both strlen and MyStrlen in the following

  6. 2-line format, where ABC is the string used in this example and where the question marks

  7. represent the integral decimal numeric values returned by the functions. By sure to enclose the

  8. string in double-quotes:

28 strlen(“ABC”) returned ?

29 MyStrlen(“ABC”) returned ?

30

  1. Manually re-run your program several times, testing with at least the following 4 strings (the last string is

  2. empty):

  3. 1. a

  4. 2. HELLO

  5. 3. C/C++ Programming I

  6. 4. (an empty string)

37

38

  1. Submitting your solution

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

  1. and with both 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.

44

45

  1. Hints:

  1. Do you know what size_t is? If not consider reviewing note 2.12. Do you know what pointer subtraction

  2. is? If not consider reviewing note 6.14. For an example of using a pointer to walk through a string see

  3. notes 6.17 and 7.2. No special case is needed for an empty string. Set the extra pointer variable you

  4. are allowed to declare equal to the parameter pointer variable then increment one of these pointers as

  5. you step through the input string looking for the null terminator character, ‘\0’. When you find it,

  1. subtract the two pointers to find the string length and return that difference. Type cast the return

  2. expression to size_t to avoid a compiler warning. Most library functions that compute values, including

  3. strlen, do no printing.

1992-2021 Ray Mitchell Page 1 of 1 of C1A6E1

Personalized C1A6 requirements exclusively for Jose Medrano (U09845800)

C/C++ Programming I (Section 174273)

  1. C1A6E2 (4 points – C Program)

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

  2. naming them C1A6E2_MyStrcmp.c and C1A6E2_main.c. Do not use #include to include either of these

  3. files in each other or in any other file. However, you may use it to include any appropriate header file(s)

  4. you need.

6

  1. File C1A6E2_MyStrcmp.c must contain a function named MyStrcmp that has the same syntax and

  2. functionally as the standard library strcmp function. If you are not familiar with strcmp look it up in your

  3. IDE’s help, the course book, any good C textbook, or online. MyStrcmp must:

  4. 1. Have the syntax (prototype): int MyStrcmp(const char *s1, const char *s2);

  5. 2. Return:

12

a. any value < 0 if the string in s1 is lexicographically less than the string in s2.

13

b.

0 if

the string in s1 is equal to the string in s2.

14

c. any value > 0 if the string in s1 is lexicographically greater than the string in s2.

  1. The values returned by strcmp and MyStrcmp do not have to be the same for non-equal strings.

  2. 3. Not use any variables other than its two formal parameters s1 and s2.

  3. 4. Not call any functions, use any macros, or display anything.

  4. 5. Not use the sizeof operator (it would not help anyway).

19

  1. File C1A6E2_main.c must contain function main, which must:

  2. 1. Use two separate user prompts to obtain two strings (both of which may be empty or contain

  3. spaces).

  4. 2. Call strcmp to compare the two strings.

  5. 3. Call MyStrcmp to compare the two strings.

  6. 4. Display the relationship between the two strings as determined by both strcmp and MyStrcmp in

  7. the following 2-line format, where ABCXYZ and DEF are the strings in this example and where the

  8. question marks represent the integral decimal numeric values returned by the functions. Be sure

  9. to enclose the strings in double-quotes:

29

strcmp(“ABCXYZ”, “DEF”) returned ?

30

MyStrcmp(“ABCXYZ”, “DEF”) returned ?

31

  1. Manually re-run your program several times, testing with at least the following 4 string pairs (the last pair

  2. consists of two empty strings):

  3. 1. a and B

  4. 2. HE and HELLO

  5. 3. HE and EHLLO

  6. 4. (an empty string) and (an empty string)

38

39

  1. Submitting your solution

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

  1. and with both 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.

45

46

  1. Hints:

  1. See note 7.2 for an example of using a pointer to walk through a string and note 7.6 for some examples

  2. of string comparisons. No special case is needed for empty strings. The value obtained by subtracting

  3. the values of the two characters currently being compared is the most straightforward value to return

  4. when a return is required. Merely simultaneously step through both strings character-at-a-time,

  5. comparing the corresponding characters in each. Return when the first pair of non-equal characters is

  1. encountered or when a null terminator character, ‘\0’, is reached in either string.

1992-2021 Ray Mitchell Page 1 of 1 of C1A6E2

Personalized C1A6 requirements exclusively for Jose Medrano (U09845800)

C/C++ Programming I (Section 174273)

  1. C1A6E3 (6 points – C Program)

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

  2. naming them C1A6E3_GetSubstring.c and C1A6E3_main.c. Do not use #include to include either of

  3. these files in each other or in any other file. However, you may use it to include any appropriate header

  4. file(s) you need.

6

  1. File C1A6E3_GetSubstring.c must contain a function named GetSubstring whose purpose is to create

  2. a new string of characters by copying them from an existing string. Its syntax (prototype) is:

  1. char *GetSubstring(const char source[], int start, int count, char result[]);

  1. where source represents the string from which to copy the characters, start is the index in source of

  2. the first character to copy, count is the number of characters to copy, and result represents an array

  1. into which the characters are to be copied. For example, if the string in source is investments, the start

  2. index is 2, and the character count is 4, the characters vest will be copied from source into result

  3. and a ‘\0’ will be appended.

15

  1. Function GetSubstring must:

  2. 1. Handle the following three situations:

18

a. If start is within the string in source and count does not extend beyond the end of it:

19

Copy count characters into the result array and append a ‘\0’.

20

b. If start is within the string in source but count does extend beyond the end of it:

21

Copy all characters remaining in source into the result array and append a ‘\0’.

22

c. If start is beyond the end of the string in source:

23

Store only a ‘\0’ in the result array.

  1. 2. Return a pointer to the first element of the result array.

  2. 3. Use only one variable other than formal parameters source, start, count, and result; it

  3. must be an automatic variable of type “pointer to char”.

  4. 4. Not call any functions, use any macros, or display anything.

  5. 5. Not use the sizeof operator (it would not help anyway).

  6. 6. Not use index or pointer offset expressions like pointer[i] and *(pointer + i). Compact or

  7. moving pointer expressions like *pointer++ and pointer++ are more appropriate. If you have

  8. trouble with this it may help to write the program using index notation first, then convert to

  9. compact or moving pointers.

33

  1. File C1A6E3_main.c must contain function main, which:

  2. 1. Prompts the user to enter a sequence of 0 or more arbitrary printable characters (which may

  3. include spaces) then stores them as a string in a 256-element character array named source.

  4. 2. Prompts the user again to enter a space-separated start index and character count on the

  5. same line then stores them in type int variables named start and count, respectively.

39 3. Calls GetSubstring(source, start, count, result), where result is a 256-element

  1. character array, and displays the results of the extraction in the following format, where the

  2. quotes, commas, and the literal word extracts are all required. This output is for the example

  3. provided in the description of the GetSubstring function above:

43 “investments”, 2, 4, extracts “vest”

  1. The pointer returned by GetSubstring, not the result array itself, must be used to display the

  2. extracted substring.

46

  1. Test your program several times, using at least the 7 user entry sets shown on the next page.

  2. Submitting your solution

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

  1. and with both 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.

1992-2022 Ray Mitchell Page 1 of 2 of C1A6E3

Personalized C1A6 requirements exclusively for Jose Medrano (U09845800)

C/C++ Programming I (Section 174273)

54

Recommended Program Test Sets

55

56

source

start

count

You should get

57

This is really fun

2

800

is is really fun

58

This is really fun

261

9

(an empty string)

59

This is really fun

0

12

This is real

60

one two three

5

87

wo three

61

one two three

18

7

(an empty string)

62

one two three

6

5

o thr

63

one two three

0

3

one

64

(an empty string)

3

23

(an empty string)

65

66

67

  1. Hints:

  1. All hints assume the prototype for GetSubstring is:

  2. char *GetSubstring(const char source[], int start, int count, char result[]);

73 Remember that for parameter declarations only the forms type name[] and type *name are

  1. functionally equivalent and both mean “name is a pointer to type”.

  1. A common error is to mistakenly return a pointer to the end of the extracted substring.

  1. If you are doing source + start or start + count you are on the wrong track.

  1. An optimal solution for GetSubstring will contain the statement *result++ = *source++;

  1. The following 2-loop algorithm is recommended but not required. Note that the loops are not

  1. nested. If it is not clear what this algorithm is doing you should draw a step-by-step diagram:

  2. 1. Save a copy of result.

  3. 2. First loop

  4. Loop through each successive character in source until either the end of the string is found or

  1. the offset specified by start is reached. Increment source and decrement start as you

  2. proceed as appropriate.

  3. End first loop

  1. 3. Second loop

  2. Copy successive characters from source (as updated in step 2) into result until the null

  3. terminator character is reached (don’t copy it) or until count characters have been copied,

  1. whichever comes first. Increment source and result as you proceed as appropriate.

  2. End second loop

  3. 4. Copy a null terminator character, ‘\0’, into *result.

  1. 5. Return the copy of the original value of result saved in step 1.

1992-2022 Ray Mitchell Page 2 of 2 of C1A6E3

Page 7 (7/11/2023)

Assignment 6 consists of FOUR (4) exercises: Solved
$24.99 $18.99