473,396 Members | 1,879 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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
May 10 '07 #1
3 1438
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 ("SortingAlgorithms.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(int 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(int array[])
{
quickSort(array, 0, arrSize-1);
}

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

x=(((rand()%1000)*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<<endl;
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;
}
}
}

}

//////////////////////////////////////////////////////
May 10 '07 #2
rest of code

[ code ]

void main() {
int arr[arrSize];
int temp[arrSize];
int choice =-1;
/////////////////////////////////////////////
cout<<"Array is shffiling ";
shuffelArray(arr);
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,temp,arrSize);
time_t start=time(NULL);
cout<<"---------------------be4 quick the time on start : "<<start<<endl<<endl;
sortItInQuick(temp);
time_t end=time(NULL);
printOnFile (temp,"Quick Sort",end-start);
cout<<endl<<"---------------------after quick the time is end-start: "<<end-start<<endl<<endl;
cout<<"The Array After sorted ";
getch();
}
break ;
case 2 :{
copyArray(arr,temp,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,"Inseration Sort",end-start);
cout<<endl<<"---------------------after inseration: and the time is : "<<end-start<<endl<<endl;
cout<<"The Array After sorted ";

getch();
}
break;
case 3 :{
copyArray(arr,temp,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<<endl;
cout<<"The Array After sorted ";

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

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

}
May 10 '07 #3
weaknessforcats
9,208 Expert Mod 8TB
Beware of the << operator. It is really a function call.

Expand|Select|Wrap|Line Numbers
  1. int x = 'A';
  2. cout << x;
  3.  
is really a call to something like:
Expand|Select|Wrap|Line Numbers
  1. ostream operator<<(ostream& os, int arg);
  2.  
This function displays the numeric value of the argument. In this case 65. However, when you:
Expand|Select|Wrap|Line Numbers
  1. char x = 'A';
  2. cout << x;
  3.  
is really a call to something like:
Expand|Select|Wrap|Line Numbers
  1. ostream operator<<(ostream& os, char arg);
  2.  
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.
May 10 '07 #4

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

Similar topics

8
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 -------------------------------------------------------------------------------- ...
1
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?...
4
by: DOTNET | last post by:
Hi, Anybody help me regarding this error: I am assigning the values to the session variables when the button is clicked and passing these session variables to the next page and when I am...
4
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,...
28
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) ...
43
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...
1
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...
3
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...
0
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...
3
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...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...
0
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
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...
0
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...
0
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,...

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.