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 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192
|
// Includes
#include<iostream>
#include<iomanip>
#include<cmath>
#include<string>
using namespace std;
// Function Prototypes
void showIntro(int, int, int, string);
int fillArray(int array[]);
void showArray(int array[], int n);
void bubbleSort(int array[], int n);
int frequencyArray(int dataArray[], int valueArray[], int countArray[], int n);
void swap(int array[], int i, int j);
void frequencyArray(int array[], int n);
// start of main
int main(void) {
// call a function to display splash screen
showIntro(3, 30, 2012, "This program will will create a frequency table for a list of values.");
cout << endl; // because month is 3, using the switch it will recognize 3 as March
// declare variables
int dataArray[50]; // create an array of doubles
int arraySize = 0;
int valueArray[50];
int countArray[50];
int frequencyValue = 0;
// Fill the array
arraySize = fillArray(dataArray);
// table header
cout << endl;
cout << "Value" << " Frequency" << endl;
// print the array
showArray(dataArray, arraySize);
// sort the array
bubbleSort(dataArray, arraySize);
// print the array
showArray(dataArray, arraySize);
//print the frequency array
frequencyValue = frequencyArray(dataArray, arraySize, valueArray, countArray);
system("pause");
return 0;
}
// function to print splash screen
void showIntro(int m, int d, int y, string descript) { // declare variables
// string for month
string month = "";
// cout << "March 30, 2012" << endl;
// use parameters to set date
//switch to set the month string
switch(m) {
case 1:
month = "January";
break;
case 2:
month = "February";
break;
case 3:
month = "March";
break;
case 4:
month = "April";
break;
case 5:
month = "May";
break;
case 6:
month = "June";
break;
case 7:
month = "July";
break;
case 8:
month = "August";
break;
case 9:
month = "September";
break;
case 10:
month = "October";
break;
case 11:
month = "November";
break;
case 12:
month = "December";
break;
default:
month = "unknown";
break;
}
cout << month << " " << d << ", " << y << endl;
cout << descript << endl;
return;
}
// function to sort an array of doubles
void bubbleSort(int array[], int n) { // use void because this is a stub function, serves as a placeholder
// nested loop to compare each element with those that follow it
for(int i=0; i<n-1; i++) {
for(int j=i+1; j<n; j++) {
// comparison
if ( array[j] < array[i]) {
swap(array, i, j);
}
}// end of inner loop
} // end of outer loop
return;
}
// function to swap elements at indeces i and j
void swap(int array[], int i, int j) {
// switching the two values by using an intermediate temporary variable, like tower of hanoi
int temp;
temp = array[j];
array[j] = array[i];
array[i] = temp;
}
// function to fill array with doubles
int fillArray(int array[]) {
int n = -99; // Number of data point
// Prompt for the number of data points
cout << "How many values will you enter? ";
cin >> n;
// Loop to enter data
for(int i = 0; i < n; i++) {
// Enter the data point
cout << "Enter data point " << (i+1) << ": ";
cin >> array[i];
}
return n;
}
// function to print array to the screen
void showArray(int array[], int n) {
// print a linefeed
cout << endl;
// Loop through the array printing the values
for(int i = 0; i < n; i++) {
// Print element i
cout << array[i] << "\t";
}
// print a linefeed
cout << endl;
}
// function to count frequency
int frequencyArray(int dataArray[], int valueArray[], int countArray[], int n) {
int frequency = 0;
int valueArray[0] = dataArray[0];
countArray[0] = 1;
for(int i=1; i<n; i++) {
if (array[i] == valueArray[frequency]);
{
countArray[frequency]++;
}
else
{
countArray[frequency] = 1;
valueArray[frequency] = dataArray[i];
}
} // end of for loop
return frequency;
}
|