Okay, I'm back again. From what I understand this bubble sort, should sort the entire struct in ascending order according to the price member... But it just prints out the struct in its original form for me. Probably just a stupid mistake, but can anyone point it out?
Code:#include <stdio.h>
#include <conio.h>
#define NUM 5
struct inv {
char partID[6];
int quantity;
float price;
};
void invSort(struct inv items[NUM]);
void invPrint(struct inv items[NUM]);
main()
{
struct inv items[NUM] = {{"HDWR9", 13, 4.53},
{"WDGT4", 2, 15.82},
{"POPL0", 4, 8.32},
{"RTYN6", 7, 5.40},
{"LOUN4", 61, 7.43}};
printf("Inventory\n\n");
invSort(items);
invPrint(items);
printf("\n\nPress any key to continue...");
getch();
return 0;
}
void invSort(struct inv items[NUM])
{
int inner, outer;
struct inv temp;
int didSwap;
for (outer = 0; outer < (NUM - 1); outer++) {
didSwap = 0;
for (inner = outer; inner < NUM; inner++) {
if (items[outer].price > items[inner].price) {
temp = items[outer];
items[outer] = items[inner];
items[inner] = temp;
didSwap = 1;
}
}
if (!didSwap) {
break;
}
}
return;
}
void invPrint(struct inv items[NUM])
{
int ctr = 0;
for (ctr = 0; ctr < NUM; ctr++) {
printf("Part ID: %s, Quantity: %d, Price: $%6.2f\n",
items[ctr].partID, items[ctr].quantity, items[ctr].price);
}
return;
}