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?

post #1 of 13
Thread Starter 
I'm trying to sort cities by revenue then list the cities in descending order by revenue. Then I have to do the same for budget.

Here's my code. I was trying to follow the example provided in the book but it's more difficult than I though it would be.

Code:

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

{
    int  itemp;
    string stemp;
    int swap = 1;

    
    while (swap)
    {    swap = 0;
        for (int count = 0; count < (NumCities - 1); count++)
        {
            if (budget[count] > budget[count + 1])
            {
                itemp = budget[count];
                budget[count] = budget[count + 1];
                budget[count + 1] = itemp;
            
            

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

                swap = 1;
            }
        }
        NumCities--;
   } 
    printArray(budget, revenue, city, NumCities);
}

void printArray(double budget[], double revenue[], string city[], int NumCities)
{   
    cout << endl << "----------------------------------------------"  << endl;
    cout << setw(10) << "City" << setw(14) << "Budget" << endl;
    cout << setw(10) << "-----" << setw(10) << "------" << endl;
    for (int count = 0; count < NumCities; count++)
    {    cout << setw(10) << city[count] << setw(10);
        cout << budget[count];
        cout << endl;
    }
    cout << endl << "----------------------------------------------" << endl;


Here's what its supposed to look like.....
Quote:
===============After Sorting By Budget================
City Revenue Budget
---- ------- ------
Bratenahl 14460032 21456932
Brookville 27569478 23565478
Bexley 28481063 25478963
Bradner 19690445 25687445
Blue Ash 35898625 25896325
Baltic 38000200 35000000
Boston Heights 91701052 35698452
Brecksville 62701723 45698523
Bluffton 64859332 47856932
Bolivar 33661245 49858745
Brooklyn Heights 72151754 52147854
Bremen 56988531 57985231
Bloomdale 38410275 58412475
Baltimore 89000300 58500000
Botkins 97898024 75895324
Brunswick 98478665 85474565
Bowling Green 78955154 86952354
Brooklyn 85325278 95321478
Bradford 89590323 96587423
==============After Sorting By Revenue ===============
City Revenue Budget
---- ------- ------
Bratenahl 14460032 21456932
Bradner 19690445 25687445
Brookville 27569478 23565478
Bexley 28481063 25478963
Bolivar 33661245 49858745
Blue Ash 35898625 25896325
Baltic 38000200 35000000
Bloomdale 38410275 58412475
Bremen 56988531 57985231
Brecksville 62701723 45698523
Bluffton 64859332 47856932
Brooklyn Heights 72151754 52147854
Bowling Green 78955154 86952354
Brooklyn 85325278 95321478
Baltimore 89000300 58500000
Bradford 89590323 96587423
Boston Heights 91701052 35698452
Botkins 97898024 75895324
Brunswick 98478665 85474565
Thanks
    
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 #2 of 13
you might want to say whats wrong with it since that not the whole code and it's a lot easier to debug when you know whats wrong with it.
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 #3 of 13
Thread Starter 
This is what it's giving me.
Quote:
----------------------------------------------
City Budget
----- ------
Baltimore -2147483648
Bexley -2147483648
Bloomdale -2147483648
Blue Ash 0
Baltic 0
Bluffton 0
Bolivar -1
Boston Heights-2147483648
Bradford -2147483648
Brooklyn -2147483648
Brooklyn Heights0
Bratenahl 0
Botkins 0
Bradner 0
Bowling Green0
Brecksville0
Bremen 0
Brunswick 0
Brookville0

----------------------------------------------
Press any key to continue . . .

    
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 #4 of 13
Whenever you see values like "-2147483648", it's telling you it's a garbage value. However, from what you've given us to see, there's no way one can even come close to giving you an answer with. We need more code (preferably all) to see how and what you are passing.
post #5 of 13
Thread Starter 
Here's all of my code
Code:

#include <iostream>

#include <fstream>
#include <iomanip>
#include <string>

using namespace std;

//Function prototypes
void fillArray (string city[], double budget[], double revenue[], int &NumCities);
void printarrays (string city[], double budget[], double revenue[], int NumCities);
void FindHiBudget (string city[], double budget[], int NumCities);
void AboveAvgRevenue (string city[], double revenue[], int NumCities);
void OverBudget (string city[], double budget[], double revenue[], int NumCities);
int searchFunction (string city[], int NumCities, string target);
void findCityData (string city[], double budget[], double revenue[], int NumCities);
void sortArrays (double budget[], double revenue[], string city[], int NumCities);
void printArray(double budget[], double revenue[], string city[], int NumCities);
//ADD YOUR PROTOTYPES HERE

const int ArraySize = 30;

int main ()
{
    string city[ArraySize];
    double budget[ArraySize];
    double revenue[ArraySize];
    double tosort[ArraySize];
    int NumCities;
    

    //We will now call on all of these functions
    fillArray (city, budget, revenue, NumCities);
    cout << left << fixed << setprecision(0);
    printarrays (city, budget, revenue, NumCities);

   //ADD THE CALL TO YOUR FindHiBudget function
   FindHiBudget (city, budget, NumCities);
    cout << "======================================================" << endl;

    
    //ADD THE CALL TO YOUR AboveAvgRevenue function
    AboveAvgRevenue (city, revenue, NumCities);
    cout << "======================================================" << endl;

    
    //ADD THE CALL TO YOUR OverBudget function
    OverBudget (city, budget, revenue, NumCities);
    cout << "======================================================" << endl;
    
    
    
    findCityData (city, budget, revenue, NumCities);
    cout << "======================================================" << endl;


    
     for (int i = 0; i < ArraySize; i++)
        budget[i] = tosort[i];

    sortArrays (budget, revenue, city, NumCities);
    cout << "======================================================" << endl;

    printArray(budget, revenue, city, NumCities);



    return 0;

}

void fillArray (string city[], double budget[], double revenue[], int &NumCities)
{
    ifstream indata;
    NumCities = 0;

    //Now we will open the file

    indata.open("prog1.txt");
    if (! indata)
    {
        cerr << "Error" << endl;
        
    }


    getline(indata, city[NumCities]);

    //We will now run a loop to get al of the data from the file and assign it to the proper array
    while (!indata.eof()&& NumCities < ArraySize)
    {    
        indata >> budget[NumCities];
        indata >> revenue[NumCities];

        indata.ignore();
        NumCities++;
        getline(indata, city[NumCities]);
        
    }
    return;
}

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

    cout << setw(20) << "City" << setw(20) << "Revenue" << setw(20) << "Budget" << endl;
    cout << setw(20) << "----" << setw(20) << "-------" << setw(20) << "------" << endl;
    

    //We will now print al of the data in a table
    for (int i = 0; i < NumCities; i++)
    {
        cout << setw(20) << city[i] << setw(20) << revenue[i] << setw(20) << budget[i] << endl;
    }
}

