Hi again!

I've been working on this program and I am thankful for all the help and tips I've gotten from this forum. However, at the moment I can't seem to get this selection sort to work on my array. I've tried it 3 different ways(will post code below) and in the first 2, they compile but the program crashes at the point where it is supposed to give me the results. The 3rd one compiles, doesn't crash, but gives me all zeroes as a result...help is always appreciated.

#include<iostream>
#include<cctype>
#include<string>
using namespace std;

const int ARRAY_SIZE=256;
void printStat(size_t allCount[], const size_t Size);
void getChar(size_t allCount[], size_t Size);
void selectionSort(size_t allCount[], size_t Size);

int main ()
{
    size_t allCount[ARRAY_SIZE]={0};
    size_t Size;
    int x,n;
    
    getChar(allCount,Size);
    selectionSort(allCount,Size);
    printStat(allCount,Size);
    
system("pause");
return 0;
}

//**************************************************************************
void getChar(size_t allCount[], size_t Size)
{
    char ch = 0;
    char end = '.';
    
    cout<<"Enter a sequence of characters(end with '.'): ";
    while( cin.get(ch)  && ch != end )
    {
     if (isalpha(ch))
      {
      if (islower(ch))
         ch=toupper(ch);
      } 
         allCount[ch] += 1;      
    }
}

//****************************************************************************
void printStat(size_t allCount[], const size_t Size)
{

 
	cout<<"\n\nLetter:     Number of Occurrences\n\n";
 
	for(char i = 'A'; i <= 'Z' && i < Size; i++){
		cout<< i <<"     =     " << allCount[i] << endl;
	}
}

//****************************************************************************
void selectionSort(size_t allCount[],size_t Size) 
{
    for (int pass=0; pass<Size-1; pass++) {
        int potentialSmallest = pass;  // assume this is smallest

        //--- Look over remaining elements to find smallest.
        for (int i=pass+1; i<Size; i++) {
            if (allCount[i] < allCount[potentialSmallest]) {
                //--- Remember index for latter swap.
                potentialSmallest = i;
            }
        }
        
        //--- Swap smallest remaining element
        int temp = allCount[pass];
        allCount[pass] = allCount[potentialSmallest];
        allCount[potentialSmallest] = temp;
    }
}
#include<iostream>
#include<cctype>
#include<string>
using namespace std;

const int ARRAY_SIZE=256;
void printStat(size_t allCount[], const size_t Size);
void getChar(size_t allCount[], const size_t Size);
void selectionSort(size_t allCount[], size_t Size);
int indexOfSmallest(const size_t allCount[],int startingIndex,size_t Size);

int main ()
{
    size_t allCount[ARRAY_SIZE]={0};
    size_t Size;
    int x,n;
    
    getChar(allCount,Size);
    selectionSort(allCount,Size);
    printStat(allCount,Size);
    
system("pause");
return 0;
}

//**************************************************************************
void getChar(size_t allCount[], const size_t Size)
{
    char ch = 0;
    char end = '.';
    
    cout<<"Enter a sequence of characters(end with '.'): ";
    while( cin.get(ch)  && ch != end )
    {
     if (isalpha(ch))
      {
      if (islower(ch))
         ch=toupper(ch);
      } 
         allCount[ch] += 1;      
    }
}

//****************************************************************************
void printStat(size_t allCount[], const size_t Size)
{

 
	cout<<"\n\nLetter:     Number of Occurrences\n\n";
 
	for(char i = 'A'; i <= 'Z' && i < Size; i++){
		cout<< i <<"     =     " << allCount[i] << endl;
	}
}

//****************************************************************************
void selectionSort(size_t allCount[],size_t Size) 
{
    for (int count=0; count<Size-1; count++) 
    {
       swap(allCount[indexOfSmallest(allCount,count,Size)],allCount[count]);
    }
}

//****************************************************************************
int indexOfSmallest(const size_t allCount[],int startingIndex,size_t Size)
{
    int targetIndex=startingIndex;
    
    for(int count=startingIndex+1;count<Size;count++)
    {
     if (allCount[count]<allCount[targetIndex])
     {
      targetIndex=count;
      }
     }
     
    return targetIndex;
}
#include<iostream>
#include<cctype>
#include<string>
using namespace std;

