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???