Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- #include <string>
- using namespace std;
- int calMode (int [], int arrSize); //function to calculate the mode
- int main() {
- ifstream myfile("info.txt");
- // stating variables
- string w;
- string x;
- int y;
- //using getline here to get the first number of the array transform it into an int using stoi(x) and pluging it into arrSize
- getline(myfile, x);
- int arrSize = 0;
- y = stoi(x);
- int arr[y];
- //if statement for opening the info.txt file
- if (myfile.is_open())
- {
- //while loop that gets the information from the info.txt file and puts it into the array
- while (!myfile.eof())
- {
- int f;
- myfile >> f;
- arr[arrSize++] = f;
- }
- for (int i = 0; i < arrSize ; ++i)
- cout << arr[i] << " ";
- cout << endl;
- }
- //calling the function that calculates the mode
- calMode(arr, arrSize);
- return 0;
- }
- /****
- * Calculates mode and displays it
- * @param - arr[]
- * @param - arrSize
- * @return - void
- ****/
- int calMode(int arr[], int arrSize)
- {
- //for loop that creates arrays of the array and compares both arrays to find the mode
- int* mode = new int[arrSize];
- for (int i = 0; i < arrSize; ++i) {
- mode[i] = 0;
- int j = 0;
- //declaration of boolean statement to stop/continue function
- bool bFound = false;
- while ((j < i) && (arr[i] != arr[j])) {
- if (arr[i] != arr[j]) {
- ++j;
- }
- }
- //increases the array mode to compare it with the other array
- ++(mode[j]);
- }
- int iMaxRepeat = 0;
- for (int i = 1; i < arrSize; ++i) {
- if (mode[i] > mode[iMaxRepeat]) {
- iMaxRepeat = i;
- }
- }
- //cout statement that shows the mode
- cout << "The mode is: " << arr[iMaxRepeat];
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement