Here is the code I currently have. Now I need a function which is declared as "int mode(int *, int)" to find the mode of those value. The function requires:
1,create an array 'frequency' of 101 integers. 100 is arbitrarily chosen as the absolute maximum of movies a college student could see in a week. This array
will keep track of frequencies of weekly movie watching values.
2, Intialize array 'frequency' elements to zeros (use a loop)
3, Populate the 'frequency' array with the data (how many students see 1, 2, 3,etc. movies a week)
The following algorithm is a useful method to easily increment random values:
Run a loop on 'student_data' from 0 to 'num_students'.
The content of 'student_data' array becomes the index of the 'frequency' array and the content of that 'frequency' array
element gets incremented. Very important to understand this process.
The problem is I didn't understand the crucial part of this function which is : The content of 'student_data' array becomes the index of the 'frequency' array and the content of that 'frequency' array element gets incremented. Very important to understand this process.
Hope someone would help me on this. Thanks a lot.
#include <iostream>
#include <iomanip>
using namespace std;
int *create_array(int);
void enter_data(int *, int);
int mode(int *, int);
int avrg(int *, int);
void showArray(int *, int);
//Main functions:
int main()
{
int *dyn_array;
int mode, students;
float avrg;
do
{
cout << "How many students you will enter? ";
cin >> students;
}while (students <= 0);
dyn_array = create_array(students);
enter_data(dyn_array, students);
cout << "The array is: \n";
showArray(dyn_array, students);
cout << "The mode is: " << mode;
delete [] dyn_array;
return 0;
}
//Separated Functions:
//Create array.
int *create_array(int num_students)
{
int *student_data;
student_data = new int(num_students);
return student_data;
}
//Get date.
void enter_data(int *student_data, int num_students)
{
for (int count = 0; count < num_students; count ++)
{
cout << "How many did student " << (count + 1) << " watch? ";
cin >> student_data[count];
while ( student_data[count] < 0 || student_data[count] > 100)
{
cout << "Invalid Entry. Please enter a number between 0 and 100: ";
cin >> student_data[count];
}
}
}
//Display array
void showArray(int *student_data, int num_students)
{
for (int count = 0; count < num_students; count ++)
{
cout << "Student " << (count + 1) << " watched " << student_data[count] << " movies" << endl;
}
}