Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › What's wrong with my bubble sort?
New Posts  All Forums:Forum Nav:

What's wrong with my bubble sort? - Page 2

post #11 of 13
if (budget[count] > budget[count + 1])
{
itemp = budget[count];
budget[count] = budget[count + 1];
budget[count + 1] = itemp;

itemp = revenue[count];
revenue[count] = revenue[count + 1];
revenue[count + 1] = itemp;

stemp = city[count];
city[count] = city[count + 1];
city[count + 1] = stemp;

swap = 1;
}
Pepe Silvia
(13 items)
 
  
CPUMotherboardGraphicsRAM
255 @ 4 Ghz MSI 870-C45 GT 520 @ 1Ghz Crucial Rendition 4GB DDR3 
Hard DriveOSMonitorPower
Hitachi 1TB Win 7 19" 420W 
  hide details  
Reply
Pepe Silvia
(13 items)
 
  
CPUMotherboardGraphicsRAM
255 @ 4 Ghz MSI 870-C45 GT 520 @ 1Ghz Crucial Rendition 4GB DDR3 
Hard DriveOSMonitorPower
Hitachi 1TB Win 7 19" 420W 
  hide details  
Reply
post #12 of 13
Thread Starter 
Quote:
Originally Posted by newbie1911 View Post
if (budget[count] > budget[count + 1])
{
itemp = budget[count];
budget[count] = budget[count + 1];
budget[count + 1] = itemp;

itemp = revenue[count];
revenue[count] = revenue[count + 1];
revenue[count + 1] = itemp;

stemp = city[count];
city[count] = city[count + 1];
city[count + 1] = stemp;

swap = 1;
}


Yeah I actually just figured it out. I must be on a roll this morning but thanks for your help, I really appreciate it. Now all I have to do it sort by Revenue and it's done.

It's kind of confusing telling what my CS teacher wants because he says
Quote:
Create a function that will perform the bubble sort on the 3 arrays. The 4 function parameters are a double array, a double array, a string array and the number of cities. It's so generic because you will sort the 3 arrays simultaneously either by budget or revenue depending on the calling statement.
I have an idea but it would be copying the arrays over to a temp array then passing that array to the sort function, printing the array then calling the sorting array again with a different array passed to it.
    
CPUMotherboardGraphicsGraphics
Core i7 930 @ 4004Mhz 1.29 V Gigabyte GA-X58A-UD3R XFX Radeon 6970 nVidia 8800GTX 
RAMHard DriveOptical DriveOptical Drive
G. SKILL 6GB 1333Mhz @ 1455Mhz OCZ Vertex 3 120GB / Seagate 500GB Lite-On DVD-RW TSSTcorp DVD/RW 
CoolingOSMonitorMonitor
Prolimatech Megahalems Windows 7 x64 Professional Samsung S24A350H Dell 20" LCD 
MonitorMonitorKeyboardPower
MAG 17" LCD Samsung 50" Plasma  Focus FK-2001 Mechanical Corsair TX750 
CaseMouseMouse PadAudio
Cooler Master Storm Scout (Modded) Razer Abyssus Disney's Mickey Mouse with circuits JVC MX-GT700 
  hide details  
Reply
    
CPUMotherboardGraphicsGraphics
Core i7 930 @ 4004Mhz 1.29 V Gigabyte GA-X58A-UD3R XFX Radeon 6970 nVidia 8800GTX 
RAMHard DriveOptical DriveOptical Drive
G. SKILL 6GB 1333Mhz @ 1455Mhz OCZ Vertex 3 120GB / Seagate 500GB Lite-On DVD-RW TSSTcorp DVD/RW 
CoolingOSMonitorMonitor
Prolimatech Megahalems Windows 7 x64 Professional Samsung S24A350H Dell 20" LCD 
MonitorMonitorKeyboardPower
MAG 17" LCD Samsung 50" Plasma  Focus FK-2001 Mechanical Corsair TX750 
CaseMouseMouse PadAudio
Cooler Master Storm Scout (Modded) Razer Abyssus Disney's Mickey Mouse with circuits JVC MX-GT700 
  hide details  
Reply
post #13 of 13
Thread Starter 
Now, just putting the finial touches on it. It's functional and everything now.

The current (and final) problem is this.
When I enter "Q" the first time because I don't want to search everything is fine and it just quits and goes to the next function. BUT when I enter a city that is found and then try to quit, it doesn't work. It will just read back " not found".
It always cuts off the first letter. If I searched for overclock.net it would read back "verclock.net not found". It's not quitting after I find a city though. What should I change?


Code:

void findCityData (string city[], double budget[], double revenue[], int NumCities)

    string target;
    int pos;
    
    cout << "Enter the name of the city or Q to quit:";
    getline (cin,target);



     while(target != "Q" && target != "q")
    {
        pos = searchFunction (city, NumCities, target);
        if (pos == -1)
        {
            cout << target << " not found." << endl;
            findCityData (city, budget, revenue, NumCities);

        }
        cout << city[pos] << " has a budget of " << budget[pos] << " and revenue totaling " << revenue[pos] << endl;
        cout << "Enter the name of the city or Q to quit:";
        cin.ignore();
        getline (cin,target);
         
        
            

    }
    
       




Fixed it by getting rid of the cin.ignore.

After a few entries, when I try to quit I get a "Bus Error" in unix.
Edited by skatingrocker17 - 2/5/11 at 11:31am
    
CPUMotherboardGraphicsGraphics
Core i7 930 @ 4004Mhz 1.29 V Gigabyte GA-X58A-UD3R XFX Radeon 6970 nVidia 8800GTX 
RAMHard DriveOptical DriveOptical Drive
G. SKILL 6GB 1333Mhz @ 1455Mhz OCZ Vertex 3 120GB / Seagate 500GB Lite-On DVD-RW TSSTcorp DVD/RW 
CoolingOSMonitorMonitor
Prolimatech Megahalems Windows 7 x64 Professional Samsung S24A350H Dell 20" LCD 
MonitorMonitorKeyboardPower
MAG 17" LCD Samsung 50" Plasma  Focus FK-2001 Mechanical Corsair TX750 
CaseMouseMouse PadAudio
Cooler Master Storm Scout (Modded) Razer Abyssus Disney's Mickey Mouse with circuits JVC MX-GT700 
  hide details  
Reply
    
CPUMotherboardGraphicsGraphics
Core i7 930 @ 4004Mhz 1.29 V Gigabyte GA-X58A-UD3R XFX Radeon 6970 nVidia 8800GTX 
RAMHard DriveOptical DriveOptical Drive
G. SKILL 6GB 1333Mhz @ 1455Mhz OCZ Vertex 3 120GB / Seagate 500GB Lite-On DVD-RW TSSTcorp DVD/RW 
CoolingOSMonitorMonitor
Prolimatech Megahalems Windows 7 x64 Professional Samsung S24A350H Dell 20" LCD 
MonitorMonitorKeyboardPower
MAG 17" LCD Samsung 50" Plasma  Focus FK-2001 Mechanical Corsair TX750 
CaseMouseMouse PadAudio
Cooler Master Storm Scout (Modded) Razer Abyssus Disney's Mickey Mouse with circuits JVC MX-GT700 
  hide details  
Reply
New Posts  All Forums:Forum Nav:
  Return Home
  Back to Forum: Coding and Programming
Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › What's wrong with my bubble sort?