Reading the ASCII code from a series of chars

My program involves populating an array of size 37 with empty queues, then sorting elements into the array based on their respective ASCII code (any special character ASCII goes into 1, numbers into 2-11, and letters into 12-37). It is to sort backwards through the strings using loops and foward using recursion. My issue is that I'm not sure how to take each respective letter in the string and extract its ASCII value.

RadixSort.h:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
template < class T >
class RadixSort
{
   private:
      static char getRadixChar(T* item, int index); //1-based
      static void binSort(QueueLinked<T>* bin, int curr_char, int num_chars, char (*getRadixChar) (T* item, int index));
      static void radixSortAsc(T** sort, int n, int num_chars, char (*getRadixChar) (T* item, int index));  //algorithm 1
      static void radixSortDesc(T** sort, int n, int num_chars, char (*getRadixChar) (T* item, int index));  //algorithm 2
 
   public:
      static T** radixSort(T** sort, int num_to_sort, int num_chars, bool asc, char (*getRadixChar) (T* item, int index));
};

template < class T >
char RadixSort<T>::getRadixChar(T* item, int index)
{
	//return 0;
	//Test stub
}


RadixSort.cpp:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#include <iostream>
using namespace std;

void deleteCDs(ListArray<CD>* list)
{
   ListArrayIterator<CD>* iter = list->iterator();

   while(iter->hasNext())
   {
      CD* cd = iter->next();
      delete cd;
   }
   delete iter;
}

int main()
{
   ListArray<CD>* list = CD::readCDs("cds.txt");
   int size = list->size();

   CD** cds = new CD*[size];

   ListArrayIterator<CD>* iter = list->iterator();
   int count = 0;
   while(iter->hasNext())
   {
      CD* cd = iter->next();
      cds[count] = cd;
      count++;
   }
   delete iter;

   //DO THIS
   //test both radix sort methods using the cds array
   
	/*while(iter->hasNext())
	  {
		 CD* cd = iter->next();
		 cd->displayCD();
	  }*/
   //Test stub
   
   /*iter = list->iterator();
   CD* cd = iter->next();
   cd = iter->next();
   cd->displayCD();
   cout<< "radixChar1: " << CD::getRadixChar(cd, 1);
   delete iter;
   delete[] cds;*/
   //Test 2
   
   

   deleteCDs(list);
   delete list;

   return 0;
}
Last edited on
after you use:
 
char c =  (*getRadixChar)(sort[index of item], character index);



put that char into a function that looks like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
int binPlace(char c)
{
     if(c >= 97 && c <= 122) //if lowercase
     {
        return c - 86;  //bin 11-37
     }
     else if(c >= 65 && c <= 90) //if uppercase
     {
        return c - 54;  //bin 11-37
     }
     else if(c >= 65 && c <= 90) //if int
     {
        return c - 47;  //bin 1-10
     }
     else //if special character
     {
         return 0; //bin 0
     }

} 


That will put the letter in the specified bin.

The code would look like this:

1
2
3
 char c =  (*getRadixChar)(sort[index of item], character index);  
 int bin_place = binPlace(c);                                  //which bin to put the item in
 bins[bin_place]->enqueue(sort[ index of item ]);  //put whole word in bin based on character 


Thanks for help. I'm assuming that code would go into my binSort function, correct? And if so, would I just be calling it into my getRadixChar function?
Last edited on
you create a function that looks like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23

template < class T >
int RadixSort<T>::binPlace(char c)
{
     if(c >= 97 && c <= 122) //if lowercase
     {
        return c - 86;  //bin index 11-37
     }
     else if(c >= 65 && c <= 90) //if uppercase
     {
        return c - 54;  //bin index 11-37
     }
     else if(c >= 65 && c <= 90) //if int
     {
        return c - 47;  //bin index 1-10
     }
     else //if special character
     {
         return 0; //bin index 0
     }

} 


In your binSort, radixSortDesc, and radixSortAsc functions you use that function to find the correct bin index to insert in.

Like this:

1
2
3
 char c =  (*getRadixChar)(sort[index of item], character index);  
 int bin_place = binPlace(c);                                  //which bin to put the item in
 bins[bin_place]->enqueue(sort[ index of item ]);  //put whole word in bin based on character  


The code above do this:

1. gets an item at the indicated index and gets one character, depending on the index
NOTE: you have go through all of the characters in all of the items, to sort all of the
words.
2. gets the correct bin index for that character

3. puts the whole item in the correct bin depending on the character that was chosen.

hopefully that is clear
Sorry don't use that for radixSortAsc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
template < class T >
int RadixSort<T>::binPlace(char c)
{
     if(c >= 97 && c <= 122) //if lowercase
     {
        return c - 86;  //bin index 11-37
     }
     else if(c >= 65 && c <= 90) //if uppercase
     {
        return c - 54;  //bin index 11-37
     }
     else if(c >= 65 && c <= 90) //if int
     {
        return c - 47;  //bin index 1-10
     }
     else //if special character
     {
         return 0; //bin index 0
     }

} 


http://www.cplusplus.com/reference/cctype/
It worked. Thanks for help, guys.
I believe we are working of the same project.
Topic archived. No new replies allowed.