//========================================================
// ADD YOUR FindHiBudget function here
//========================================================
void FindHiBudget (string city[], double budget[], int NumCities)
{
    int i = 0;
    int HiBudget = 0;
    int temp = 0;
    
for (i = 0; i < NumCities; i++)
{
    if (budget[i] > HiBudget)
    {
        HiBudget = budget[i];
        temp = i;
    }
    
    
}
cout << "======================================================" << endl;
cout << "The city with the highest budget is " << city[temp] << endl;
}



//========================================================
// ADD YOUR AboveAvgRevenue function here
//========================================================
void AboveAvgRevenue (string city[], double revenue[], int NumCities)
{
    double average = 0;
    int sum = 0;
    int i = 0;
    
    for (i = 0; i < NumCities; i++)
    {
        
        sum = sum + revenue[i];
        
    }
    
    average = sum / NumCities;
    cout << "The average revenue is " << average << endl;
    cout << "The cities with the above average revenue are: " << endl;
    
    for (i = 0; i < NumCities; i++)
    {
        if (revenue[i] > average)
            cout << city[i] << endl;
    }
}
    






//========================================================
// ADD YOUR OverBudget function here
//========================================================
 void OverBudget (string city[], double budget[], double revenue[], int NumCities)
{
    int i = 0;
    
    cout << "The cities that are over budget are: " << endl;
    
    for (i = 0; i < NumCities; i++)
    {
        if (budget[i] > revenue[i])
            cout << city[i] << " by " << budget[i] - revenue[i] << endl;
    }
}
    
    

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);
            

    }

       


}










    
    
