#include <iostream>
#include <string>
#include <cctype>
#include <iomanip>
#define N 36

using namespace std;

string EraseWhiteSpaces(string &str_nospaces)
{
        int i;
        for (int i=0;i<str_nospaces.length();i++)
        {
                if (str_nospaces[i]==' '||(str_nospaces[i]=='\t'))
                {
                        str_nospaces.erase(i,1);
                        i--;
                }
        }
        return str_nospaces;
}

string ConvertToLowerCase(string &str_lowercase)
{
        int i;
        for (i=0;i<str_lowercase.length();i++)
                str_lowercase[i]=tolower(str_lowercase[i]);
        return str_lowercase;
}

void SortedFrequences(int frequences[],char chars[])
{
        int i,j,tmp1;
        char tmp2;
        for (i=0;i<N;i++)
        {
                for (j=i+1;j<N;j++)
                {                          //THE PROBLEM IS HERE!!!!!!!!
                        if (frequences[i]<frequences[j])
                        {
                                tmp1=frequences[i];
                                frequences[i]=frequences[j];
                                frequences[j]=tmp1;
                                tmp2=chars[i];
                                chars[i]=chars[j];
                                chars[j]=tmp1;
                        }
                }
        }
        cout<<"character:       frequency:"<<endl;
        for (i=0;i<N;i++)
        {
                if (frequences[i]==0)
                        break;
                cout<<chars[i]<<":"<<frequences[i]<<endl;
        }
}

int main()
{
        int i,k=0,l=0,j;
        string str,fstr="";
        char xaraktires[N];
        int suxnotites[N]={0};
        while (getline(cin,str))
        {
                fstr+=str;
        }
        EraseWhiteSpaces(fstr);
        ConvertToLowerCase(fstr);
        for (i=0;i<26;i++)
        {
                xaraktires[i]=97+k;
                k++;
        }
        for (i=26;i<N;i++)
        {
                xaraktires[i]=48+l;
                l++;
        }
        for (i=0;i<fstr.length();i++)
        {
                for (j=0;j<N;j++)
                {
                        if (fstr[i]==xaraktires[j])
                        {
                                suxnotites[j]++;
                                break;
                        }
                }
        }
        SortedFrequences(suxnotites,xaraktires);
        return 0;
}

I have this bubblesort function in my code. I have two arrays, one for the characters of a string and one for their frequence. what's the problem with my code? please I have a deadline in less than two hours!

Could you please tell us what is it doing? What should it do that it is not?

my program is supposed to accept a string or a file as an input and then print each character that is in it with its frequence with descending order. for example I have "aAwda 9dW" as an input which will always contain a-z and 0-9. No punctuation. I should have this as the output:
Character: Frequency:
a/A: 3
d/D: 2
w/W: 2
9: 1
In my program I erase the white spaces of the string and then I use tolower because I don't care if they are lower or upper case. I create two arrays. One with a-z & 0-9 and one with their frequencies which I find during the main program. then I send those arrays to a bubblesort function. if I print the arrays before the two for loops everything is fine but inside the for loops something is going totally wrong...

that's my final program. the comments are in greek. sorry :)

#include <iostream>
#include <string>
#include <cctype>
#include <iomanip>
#define N 36

using namespace std;

string EraseWhiteSpaces(string &str_nospaces)
{
        int i;
        for (int i=0;i<str_nospaces.length();i++)
        {
                if (str_nospaces[i]==' '||(str_nospaces[i]=='\t'))
                {
                        str_nospaces.erase(i,1);                        //an vrw mi ektupwsimo char ton kanw erase k meiwnw to metriti
                        i--;
                }
        }
        return str_nospaces;
}

string ConvertToLowerCase(string &str_lowercase)
{
        int i;
        for (i=0;i<str_lowercase.length();i++)
                str_lowercase[i]=tolower(str_lowercase[i]);             //metatrepw ta kefalaia se mikra k epistrefw ka8e char sto string
        return str_lowercase;
}

