Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- using namespace std;
- //function prototype
- void calculateMode(int [], int );
- //**************
- //*Main function
- //* has getting text file then storing into array and then function to finding the mode
- //************
- int main ()
- {
- const int ARRAY_SIZE = 50; // constant so we can use it outside of main
- int numbers [ARRAY_SIZE];
- int count = 0; // loop counter varible
- ifstream inputFile; // input file stream object
- //opening the file
- inputFile.open("file.txt");
- //reading the numbers from the file then putting it into the array
- while (count < ARRAY_SIZE && inputFile >> numbers [count])
- count ++;
- // closing file
- inputFile.close();
- // calling on our function for calculating mode
- calculateMode(numbers, ARRAY_SIZE);
- return 0;
- }
- //*********
- //* Calculating the mode funtion
- //* calculates mode and then displays it
- //*********
- void calculateMode(int ARRAY_SIZE[], int size)
- {
- int MODE_COUNTER[100] = { 0 }; // since my values are allways like 0-99
- int mode = 0;
- int max = 0;
- for (int i = 0; i < size; i++) // finding the mode using index
- if (++MODE_COUNTER[ARRAY_SIZE[i]] > max)
- // for loop for finding the number that is most frequent
- {
- mode = ARRAY_SIZE[i];
- max = MODE_COUNTER[ARRAY_SIZE[i]];
- }
- cout << "The mode is " << mode << endl; // cout the mode
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement