(5/5)

# i. Create a dummy Team key for the Cubs by calling the createTeam function using empty strings and zero values except for the team name (which should be \"Cubs\").

INSTRUCTIONS TO CANDIDATES

We have been given starter codes, we just have to complete 3 functions.

3.1 Sorting the Wrong Way

In this activity, you will implement a selection sort algorithm to sort an array of structures. Refer to lecture notes, your text, or any online source on how to implement the selection sort algorithm. The order by which you will sort the array will be according to the total team payroll (in dollars) in increasing order.

Instructions

1. Familiarize yourself with the Team structure and the functions provided to you (the program automatically loads the data from the CSV file and provides an array of teams).

2. Implement the selectionSortTeamsByPayroll function in the team.c file as specified by the header documentation.

3. Compile and run your program (use make which produces an executable called mlbDriver) and verify the results.

3.2 Slightly Better Sorting

The Team structure has many different data fields; wins, losses (and win percentage), team name, city, state, payroll, average salary. Suppose that we wanted to re-sort teams according to one of these fields in either ascending or descending order (or even some combination of fields? state/city for example). Doing it the wrong way as above we would need to implement no less than 16 different sort functions!

Most of the code would be the same though. Only the criteria on which we update the index of the minimum element would differ. Instead, it would be better to implement one sorting function and make it configurable: provide a means by which the function can determine the relative ordering of any two elements. The way of doing this in C is to define a comparator function.

Comparator functions must have the following signature:

int cmp(const void *a, const void *b);

Such a function takes two arguments a and b (specified by generic void pointers) and returns an integer with the following "contract": it returns

A negative value if a < b

Zero if a is equal to b

A positive value if a > b

Several examples have been provided for comparing structures based on several different criteria. The usual pattern is to cast the arguments to the expected types, then to examine the relevant field(s) and return a result that orders the two teams appropriately.

Instructions

1. Implement the selectionSortTeams function by using the code in Activity 1 with appropriate modifications (use the provided compar function to find the minimum element each time)

2. Look at the comparator functions provided to you and to the bubble sort algorithm example on how you might use a comparator function

3. Implement your own comparator function that orders Teams according to the total payroll in decreasing order.

3.3. Sorting the Right Way

The better way of doing this is to leverage the standard C library’s sorting function. The signature of this function is as follows:

void qsort(void *base, size_t nmemb, size_t size,

int(*compar)(const void *, const void *));

where

base is the array to be sorted

nmemb is the number of elements in the array

size is the size of each element (use sizeof())

compar is a comparator function pointer

Instructions

1. Examine the source files and observe how comparator functions are implemented and how the qsort function is called.

2. Implement your own comparator function that orders Teams according to payroll in increasing order.

3. Use your function in the main program to re-sort the array and print out the results.

3.4. Searching

The standard C library offers several search functions. The two that we will focus on are as follows.

lfind a linear search implementation that does not require the array to be sorted. The function works by iterating through the array and calling your comparator function on the provided key. It returns the first instance such that the comparator returns zero (equal).

bsearch a binary search implementation that requires the array to be sorted according to the same comparator used to search.

Both functions require a comparator function (as used in sorting) and a key upon which to search. A key is a "dummy" instance of the same type as the array that contains values of fields that you're searching for.

Instructions

1. Examine the searching code segment in the mlbDriver.c file and understand how each function is called.

2. Observe that each of the 4 searches have different results. Discuss with your partner (or yourself) why this is the case.

3. Based on your observations add code to search the array for the team representing the Chicago Cubs.

i. Create a dummy Team key for the Cubs by calling the createTeam function using empty strings and zero values except for the team name (which should be "Cubs").

ii. Sort the array by team name by calling qsort using the appropriate comparator function.

iii. Call the bsearch function using your key and the appropriate comparator function to find the Team representing the Chicago Cubs.

iv. Print out the team by using the printTeam function.

(5/5)

## Related Questions

##### . Introgramming & Unix Fall 2018, CRN 44882, Oakland University Homework Assignment 6 - Using Arrays and Functions in C

DescriptionIn this final assignment, the students will demonstrate their ability to apply two ma

##### . The standard path finding involves finding the (shortest) path from an origin to a destination, typically on a map. This is an

Path finding involves finding a path from A to B. Typically we want the path to have certain properties,such as being the shortest or to avoid going t

##### . Develop a program to emulate a purchase transaction at a retail store. This program will have two classes, a LineItem class and a Transaction class. The LineItem class will represent an individual

Develop a program to emulate a purchase transaction at a retail store. Thisprogram will have two classes, a LineItem class and a Transaction class. Th

##### . SeaPort Project series For this set of projects for the course, we wish to simulate some of the aspects of a number of Sea Ports. Here are the classes and their instance variables we wish to define:

1 Project 1 Introduction - the SeaPort Project series For this set of projects for the course, we wish to simulate some of the aspects of a number of

##### . Project 2 Introduction - the SeaPort Project series For this set of projects for the course, we wish to simulate some of the aspects of a number of Sea Ports. Here are the classes and their instance variables we wish to define:

1 Project 2 Introduction - the SeaPort Project series For this set of projects for the course, we wish to simulate some of the aspects of a number of

Hire Me

Hire Me