Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

CONVERTING PROGRAM TO TEMPLATE FUNCTION. PLEASE HELP!! 1

Status
Not open for further replies.

DonCL

Programmer
Jul 16, 2001
43
US
Hello, I really need help with this program. There are three parts, which i am having a problem understanding. If anyone has any suggestions or solutions, i would really appreciate it. Thanks. Here it is:

A driver generates two different arrays (integers and float numbers) and calls template function. Then it puts sorted arrays in a file.

a. Input Phase: within the driver, use a random generator to generate 100 integers and 100 float numbers (w/ single decimal).

b. Processing Phase: convert the following program to template function:
Code:
// This program puts values into an array, sorts the values into ascending order, and proints the resulting array.

#include <iostream>
using std::cout;
using std::endl;
#include <iomanip>
using std::setw;

void bubbleSort( int*, const int);

int main()
{
const int arraySize=10;
int a[arraySize] = {2,6,4,8,10,12,89,68,45,37};
int i;
cout << &quot;Data items in original order\n&quot;;
for (i=0; i<arraySize; i++)
cout << setw(4)<< a[i];
bubbleSort(a, arraySize);  //sort the array
cout << &quot;\nData items in ascending order\n&quot;;
for (i=0; i<arraySize; i++)
cout << setw(4) << a[i];
cout << endl;
return 0;
}

void bubbleSort(int *array, const int size)
{
void swap (int *const, int *const);
for (int pass = 0; pass < size - 1; pass++)
for int j=0; j< size -1; j++)
if (array[j] > array[j+1])
swap( &array[j], &array[j+1]);
}

void swap (int *const element1Ptr, int *const element2Ptr)
{
int hold = *element1Ptr;
*element1Ptr = *element2Ptr;
*element2Ptr = hold;
}
_______
c. Output phase: a file created by driver contains with 100 sorted integers and 100 sorted float numbers.

Thanks to all for your help!!!​

 

template<class xx>void bubbleSort(xx *array, const int size)
{
void swap (int *const, int *const);//what does it mean there?
for (int pass = 0; pass < size - 1; pass++)
for int j=0; j< size -1; j++)
if (array[j] > array[j+1])
swap( &array[j], &array[j+1]);
}

template<class xx>void swap (xx *const element1Ptr, xx *const element2Ptr)
{
xx hold = *element1Ptr;
*element1Ptr = *element2Ptr;
*element2Ptr = hold;
}
John Fill
1c.bmp


ivfmd@mail.md
 
void swap (int *const, int *const);//what does it mean there?
this is actually (int * const, int * const); i dont know if it makes a difference, but that is what is on the program.
Hey John, is that how the whole program is like? Im supposed to use a random generator somewhere in the program to generate 100 integers, and 100 float numbers. can you please help me out with that? thanks for your help, i really appreciate it.
 
the buble sort will be for both, float and integers and chars. If you overwrite some operators for other classes, theese functions can be extended

template<class xx>void bubbleSort(xx *array, const int size);
int main()
{
const int arraySize=10;
int a[arraySize] = {2,6,4,8,10,12,89,68,45,37};
int i;
cout << &quot;Data items in original order\n&quot;;
for (i=0; i<arraySize; i++)
cout << setw(4)<< a;
bubbleSort(a, arraySize); //sort the array
cout << &quot;\nData items in ascending order\n&quot;;
for (i=0; i<arraySize; i++)
cout << setw(4) << a;
cout << endl;
return 0;
}
template<class xx>void bubbleSort(xx *array, const int size)
{
void swap (int *const, int *const);//what does it mean there?
for (int pass = 0; pass < size - 1; pass++)
for int j=0; j< size -1; j++)
if (array[j] > array[j+1])
swap( &array[j], &array[j+1]);
}

template<class xx>void swap (xx *const element1Ptr, xx *const element2Ptr)
{
xx hold = *element1Ptr;
*element1Ptr = *element2Ptr;
*element2Ptr = hold;
}
John Fill
1c.bmp


ivfmd@mail.md
 
Thanks for the responses. One other thing that im not too clear on. where does the random generator come in? thanks again.
 
I don't know if this is what you're asking

int randint[100];
float randfl[100];

for(int count = 0; count < 100; count++)
{
randint[count] = (int)rand();
randfl[count] = (float)rand();
} -Feryl
--> Just a silly software engineer wannabee.
 
I think you can genearte randoms like

template<class xx>xx trand()
{
return (xx)rand()/(xx)rand();
} John Fill
1c.bmp


ivfmd@mail.md
 
I think you can genearte randoms like

template<class xx>xx trand(xx)
{
return (xx)rand()/((xx)rand()+1);//+1 on the case rand()==0
}

so you will place a parametter for compiller to create the right function, because it can't distinct functions by return value. John Fill
1c.bmp


ivfmd@mail.md
 
Thanks for everyone's help! i appreciate it.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top