Question: Here is the code to get started: Exercise 3 For this exercise, you wi ill implement string bubble sort by explicitly using pointers instead of

 Here is the code to get started: Exercise 3 For this

Here is the code to get started:

exercise, you wi ill implement string bubble sort by explicitly using pointers

Exercise 3 For this exercise, you wi ill implement string bubble sort by explicitly using pointers instead of particular, you will implement the data structure as a 1-dimensional array of character pointers, char Strings[NUMJ. Thus, the index into the data structure yields a pointer, e.g., Stringslil gives you the i th pointer (of type char*). Stringsli]lil will, inturn, give you the j-th character of the i-th string This behavior is superficially identical to that of Exercises 1and 2, butthe implementation of the data structure is quite different. A benefit of this approach is that it allows the programmer to define the length of strings at runtime, instead of fixing it during coding Study the malloc0 and free0 functions, which are part of the Cstandard library, and are used to allocate rence for and free up, respectively, space in memory for a variable (see P&M Ch. 26; and the malloclfree in stdlib.h). The C function malloc0 is quite like the new operator in Java. Here is an example on how to use malloc0 to allocate 57 bytes of memory for a string (of max length 56plus one for the NULL terminating character char s s malloc(57); allocate 57 bytes for s puts("Enter a string:"); fgets(s, 57, stdin); print ("You typed In%sin", s); free(s) good practice to free up those 57 bytes so something else can use that space Observe that the following lines of code in Cand Java are equivalent char s malloc(57); in C charo s new char[57]; in Java Note: If you wanted to use a data type that is biggerthan a single byte, use the sizeof0 operator to determine the size in bytes float* s malloc(57'sizeof(float)); in C float[] s new float[571 in Java In C, there is a free0 function which frees up the memory allocated for a variable by malloc0.There is no such function in Java (the JVM does automatic garbage collection, and does not require the programmer to keep track of when a data structure is no longer needed). It is good practice to use free0 to free up the memory that was used for a variable right before that variable is about togo outof scope, or otherwise lost ifyou do not use free0, but it may start hogging to the program. Your program will still run corr memory unnecessarily, a frequent cause of "memory leaks" which often cause programs to bloat up during the course of the execution Using the starter file ex3.c, implement bubble sort of strings. Your code must strictly follow thes specifications Add "#include " near the top ofthe Cfile to tell the compiler that you will be using the C standard library (malloc is partof that library) Use malloc0 to allocate only as much memory space as needed for each string read. (Remember that a C string ends with a NULL characte In the main code fragment that performs the sort, the comparison of two strings must still be done by strcmp0 or strncmp0 The swap of two strings must be done by swapping pointervalues. You must not swap two strings using strcpy0/strncpy0 or using your own loop to swap them a character at a time Add a loop near the end of the code to free0 up space that was allocated foreach of the strings Name the file containing your code ex3.c. Compile and run your program on inputs of your choice, and also make sure it runs correctly on the sample inputs provided

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!