void SortedFrequences(int frequences[],char chars[])
{
        int i,j,tmp1;                                                   //edw uparxei provlima! an ektupwsw ton pinaka chars prin tis for ola einai swsta
        char tmp2,temp;                                                 //an to kanw mesa apo tis for kapoia grammata e3afanizontai k dn katalavainw giati
        for (i=0;i<N;i++)
        {
                for (j=i+1;j<N;j++)                                     //f8inousa ta3inomisi kai stous 2 pinakes me vasi ti suxnotita
                {
                        if (frequences[i]<frequences[j])
                        {
                                tmp1=frequences[i];
                                frequences[i]=frequences[j];
                                frequences[j]=tmp1;
                                tmp2=chars[i];
                                chars[i]=chars[j];
                                chars[j]=tmp1;
                        }
                }
        }
        cout<<"character:       frequency:"<<endl;
        for (i=0;i<N;i++)
        {
                if (frequences[i]==0)                                   //ektupwnw mexri na sunantisw mideniki suxnotita
                        break;
                if (isalpha(chars[i]))                                  //an o char dn einai ari8mos 8elw na ektupwsw kto antistoixo kefalaio
                {                                                       //kai kanw alli stoixisi ap'oti otan einai ari8mos
                        temp=chars[i]-32;
                        cout<<setw(4)<<chars[i]<<"/"<<temp<<":"<<setw(14)<<frequences[i]<<endl;
                }
                else
                        cout<<setw(6)<<chars[i]<<":"<<setw(14)<<frequences[i]<<endl;
        }
}

int main()
{
        int i,k=0,l=0,j;
        string str,fstr="";
        char xaraktires[N];
        int suxnotites[N]={0};
        while (getline(cin,str))
                fstr+=str;                               //pairnei ka8e grammi tis eisodou k tin pros8etei sto string fstr
        EraseWhiteSpaces(fstr);                          //kalei ti sunartisi pou svinei ka8e mi ektupwsimo xaraktira
        ConvertToLowerCase(fstr);                        //kalei ti sunartisi pou metatrepei ola ta kefalaia se mikra
        for (i=0;i<26;i++)
        {
                xaraktires[i]=97+k;                      //gemizw ton pinaka me a-z vasei tou ASCII
                k++;
        }
        for (i=26;i<N;i++)
        {
                xaraktires[i]=48+l;                      //gemizw ton upoloipo pinaka me 0-9 vasei tou ASCII
                l++;
        }
        for (i=0;i<fstr.length();i++)
        {
                for (j=0;j<N;j++)                        //pairnw ka8e char tou string k to psaxnw ston pinaka
                {
                        if (fstr[i]==xaraktires[j])
                        {                                //otan to vrw au3anw ti suxnotita sto antistoixo keli tou allou pinaka
                                suxnotites[j]++;
                                break;
                        }
                }
        }
        SortedFrequences(suxnotites,xaraktires);         //kalei ti bubblesort
        return 0;
}
for (i=0;i<N;i++)
{
for (j=i+1;j<N;j++) //f8inousa ta3inomisi kai stous 2 pinakes me vasi ti suxnotita
{
if (frequences[i]<frequences[j])
{
tmp1=frequences[i];
frequences[i]=frequences[j];
frequences[j]=tmp1;
tmp2=chars[i];
chars[i]=chars[j];
chars[j]=tmp1;
}
}
}

I found what was wrong with my code! I typed chars[j]=tmp1; instead of tmp2... stupid mistake! :P

Look at this part of the code:

tmp[b]2[/b]=chars[i];
   chars[i]=chars[j];
   chars[j]=tmp[b]1[/b];

You want to use tmp2 instead of tmp1 when assigning a value at chars[j].

EDIT: Oh, I see you found it yourself :P sorry for my late response!

Edited 4 Years Ago by mikrosfoititis: Changes made in the thread ignored while typing a reply

This article has been dead for over six months. Start a new discussion instead.