insertion sort of char array and occurence of the letters

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
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?
And a syntax error here:

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

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.
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:(
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
You're assigning ';' to I.

I got my stuff mixed up.

-Albatross
Last edited on
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...
= is assignment in C++
== is comparison

You want comparison. Your line is assigning.
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??
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.