hello! i have written a program to read in a text file and then ask the user to process the file with a different sorting method.
so far i have been able to get my program to read in the file but my bubble sort seems to not be sorting the elements right. ive tried thinking of as many solutions as possible but im not sure whats wrong.the text file looks like this
1
65
44
34
23
6
any help or ideas on where to start looking for the problem would be greatly appriciated.
/******************************************************
*
* Program3.cpp
Author: Kevin Thomas Jack
*
******************************************************/
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
#include <cctype>
using namespace std;
void displayMenu();
void displayArray (double x[], int n);
void readArray (double x[], int &n);
void bubbleSort (double x[],int n);
//void quickSort(double x[],int a, int b);
//void mergeSort (double x[], double y[], int n, int m);
int main ()
{
double x[200];
double y[200];
int n;
bool menuActive;
char response, menuChoice, ch;
menuActive = true;
while (menuActive)
{
displayMenu();
cout<<"Enter menu Choice ";
cin>>menuChoice;
cin.ignore (80,'\n');
cout<<endl;
switch (menuChoice)
{
case '1': case 'b': case 'B':
readArray (x,n);
bubbleSort (x,n);
cout<<endl<<"Sorted array using Bubble Sort: "<<endl;
displayArray (x,n);
break;
case '2': case 'q': case 'Q':
readArray (x,n);
cout<<endl<<"Sorted array using Quick Sort: "<<endl;
//quickSort(x, 0, n-1);
//displayArray (x,n);
break;
case '3':
break;
case '4':
menuActive=false;
break;
default:
cout<<"INVALID MENU CHOICE! try again: ";
}//end switch
}//end while
return 0;
}// end main
void readArray(double x[], int &n)
{
char filename[50];
ifstream sourceFile;
int i;
do
{
cout<<"Enter filename of source file: ";
cin.getline (filename,51);
sourceFile.open(filename);
if (sourceFile.fail())
cout<<"Source file could nt be opened!"<<endl;
sourceFile.clear();
}
while (sourceFile.fail());
sourceFile.clear();
i=0;
while(!sourceFile.eof())
{
sourceFile >> x[i];
if(!sourceFile.eof())
i++;
}
sourceFile.clear();
sourceFile.close();
n=i;
}
void displayArray (double x[], int n)
{
char ch;
for (int i=0;i<n;i++)
{
cout<<x[i]<<endl;
if((i+1)%10==0)
cin.get(ch);
}
}
void displayMenu ()
{
cout<<" *******************************"<<endl;
cout<<" * *"<<endl;
cout<<" * MAIN MENU *"<<endl;
cout<<" * *"<<endl;
cout<<" * 1) (B)ubble Sort *"<<endl;
cout<<" * 2) (Q)uick Sort *"<<endl;
cout<<" * 3) (M)erge Files *"<<endl;
cout<<" * 4) (S)top Processing *"<<endl;
cout<<" * *"<<endl;
cout<<" *******************************"<<endl;
}
void bubbleSort (double x[], int n)
{
int pass=1;
bool done=false;
while (!done && pass<=n-1)
{
done=true;
for (int j=n-j;j>=pass;j--)
{
if (x[j]<x[j-1])
{
double temp = x[j];
x[j]=x[j-1];
x[j-1]=temp;
done=false;
}
}
pass++;
}
}