Help With Changing Integer Data Type Into Character?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ayman723
    New Member
    • Sep 2006
    • 40

    Help With Changing Integer Data Type Into Character?

    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
  • ayman723
    New Member
    • Sep 2006
    • 40

    #2
    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.
    [ code ]

    #include <iostream>
    #include <CONIO.H>
    #include <STDLIB.H>
    #include <STDIO.H>
    #include <TIME.H>
    #include <FSTREAM>
    ///////////////////////////////////////////////////////
    using namespace std ;
    const int arrSize=30000;
    fstream outfile ("SortingAlgori thms.txt");
    //////////////////////////////////////////////////////
    void insert_special( int x, int heap[], int start, int end ){
    // insert x into the sub-heap from start to end
    // location start is empt
    int child = 2*start + 1; // left child of start
    while (child <= end) {
    if (child < end && heap[child] < heap[child + 1])
    child++; // child is the larger child of start
    if (x > heap[child])
    break; // x stays in start
    else {
    heap[start] = heap[child];
    start = child;
    child = 2*start + 1;
    }
    }
    heap[start] = x;
    }
    void buildHeap(int a[], int size )
    // Build a heap from an array of items
    // Binary tree with one node satisfies heap properties
    // Therefore ignore leaves and start from the parent
    // of the last item
    {
    int i;
    for (i = (size - 2)/2; i >= 0; i--) // start from parent of last item
    insert_special( a[i], a, i, size-1);
    }

    void heapSort( int a[], int size )
    // sort an array of the given size
    {
    int i;
    int temp;
    buildHeap( a, size );
    for (i = size-1; i >=1; i--) {
    temp = a[i]; // extract the last element from the array
    a[i] = a[0]; // move top of heap to the end of the array
    insert_special( temp, a, 0, i-1 ); // restore heap properties
    }
    }
    //////////////////////////////////////////////////////
    void mergeConquer(in t a[], int left, int mid, int right) {
    int x=0;//counters
    int y=0;
    int z=0;
    int lno = mid - left + 1;
    int rno = right - mid;
    int *L = new int[lno];
    int *R = new int[rno];
    //////////////////////////////////////
    for ( y = 0; y < lno; y++) {
    L[y] = a[left + y];
    }
    ////////////////////////////////////
    for (z = 0; z < rno; z++) {
    R[z] = a[mid + z + 1];
    }
    y = 0;
    z = 0;
    /////////////////////////////////
    for (int i = left; i <= right; i++) {
    if ( (y < lno) && (z < rno)) {
    if (L[y] <= R[z]) {
    a[i] = L[y];
    y++;
    }
    else {
    a[i] = R[z];
    z++;
    }
    }
    else if ( (y < lno) && (z >= rno)) {
    a[i] = L[y];
    y++;
    }
    else if ( (y >= lno) && (z < rno)) {
    a[i] = R[z];
    z++;
    }

    }

    }
    ///////////////////////////////////////////////////////////////////////////////////////
    void mergeDivide(int a[], int left, int right) {
    int mid = (left + right) / 2;
    if (left < right) {
    //cout << "========== ==";
    mergeDivide(a, left, mid);
    mergeDivide(a, mid + 1, right);
    mergeConquer(a, left, mid, right);
    }

    }

    /////////////////////////////////////////////////////
    void swap(int array[], int index1, int index2)
    {
    int temp = array[index1];
    array[index1] = array[index2];
    array[index2] = temp;
    }
    //////////////////////////////////////////////////////////////////////////////////////////
    void quickSort(int array[], int start, int end)
    {
    int i = start;//counters
    int k = end;

    if (end - start >= 1)
    {
    int pivot = array[start];

    while (k > i)
    {
    while (array[i] <= pivot && i <= end && k > i)
    i++;
    while (array[k] > pivot && k >= start && k >= i)
    k--;
    if (k > i)
    swap(array, i, k);
    }
    swap(array, start, k);

    quickSort(array , start, k - 1);
    quickSort(array , k + 1, end);
    }
    else
    {
    return;
    }
    }
    //////////////////////////////////////////////////////////////////////////////////////////
    void sortItInQuick(i nt array[])
    {
    quickSort(array , 0, arrSize-1);
    }

    ////////////////////////////////////////////////////////////////////////////////////////
    void shuffelArray(in t a[] ){
    int x;
    srand ( time(NULL) );
    for(int i=0;i< arrSize;i++){

    x=(((rand()%100 0)*2)+165959802 );
    //x=(rand()%100);
    //cout<<x<<endl;
    a[i]=x;
    //getch();
    }

    getch();
    cout<<endl<<"------------------------------------------- end intioalzition"< <endl;
    }
    ////////////////////////////////////////////////////////////
    void printOnFile(int arr[],char name[],int time){
    outfile<<endl<< "-------------------------------------------------------------------"<<endl;
    outfile<<"-------------------------------------------------------------------"<<endl;
    outfile<<"-------------------------------------------------------------------"<<endl;
    outfile<<"-------------------------------------------------------------------"<<endl;
    outfile<<name<< endl<<"and the time is : "<<time<<en dl;
    outfile<<"-------------------------------------------------------------------"<<endl;
    outfile<<"-------------------------------------------------------------------"<<endl;
    outfile<<"-------------------------------------------------------------------"<<endl;
    outfile<<"-------------------------------------------------------------------"<<endl;
    /*for(int i=0;i<arrSize;i ++){
    outfile <<arr[i]<<" ";
    }*/

    }
    ////////////////////////////////////////////////////////////
    void copyArray(int arr1[],int arr2[],int size){
    arr2=new int[size];
    for(int i=0;i<size;i++)
    arr2[i]=arr1[i];

    }
    ////////////////////////////////////////////////////////////

    void sortInsertions( int a[]) {

    for (int j = 2; j < arrSize; j++) {
    for (int k = 0; k < j; k++) {
    if (a[j] < a[k]) {
    int temp = a[k];
    a[k] = a[j];
    a[j] = temp;
    }
    }
    }

    }

    //////////////////////////////////////////////////////

    Comment

    • ayman723
      New Member
      • Sep 2006
      • 40

      #3
      rest of code

      [ code ]

      void main() {
      int arr[arrSize];
      int temp[arrSize];
      int choice =-1;
      /////////////////////////////////////////////
      cout<<"Array is shffiling ";
      shuffelArray(ar r);
      cout<<endl<<"-------------------------------------------"<<endl;
      ////////////////////////////////////////////
      while (choice !=0){
      cout<<"-------------------------------------------"<<endl;
      cout<<"-------------------------------------------"<<endl;
      cout<<"1- Quick Sort "<<endl;
      cout<<"2- inseration Sort "<<endl;
      cout<<"3- merage Sort "<<endl;
      cout<<"4- Heap Sort "<<endl;
      cout<<"-------------------------------------------"<<endl;
      cout<<"Enter your choice :";
      cin>>choice ;
      switch (choice) {
      case 1:{
      //cout<<"THis choice is 1 ";
      copyArray(arr,t emp,arrSize);
      time_t start=time(NULL );
      cout<<"---------------------be4 quick the time on start : "<<start<<endl< <endl;
      sortItInQuick(t emp);
      time_t end=time(NULL);
      printOnFile (temp,"Quick Sort",end-start);
      cout<<endl<<"---------------------after quick the time is end-start: "<<end-start<<endl<<en dl;
      cout<<"The Array After sorted ";
      getch();
      }
      break ;
      case 2 :{
      copyArray(arr,t emp,arrSize);
      time_t start=time(NULL );
      cout<<"---------------------be4 inseration : and the time is : "<<start<<endl< <endl;
      sortInsertions (temp);
      time_t end=time(NULL);
      //print (temp);
      printOnFile (temp,"Inserati on Sort",end-start);
      cout<<endl<<"---------------------after inseration: and the time is : "<<end-start<<endl<<en dl;
      cout<<"The Array After sorted ";

      getch();
      }
      break;
      case 3 :{
      copyArray(arr,t emp,arrSize);
      time_t start=time(NULL );
      cout<<"---------------------be4 mearge : and the time is : "<<start<<endl< <endl;
      mergeDivide (temp,0,arrSize-1);
      time_t end=time(NULL);
      //print (temp);
      printOnFile (temp,"Merage Sort",end-start);
      cout<<endl<<"---------------------after mearge : and the time is : "<<end-start<<endl<<en dl;
      cout<<"The Array After sorted ";

      getch();
      }
      break;
      case 4 :{
      copyArray(arr,t emp,arrSize);
      time_t start=time(NULL );
      cout<<"---------------------be4 heap : and the time is : "<<start<<endl< <endl;
      heapSort(temp,a rrSize);
      time_t end=time(NULL);
      printOnFile (temp,"Heap Sort",end-start);
      cout<<endl<<"---------------------after heap: and the time is : "<<end-start<<endl<<en dl;
      cout<<"The Array After sorted ";

      getch();
      }
      break;
      choice=-1;
      }
      }
      outfile.close() ;

      }

      Comment

      • weaknessforcats
        Recognized Expert Expert
        • Mar 2007
        • 9214

        #4
        Beware of the << operator. It is really a function call.

        Code:
        int x = 'A';
        cout << x;
        is really a call to something like:
        Code:
        ostream operator<<(ostream& os, int arg);
        This function displays the numeric value of the argument. In this case 65. However, when you:
        Code:
        char x = 'A';
        cout << x;
        is really a call to something like:
        Code:
        ostream operator<<(ostream& os, char arg);
        This function displays the glyph of the argument. In this case A.

        All you need to do is assign your chars to a temporary int and display the int.

        Comment

        Working...