Hi, everyone. I'm just starting in C++, and one of my homework assignments is driving me INSANE. I'm sure it's a stupid error, but I can't seem to find it.
The webpage for the assignment is located at:
http://www.cwu.edu/~schwing/cs361/CPPprog2.html
You can get the plastic.dat file here.
I'm SURE the problem is in
I normally wouldn't be asking for help, but the program is already late, and I honestly cannot find the error. Please help???
Edit, 3:30 PM: Oh, and I'm using VS.Net 2003 Pro, if that helps. If possible, I would *really* like to try to solve this today... I need to go home for the weekend, and I have to catch the bus at 8:30pm. :-/ If that doesn't work out, I would still appreciate any help and suggestions!
The webpage for the assignment is located at:
http://www.cwu.edu/~schwing/cs361/CPPprog2.html
You can get the plastic.dat file here.
I'm SURE the problem is in
void performBucketSort(Supplier s[], int iSizeOfArray, int iMaxDigits). Argh!#include <iostream>
#include <fstream>
#include <math.h>
using namespace std;
struct Supplier
{
char companyID[5];
int price;
};
void performBucketSort(Supplier s[], int iSizeOfArray, int iMaxDigits);
void printCheapestTenSuppliers(Supplier s[]);
int getSignificantDigit(int iNumber, int iPlacesFromRightmost);
const int MAX_DIGITS = 4;
const int SUPPLIERS_ARRAY_SIZE = 1000;
int main()
{
// Create needed variables
Supplier suppliers[SUPPLIERS_ARRAY_SIZE];
ifstream fin("plastic.dat");
for(int i = 0; i < SUPPLIERS_ARRAY_SIZE; i++)
{
cout << i << endl;
fin >> suppliers[i].companyID >> suppliers[i].price;
}
cout << "read in file" << endl;
for(int i = 0; i < 10; i++)
{
cout << suppliers[i].companyID << " " << "$" << suppliers[i].price << endl;
}
// Now do the bucket sort...
performBucketSort(suppliers, SUPPLIERS_ARRAY_SIZE, MAX_DIGITS);
cout << "bucket sort performed" << endl;
// Print out the top ten suppliers.
printCheapestTenSuppliers(suppliers);
// Read file into array of type Supplier
// Do bucket sort
// Get max number of digits
// Loop over digits of the integers starting with least significant.
//
// Distribution pass: Place each value of single-subscripted array into bucket in
// double-subscripted array based on value in current digit position of loop.
// For example, if the data is to be given by four digit integers and the current
// digit being sorted in the loop is the 3rd, then 623 is placed in bucket 6, 9476
// is placed in bucket 4 and 17 is placed in bucket 0.
// Gathering pass: Values placed back into single-subscripted array by going in
// order through each of the rows (buckets) and retrieving values in order from
// each bucket. For the above numbers the retrieval order would be 17, 9476, 623.
// Continue to loop until after consideration of the most significant digit. After
// final gathering pass, the array is ordered.
// Print cheapest ten suppliers
// Use char as a placeholder
char chExit;
cout << "Press any key to exit... ";
cin >> chExit; // Once char has been read in, program will exit.
// Use char as a placeholder
cout << "Press any key to exit... ";
cin >> chExit; // Once char has been read in, program will exit.
}
// Bucket sort. Pass in supplier array by reference.
void performBucketSort(Supplier s[], int iSizeOfArray, int iMaxDigits)
{
// Create double-subscripted array.
Supplier suppliersBucket[SUPPLIERS_ARRAY_SIZE][10];
// int array
int iSize[10];
// We want enough slots that if for example, all numbers ended in 0,
// they would all fit in the 0 bucket; therefore, we'll use
// SUPPLIERS_ARRAY_SIZE for the number of slots.
// Loop to control number of passes
for(int iPass = 0; iPass < iMaxDigits; iPass++)
{
for(int i = 0; i < 10; i++)
{
iSize[i] = 0;
cout << i << " " << s[i].companyID << " " << s[i].price;
}
// Loop to place numbers into buckets
for(int i = 0; i < iSizeOfArray; i++)
{
// Store current number's significant digit
int iSignificantDigit = getSignificantDigit(s[i].price, iPass);
suppliersBucket[iSize[iSignificantDigit]][iSignificantDigit] = s[i];
iSize[iSignificantDigit]++;
}
/////////////////////////////////////////
// Now copy the numbers back in order. //
/////////////////////////////////////////
int iSlotToPlaceRecord = 0;
for(int iCurrentBucket = 0; iCurrentBucket < 10; iCurrentBucket++)
{
for(int i = 0; i < iSize[i]; i++)
{
s[iSlotToPlaceRecord] = suppliersBucket[i][iCurrentBucket];
iSlotToPlaceRecord++;
if(i < 10)
{
cout << i << " " << s[iSlotToPlaceRecord].price << endl;
}
}
}
printCheapestTenSuppliers(s);
char c;
cin >> c;
}
}
void printCheapestTenSuppliers(Supplier s[])
{
cout << "Company ID Price per Ton" << endl;
for(int i = 0; i < 10; i++)
{
cout << i << " " << s[i].companyID << " " << "$" << s[i].price << endl;
}
char chContinue;
cout << "Press a key to continue... ";
cin >> chContinue;
}
int getSignificantDigit(int iNumber, int iPlacesFromRightmost) // 0 would be for the rightmost digit.
{
// Following formula, if iPlacesFromRightmost is renamed to n, means:
// Take the number you want to get a significant digit from, and get the remainder of 10^(n + 1)
// Must add 1 to n if using it for mod because n is
// one less than the power of 10 we want to use..
// Divide result by 10^(n) and cast it to an int to get the single digit.
// This will work on any number.
return (int) (iNumber % (int) (pow(10, (iPlacesFromRightmost + 1))) / (int) (pow(10, iPlacesFromRightmost)));
}
I normally wouldn't be asking for help, but the program is already late, and I honestly cannot find the error. Please help???
Edit, 3:30 PM: Oh, and I'm using VS.Net 2003 Pro, if that helps. If possible, I would *really* like to try to solve this today... I need to go home for the weekend, and I have to catch the bus at 8:30pm. :-/ If that doesn't work out, I would still appreciate any help and suggestions!
