Write a function that orders an array of items

Assignment Help Computer Engineering
Reference no: EM132107454

Programming in C

Problem 1

Ordering Arrays Using Pointers

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). Don't 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

Problem 2

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, dinner's 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 (it's 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]

Reference no: EM132107454

Questions Cloud

Concept of co-creation of value : What are the differences between the concept of "Co-creation of value" and the concept of "customer participation in co-production" for services products.
Anova analysis for marketing agents : What is the purpose, or value I suppose, of knowing how to run and understand the results of the ANOVA analysis for marketing agents.
Larger competitors like coles and woolworths : Imagine you are the marketing manager for ALDI. From a marketing perspective, how would you attempt to beat larger competitors like Coles and Woolworths?
Develop the voice of the company : How does the marketing department help to develop the voice of the company?
Write a function that orders an array of items : Write the function so that it can order an array of arbitrary items by passing a not only the array.
Placed at the beginning or end of a survey : In Marketing Research should demographic questions be placed at the beginning or end of a survey? And reasons why?
What is at risk in health care marketing : What is at risk in health care marketing when the brand gets it wrong?
Write an expression that will cause the following code : Write an expression that will cause the following code to print "Equal" if the value of sensorReading is "close enough" to targetValue.
What is social media marketing : What is social media marketing? And how can it have a negative impact on a company?

Reviews

Write a Review

Computer Engineering Questions & Answers

  Questionwrite a short program of five to ten lines in

questionwrite a short program of five to ten lines in assembly language to accomplish something. your program should

  Describe the two site examples which have images or

what are two site examples that have images or multimedia that are used effectively on the site? why do you think they

  What is the objective of unique labeling in real world

Why should labels be unique, what objective does unique labeling serve in real world? Why is assignment of accountability significant? What would be consequence of not having it?

  How you would find that bring it back to foreground again

Explain how you would find that process and bring it back to the foreground again. If you have an out-of-control process running in the background.

  Write a program that generates random numbers

Write a program that generates 1000 random numbers between 0 and 100000. Display number of odd values generated as well as smallest and the largest of values.

  Find whether the numbers would be an arithmetic

assume you are given a list of n integers in random order. Describe an algorithm that will determine whether the numbers would be an arithmetic progression if they were sorted.

  Why use asymptotic notation instead of running time

Why use asymptotic notation instead of running time or operation counts and when does it make more or less sense?

  Describe the output for sequence of queue operations

Describe the output for the following sequence of queue operations: enqueue(5), enqueue(3), dequeue(), enqueue(2), enqueue(8), dequeue(), dequeue(), enqueue(9).

  Investigate the growth in the size of main memory

Investigate the growth in the size of main memory (RAM) from the time the platform was developed to the present day. Create a chart showing milestones in memory growth and the approximate date.

  What two entities does a local loop connect

What is the typical frequency range for the human? What features make frame relay so attractive? voice? What two entities does a local loop connect?

  State diagram for coffee machine controller

Raw behavioural specs utilizing the state diagram for the coffee machine controller explained as follow. State events, State actions.

  Discuss idea that applications have to visually consistent

From the e-Activity, discuss the idea that all applications have to be visually consistent. Argue whether you agree or disagree with the idea and why.

Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd