Now, I could easily do this if I could just keep the 2D array of the bucket in a rectangular shape, but instead it's supposed to be able to become a sort of stairsteppish thing, and each column will add as many rows as it needs during the sort, and nothing more.
So here's where my problem is, I know some basics of dynamic memory, but what I need to be able to do is make each column in the bucket have a different number of rows, and I just don't know the code to do that.
Here's how the program looks so far, it's completely set up to go dynamic, I just need to know how to have each column have a dynamic number of rows.
Code
#include <iostream.h>
#include <stdlib.h>
#include <windows.h>
#include <iomanip.h>
class sort
{
private:
int totalnumbers;
int bucket[11][10];
int numberlist[10];
public:
int currentplace ;
void input(int amount);
void organize();
void cleanbucket();
void clearbucket();
void output();
void showbucket();
};
void sort::input(int amount);
void sort::organize();
void sort::cleanbucket();
void sort::clearbucket();
void sort::output();
void sort::showbucket();
void gotoxy(short x, short y);
main()
{
sort bucket;
bucket.clearbucket();
bucket.currentplace = 1;
system("title Bucket Sort");
system("color 0A");
cout << "Welcome to the Bucket Sort Program, this program will sort ten numbers that\nyou enter"
<< " into it from least to greatest. The numbers must be from 0 to 999." << endl;
bucket.input(10);
for(int amount = 3; amount > 0; amount--)
{
bucket.organize();
bucket.showbucket();
bucket.cleanbucket();
}
bucket.output();
return 0;
}
void gotoxy(short x, short y) //Moves cursor to an XY position on the console
{
HANDLE hConsoleOutput;
COORD Cursor_an_Pos = {x,y};
hConsoleOutput = GetStdHandle (STD_OUTPUT_HANDLE);
SetConsoleCursorPosition(hConsoleOutput, Cursor_an_Pos);
}
void sort::input(int amount)
{
totalnumbers = amount-1;
for(int currentinput = 0; currentinput < amount; currentinput++)
{
cout << "\nPlease enter number " << currentinput+1 <<": ";
cin >> numberlist[currentinput];
system("cls");
cout << "Welcome to the Bucket Sort Program, this program will sort ten numbers that\nyou enter"
<< " into it from least to greatest. The numbers must be from 0 to 999." << endl;
gotoxy(0, 2);
}
}
void sort::organize()
{
for(int currentnumber = 0; currentnumber < totalnumbers+1; currentnumber++)
{
bucket[bucket[0][(numberlist[currentnumber]%(10*currentplace))/currentplace]+1][(numberlist[currentnumber]%(10*currentplace))/currentplace] = numberlist[currentnumber];
bucket[0][(numberlist[currentnumber]%(10*currentplace))/currentplace]+=1;
numberlist[currentnumber] = 0;
}
currentplace *= 10;
}
void sort::cleanbucket()
{
int currentplacement = 0;
for(int currentrow = 0; totalnumbers >= currentrow; currentrow++)
{
for(int numbers = 1; numbers < bucket[0][currentrow]+1; numbers++)
{
numberlist[currentplacement] = bucket[numbers][currentrow];
currentplacement++;
}
}
clearbucket();
}
void sort::clearbucket()
{
for(int x = 0; x < 11; x++)
{
for(int y = 0; y < 10; y++)
{
bucket[x][y] = 0;
}
}
}
void sort::output()
{
system("cls");
cout << "Your numbers in order from least to greatest go:" << endl;
for(int amount = 0; totalnumbers+1 > amount; amount++)
{
cout << numberlist[amount] << endl;
}
}
void sort::showbucket()
{
system("cls");
for(int x = 0; x < 10; x++)
{
for(int y = 0; y < 11; y++)
{
cout << setw(4) << bucket[y][x] << ' ';
}
cout << endl << endl;
}
system("pause");
}
#include <stdlib.h>
#include <windows.h>
#include <iomanip.h>
class sort
{
private:
int totalnumbers;
int bucket[11][10];
int numberlist[10];
public:
int currentplace ;
void input(int amount);
void organize();
void cleanbucket();
void clearbucket();
void output();
void showbucket();
};
void sort::input(int amount);
void sort::organize();
void sort::cleanbucket();
void sort::clearbucket();
void sort::output();
void sort::showbucket();
void gotoxy(short x, short y);
main()
{
sort bucket;
bucket.clearbucket();
bucket.currentplace = 1;
system("title Bucket Sort");
system("color 0A");
cout << "Welcome to the Bucket Sort Program, this program will sort ten numbers that\nyou enter"
<< " into it from least to greatest. The numbers must be from 0 to 999." << endl;
bucket.input(10);
for(int amount = 3; amount > 0; amount--)
{
bucket.organize();
bucket.showbucket();
bucket.cleanbucket();
}
bucket.output();
return 0;
}
void gotoxy(short x, short y) //Moves cursor to an XY position on the console
{
HANDLE hConsoleOutput;
COORD Cursor_an_Pos = {x,y};
hConsoleOutput = GetStdHandle (STD_OUTPUT_HANDLE);
SetConsoleCursorPosition(hConsoleOutput, Cursor_an_Pos);
}
void sort::input(int amount)
{
totalnumbers = amount-1;
for(int currentinput = 0; currentinput < amount; currentinput++)
{
cout << "\nPlease enter number " << currentinput+1 <<": ";
cin >> numberlist[currentinput];
system("cls");
cout << "Welcome to the Bucket Sort Program, this program will sort ten numbers that\nyou enter"
<< " into it from least to greatest. The numbers must be from 0 to 999." << endl;
gotoxy(0, 2);
}
}
void sort::organize()
{
for(int currentnumber = 0; currentnumber < totalnumbers+1; currentnumber++)
{
bucket[bucket[0][(numberlist[currentnumber]%(10*currentplace))/currentplace]+1][(numberlist[currentnumber]%(10*currentplace))/currentplace] = numberlist[currentnumber];
bucket[0][(numberlist[currentnumber]%(10*currentplace))/currentplace]+=1;
numberlist[currentnumber] = 0;
}
currentplace *= 10;
}
void sort::cleanbucket()
{
int currentplacement = 0;
for(int currentrow = 0; totalnumbers >= currentrow; currentrow++)
{
for(int numbers = 1; numbers < bucket[0][currentrow]+1; numbers++)
{
numberlist[currentplacement] = bucket[numbers][currentrow];
currentplacement++;
}
}
clearbucket();
}
void sort::clearbucket()
{
for(int x = 0; x < 11; x++)
{
for(int y = 0; y < 10; y++)
{
bucket[x][y] = 0;
}
}
}
void sort::output()
{
system("cls");
cout << "Your numbers in order from least to greatest go:" << endl;
for(int amount = 0; totalnumbers+1 > amount; amount++)
{
cout << numberlist[amount] << endl;
}
}
void sort::showbucket()
{
system("cls");
for(int x = 0; x < 10; x++)
{
for(int y = 0; y < 11; y++)
{
cout << setw(4) << bucket[y][x] << ' ';
}
cout << endl << endl;
}
system("pause");
}
Help?
Edit: I guess the big thing I need is the code to change the size of memory that a pointer points to, if you give me that, I've got it, I just don't know how to resize it without deleting the entire thing.
Post has been edited 2 time(s), last time on Dec 27 2008, 5:37 am by Falkoner. Reason: Change to codebox
None.