int searchFunction (string city[], int NumCities, string target)
{

    
    int first = 0,           // First array element
       last = NumCities - 1, // Last array element
       middle,              // Mid point of search
       position = -1;       // Position of search value
   bool found = false;      // Flag

 
   
   while (!found && first <= last)
   {
      middle = (first + last) / 2;  // Calculate mid point
      if (city[middle] == target)   // If value is found at mid
      {
         found = true;
         position = middle;
      }
      else if (city[middle] > target) // If value is in lower half
         last = middle - 1;
      else
         first = middle + 1;          // If value is in upper half
   }

  

   return position;
}




void sortArrays (double budget[], double revenue[], string city[], int NumCities)
{
    int  itemp;
    string stemp;
    int swap = 1;

    
    while (swap)
    {    swap = 0;
        for (int count = 0; count < (NumCities - 1); count++)
        {
            if (budget[count] > budget[count + 1])
            {
                itemp = budget[count];
                budget[count] = budget[count + 1];
                budget[count + 1] = itemp;
            
            

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

                swap = 1;
            }
        }
        NumCities--;
   } 
    printarrays (city, budget, revenue, NumCities);
}

void printArray(double budget[], double revenue[], string city[], int NumCities)
{   
    cout << endl << "----------------------------------------------"  << endl;
    cout << setw(10) << "City" << setw(14) << "Budget" << endl;
    cout << setw(10) << "-----" << setw(10) << "------" << endl;
    for (int count = 0; count < NumCities; count++)
    {    cout << setw(10) << city[count] << setw(10);
        cout << budget[count];
        cout << endl;
    }
    cout << endl << "----------------------------------------------" << endl;


And here's what the output is supposed to look like.
Code:

City                Revenue             Budget

----                -------             ------
Baltic              38000200            35000000
Baltimore           89000300            58500000
Bexley              28481063            25478963
Bloomdale           38410275            58412475
Blue Ash            35898625            25896325
Bluffton            64859332            47856932
Bolivar             33661245            49858745
Boston Heights      91701052            35698452
Botkins             97898024            75895324
Bowling Green       78955154            86952354
Bradford            89590323            96587423
Bradner             19690445            25687445
Bratenahl           14460032            21456932
Brecksville         62701723            45698523
Bremen              56988531            57985231
Brooklyn            85325278            95321478
Brooklyn Heights    72151754            52147854
Brookville          27569478            23565478
Brunswick           98478665            85474565
======================================================
The city with the highest budget is Bradford
======================================================
The average revenue is 59148500
The cities with above average revenue are:
Baltimore
Bluffton
Boston Heights
Botkins
Bowling Green
Bradford
Brecksville
Brooklyn
Brooklyn Heights
Brunswick
======================================================
The cities over budget are:
Bloomdale by 20002200
Bolivar by 16197500
Bowling Green by 7997200
Bradford by 6997100
Bradner by 5997000
Bratenahl by 6996900
Bremen by 996700
Brooklyn by 9996200
======================================================
Enter the name of the city or Q to quit:Q
===============After Sorting By Budget================
City                Revenue             Budget
----                -------             ------
Bratenahl           14460032            21456932
Brookville          27569478            23565478
Bexley              28481063            25478963
Bradner             19690445            25687445
Blue Ash            35898625            25896325
Baltic              38000200            35000000
Boston Heights      91701052            35698452
Brecksville         62701723            45698523
Bluffton            64859332            47856932
Bolivar             33661245            49858745
Brooklyn Heights    72151754            52147854
Bremen              56988531            57985231
Bloomdale           38410275            58412475
Baltimore           89000300            58500000
Botkins             97898024            75895324
Brunswick           98478665            85474565
Bowling Green       78955154            86952354
Brooklyn            85325278            95321478
Bradford            89590323            96587423
==============After Sorting By Revenue ===============
City                Revenue             Budget
----                -------             ------
Bratenahl           14460032            21456932
Bradner             19690445            25687445
Brookville          27569478            23565478
Bexley              28481063            25478963
Bolivar             33661245            49858745
Blue Ash            35898625            25896325
Baltic              38000200            35000000
Bloomdale           38410275            58412475
Bremen              56988531            57985231
Brecksville         62701723            45698523
Bluffton            64859332            47856932
Brooklyn Heights    72151754            52147854
Bowling Green       78955154            86952354
Brooklyn            85325278            95321478
Baltimore           89000300            58500000
Bradford            89590323            96587423
Boston Heights      91701052            35698452
Botkins             97898024            75895324
Brunswick           98478665            85474565 
    
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 #6 of 13
Thread Starter 
Edit.....new code.
Code:

#include <iostream>

#include <fstream>
#include <iomanip>
#include <string>

using namespace std;

//Function prototypes
void fillArray (string city[], double budget[], double revenue[], int &NumCities);
void printarrays (string city[], double budget[], double revenue[], int NumCities);
void FindHiBudget (string city[], double budget[], int NumCities);
void AboveAvgRevenue (string city[], double revenue[], int NumCities);
void OverBudget (string city[], double budget[], double revenue[], int NumCities);
int searchFunction (string city[], int NumCities, string target);
void findCityData (string city[], double budget[], double revenue[], int NumCities);
void sortArrays (double budget[], double revenue[], string city[], int NumCities);
void printArray(double budget[], double revenue[], string city[], int NumCities);
//ADD YOUR PROTOTYPES HERE

const int ArraySize = 30;

int main ()
{
    string city[ArraySize];
    double budget[ArraySize];
    double revenue[ArraySize];
    //double tosort[ArraySize];
    int NumCities;
    

    //We will now call on all of these functions
    fillArray (city, budget, revenue, NumCities);
    cout << left << fixed << setprecision(0);
    printarrays (city, budget, revenue, NumCities);

   //ADD THE CALL TO YOUR FindHiBudget function
   FindHiBudget (city, budget, NumCities);
    cout << "======================================================" << endl;

    
    //ADD THE CALL TO YOUR AboveAvgRevenue function
    AboveAvgRevenue (city, revenue, NumCities);
    cout << "======================================================" << endl;

    
    //ADD THE CALL TO YOUR OverBudget function
    OverBudget (city, budget, revenue, NumCities);
    cout << "======================================================" << endl;
    
    
    
    findCityData (city, budget, revenue, NumCities);
    cout << "======================================================" << endl;


    
     //for (int i = 0; i < ArraySize; i++)
        //budget[i] = tosort[i];

    sortArrays (budget, revenue, city, NumCities);
    cout << "======================================================" << endl;




    return 0;

}

void fillArray (string city[], double budget[], double revenue[], int &NumCities)
{
    ifstream indata;
    NumCities = 0;

    //Now we will open the file

    indata.open("prog1.txt");
    if (! indata)
    {
        cerr << "Error" << endl;
        
    }


    getline(indata, city[NumCities]);

    //We will now run a loop to get al of the data from the file and assign it to the proper array
    while (!indata.eof()&& NumCities < ArraySize)
    {    
        indata >> budget[NumCities];
        indata >> revenue[NumCities];

        indata.ignore();
        NumCities++;
        getline(indata, city[NumCities]);
        
    }
    return;
}

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

    cout << setw(20) << "City" << setw(20) << "Revenue" << setw(20) << "Budget" << endl;
    cout << setw(20) << "----" << setw(20) << "-------" << setw(20) << "------" << endl;
    

    //We will now print al of the data in a table
    for (int i = 0; i < NumCities; i++)
    {
        cout << setw(20) << city[i] << setw(20) << revenue[i] << setw(20) << budget[i] << endl;
    }
}