const int ARRAY_SIZE=256;
void printStat(size_t allCount[], const size_t Size);
void getChar(size_t allCount[], const size_t Size);
void sort(size_t allCount[], size_t Size);

int main ()
{
    size_t allCount[ARRAY_SIZE]={0};
    size_t Size;
    
    getChar(allCount,Size);
    sort(allCount,Size);
    printStat(allCount,Size);
    
system("pause");
return 0;
}

//**************************************************************************
void getChar(size_t allCount[], const size_t Size)
{
    char ch = 0;
    char end = '.';
    
    cout<<"Enter a sequence of characters(end with '.'): ";
    while( cin.get(ch)  && ch != end )
    {
     if (isalpha(ch))
      {
      if (islower(ch))
         ch=toupper(ch);
      } 
         allCount[ch] += 1;      
    }
}

//****************************************************************************
void printStat(size_t allCount[], const size_t Size)
{

 
	cout<<"\n\nLetter:     Number of Occurrences\n\n";
 
	for(char i = 'A'; i <= 'Z' && i < Size; i++){
		cout<< i <<"     =     " << allCount[i] << endl;
	}
}

//***************************************************************************
// Step through each element of the array
void sort(size_t allCount[], size_t Size)
{
for (int nStartIndex = 0; nStartIndex <ARRAY_SIZE; nStartIndex++)

{

// nSmallestIndex is the index of the smallest element

// we've encountered so far.

int nSmallestIndex = nStartIndex;

 

// Search through every element starting at nStartIndex+1

for (int nCurrentIndex = nStartIndex + 1; nCurrentIndex <ARRAY_SIZE; nCurrentIndex++)

{

// If the current element is smaller than our previously found smallest

if (allCount[nCurrentIndex] < allCount[nSmallestIndex])

// Store the index in nSmallestIndex

nSmallestIndex = nCurrentIndex;

}

 

// Swap our start element with our smallest element

swap(allCount[nStartIndex], allCount[nSmallestIndex]);

}
}

:-/
what am I doing wrong???

Hmm, I was thinking...correct me if my logic is wrong:
The array I have only stores the occurrence of each letter. That is why if I type in 4 a's and then ask for allCount[65], I get 4 as the answer. Because allCount has 256 available slots, many of which are filled with zeroes, when I sort, I am merely moving the # of occurrences from say allCount[65] to allCount[0]. Sooo, then when I use my print function, it is still basically looking at allCount[65]-allCount[91] which is full of the swapped zeroes. So, maybe my last attempt isn't exactly wrong, I just need a new approach. Because once my occurrences have been sorted, I no longer have a way of knowing(let alone printing to screen) which occurrence went with what letter. Suggestions please! I am back to thinking that maybe I need to copy the 1st array into another, sort the 2nd, and then compare them in some sort of way so that I know which letter had how many occurrences.

In all of your program you have never initialized the Size variable!
I didn't know that the array has to be sorted from the number of occurrence, sorry.

Edit : would you mind using something like map. It would be
easy. Or actually using struct would be easy too. And you can use the
std::sort, so you don't have to sort it. Or we can make another
array and goon from there.

It must be sorted by the number of occurrences-from highest to lowest.

Would you mind using structs?

I wouldn't mind...but I just started looking at structs. We're allowed to use them but I'm not comfortable with them at all yet.

It's almost like we went out of order because I think we should've gone over structs BEFORE arrays. lol!

What part of them is troubling you?

You will only need this :

struct Letter
{
    //variables
     char  representiveLetter;
     int numberOfOccurance;
    
    //constructor. 
    Letter() {
     representiveLetter = 0;
     numberOfOccurance = 0;  
   }
};

From there you can do the program easy.

Hope everyone's Thanksgiving was good!

Anyway, sorry I'm just now responding. I actually finished this program without using structs. I used 2 1D arrays: one to store the occurrences and one to store the letter. One day soon I will go back over structs but for now I have to move on to the next project(whose due date was moved up 2 weeks! ugh!)

Thanks for all your help though!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.