Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Sorting, infile and outfile problem

Status
Not open for further replies.

ggreg

Programmer
Mar 9, 2001
201
US
here's my code can anyone tell me what is wrong with the sorting function
the infile and outfile???
the data file is 10 numbers like--
10.39
60.36
20.85
39.77
15.58
44.22
88.22
30.37
10.39
59.30



//this program uses the bubble sort to sort a numeric array
#include <iostream.h>
#include <fstream.h>
//function prototypes
void fillArray(float [10]);
void displayArray(float [10]);
void sortArray(short[10], float [10], short);
void writeToFile(float [10]);
void main()
{
//declare and initialize array
float number[10]={0};
//open file for input
ifstream infile;
infile.open(&quot;C:\\WINDOWS\\Desktop\\numbers.dat&quot;, ios::in);
ofstream outfile;
outfile.open(&quot;C:\\WINDOWS\\Desktop\\numbers.dat&quot;, ios::eek:ut);
//verify that open was successful
if(!infile.fail()|| !outfile.fail())
{
//fill array with data from file
fillArray(number);
//display array
displayArray(number);
//sort array
sortArray(num,number,size);
//display sorted array
displayArray(number);
//write sorted information to a file
writeToFile(number);
}
else
cout << &quot;Error opening file.&quot; << endl;
//end if (!inFile.fail())
} //end of main function
//*****programmer-defined function definitions*****
void fillArray(float number[])
{
for (short x=0;x<10;x=x+1)
{
infile>>number[x];
//infile.ignore(1);
}
infile.close();
} //end of fillArray function
void displayArray(float number[])
{
for(short d =0;d<10;d=d+1)
cout<<number[d]<<&quot; &quot;;
cout<<endl<<endl;
} //end of displayArray function
void sortArray(short num[],float number[],short size)
{
short maxsub =size-1;
short tempnumber =0;
char swap ='Y';
short lastswap = 0;
while (swap == 'Y')
{
swap ='N';
for (short a=0;a<maxsub;a=a+1)
{
if(num[a]>num[a+1])
{
swap ='Y';
tempnumber = num[a];
num[a]=num[a+1];
num[a+1]=tempnumber;
lastswap = a;
}
}
}
} //end of sortArray function
void writeToFile(float number[])
{
for (short y = 0; y < 10;y=y+1)
{
outfile <<number[y]<<endl;
}
outfile.close();
} //end of writeToFile function
Thanks for any help
 
infile & outfile are out of scope. You declare them in the main and use it in a funcion. You should be seeing errors that it does not know what infile and outfile are. Try moving them global for the time being and see what happens.

Matt
 
I believe the best way to sort numbers in C++ is:
template<class T>void sortArray(T num[],short size)
{
for(short a=0;a<size;a=a+1)
{
if(num[a]>num[a+1])
{
num[a]^=num[a+1]^=num[a]^=num[a+1];
}
}

}
John Fill
1c.bmp


ivfmd@mail.md
 
Took me a bit to figure out waht you were doing =) I love binary stuff. I think it needs to be in a nested for loop though. If I am not mistaken, the:

num[a]^=num[a+1]^=num[a]^=num[a+1];

is doing a swap and with the bubble sort it will have to execute the summation of n-1 times.

Matt
 
John,
your dealing with class and that is the last chapter we
covered with my C++ Class so going to assume that you do
have the best way to sort but had to do the sort the way I am doing it.... Now Matt it took me awhile to think about how to do what you were trying to tell me about making them global...but since the problem is set up with functions
I moved my infile and outfile within their functions and
that got rid of my errors ( do not know if program is going
to work right with moving them in the function) but now
I am down to two errors all dealing with
D:\Program Files\Microsoft Visual Studio\MyProjects\sort\sort.cpp(40) : error C2065: 'num' : undeclared identifier
D:\Program Files\Microsoft Visual Studio\MyProjects\sort\sort.cpp(40) : error C2065: 'size' : undeclared identifier
Error executing cl.exe.
which points me back to my function statement on my sort

sortArray(num,number,size);
is the above statement written with the way I am using it???

hey can I vote for both of you?






 
Look in function main. Did you declare anywhere the red one? I don't see theyrs declaration anythere.

if(!infile.fail()|| !outfile.fail())
{
//fill array with data from file
fillArray(number);
//display array
displayArray(number);
//sort array
sortArray(num,number,size);
//display sorted array
displayArray(number);
//write sorted information to a file
writeToFile(number);
}
John Fill
1c.bmp


ivfmd@mail.md
 
I sent you off an email earlier this morning with some explanations in it. Did you get it? On a side note, the binary method above will not work on floats (99% sure but dont have time to verify it)

Matt
 
Matt,

I still dont have it working but what the instructor
said was I needed to make more floats than I have now!
if he shows me tonight will post the answer tomorrow,
He said that num needs to be a float and something else needs to be a float


 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top