Bubble sorts, wtf?

So I have this program:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
/*
 *******************************************************************************
 *Title:                                                                       *
 *                                                                             *
 *Name:                                                                        *
 *                                                                             *
 *Assignment:                                                                  *
 *                                                                             *
 *File:                                                                        *
 *                                                                             *
 *Description:                                                                 *
 *       This program accepts information from a file, and accepts user input  *
 *        to process and display the information taken from that file.         *
 *Source:                                                                      *
 *                                                                             *
 *******************************************************************************
 */
#include <iostream>                             //Required for stream i/o
#include <iomanip>                              //Required for stream formatting
#include <string>                               //Required for string functions
#include <fstream>                              //required for file streams

using namespace std;                            //Required CS161 namespace

/*
 +-----------------------------------------------------------------------------+
 |Name:                                                                        |
 |               main                                                          |
 |Parameters:                                                                  |
 |               int argc:     Count of the command line arguments passed by   |
 |                             the operating system.                           |
 |               char **argv:  Array of strings.  argv[0] is the program name, |
 |                             argv[1] is the first command line argument.     |
 |Return:                                                                      |
 |               int s:        Integer indicating exit status. 0 = normal exit.|
 |Description:                                                                 |
 |               The "main" function is the entry point for C++ programs.      |
 |               Every C++ program must have a "main" function, and this is    |
 |               where execution always starts.                                | 
 +-----------------------------------------------------------------------------+
*/ 
int main(const int argc, const char **argv)
{
   char userHoldCharacter;                      //Character to hold user input
   char menuOption = ' ';                       //user main menu entry
   int counter = 0;                             //used for array processing
   int idx = 0;                                 //used for array processing   
   int index = 0;                               //used for array processing
   int listSize = 0;                            //used for array processing
   int searchNumber = 0;                        //user input for search query
   float markup;                                //used for calculating 
                                                //product markup percent
   
   struct productInformation                    //structure variable for 
                                                //storing product information
   {
          int IDNumber;
          float wholeSale;
          float retail;
          float markup;
          
   };
   productInformation myArray[100];             //product info storage array 
    for(idx = 0; idx < 101; idx++)              //initializing array
   {
           myArray[idx].IDNumber = 0;
           myArray[idx].wholeSale = 0.0;
           myArray[idx].retail = 0.0;
           myArray[idx].markup = 0.0;
           
   }
   
   ifstream inFile;                             //initialize input file stream
   inFile.open(argv[1]);                        //open file:
                                                //file name passed as parameter                 
                                                
   if(inFile == false)                          //error for incorrect file name
   {
             cout << "Unable to Open File" << endl;    
   }
if(inFile != false)                             

{
  
  
  for(counter = 0; counter < 100; counter++)    
   {
      inFile >> myArray[counter].IDNumber
             >> myArray[counter].wholeSale
             >> myArray[counter].retail;
               
      if (counter <= myArray[counter].IDNumber)     
                                                       
                                                     
                                                       
      {
          listSize = counter;
      }
                   
   }
 
   cout << "Please enter one of the following: \n"     
        << "L - list all products \n"                  
        << "F - Find a product by ID \n"
        << "Q - Quit the program" << endl;
   
   cout << "Enter Option: ";
   cin >> menuOption;
 
   if(menuOption == 'L' || menuOption == 'l')        
   {                                                   
   
   for(index = 0; index <= listSize; index++)
   {
     cout << setprecision(2) 
          << "ID: " << setw(1) << myArray[index].IDNumber
          << setw(10) << "Cost: " << "$" << (myArray[index].wholeSale)
          << setw(10) << "Price: " << "$" 
          << myArray[index].retail << " ";
   
     markup = ((myArray[index].retail / myArray[index].wholeSale) - 1.00) * 100;
   
     cout << fixed << setprecision(2) << setw(10)
          << "Markup: " << markup << "%" << "\n";
   }
   
}
   else if(menuOption == 'F' || menuOption == 'f')       
   {
        
        cout << "Please enter the product ID number search query: ";
        cin >> searchNumber;
        for(index = 0; index <= listSize; index++)
        {
        if(myArray[index].IDNumber == searchNumber)
        {
                 cout << setprecision(2) 
          << "ID: " << setw(1) << myArray[index].IDNumber
          << setw(10) << "Cost: " << "$" << (myArray[index].wholeSale)
          << setw(10) << "Price: " << "$" 
          << myArray[index].retail << " ";
   
          markup = ((myArray[index].retail / myArray[index].wholeSale) 
                    - 1.00) * 100;
          cout << fixed << setprecision(2) << setw(10)
          << "Markup: " << markup << "%" << "\n";   
          break;      
         }
         
         }
   }
   
   else if(menuOption == 'Q' || menuOption == 'q')    
   {
        return 0;
   }
   
   else
   {
       cout << "Incorrect Menu Option" << endl;      
   }                                                  

             
}                
               
   inFile.clear();                                    
   inFile.close();                                    


/*
   The following code is needed in windowed environments to prevent the
   closure of the execution window before the user has a chance to see
   the output display. 
*/
   cout << "Press any key followed by <enter> to exit the program" << endl;
   cin >> userHoldCharacter;                  
   cout << "[normal end]" << endl;            
   return 0;                                    

}                                             

I need to include another option A that lists the product information in alphabetical order. Since the program doesn't deal with very high numbers, a bubble sort seem to be the best option but I haven't found a way to implement it w/o screwing something else up in the prg.
Last edited on
Can you please post it correctly using [c0de] your code goes here [/c0de] tags. Replace 0 with o so it spells code. This will preserve your indentation and make it easier to read.
Try to use a function that passes as a reference the array that you read and sorts it.
Here:
http://mathbits.com/MathBits/CompSci/Arrays/Bubble.htm
you can find a sample of bubble shorting, just reformat it for your needs.

Also the line:
 
for(idx = 0; idx < 101; idx++) 

you should go until 100, not 101 because you have 100 positions array.
I fixed the code, sorry about that. I'm not quite sure which variables would be my I and J and whatnot if I were using the aforementioned c0de.
I don't have the file to test it but I think that this code will do the shorting:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
//------shorting------
int i, j, flag = 1;    // set flag to 1 to begin initial pass
productInformation temp;  // holding variable
int arrayLength = 100; 
for(i = 1; (i <= arrayLength) && flag; i++){
	flag = 0;
	for (j=0; j < (arrayLength -1); j++){
		if (myArray[j+1].IDNumber > myArray[j].IDNumber){      // ascending order simply changes to < 
			temp = myArray[j];             // swap elements
			myArray[j] = myArray[j+1];
			myArray[j+1] = temp;
			flag = 1;               // indicates that a swap occurred.
		}
	}
}
//----end shorting---- 
...and after all of that, is temp the variable being displayed in a cout statement?
No,no.
temp is just a temporary variable made just to change the order of myArray. When you change position of two variables you need a temporary var that will handle the value of one. myArray is still your main array with your information...
Topic archived. No new replies allowed.