May 6, 2010 at 1:23pm UTC
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 UTC
May 6, 2010 at 3:38pm UTC
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 UTC
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 UTC
May 6, 2010 at 4:00pm UTC
You're assigning ';' to I.
I got my stuff mixed up.
-Albatross
Last edited on May 6, 2010 at 7:07pm UTC
May 6, 2010 at 4:09pm UTC
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 UTC
= is assignment in C++
== is comparison
You want comparison. Your line is assigning.
May 6, 2010 at 7:43pm UTC
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 UTC
Try an integer array of length 26 along with a switch statement. Or mess about with the ASCII values.
-Albatross