# really wrong answers in a sorting function

Page 1 of 1

## 5 Replies - 375 Views - Last Post: 11 May 2013 - 04:37 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=320615&amp;s=45ae16f08577a5ec1e7deac4b20fb986&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 breezett93

Reputation: 1
• Posts: 97
• Joined: 10-March 13

# really wrong answers in a sorting function

Posted 08 May 2013 - 07:24 PM

Hi everyone

I am trying to build a sorting function to work with letters and characters etc. The setup should look familiar because I have seen the foundation used in other programs. I gave the function a really simple short data file to see how accurate it is, and it is VERY wrong. I have read about various ways of accomplishing this, but I still am getting strange answers. If someone could pinpoint what I am doing wrong, that would be wonderful.
```void sort(int letterCount[], char whichLetter[] , int n) //sorts the incoming data
{
//declare objects
int hold;
char letter;

for(int count = 0; count < n; count++)
{
for(int count2 = 0 ; count2 < n; count2++)
{
if(letterCount[count]<letterCount[count2 + count])
{
hold = letterCount[count2 + count];
letterCount[count2 + count] = letterCount[count];
letterCount[count] = hold;
letter = whichLetter[count2 + count];
whichLetter[count2 + count] = whichLetter[count];
whichLetter[count] = letter;
}
}
}
}

```

```void countLetters(ifstream &input, int letterCount[], int &totalCount) //Counts each individual occurrance
{

char hold('a');
totalCount = 0;

// Sets the array letterCount values to zero
for(int count = 0; count < 26; count++)
{
letterCount[count] = 0;
}

// A loop that goes until it reaches the
// end of file
while(!input.eof())
{
// Holds each char value
input >> hold;

// Checks if the char value is a letter
// and keeps count of the letters
if(isalpha(hold))
{
letterCount[toupper(hold) - 'A']++;
totalCount++;
}
}
}

```

```int main()
{
//Declare variables
int letterCount[26];
int totalCount = 0;
char whichLetter[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
string filename;

//Open input file
cout << "Enter the name of the file to analyze: ";
getline(cin, filename);
cout << endl;
ifstream input(filename.c_str());
if (input.fail())
{
cerr << "Unable to open " << filename << "." << endl;
exit(-1);
}

countLetters(input, letterCount, totalCount);
sort(letterCount, whichLetter, 26);

//Display results
cout << "Out of " << totalCount << " characters, here are the " << TOP_HOW_MANY << " most common in the file." << endl;
for(int count = 0; (count < TOP_HOW_MANY) && (letterCount[count] > 0); count++)
{
cout << " #" << setw(2) << (count + 1) << ": " << whichLetter[count] << " occurs " << letterCount[count] << " times." << endl;
}
cin.get();
cin.get();

//Exit program
return 0;
}

```

Is This A Good Question/Topic? 0

## Replies To: really wrong answers in a sorting function

### #2 Skydiver

• Code herder

Reputation: 4254
• Posts: 13,612
• Joined: 05-May 12

## Re: really wrong answers in a sorting function

Posted 08 May 2013 - 08:13 PM

Check your loop constraints. You use count + count2 where both count and count2 can be equal to n - 1. So let's see what happens when both are n - 1, and n == 26:
```count + count2 ==
n - 1 + n - 1 ==
2 * ( n - 1) ==
2 * (26 - 1) ==
2 * 25 ==
50

```

That means that when you index into letterCount[count + count2] and whichLetter[count + count2], you are accessing up to letterCount[50] and whichLetter[50]. As best as I can read your code, you only have 26 elements in both of those arrays.

### #3 breezett93

Reputation: 1
• Posts: 97
• Joined: 10-March 13

## Re: really wrong answers in a sorting function

Posted 08 May 2013 - 08:28 PM

Oh ok. I need to adjust the counts then because the array length is 26 not 50. Thanks

### #4 Skydiver

• Code herder

Reputation: 4254
• Posts: 13,612
• Joined: 05-May 12

## Re: really wrong answers in a sorting function

Posted 08 May 2013 - 08:39 PM

Take a closer look at the bubble sort algorithm: http://en.wikipedia....iki/Bubble_sort

It looks like you got the general concept, but need to hone in on the details.

Now, an aside: Managing parallel arrays like you are doing should be avoided. As you can see, it gets tedious. The better approach is to use a structure. Something like:
```struct LetterFrequency
{
char letter;
int  count;
};

```

### #5 jjl

• Engineer

Reputation: 1120
• Posts: 4,647
• Joined: 09-June 09

## Re: really wrong answers in a sorting function

Posted 09 May 2013 - 08:57 PM

You could also take advantage of the ascii table and use a single array.

```   /* Each index represents an ascii character 0-127 */
int freq[ASCII_SIZE] = {0};
char buff[256];
char *temp;

/* Get input */
fgets(buff, sizeof(buff), stdin);

for(temp = buff; *temp; ++temp) {
/* Increment array at the index of each character */
++freq[(int)*temp];
}

```

EDIT: Didn't realize you were using C++, ignore the C IO; however, the concept still applies

This post has been edited by jjl: 09 May 2013 - 08:58 PM

### #6 breezett93

Reputation: 1
• Posts: 97
• Joined: 10-March 13

## Re: really wrong answers in a sorting function

Posted 11 May 2013 - 04:37 PM

@Sky Thanks for the link, much more informative than my previous source of knowlege. I havent worked with structures before so id have to read into them.
@jj I was debating using ascii keys. looks like they work very well.