Hi all,
I've been working on a program that is supposed to sort and array in descending order using the bubble sort mechanism, but I just can't seem to get it. I've looked up some sites on the internet , and I searched this board but I still didn't get to anything. Below is the code I have reached to:
----------------------------------------------------------------------------
#include <iostream.h>
#include <fstream.h>
int main ()
{
ifstream inFile ("insort.txt" , ios::in);
int i, vector[20], max, dim,temp,k,mpos;
inFile>>dim;
for (i=0 ; i<dim ; i++)
inFile>>vector[i];
for (i=0 ; i<=dim-1 ; i++)
for (k=i; k<=dim ; k++){
if (vector[k]>max){
max=vector[k];
mpos=k;
}
temp=vector[mpos];
vector[mpos]=vector[i];
vector[i]=temp;
}
for (i=0 ; i<dim ; i++)
cout<<vector[i]<<endl;
return 0;
}
------------------------------------------------------------------
I can't figure out what's wrong with it.
Help is most appreciated.
Thank you.