Chapter 8: BUBBLE SORT Using Array of Structures Here is the data. A company has
ID: 3571528 • Letter: C
Question
Chapter 8: 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
842
15
14.95
416
16
18.95
127
17
16.95
514
18
21.95
437
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
14
842
12.95
17
514
16.95
18
437
21.95
15
416
14.95
19
269
31.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 <stdio.h>
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 ] = {
{ 14, 12.95, 842},
{ 15, 14.95, 416},
{ 16, 18.95, 127},
{ 17, 16.95, 514},
{ 18, 21.95, 437},
{ 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
}
Product Number
Unit Price
Units Sold in six months
14
12.95
842
15
14.95
416
16
18.95
127
17
16.95
514
18
21.95
437
19
31.95
269
20
14.95
97
Explanation / Answer
#include <stdio.h>
#include <stdbool.h>
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.
Product_data_t TableData [ 7 ];
void BubbleSort (Product_data_t aData[], int count );
void ShowResults (Product_data_t *aData, int count) ;
int main()
{
Product_data_t TableData [ 7 ] = {
{ 14, 12.95, 842},
{ 15, 14.95, 416},
{ 16, 18.95, 127},
{ 17, 16.95, 514},
{ 18, 21.95, 437},
{ 19, 31.95, 269},
{ 20, 14.95, 97}};
BubbleSort (TableData, 7 );
// printf(" %d %f",TableData[2].UnitSold,TableData[2].UnitPrice);
ShowResults (TableData, 7 ) ;
return 0;
}
void BubbleSort(Product_data_t aData[], int count)
{
int temp;
unsigned char temp_name;
float temp_cost;
int i,j;
bool swapped = false;
// loop through all numbers
for(i = 0; i < count-1; i++) {
swapped = false;
// loop through numbers falling ahead
for(j = 0; j < count-1-i; j++) {
if(aData[j].UnitSold < aData[j+1].UnitSold) {
temp = aData[j].UnitSold;
aData[j].UnitSold = aData[j+1].UnitSold;
aData[j+1].UnitSold = temp;
temp_name = aData[j].ProductNum;
aData[j].ProductNum = aData[j+1].ProductNum;
aData[j+1].ProductNum = temp_name;
temp_cost = aData[j].UnitPrice;
aData[j].UnitPrice = aData[j+1].UnitPrice;
aData[j+1].UnitPrice = temp;
swapped = true;
// printf(" => swapped [%d, %d] ",list[j],list[j+1]);
}else {
//printf(" => not swapped ");
}
}
// if no number was swapped that means
// array is sorted now, break the loop.
if(!swapped) {
break;
}
// printf("Iteration %d#: ",(i+1));
// display();
}
}
void ShowResults (Product_data_t aData[], int count) {
int i;
// navigate through all items
for(i = 0; i < count; i++) {
printf("%d %d %f ",aData[i].ProductNum,aData[i].UnitSold,aData[i].UnitPrice);
}
}
output:
14 842 12.950000
17 514 16.950001
18 437 21.950001
15 416 416.000000
19 269 31.950001
16 127 127.000000
20 97 14.950000
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.