LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-11-2003, 03:37 PM   #1
Becks
LQ Newbie
 
Registered: Sep 2003
Posts: 2

Rep: Reputation: 0
Bubble sort?


Hi all

I'm a newbie with problems. This forum may be my last hope :-)

Let's say I type in the numbers 12344

I would like the answer

1:1
2:1
3:1
4:2

I want it to count every number and print how many og each number there is. Like now it 1 number 1 , 1 number 2, 1 number 3 and 2 number 4.

Bubble sort array I guess but how?

I hope you understand my probelm :-)

Thx for any suggestions \ Becks
 
Old 09-11-2003, 04:23 PM   #2
kev82
Senior Member
 
Registered: Apr 2003
Location: Lancaster, England
Distribution: Debian Etch, OS X 10.4
Posts: 1,263

Rep: Reputation: 51
which language are we talking? this smells like homework to me, but anyway first solution that comes to mind is

awk '{ fq[$1]++ } END { for (num in fq) print num, fq[num] }' | sort

if you want to do it in C or any language the procedure is pretty much the same, loop through the input and count how many of each number you have store that in an array and print out the answer.
 
Old 09-11-2003, 04:32 PM   #3
Becks
LQ Newbie
 
Registered: Sep 2003
Posts: 2

Original Poster
Rep: Reputation: 0
Yes its homework... but I do it for me only... no teacher will look at it. But I couldn't seem to figure it out and things like that bother me alot. Itss c++ by the way.

So dont feel bad ... your not helping me cheat .
 
Old 09-11-2003, 05:10 PM   #4
kev82
Senior Member
 
Registered: Apr 2003
Location: Lancaster, England
Distribution: Debian Etch, OS X 10.4
Posts: 1,263

Rep: Reputation: 51
the c++ way is exactly the same as the awk way because c++ is lucky enough to have associative containers. put your numbers space seperated on the command line.
Code:
#include <iostream>
#include <string>
#include <map>

int main(int argc, char **argv)
{
    if(argc<2) return 1;

    std::map<std::string, int> aa;

    for(int i=1; i<argc; i++)
        aa[argv[i]]++;

    for(std::map<std::string, int>::iterator i=aa.begin();i!=aa.end();i++)
        std::cout << i->first << ":" << i->second << std::endl;

    return 0;
}
<edit> i had questions here before but ive answered them myself now so their gone.

Last edited by kev82; 09-11-2003 at 05:41 PM.
 
Old 09-12-2003, 01:28 PM   #5
LinuxTiro
Member
 
Registered: Aug 2003
Posts: 59

Rep: Reputation: 15
as u asked for something using bubble sort, how abt this for a code.

Code:
------------------------------------------------------------------------------

#include<iostream>
using namespace std;

int main(void)
{
         int i,n,j,flag;
 
        cout<<"enter no of inputs";
        cin>>n;
        int a[n];
        for(i=0;i<n;i++)
                cin>>a[i];

        for(i=0;i<n;i++)
                for(j=i+1;j<n;j++)
                        if(a[i]>=a[j])
                        {
                                a[i]=a[i]+a[j];
                                a[j]=a[i]-a[j];
                                a[i]=a[i]-a[j];
                        }

        for(i=0;i<n;i++)
        {flag=1;
                while(a[i]==a[++i])
                flag++;
        cout<<a[--i]<<":"<<flag<<endl;}


return(0);
}
-----------------------------------------------------------------------------------~

Last edited by LinuxTiro; 09-13-2003 at 01:04 AM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
frozen bubble rrr-jr Linux - Games 1 03-24-2005 11:46 AM
frozen bubble acidjuice Slackware 7 03-02-2005 03:39 PM
When will the bubble burst. cadj General 7 09-09-2004 09:54 AM
Frozen Bubble real-tech Mandriva 2 11-18-2003 10:58 AM
Frozen Bubble biblefreak Linux - Software 6 12-27-2002 11:51 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:16 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration