COP 3223C Homework 2

1 DESCRIPTION

For this assignment you will be working with numeric types, functions, and arrays. You will write three functions named IsDecimalInteger, GetSortingOrder, and RemoveExtraSpaces. All three functions should be in a single C source file named homework2.c. The descriptions of each function are listed below in the Function Requirements Section. Feel free to implement additional “helper” functions as you see fit.

2 DELIVERABLES

A single C source file named homework1.c must be submitted to Webcourses by the assignment deadline (posted in Webcourses).

The source file you submit should contain the following functions. Please note that these functions are useful tools, which are not a part of the C Standard Library. Being developed well, you can use them in real-world projects.

_Bool IsDecimalInteger(char str[]);

Description: The function takes a string (character array) and checks if it is a valid decimal representation of an integer number. Please note that using functions like atoi(…) from the Standard Library is not really helpful here since the actual (mathematical) integer number might be larger than the maximum possible value of the type unsigned long long int. Your function must not have any restriction on the integer values (the length of the string representation). If str represents an integer, the function must return 1. Otherwise – return 0.

char GetSortingOrder(double arr[], size_t arr_size);

Description: The function checks if the array arr (whose elements are of type double) is sorted (elements come in non-decreasing order: if ? < ?, then ???[?] ≤ ???[?]), reverse sorted (elements come in non-increasing order), or unsorted.

Please note that, according to this definition, an array whose elements are all equal is both – sorted and reverse sorted. Let us call such an array a constant array.

Return Value: The function must return the following values:

• If the array is sorted, then return 1;

• If the array is reverse sorted, then return 2;

• If the array is constant, then return 3;

• If the array is unsorted, then return 0;

• If the array’s size (arr_size) is zero (empty array), then return -1 (error codes are often negative – this is not required, but rather is a usual convention in the world of programming).

10 Bonus Points if your function will go through the array only once. It is important to reduce the number of operations in your code to make program run faster. For an array of 10 elements this would not be even noticed, but for an array of 2,000,000,000 elements this is very essential.

void RemoveExtraSpaces(char txt[]);

Description: The function takes a text (character array) and removes the extra spaces: Any spaces at the beginning of the text;

Any spaces at the end of the text;

If there are more than one consecutive space character (‘ ‘) inside the text, one space remains, and the other ones are removed.

There is no output value. The function has to modify the array passed to it as the parameter txt. As you remember, modifications in an array passed to a function are reflected in the calling function.

Please note that the string (character array) ends at the first End-Of-String symbol (‘\0’). Thus, the length of the string can be determined easily.

Finally, having such a function in your library is a good thing. It can correct a text your program is obtaining from user’s input or from files. You can further extend its functionality by removing spaces before punctuation signs and adding them after those. For example, “Thus ,if we think

carefully…” is replaced by “Thus, if we think carefully…”.

This is one of those exercises, which can go a long way. In any case, working with strings efficiently is a desired quality for the real world of Engineering.