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:
// 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 << "Data items in original order\n";
for (i=0; i<arraySize; i++)
cout << setw(4)<< a;
bubbleSort(a, arraySize); //sort the array
cout << "\nData items in ascending order\n";
for (i=0; i<arraySize; i++)
cout << setw(4) << a;
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!!!
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:
// 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 << "Data items in original order\n";
for (i=0; i<arraySize; i++)
cout << setw(4)<< a;
bubbleSort(a, arraySize); //sort the array
cout << "\nData items in ascending order\n";
for (i=0; i<arraySize; i++)
cout << setw(4) << a;
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!!!