473,698 Members | 2,403 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Help With Changing Integer Data Type Into Character?

40 New Member
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
3 1339
ayman723
40 New Member
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
ayman723
40 New Member
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
3,652 Recognized Expert Specialist
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

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

Similar topics

8
5473
by: baustin75 | last post by:
Posted: Mon Oct 03, 2005 1:41 pm Post subject: cannot mail() in ie only when debugging in php designer 2005 -------------------------------------------------------------------------------- Hello, I have a very simple problem but cannot seem to figure it out. I have a very simple php script that sends a test email to myself. When I debug it in PHP designer, it works with no problems, I get the test email. If
1
2608
by: Wayne | last post by:
I'm using the following code from the Access Web to open a folder. The folder opens in "List" View. Is there an addition that I can make to the code to force the folder to open in "Details" view? 'This code was originally written by Ken Getz. 'It is not to be altered or distributed, 'except as part of an application. 'You are free to use it in any application, 'provided the copyright notice is left unchanged. '
4
2466
by: Warren Sirota | last post by:
Hi, Please let me know if I am interpreting this correctly. I've done a little testing of the difference between passing parameters byVal and byRef, and the results were slightly non-intuitive, as I expected. I haven't seen this behavior explained precisely in the .net world yet, so I wanted to check and make sure I've got it right. I apologize that this is a bit long. I've tried to keep it concise. There are code fragments here, but...
28
1881
by: Siv | last post by:
Hi, If I run the following: strSQL = "Select * FROM Clients;" da = New OleDb.OleDbDataAdapter(strSQL, Conn) 'Create data adapter cb = New OleDb.OleDbCommandBuilder(da) 'Create command builder using the datadapter dt = New Data.DataTable da.Fill(dt) 'pour in the data using the adapter
43
2224
by: ZillionDollarSadist | last post by:
Hello, I'm working at a simple Access '97 + VB4 application, and I ran into a terrible problem: something I never modified now gives me a totally unwanted "Invalid use of null" error. It happens in a Text.LostFocus event, this block: Do While Not TB6.EOF If Year(TB6(0)) = Val(Trim(Text1.Text)) Then !!!!!!!!!!!!!Here!!!!!!!!! ...
1
3712
by: Rahul | last post by:
Hi Everybody I have some problem in my script. please help me. This is script file. I have one *.inq file. I want run this script in XML files. But this script errors shows . If u want i am attach this script files and inq files. I cant understand this error. Please suggest me. You can talk with my yahoo id b_sahoo1@yahoo.com. Now i am online. Plz....Plz..Plz...
3
1449
by: ayman723 | last post by:
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...
0
3186
by: SOI_0152 | last post by:
Hi all! Happy New Year 2008. Il hope it will bring you love and happyness I'm new on this forum. I wrote a stored procedure on mainframe using DB2 7.1.1 and IBM language c. Everything works fine. Now we decided to move from mainframe IMS-DB2 to Windows 2003 server-DB2 UDB for LUW 9.5.
3
12584
by: amija0311 | last post by:
Hi, I am new using DB2 9.1 database by windows base. I want to query the data that contain string then translate the string into integer using DB2. The problems is If the data is null, i got the problem to translate. How to translate string also allow null data to integer. If null data it will read as space. My Data :- GEOSEG_ID SEQNO
0
8680
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8609
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9169
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8899
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8871
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7738
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4371
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3052
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2335
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.