automatic number sorting function

Post questions regarding programming in C/C++ in Linux/Unix.

Moderators: Darobat, RecursiveS, Dante Shamest, Bugdude, Wizard

automatic number sorting function

Postby Kenji » Fri May 07, 2004 11:16 am

Hello all,
For this highscore list thing i need a function that sorts all the numbers eneterd by a user. So like I have an array that the user enters all the numbers into called number[counter] where counter is the variable i increment. So yeah I just need it to do something like if the user enteres the following
234
33
3323
1
75

the function would reorganize the list to
1
33
75
234
3323

Then that list gets written to a file, and the file can be read later in the programs. This function also is gonna be used for some other program that needs to sort the numbers. Please help me out, I can't think of a good way to do this.
"The day you want to stop learning is the day you should die."
User avatar
Kenji
 
Posts: 32
Joined: Mon May 03, 2004 8:29 pm
Location: MN

Postby Kenji » Fri May 07, 2004 11:34 am

Also I need it to detect duplicate entries when outputting, and not display them. that can be in a different function, but if anybody has a code snipped that i could use that would be good.
"The day you want to stop learning is the day you should die."
User avatar
Kenji
 
Posts: 32
Joined: Mon May 03, 2004 8:29 pm
Location: MN

Well........

Postby Sin » Fri May 07, 2004 11:36 am

I am learning that exact stuff in my Computer Science class now. A bubble sort is the right idea. I will get the code for a 'database'. I already know how to make it so that it saves into a text document. But I need to go on and have it sort the list. Then save the whole list into a text document. Now that hard just takes time. I am looking up how to do the bubble sort right now.
It takes the lowest one and puts it at the end. Then compares the next ect........Til it puts it in decending or rising order........ 8)
Sin
 
Posts: 1570
Joined: Tue Oct 07, 2003 10:16 am

Postby DannyBoy » Fri May 07, 2004 11:45 am

Use a vector to hold the scores instead of an array. Then use the sort function to put the values in order.

As for outputting your now sorted vector, simply check the number to be output against the previous number in the vector before outputting. If they are different then you can output the number.
User avatar
DannyBoy
 
Posts: 1160
Joined: Fri Feb 13, 2004 12:56 pm
Location: In the Billiard Room with the Lead Pipe

Postby Guest » Fri May 07, 2004 2:26 pm

Dannyboy, could you perhaps give me sample code of how to do what your talking about. I only know of vectors, I haven't really worked with them. Also how could I compare one output to ones previuos to it, there can be no duplicates anywhere is the list.
Guest
 

Postby tomcant » Fri May 07, 2004 2:35 pm

Code: Select all
vector<int> vec;

vec.push_back(2);
vec.push_back(1);
vec.push_back(4);
vec.push_back(3);

vec.sort(vec.begin(), vec.end());


That should work :)
If it wasn't for C, we would be using BASI, PASAL and OBOL.
User avatar
tomcant
 
Posts: 3101
Joined: Tue Sep 23, 2003 1:56 am
Location: Colchester, UK

Postby Kenji » Fri May 07, 2004 2:48 pm

Here this is what I have for code

#include<iostream.h>
#include<fstream>
#include<vector>
using namespace std;
void write(void);
void read(void);
void numsorter(void);
vector<vector<int> > number;
int counter = 0;

int main()
{
int go = 0;
cout<<"(1) Write numbers to file\n";
cout<<"(2) Read numbers from file\n";
cin>>go;
if(go == 1)
{
write();
}
if(go == 2)
{
read();
}
return 0;
}
void numsorter(void)
{

sort(number);

}
void write(void)
{


cout<<"Enter a list of numbers, press 0 to terminiate\n";
do
{
counter++;
cin>>number[counter];
if(number[counter] == 0)
{
cout<<"Detected a zero!\n";
}
}
while(number[counter] != 0);

int counter1 = 0;
number[0];

int somevar = counter;
counter = 0;
while(counter1 <= somevar)
{
counter1++;
counter++;
ofstream outFile ("numbers.txt", ios::app);
outFile <<number[counter]<<"\n";
outFile.close();
}
main();
}
void read(void)
{
counter = 0;
ifstream inFile ("numbers.txt", ios::in);

if(!inFile)
{
cout<<"Whoa, what did you do to the numbers file? It ain't there!\n";
main();
}
cout<<"\n";
while(inFile >> number[counter])
{

cout<<number[counter]<<"\n";
counter++;
}
cout<<"\n";
inFile.close();
main();

}



And this is what i get for errors

error C2065: 'sort' : undeclared

error C2679: binary '>>' : no operator defined which takes a right-hand operand of type 'class std::vector<int,class std::allocator<int> >' (or there is no acceptable conversion)

error C2676: binary '==' : 'class std::vector<int,class std::allocator<int> >' does not define this operator or a conversion to a type acceptable to the predefined operator

error C2676: binary '!=' : 'class std::vector<int,class std::allocator<int> >' does not define this operator or a conversion to a type acceptable to the predefined operator

error C2679: binary '<<' : no operator defined which takes a right-hand operand of type 'class std::vector<int,class std::allocator<int> >' (or there is no acceptable conversion)

error C2679: binary '>>' : no operator defined which takes a right-hand operand of type 'class std::vector<int,class std::allocator<int> >' (or there is no acceptable conversion)

fatal error C1903: unable to recover from previous error(s); stopping compilation


