By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,274 Members | 2,229 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,274 IT Pros & Developers. It's quick & easy.

Help With Changing Integer Data Type Into Character?

P: 40
hi

I had this problem where I was asked to :
Write a program that compares and records the time taken to sort an array of social security numbers using four sorting algorithms.

The program must generate random social security numbers. Each group must do an extensive testing on several randomly generated arrays so that a meaningful comparative analysis can be made on different sorting methods. Increase the size of the input to get a growth rate for each algorithm.
You are assigned to do elementary sorting methods: Insertion sort, Quick sort, Merge Sort, Heap Sort . Generate random SSNs as 9 characters: "023568708" using rand().

my question is :
the code is for integer data type and we need to change it to character type, BUT when I print the output it have to appear in this form:
12547865
25044745
01247855
01247524
75520455
each number consist of nine digits and must be stored as character data type. I tried to swiching to character type but then each number or character is printed in this form:
4
d
q
5
g
t
r

I am going to post the code in another thread because its long.
any help will be highly appreciated.
thanks
ayman


-----------------
May 9 '07 #1
Share this Question
Share on Google+
3 Replies


P: 40
here is the code I came up with. I had to split it, please copy and past the two halves so you would have an idea about the program.

Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <CONIO.H>
  3. #include <STDLIB.H>
  4. #include <STDIO.H>
  5. #include <TIME.H>
  6. #include <FSTREAM>
  7. ///////////////////////////////////////////////////////
  8. using namespace std ;
  9. const int arrSize=30000;
  10.  fstream outfile ("SortingAlgorithms.txt");
  11. //////////////////////////////////////////////////////
  12. void insert_special( int x, int heap[], int start, int end ){
  13. // insert x into the sub-heap from start to end
  14. // location start is empt
  15.   int child = 2*start + 1;        // left child of start
  16.   while (child <= end) {
  17.      if (child < end && heap[child] < heap[child + 1])
  18.         child++;         // child is the larger child of start
  19.      if (x > heap[child])
  20.         break;           // x stays in start
  21.      else {
  22.         heap[start] = heap[child];
  23.         start = child;
  24.         child = 2*start + 1;
  25.      }
  26.   }
  27.   heap[start] = x;
  28. }
  29. void buildHeap(int a[], int size )
  30. // Build a heap from an array of items
  31. // Binary tree with one node satisfies heap properties
  32. // Therefore ignore leaves and start from the parent
  33. // of the last item
  34. {
  35.   int i;
  36.   for (i = (size - 2)/2; i >= 0; i--)     // start from parent of last item
  37.      insert_special( a[i], a, i, size-1);
  38. }
  39.  
  40. void heapSort( int a[], int size )
  41. // sort an array of the given size
  42. {
  43.   int i;
  44.   int temp;
  45.   buildHeap( a, size );
  46.   for (i = size-1; i >=1; i--) {
  47.      temp = a[i];    // extract the last element from the array
  48.      a[i] = a[0];    // move top of heap to the end of the array
  49.      insert_special( temp, a, 0, i-1 );      // restore heap properties
  50.   }
  51. }
  52. //////////////////////////////////////////////////////
  53. void mergeConquer(int  a[], int left, int mid, int right) {
  54.        int x=0;//counters
  55.        int y=0;
  56.        int z=0;
  57.        int lno = mid - left + 1;
  58.        int rno = right - mid;
  59.        int *L  = new int[lno];
  60.        int *R  = new int[rno];
  61. //////////////////////////////////////
  62.  for ( y = 0; y < lno; y++) {
  63.                L[y] = a[left + y];
  64.        }
  65. ////////////////////////////////////
  66.  for (z = 0; z < rno; z++) {
  67.    R[z] = a[mid + z + 1];
  68.  }
  69.  y = 0;
  70.  z = 0;
  71.  /////////////////////////////////
  72.  for (int i = left; i <= right; i++) {
  73.    if ( (y < lno) && (z < rno)) {
  74.      if (L[y] <= R[z]) {
  75.        a[i] = L[y];
  76.        y++;
  77.      }
  78.      else {
  79.        a[i] = R[z];
  80.        z++;
  81.      }
  82.    }
  83.    else if ( (y < lno) && (z >= rno)) {
  84.      a[i] = L[y];
  85.      y++;
  86.    }
  87.    else if ( (y >= lno) && (z < rno)) {
  88.      a[i] = R[z];
  89.      z++;
  90.    }
  91.  
  92.  }
  93.  
  94. }
  95. ///////////////////////////////////////////////////////////////////////////////////////
  96. void mergeDivide(int  a[], int left, int right) {
  97.  int mid = (left + right) / 2;
  98.  if (left < right) {
  99.    //cout << "============";
  100.    mergeDivide(a, left, mid);
  101.    mergeDivide(a, mid + 1, right);
  102.    mergeConquer(a, left, mid, right);
  103.  }
  104.  
  105. }
  106.  
  107. /////////////////////////////////////////////////////
  108. void swap(int array[], int index1, int index2)
  109. {
  110.        int temp = array[index1];
  111.        array[index1] = array[index2];
  112.        array[index2] = temp;
  113. }
  114. //////////////////////////////////////////////////////////////////////////////////////////
  115. void quickSort(int array[], int start, int end)
  116. {
  117.        int i = start;//counters
  118.        int k = end;
  119.  
  120.        if (end - start >= 1)
  121.        {
  122.                int pivot = array[start];
  123.  
  124.                while (k > i)
  125.                {
  126.                        while (array[i] <= pivot && i <= end && k > i)
  127.                                i++;
  128.                        while (array[k] > pivot && k >= start && k >= i)
  129.                            k--;
  130.                        if (k > i)
  131.                                swap(array, i, k);
  132.                }
  133.                swap(array, start, k);
  134.  
  135.                quickSort(array, start, k - 1);
  136.                quickSort(array, k + 1, end);
  137.        }
  138.        else
  139.        {
  140.                return;
  141.        }
  142. }
  143. //////////////////////////////////////////////////////////////////////////////////////////
  144. void sortItInQuick(int array[])
  145. {
  146.        quickSort(array, 0, arrSize-1);
  147. }
  148.  
  149. ////////////////////////////////////////////////////////////////////////////////////////
  150. void shuffelArray(int a[] ){
  151.   int x;
  152.   srand ( time(NULL) );
  153.   for(int i=0;i< arrSize;i++){
  154.  
  155.        x=(((rand()%1000)*2)+165959802);
  156.        //x=(rand()%100);
  157.        //cout<<x<<endl;
  158.        a[i]=x;
  159.        //getch();
  160.   }
  161.  
  162.   getch();
  163.   cout<<endl<<"------------------------------------------- end intioalzition"<<endl;
  164. }
  165. ////////////////////////////////////////////////////////////
  166. void printOnFile(int arr[],char name[],int time){
  167.        outfile<<endl<<"-------------------------------------------------------------------"<<endl;
  168.        outfile<<"-------------------------------------------------------------------"<<endl;
  169.        outfile<<"-------------------------------------------------------------------"<<endl;
  170.        outfile<<"-------------------------------------------------------------------"<<endl;
  171.        outfile<<name<<endl<<"and the time is : "<<time<<endl;
  172.        outfile<<"-------------------------------------------------------------------"<<endl;
  173.        outfile<<"-------------------------------------------------------------------"<<endl;
  174.        outfile<<"-------------------------------------------------------------------"<<endl;
  175.        outfile<<"-------------------------------------------------------------------"<<endl;
  176.        /*for(int i=0;i<arrSize;i++){
  177.                outfile <<arr[i]<<"  ";
  178.        }*/
  179.  
  180. }
  181. ////////////////////////////////////////////////////////////
  182. void copyArray(int arr1[],int arr2[],int size){
  183.        arr2=new int[size];
  184.        for(int i=0;i<size;i++)
  185.                arr2[i]=arr1[i];
  186.  
  187. }
  188. ////////////////////////////////////////////////////////////
  189.  
  190. void sortInsertions(int  a[]) {
  191.  
  192.  for (int j = 2; j < arrSize; j++) {
  193.    for (int k = 0; k < j; k++) {
  194.      if (a[j] < a[k]) {
  195.        int temp = a[k];
  196.        a[k] = a[j];
  197.        a[j] = temp;
  198.      }
  199.    }
  200.  }
  201.  
  202. }
  203.  
  204. //////////////////////////////////////////////////////
