Question: *Program is to be written in C* PR01 In this program you will write a function that orders an array of items. While our immediate
*Program is to be written in C*
PR01
In this program you will write a function that orders an array of items. While our immediate example will be a deck of cards, we will write the function so that it can order an array of arbitrary items by passing a not only the array, but also the size of the array and a pointer to an appropriate comparator function.
To be truly generic, our array must be able to contain data elements beyond primitive data types (int, double, etc.). We want to be able to sort, for example, an array of strings. More generally, we will want to be able to sort an array of structures (which we will study shortly). To accomplish this, our array will ALWAYS be an array of void pointers where each pointer contains the address of one of the elements in the array, whether that item is a single character or a multimegabyte data record. The pointers will be of type void because our sort function has no way of knowing what type of data the pointer points to.
Our comparator function will take two pointers to two items to be compared. If the first item should be placed before the second item, the function should return a negative integer. If the first item should be placed after the second item, the function should return a positive integer. If the two items are equivalent (in terms of ordering) the function should return zero.
You may use any sorting algorithm you like (except bogosort), including the most basic, brute force version of bubble sort the sorting algorithm is not the focus here.
To test your sorting algorithm, generate a data set containing ten random integers in the range -100 to +100 (such that -100 and +100 are NOT possible values). Remember, your array will be an array of pointers where each pointer points to one of the integers in the data set. There are two ways to accomplish this. The first is to dynamically allocate memory for each integer separately (in a loop) and store the pointer in the array. This is the most general purpose approach. The second is to create an array of integers and then loop through this array assigning a pointer to each element to the corresponding pointer in the array of pointers that will be passed to the sort function. For this problem, allocated each integer individually (you will use the other method in the next problem). Dont forget to free up any memory you have allocated before your program terminates.
Write two comparator functions, one that sorts ascending and the other that sorts descending. Note that once you have one of these, there is nothing to prevent you from having the other call the first one and modify the return value appropriately.
Have your program print the data out three ways first is the original data, second is the data in ascending order, and second is the data in descending order. Each of these three printouts should be on a single line in columns that are four wide and with each value having an explicitly positive/negative sign and being exactly two digits long (so add a leading zero if needed).
The following is what a run might look like:
O: -58 +60 +66 -66 -34 -96 +36 +06 -02 +87 +34 -13 +98 +12 -88 A: -96 -88 -66 -58 -34 -13 -02 +06 +12 +34 +36 +60 +66 +87 +98 D: +98 +87 +66 +60 +36 +34 +12 +06 -02 -13 -34 -58 -66 -88 -96
PR02
Write a program that uses the sort function from the prior problem to sort a string of characters, in ascending order according to their ASCII values. For this task use the string Mary had a little lamb -- and with a bit of berry jam, dinners set to go.
The ideal goal here is to sort this string in place (i.e., modify the contents of the string so that when it is printed out the characters appear in sorted order). However, the sort function sorts the pointers so that they point to the data elements in the right order. So instead what you can do is first perform the sort on an array of pointers as before. To accomplish this, you want to use the second method to create your array of pointers; namely have each pointer point to the next character in the string. Be sure to NOT include the NUL terminator first, this is not part of the string (its a representation artifact), and second, it needs to stay at the end of the sorted string.
Once the array of pointers is sorted, create a temporary character array of the correct size (i.e., dynamically allocated it to be the same length as the original string) and copy the characters into this array in order. Then copy the elements of this array back to the original string. Since you are modifying the string, you do NOT want to use a string literal as your data set. You need an actual character array that contains the string. The simplest way to do this is to initialize such an array. Do NOT simply assign a pointer to a string literal!
To indicate the extents of the string, surround the printed string with square brackets. Your output should look something like:
O:[Mary had a little lamb -- and with a bit of berry jam, dinner's set to go.] A:[ ',--.Maaaaaaabbbdddeeeefghhiiiijlllmmnnnooorrrrssttttttwyy]
PR03
In this problem you will shuffle a deck of cards where each card is represented by a two- character string. The first character indicates the cards value and the second indicates the cards suit. The two sequences, in ascending order, are
Value: (A,2,3,4,5,6,7,8,9,T,J,Q,K) (where A=Ace, T=10, J=Jack, Q=Queen, K=King) Suite: (S,D,C,H) (where Spades, Diamonds, Clubs, Hearts)
First, create an array of pointers to cards that are in the correct order (AS first and KH last). Print these out using four lines, thirteen cards to a line, with a column width of three.
Now shuffle the deck of cards ten times using your sort function and a comparator function that randomly chooses the ordering for the pair of elements passed to it. Print out the deck after each call to the sort function (being sure to have some kind of delimiter, even if only a blank line, between the printout for each pass).
Your output should look something like:
AS 2S 3S 4S 5S 6S 7S 8S 9S TS JS QS KS AD 2D 3D 4D 5D 6D 7D 8D 9D TD JD QD KD AC 2C 3C 4C 5C 6C 7C 8C 9C TC JC QC KC AH 2H 3H 4H 5H 6H 7H 8H 9H TH JH QH KH
After shuffle #1 QH TH KH 8H JH 9C 2H 5H 4H 7C 6H 9H JC 3H KD 7H TC KC QC 3C 2C AH 6C 8C 5D 5C 4C 9D 8D JD AC JS 3D QS 6S TD 2D QD 7D AS KS 8S 3S 6D 7S AD 4S TS 2S 9S 5S 4D
After shuffle #2 4D TS 9S 5S 6D KS 2S 4S 7S AD 6S 8S 3S QS 2D 7D TD JD AS 8D QD 4C 3D 5D 5C JS QC 7H AH 9D 2C 3H AC 3C 2H 8C KC 6C 9H JC JH KD 7C 6H 9C 4H QH KH 5H TC TH 8H
...(remaining results removed for brevity)
After shuffle #10 8D 3D 6D 7D 6S KS JS 3H AD TS 9S 2D 5S 3S QS 2S 7H JD 7S TD KD JC 5D 4D 5H AS KC 2C 9D 7C AC 8S 2H 4C QH 3C 6H 5C JH 9C KH AH QD 4S 9H 6C 8H 4H TH 8C QC TC
HINTS AND SUGGESTIONS As always: Think incremental development!
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
