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
|
#include <iostream>
#include <fstream>
using namespace std;
struct FileData
{
int * dataArray; // pointer to dynamic data array
int count; // total number of elements in the file
int maxElem = 0; // largest element in the file
};
struct ModeData
{
int mode; // element that is mode
int frequency; // frequency of the mode
};
//Prototypes
FileData readDataFromFile(ifstream & inFile, int size);
ModeData findMode(int arr[], int range, int numElem);
void displayMode(int mode, int frequency);
void main()
{
ifstream file;
const int SIZE = 700; // size of dataArray
FileData fileData;
ModeData modeData;
fileData.dataArray = new int[SIZE];
file.open("RandomModeOneMode.txt"); // opens file with one mode
//file.open("RandomModeNoModes.txt"); // opens file with no mode
fileData = readDataFromFile(file, SIZE);
modeData = findMode(fileData.dataArray, fileData.count, fileData.maxElem);
displayMode(modeData.mode, modeData.frequency);
delete[] fileData.dataArray;
system("Pause");
}//end main
/**************************************************************************************************************************
readDataFromFile *
ifstream(to be read from), int(size of dynamic array) *
Gets data from input file and stores it in a dynamic array. Also counts the number of elements and finds the max element *
**************************************************************************************************************************/
FileData readDataFromFile(ifstream & inFile, int size)
{
FileData tempfileData;
tempfileData.dataArray = new int[size]; //dynamic array to hold elements from file
if (!inFile)
cout << "File failed to open properly.\n";
else
{
//cycles through counting elements in array, storing them all in tempfiledata, and finding the max element
for (tempfileData.count = 0; tempfileData.count < size && inFile >> tempfileData.dataArray[tempfileData.count]; ++(tempfileData.count))
{
if (tempfileData.dataArray[tempfileData.count] > tempfileData.maxElem)
tempfileData.maxElem = tempfileData.dataArray[tempfileData.count]; // stores the new maxelem if it is greater than the previous
}//end for
}//end else
return tempfileData;
}//end readDataFromFile
/**************************************************************************************************************
findMode *
int[](number set to find mode of), int(range of numbers being looked at), int(size of array being looked at) *
Finds mode of the argument array and returns the mode and frequency *
**************************************************************************************************************/
ModeData findMode(int arr[], int range, int numElem)
{
ModeData modeData = { 0, 0 };
int * mode = nullptr;
mode = new int[range + 1]; //creates a counting array to be incremented for each time a number appears in arr[]
//ensures all elements are 0 before being used as a counter
for (int i = 0; i < numElem; ++i)
mode[i] = 0;
//counts and stores how many times each number appears
for (int i = 0; i < numElem; ++i)
mode[arr[i]] += 1;
//finds the actual mode and frequency and stores them in modeData
for (int i = 0; i < numElem; ++i)
if (mode[i] > modeData.frequency)
{
modeData.frequency = mode[i];
modeData.mode = i;
}//end if
delete[] mode;
return modeData;
}//end findMode
/**********************************************************************************
displayMode *
int(mode of a data set), int(frequency the mode appears) *
Displays the mode and frequency of a data set, or states that one does not exist *
**********************************************************************************/
void displayMode(int mode, int frequency)
{
if (frequency != 1)
{
cout << "The mode of this data set is: " << mode << endl;
cout << "The frequency of the mode is: " << frequency << endl;
}//end if
else
cout << "There is no mode in this data set.\n";
}//end displayMode
|