//========================================================
// ADD YOUR FindHiBudget function here
//========================================================
void FindHiBudget (string city[], double budget[], int NumCities)
{
    int i = 0;
    double HiBudget = 0;
    int temp = 0;
    
for (i = 0; i < NumCities; i++)
{
    if (budget[i] > HiBudget)
    {
        HiBudget = budget[i];
        temp = i;
    }
    
    
}
cout << "======================================================" << endl;
cout << "The city with the highest budget is " << city[temp] << endl;
}



//========================================================
// ADD YOUR AboveAvgRevenue function here
//========================================================
void AboveAvgRevenue (string city[], double revenue[], int NumCities)
{
    double average = 0;
    double sum = 0;
    int i = 0;
    
    for (i = 0; i < NumCities; i++)
    {
        sum = sum + revenue[i];
    }
    
    average = sum / NumCities;
    cout << "The average revenue is " << average << endl;
    cout << "The cities with the above average revenue are: " << endl;
    
    for (i = 0; i < NumCities; i++)
    {
        if (revenue[i] > average)
            cout << city[i] << endl;
    }
}
    






//========================================================
// ADD YOUR OverBudget function here
//========================================================
 void OverBudget (string city[], double budget[], double revenue[], int NumCities)
{
    int i = 0;
    
    cout << "The cities that are over budget are: " << endl;
    
    for (i = 0; i < NumCities; i++)
    {
        if (budget[i] > revenue[i])
            cout << city[i] << " by " << budget[i] - revenue[i] << endl;
    }
}
    
    

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);
            

    }

       


}










    
    