HELP!!!!!!
"The day you want to stop learning is the day you should die."
User avatar
Kenji
 
Posts: 32
Joined: Mon May 03, 2004 8:29 pm
Location: MN

Postby Alvaro » Fri May 07, 2004 3:07 pm

Code: Select all
#include <iostream>
#include <vector>
#include <algorithm>

int main(){
        vector<int> v;
        int input_number;
        while(std::cin >> input_number)
                v.push_back(input_number);
        sort(v.begin(),v.end());
        for(int i=0;i<v.size();++i)
                std::cout << v[i] << std::endl;
}


That takes care of the sorting problem. You'll have to mark the end of input when you are done entering numbers (Control+D on my computer, might be Control+Z or something else on yours).
User avatar
Alvaro
Moderator
 
Posts: 5185
Joined: Mon Sep 22, 2003 4:57 pm
Location: NY, USA

Postby Kenji » Fri May 07, 2004 3:13 pm

Wow dude, that is some funky code. I have mo idea how that really works, and therfore I sadly cannot implement into my program. Mabye some comments or something would be good. Thanks so much for the help though.
"The day you want to stop learning is the day you should die."
User avatar
Kenji
 
Posts: 32
Joined: Mon May 03, 2004 8:29 pm
Location: MN

Postby Alvaro » Fri May 07, 2004 3:31 pm

This code is no big mystery.
Code: Select all
#include <iostream>
#include <vector>
#include <algorithm> // needed for sort

int main(){
        vector<int> v;
       
        // Read input
        int input_number;
        while(std::cin >> input_number)
                v.push_back(input_number); // grow the vector by one element and put input_number at the end
       
        // Sort
        sort(v.begin(),v.end());
       
        // Output
        for(int i=0;i<v.size();++i)
                std::cout << v[i] << std::endl;
}

User avatar
Alvaro
Moderator
 
Posts: 5185
Joined: Mon Sep 22, 2003 4:57 pm
Location: NY, USA

Postby Kenji » Fri May 07, 2004 4:29 pm

Dude, I can't get your code working, I am too stupid :roll: Plus I wouldn't know how write that stuff to the file and then read it back out again.
"The day you want to stop learning is the day you should die."
User avatar
Kenji
 
Posts: 32
Joined: Mon May 03, 2004 8:29 pm
Location: MN

Postby Kenji » Sat May 08, 2004 2:25 pm

Okay i worked with your code a little bit and this is what i came up with. I know it is very not good but... i tried. I don't know how to access the different values in vectors. Like i said i usually use arrays. I wrote in some psuedo code (not really code, just a decription of what the code will do when it is written). If anybody can help, that would be awsome.

#include <iostream>
#include <vector>
#include <algorithm> // needed for sort
#include <fstream> //needed for opening and writing to files
using namespace std;
int main()
{
vector<int> v;
ofstream outFile ("numbers.txt", ios::app); //creats a file called number.txt and appends to end of it

// Read input
cout<<"Input some numbers, press 0 to terminate\n"; //tells user to enter number
int input_number; //creates the input_number variable
while(std::cin >> input_number && input_number != 0)
{
v.push_back(input_number); // grow the vector by one element and put input_number at the end

//PSEUDO CODE I DON'T KNOW HOW TO WRITE WHAT I NEED--------------------------------
/*
if number is a duplicate of another number
don't write the number to the file
if number is not a duplicate
write the number to the file

*/

outFile <<input_number<<"\n";
}
outFile.close(); //closes the file

// Sort
sort(v.begin(),v.end()); //sorts the numbers into numerical order

// Output
ifstream inFile ("numbers.txt", ios::in); //opens numbers text file

if(!inFile) //if i can't open the file
{
cout<<"Whoa, what did you do to the numbers file? It ain't there!\n";
main(); //go back to begining, i know i shouldn't call main but i am being lazy
}
cout<<"\n";
while(inFile >> input_number)
{
//PSUEDO CODE
/* Output all numbers that are in the file one row at a time */

}
cout<<"\n";
inFile.close(); //closes the file
return 0;
}
"The day you want to stop learning is the day you should die."
User avatar
Kenji
 
Posts: 32
Joined: Mon May 03, 2004 8:29 pm
Location: MN

Postby schloob » Sat May 08, 2004 2:35 pm

if (v[i]!=v[i-1]) output ;-\ as long as i isnt 0
:]
User avatar
schloob
 
Posts: 1853
Joined: Mon Feb 16, 2004 10:29 am
Location: Seattle

Postby Kenji » Sat May 08, 2004 4:03 pm

will that code only detect if it was a dupliate entry right behind it. Like if the input was
5
6
7
8
5

would it detect the 2nd five, or will your code only catch it if its

6
7
5
5 //detect duplicate entry here

looks like that might be the way to go though thanks.
"The day you want to stop learning is the day you should die."
User avatar
Kenji
 
Posts: 32
Joined: Mon May 03, 2004 8:29 pm
Location: MN

Postby DannyBoy » Sat May 08, 2004 7:02 pm

That's why you sort the vector beforehand, as it groups all the duplicates together. :wink:
User avatar
DannyBoy
 
Posts: 1160
Joined: Fri Feb 13, 2004 12:56 pm
Location: In the Billiard Room with the Lead Pipe

Next

Return to Unix/Linux

Who is online

Users browsing this forum: No registered users and 0 guests