1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
|
//
// main.cpp
// Question1_Project9
//
// Created by Tyler Reymer on 11/5/14.
// Copyright (c) 2014 Tyler Reymer. All rights reserved.
//
#include <iostream>
#include <string>
#include <cstring>
#include <ctime>
#include <algorithm>
using namespace std;
// Declare template class
template <class T, int n>
class TWO
{
private: T a[n];
public:
void ReadData();
void ReadData(string months[12]);// Read data into array a
void DisplayData();
void SortArray(); // Display array a
~TWO();
};
// Reads data into arrays for objects P and Q
template <class T, int n>
void TWO<T, n>::ReadData()
{
int random = 0;
for (int i = 0; i < n; i++)
{
// Generates 10 random numbers below 20
random = rand() % 20;
a[i] = random;
}
}
template <class T, int n>
void TWO<T, n>::ReadData(string months[12])
{
for(int i = 0; i < n; ++i)
{
a[i] = months[i];
}
}
// Displays both objects
template <class T, int n>
void TWO<T, n>::DisplayData()
{
for (int i = 0; i < n; ++i)
{
cout << a[i] << ' ';
}
cout << endl;
}
// Sorts both arrays
template <class T, int n>
void TWO<T, n>::SortArray()
{
sort(a, a + n);
}
template <class T, int n>
TWO<T, n>::~TWO()
{}
int main()
{
string b[12] = {"Dec", "Nov", "Oct", "Sept", "Aug", "Jul", "Jun", "May", "Apr", "Mar", "Feb", "Jan"};
// Create objects
TWO <string, 12> Q;
TWO <int, 10> P;
// Seed time
srand(time(NULL));
// Call read data function
P.ReadData();
Q.ReadData(b);
// Output arrays
cout << "Array's P and Q: " << endl;
P.DisplayData();
Q.DisplayData();
// Sort arrays
P.SortArray();
Q.SortArray();
cout << endl;
// Display sorted arrays
cout << "Sorted array's P and Q: " << endl;
P.DisplayData();
Q.DisplayData();
// Pause cmd window
system("PAUSE");
// Terminate
return 0;
}
|