int searchFunction (string city[], int NumCities, string target)
{

    
    int first = 0,           // First array element
       last = NumCities - 1, // Last array element
       middle,              // Mid point of search
       position = -1;       // Position of search value
   bool found = false;      // Flag

 
   
   while (!found && first <= last)
   {
      middle = (first + last) / 2;  // Calculate mid point
      if (city[middle] == target)   // If value is found at mid
      {
         found = true;
         position = middle;
      }
      else if (city[middle] > target) // If value is in lower half
         last = middle - 1;
      else
         first = middle + 1;          // If value is in upper half
   }

  

   return position;
}




void sortArrays (double budget[], double revenue[], string city[], int NumCities)
{
    double  itemp;
    string stemp;
    int swap = 1;

    
    while (swap)
    {    swap = 0;
        for (int count = 0; count < (NumCities - 1); count++)
        {
            if (budget[count] > budget[count + 1])
            {
                itemp = budget[count];
                budget[count] = budget[count + 1];
                budget[count + 1] = itemp;
            
            

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

                swap = 1;
            }
        }
        NumCities--;
   } 
    printarrays (city, budget, revenue, NumCities);
}

void printArray(double budget[], double revenue[], string city[], int NumCities)
{   
    cout << endl << "----------------------------------------------"  << endl;
    cout << setw(10) << "City" << setw(14) << "Budget" << endl;
    cout << setw(10) << "-----" << setw(10) << "------" << endl;
    for (int count = 0; count < NumCities; count++)
    {    cout << setw(10) << city[count] << setw(10);
        cout << budget[count];
        cout << endl;
    }
    cout << endl << "----------------------------------------------" << endl;


Text file:
Quote:
Baltic
35000000
38000200
Baltimore
58500000
89000300
Bexley
25478963
28481063
Bloomdale
58412475
38410275
Blue Ash
25896325
35898625
Bluffton
47856932
64859332
Bolivar
49858745
33661245
Boston Heights
35698452
91701052
Botkins
75895324
97898024
Bowling Green
86952354
78955154
Bradford
96587423
89590323
Bradner
25687445
19690445
Bratenahl
21456932
14460032
Brecksville
45698523
62701723
Bremen
57985231
56988531
Brooklyn
95321478
85325278
Brooklyn Heights
52147854
72151754
Brookville
23565478
27569478
Brunswick
85474565
98478665
    
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 #7 of 13
Code:
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>

using namespace std;

//Function prototypes
void fillArray (string city[], double budget[], double revenue[], int &NumCities);
void printarrays (string city[], double budget[], double revenue[], int NumCities);
void FindHiBudget (string city[], double budget[], int NumCities);
void AboveAvgRevenue (string city[], double revenue[], int NumCities);
void OverBudget (string city[], double budget[], double revenue[], int NumCities);
int searchFunction (string city[], int NumCities, string target);
void findCityData (string city[], double budget[], double revenue[], int NumCities);
void sortArrays (double budget[], double revenue[], string city[], int NumCities);
void printArray(double budget[], double revenue[], string city[], int NumCities);
//ADD YOUR PROTOTYPES HERE

const int ArraySize = 30;

int main ()
{
    string city[ArraySize];
    double budget[ArraySize];
    double revenue[ArraySize];
    //double tosort[ArraySize];
    int NumCities;
    

    //We will now call on all of these functions
    fillArray (city, budget, revenue, NumCities);
    cout << left << fixed << setprecision(0);
    printarrays (city, budget, revenue, NumCities);

   //ADD THE CALL TO YOUR FindHiBudget function
   FindHiBudget (city, budget, NumCities);
    cout << "======================================================" << endl;

    
    //ADD THE CALL TO YOUR AboveAvgRevenue function
    AboveAvgRevenue (city, revenue, NumCities);
    cout << "======================================================" << endl;

    
    //ADD THE CALL TO YOUR OverBudget function
    OverBudget (city, budget, revenue, NumCities);
    cout << "======================================================" << endl;
    
    
    
    //findCityData (city, budget, revenue, NumCities);
    cout << "======================================================" << endl;


    
     //for (int i = 0; i < ArraySize; i++)
        //budget[i] = tosort[i];

    sortArrays (budget, revenue, city, NumCities);
    cout << "======================================================" << endl;



system("PAUSE");
    return 0;

}

void fillArray (string city[], double budget[], double revenue[], int &NumCities)
{
    ifstream indata;
    NumCities = 0;

    //Now we will open the file

    indata.open("prog1.txt");
    if (! indata)
    {
        cerr << "Error" << endl;
        
    }


    getline(indata, city[NumCities]);

    //We will now run a loop to get al of the data from the file and assign it to the proper array
    while (!indata.eof()&& NumCities < ArraySize)
    {    
        indata >> budget[NumCities];
        indata >> revenue[NumCities];

        indata.ignore();
        NumCities++;
        getline(indata, city[NumCities]);
        
    }
    return;
}

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

    cout << setw(20) << "City" << setw(20) << "Revenue" << setw(20) << "Budget" << endl;
    cout << setw(20) << "----" << setw(20) << "-------" << setw(20) << "------" << endl;
    

    //We will now print al of the data in a table
    for (int i = 0; i < NumCities; i++)
    {
        cout << setw(20) << city[i] << setw(20) << revenue[i] << setw(20) << budget[i] << endl;
    }
}

