insertion sort of char array and occurence of the letters

May 6, 2010 at 1:23pm
hi!! I have been trying to do char array which provide correct insertion sort order for the texts written in small caps using the English alphabet,and when you put a semicolon the middle of character, the sort will be finished and then the program shows the occurrence (frequency) of each letter for example “xcc f c x ; axbxcx”
Letter occurrence
c 3
f 1
x 2

that's my program;

#include <iostream>

void InsertionSort(char* array, int size);

using namespace std;

int main()
{
int size;

cout << "Enter the array size:" << endl;
cin>>size;
char myArray[size];
cout<<"myArray=";
cin.getline (myArray,size);


cout<<"\nBefore sort:"<<endl;

for(int i=0;i<size;i++)

{
if(i=';') break;
cout<<myArray[i]<<endl;
}

InsertionSort(myArray,size);

cout<<"\nAfter sort:"<<endl;
for(int i=0;i<size;i++)
cout<<myArray[i]endl;

return 0;
}

void InsertionSort(char* array, int size)
{
int i,j,temp;
for(i=1;i<size;i++)
{
j=i;

while(j>0&&array[j-1]<array[j])
{

temp=array[j];
array[j]=array[j-1];
array[j-1]=temp;

j--;
}
}
}

but I couldn't add occurrence part and also this program is not working:(
what is my fault and how can I add occurrence part??
Last edited on May 6, 2010 at 3:50pm
May 6, 2010 at 2:01pm
Let me introduce you to the wide key at the bottom of your keyboard called the "spacebar".
Your code is badly in need of spaces to make it readable.

The first thing I see is

 
if(i=';')


is always true. Do you know why?
May 6, 2010 at 2:02pm
And a syntax error here:

 
while(j>0&&array[j-1]array[j]){

May 6, 2010 at 2:07pm
Your InsertionSort() function works if you correct the above syntax error.

 
cin >> size;


reads stdin up to, but not including, the carriage return.

Then, when you do the

 
cin.getline (myArray,size);


The first character it sees is the newline, which causes it to not read anything else.
May 6, 2010 at 3:38pm
thank you very much for your helping, but how can I correct it. I'm new for char array, so I couldnt understand what I must do:(
May 6, 2010 at 3:58pm
I readjusted my project I hope so now it is readable.

if(i=';')

is always true. Do you know why?

why I dont know:(

And how can I correct part of cin>>size??
Last edited on May 6, 2010 at 4:02pm
May 6, 2010 at 4:00pm
You're assigning ';' to I.

I got my stuff mixed up.

-Albatross
Last edited on May 6, 2010 at 7:07pm
May 6, 2010 at 4:09pm
ok, I understood my fault, but I want to stop the sorting when I put a semicolon as I explained above, how can I write it, please help me...
May 6, 2010 at 7:00pm
= is assignment in C++
== is comparison

You want comparison. Your line is assigning.
May 6, 2010 at 7:43pm
ok, thanks I realized my stupidity, now my program is working, but I couldnt add frequency of each letter , is there any method to do it which I dont know??
have you any idea about it??
May 6, 2010 at 8:13pm
Try an integer array of length 26 along with a switch statement. Or mess about with the ASCII values.

-Albatross
Topic archived. No new replies allowed.