May 9 '07 #2

P: 40
Expand|Select|Wrap|Line Numbers
  1. void main() {
  2.                        int arr[arrSize];
  3.                        int temp[arrSize];
  4.                        int choice =-1;
  5.                        /////////////////////////////////////////////
  6.                        cout<<"Array is shffiling ";
  7.                        shuffelArray(arr);
  8.                        cout<<endl<<"-------------------------------------------"<<endl;
  9.                        ////////////////////////////////////////////
  10.                        while (choice !=0){
  11.                                cout<<"-------------------------------------------"<<endl;
  12.                                cout<<"-------------------------------------------"<<endl;
  13.                                cout<<"1- Quick Sort "<<endl;
  14.                                cout<<"2- inseration Sort "<<endl;
  15.                                cout<<"3- merage Sort "<<endl;
  16.                                cout<<"4- Heap Sort "<<endl;
  17.                                cout<<"-------------------------------------------"<<endl;
  18.                                cout<<"Enter your choice :";
  19.                                cin>>choice ;
  20.                                switch (choice) {
  21.                                        case 1:{
  22.                                                //cout<<"THis choice is 1 ";
  23.                                                        copyArray(arr,temp,arrSize);
  24.                                                        time_t start=time(NULL);
  25.                                                        cout<<"---------------------be4 quick the time on start : "<<start<<endl<<endl;
  26.                                                        sortItInQuick(temp);
  27.                                                        time_t end=time(NULL);
  28.                                                        printOnFile (temp,"Quick Sort",end-start);
  29.                                                        cout<<endl<<"---------------------after  quick the time is end-start: "<<end-start<<endl<<endl;
  30.                                                        cout<<"The Array After sorted ";
  31.                                                        getch();
  32.                                                   }
  33.                                                break ;
  34.                                        case 2 :{
  35.                                                copyArray(arr,temp,arrSize);
  36.                                                        time_t start=time(NULL);
  37.                                                        cout<<"---------------------be4 inseration : and the time is : "<<start<<endl<<endl;
  38.                                                        sortInsertions (temp);
  39.                                                        time_t end=time(NULL);
  40.                                                        //print (temp);
  41.                                                        printOnFile (temp,"Inseration Sort",end-start);
  42.                                                        cout<<endl<<"---------------------after  inseration: and the time is : "<<end-start<<endl<<endl;
  43.                                                        cout<<"The Array After sorted ";
  44.  
  45.                                                        getch();
  46.                                                   }
  47.                                                break;
  48.                                        case 3 :{
  49.                                                copyArray(arr,temp,arrSize);
  50.                                                        time_t start=time(NULL);
  51.                                                        cout<<"---------------------be4 mearge : and the time is : "<<start<<endl<<endl;
  52.                                                        mergeDivide (temp,0,arrSize-1);
  53.                                                        time_t end=time(NULL);
  54.                                                        //print (temp);
  55.                                                        printOnFile (temp,"Merage Sort",end-start);
  56.                                                        cout<<endl<<"---------------------after  mearge : and the time is : "<<end-start<<endl<<endl;
  57.                                                        cout<<"The Array After sorted ";
  58.  
  59.                                                        getch();
  60.                                                   }
  61.                                                break;
  62.                                                case 4 :{
  63.                                                copyArray(arr,temp,arrSize);
  64.                                                        time_t start=time(NULL);
  65.                                                        cout<<"---------------------be4 heap : and the time is : "<<start<<endl<<endl;
  66.                                                        heapSort(temp,arrSize);
  67.                                                        time_t end=time(NULL);
  68.                                                        printOnFile (temp,"Heap Sort",end-start);
  69.                                                        cout<<endl<<"---------------------after  heap: and the time is : "<<end-start<<endl<<endl;
  70.                                                        cout<<"The Array After sorted ";
  71.  
  72.                                                        getch();
  73.                                                   }
  74.                                                break;
  75.                                choice=-1;
  76.                                }
  77.                        }
  78.                        outfile.close();
  79.  
  80. }
May 9 '07 #3

Ganon11
Expert 2.5K+
P: 3,652
ayman:

Please find time to read our FAQ, which includes the guidelines for posting a question. Specifically, (a) please do not use 3 threads for 1 question - adding replies to a single thread would have been sufficient, and (b) please use [code] tags when posting your code.
May 9 '07 #4

Post your reply

Sign in to post your reply or Sign up for a free account.