//========================================================
// ADD YOUR FindHiBudget function here
//========================================================
void FindHiBudget (string city[], double budget[], int NumCities)
{
    int i = 0;
    double HiBudget = 0;
    int temp = 0;
    
for (i = 0; i < NumCities; i++)
{
    if (budget[i] > HiBudget)
    {
        HiBudget = budget[i];
        temp = i;
    }
    
    
}
cout << "======================================================" << endl;
cout << "The city with the highest budget is " << city[temp] << endl;
}



//========================================================
// ADD YOUR AboveAvgRevenue function here
//========================================================
void AboveAvgRevenue (string city[], double revenue[], int NumCities)
{
    double average = 0;
    double sum = 0;
    int i = 0;
    
    for (i = 0; i < NumCities; i++)
    {
        sum = sum + revenue[i];
    }
    
    average = sum / NumCities;
    cout << "The average revenue is " << average << endl;
    cout << "The cities with the above average revenue are: " << endl;
    
    for (i = 0; i < NumCities; i++)
    {
        if (revenue[i] > average)
            cout << city[i] << endl;
    }
}
    






//========================================================
// ADD YOUR OverBudget function here
//========================================================
 void OverBudget (string city[], double budget[], double revenue[], int NumCities)
{
    int i = 0;
    
    cout << "The cities that are over budget are: " << endl;
    
    for (i = 0; i < NumCities; i++)
    {
        if (budget[i] > revenue[i])
            cout << city[i] << " by " << budget[i] - revenue[i] << endl;
    }
}
    
    

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);
            

    }

       


}










    
    
