Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

(Write a program, and show the output too ) C programming Here is the assignment : Bubble Sort using Array of Structures Here is the

(Write a program, and show the output too) C programming

Here is the assignment : Bubble Sort using Array of Structures

Here is the document

BUBBLE SORT Using Array of Structures

Here is the data. A company has several products identified by Product number, unit price and number of units sold in the six months

Product Number

Unit Price

Units Sold in six months

14

12.95

242

15

14.95

416

16

18.95

127

17

16.95

314

18

21.95

337

19

31.95

269

20

14.95

97

Assignment: Sort this table based on the units sold and display the table as follows. You see how we sorted the table ?

Product Number

Units Sold

Unit Price

15

416

14.95

18

337

21.95

17

314

16.95

19

269

31.95

14

242

12.95

16

127

18.95

20

97

14.95

How do we do it ? The program listing is given later. Complete the program

The program contains a typedef to Product_data_t with member fields to hold the product Number, unit price in float, unit sold in int.

typedef struct product_data {

unsigned char ProductNum , // to store the product number

float UnitPrice , // to store the price

int UnitSold // to store the units sold

} Product_data_t ; // NOTE THIS IS JUST A TYPE.

We then need an array of structures like this

Product_data_t TableData [ 7 ] ; // unitData is an array of structures. We have seven rows

You have to write a bubble Sort Function: This function accepts the array of structures and the number of elements in the array as input.

The prototype could be defined as

void BubbleSort (Product_data_t aData[], int count ) ;

This function will sort the array of structures in descending order of units sold

Then, write another function ShowResults that takes the array of structure and the number of elements to display the following Table

The prototype of this function could be

void ShowResults (Product_data_t *aData, int count) ;

// PROGRAM LISTING .

#include

typedef struct product_data {

unsigned char ProductNum ; // to store the product number

float UnitPrice ; // to store the price

int UnitSold ; // to store the units sold

} Product_data_t ; // NOTE THIS IS JUST A TYPE.

void BubbleSort (Product_data_t aData[], int count ) ; // prototype

void BubbleSort (Product_data_t aData[], int count )

{

// TASK 1

// complete this function to sort the structure using bubble sort

// Note: Structures can assigned to each other, but cannot be compares unless we compare each fields.

// when swaping structures, you use temp structure to swap value just like how you swap int variables.

}

void PrintData ( struct product_data aData[], int count )

{

// TASK 2

// complete this function

// use a for loop to print the table

}

int main ( )

{

// I have given the data for you.

Product_data_t TableData [ 7 ] =

// Initialize the structure using the values given above

{

14, 12.95, 242,

15, 14.95, 416,

16, 18.95, 127,

17, 16.95, 314,

18, 21.95, 337,

19, 31.95, 269,

20, 14.95, 97

}

;

// TASK 3 How do you call the BubbleSort function and pass the parameters

// TAST 4 How to call the PrintData function and pass the parameters

}

You use bubble sort to sort an array of structures. .

Bubble sort algorithm is :

say int data [ 5 ] = { 8, 3, 9, 2, 1 } :

you sort this using this simple Bubble sort

for ( i = 0 ; i < 5 ; i++ )

for ( j = 0 ; j < 4 ; j++ )

{

if ( data [ j ] > data [ j + 1 ] )

swap data [ j ] and data [ j + 1 ]

}

this is the most simple bubble sort algorithm.

You would use structure instead of array.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Students also viewed these Databases questions

Question

List the different categories of international employees. page 642

Answered: 1 week ago

Question

Explain the legal environments impact on labor relations. page 590

Answered: 1 week ago