int searchFunction (string city[], int NumCities, string target)
{

    
    int first = 0,           // First array element
       last = NumCities - 1, // Last array element
       middle,              // Mid point of search
       position = -1;       // Position of search value
   bool found = false;      // Flag

 
   
   while (!found && first <= last)
   {
      middle = (first + last) / 2;  // Calculate mid point
      if (city[middle] == target)   // If value is found at mid
      {
         found = true;
         position = middle;
      }
      else if (city[middle] > target) // If value is in lower half
         last = middle - 1;
      else
         first = middle + 1;          // If value is in upper half
   }

  

   return position;
}




void sortArrays (double budget[], double revenue[], string city[], int NumCities)
{
    double  itemp;
    string stemp;
    int swap = 1;

    
    while (swap)
    {    swap = 0;
        for (int count = 0; count < (NumCities - 1); count++)
        {
            if (budget[count] > budget[count + 1])
            {
                itemp = budget[count];
                budget[count] = budget[count + 1];
                budget[count + 1] = itemp;
            
            

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

                swap = 1;
            }
        }
        //NumCities--;
   } 
    printarrays (city, budget, revenue, NumCities);
}

void printArray(double budget[], double revenue[], string city[], int NumCities)
{   
    cout << endl << "----------------------------------------------"  << endl;
    cout << setw(10) << "City" << setw(14) << "Budget" << endl;
    cout << setw(10) << "-----" << setw(10) << "------" << endl;
    for (int count = 0; count < NumCities; count++)
    {    cout << setw(10) << city[count] << setw(10);
        cout << budget[count];
        cout << endl;
    }
    cout << endl << "----------------------------------------------" << endl;
}
i commented
Code:
//findCityData (city, budget, revenue, NumCities);
because it wouldn't let me proceed to the sorting and
Code:
//NumCities--;
because it's not needed and added
Code:
getchar();
so it wouldn't exit upon finishing.
sorted the list fine on my end.
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 #8 of 13
Thread Starter 
Quote:
Originally Posted by newbie1911 View Post
i commented
Code:
//findCityData (city, budget, revenue, NumCities);
because it wouldn't let me proceed to the sorting and
Code:
//NumCities--;
because it's not needed and added
Code:
getchar();
so it wouldn't exit upon finishing.
sorted the list fine on my end.

There's something special you have to do tot he project in VS2010 to make it not exit or just put in a system pause.

Did the sorting turn out different for you?
I didn't have any problems making it that far in VS or unix.


I just took out the
Quote:
NumCities--;
in the sort arrays loop and now when it goes to sort by Budget all the budget numbers are right and they're in the correct order, although the revenue numbers are wrong.
Edited by skatingrocker17 - 2/5/11 at 7:57am
    
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 #9 of 13
http://www.gidnetwork.com/b-61.html is why you shouldn't use system("PAUSE").
other than that everything works fine and the sorting works as supposed to.
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 #10 of 13
Thread Starter 
Quote:
Originally Posted by newbie1911 View Post
http://www.gidnetwork.com/b-61.html is why you shouldn't use system("PAUSE").
other than that everything works fine and the sorting works as supposed to.
I'm not using pause, my CS teacher did something to the VS project that makes it not exit.

When the program sorts by Budget the corresponding city and budget align but the revenue is off, all the revenue information is there but it's not matching with the correct city. I'm using the same subscript for all 3 arrays so I'm not sure why it wouldn't.

Edit, just realized it's because the array isn't sorting Revenue yet so it's just passing all the numbers as they are.

I'm not sure how to get it to sort 1 array correctly and have all the numbers align then sort the Budget array correctly with the numbers aligning as well.

Another Edit: I just added this and it worked.
Code:

rtemp = revenue[count];

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

Edited by skatingrocker17 - 2/5/11 at 8:16am
    
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?