// Programming Environment: Bloodshed
// Professor: Ronald J Hart
// Program Description: This program can be used to gather the statical data
// about the number of movies college students see in a month. The program
// asks the user on how many students were survayed and then taking that integer
// value, it dynamically allocates memory as directed by user as an array of
// that many elements (integers). It also validates the user input limiting
// them to enter negative values. It uses a seperate function to display what
// the user has input for each student's movie watch behaviour. Then, it uses
// a seperate function (with pointer as an argument to it) to calculate the
// average, median, and mode of the values entered.
// Date/Time: 4/2/2010 6:51:27 AM
//------------------------------------------------------------------------------
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include "extras.h"
#include "Generic_Class.h"
using namespace std;
//---------------------------------------------------------------------------
// Named Constants
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
// Structure Declarations
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
// Function Prototypes
double Get_Average(int*, int);
void Display_Array(int*, int);
double Get_Median (int*, int);
void Sort_Array (int*, int);
int Get_Mode (int*, int);
//---------------------------------------------------------------------------
bool Do_Again (void);
//---------------------------------------------------------------------------
int main (void) {
cout.setf (ios::fixed, ios::floatfield);
cout.setf (ios::showpoint);
cout.precision (2);
do {
int num_students = 0;
while (num_students <= 0) {
cout << endl << "Enter the number of students survayed: ";
cin >> num_students;
}
// Dynamically allocating pointer named student_array that holds
// num_students.
int* student_array = new int[num_students];
for(int i = 0; i < num_students; ++i) {
// Now storing the values in the student_array.
cout << endl << "Enter the number of movies seen by student " << i+1
<< " : ";
cin >> student_array[i];
while (student_array[i] < 0) {
// Validating the user's input to student array for non-zero entries.
cout << endl << "Invalid Entry!!"
<< "Re-enter the number of movies seen by student " << i+1
<< " : ";
cin >> student_array[i];
}
}
cout << endl;
Display_Array(student_array, num_students);
// This function displays the contents of the student_array.
double average = Get_Average(student_array, num_students);
// Average variable gets average from Get_Average function.
cout << endl << "The average number of movies college students see per month: "
<< average; // Displays the Average.
Sort_Array(student_array, num_students);
// Sorting the elements of student_array as we need the sorted array to
// calculate the median of the of the elements in the array.
cout << endl << endl << "The number of movies seen by each students (Sorted): ";
for (int i = 0; i < num_students; ++i) {
// Displaying the sorted array elements just for the shake of
// information to the users.
cout << student_array[i] << " ";
}
// The median variable gets the median from Get_Median().
double median = Get_Median(student_array, num_students);
cout << endl << "The median of number of movies college students see per month: "
<< median; // displays the median.
// The variable mode gets the mode from Get_Mode().
int Mode = Get_Mode (student_array, num_students);
// If it finds the mode then it will display the mode else it would disply
// that the mode doesn't exists.
if (Mode > 0) {
cout << endl << "The Mode of number of movies college students see per month: " << Mode;
} else {
cout << endl << "No Mode Exists";
}
} while (Do_Again ());
return 0;
}
//------------------------------------------------------------------------
// Function Definitions
double Get_Average(int* array_num_movies, int student_num) {
// This function calculates the average number of movies that the students
// saw by returning a double to main which is the average number of movies
// that the student who were survayed and stored in array saw.
double average_num_movies = 0.0;
for (int i = 0; i < student_num; ++i) {
average_num_movies += array_num_movies[i];
}
average_num_movies /= student_num;
return average_num_movies;
}
void Display_Array(int* dynamic_array, int num_elements) {
// This function uses the definations to display the contents of the array
// loaded dynamically.
cout << endl << "The number of movies seen by each students: ";
for(int i = 0; i < num_elements; ++i) {
cout << dynamic_array[i] << " ";
}
}
double Get_Median (int* in_array, int num_elements) {
// This function definations returns the median to main. If the num_elements is greater
// than 1 then it sorts the array so that it could give median for odd as well
// as even num_elements.
double median;
if (num_elements > 1) {
Sort_Array (in_array, num_elements);
}
if (num_elements % 2 == 1) {
// Odd number of elements.
median = static_cast(in_array[num_elements/2]);
} else {
// Even number of elements.
median = (in_array[num_elements/2 - 1] +
in_array[num_elements/2]) / 2.0;
}
return median;
}
void Sort_Array (int* in_array, int num_elements) {
// This function definations would sort the array for further calculations
int temp;
bool swapped = true;
while(swapped) {
swapped = false;
for (int i = 0; i < num_elements; ++i) {
if (in_array[i] > in_array[i + 1]) {
temp = in_array[i];
in_array[i] = in_array[i + 1];
in_array[i + 1] = temp;
swapped = true;
}
}
}
}
int Get_Mode (int* in_array, int num_elements) {
// Function receives two parameters: first is the address
// on an integer array; second is the number of elements
// in the given array. This function returns the statistical
// mode of the given array of integers. (Note: We assume
// either one mode value exists or not; i.e., bi-modal and
// multi-modal situations are not considered.)
int frequency;
int greatest_frequency = 0;
int mode;
for (int i = 0; i < num_elements; ++i) { // For each array element...
frequency = 0;
for (int j = 0; j < num_elements; ++j) {
if (in_array[i] == in_array[j]) {
++frequency;
}
}
if (frequency > greatest_frequency) {
greatest_frequency = frequency;
mode = in_array[i];
}
}
if (greatest_frequency == 1) {
mode = -1;
}
return mode;
}
//------------------------------------------------------------------------
bool Do_Again (void) {
(void) fflush (stdin);
cout << endl << endl << "\aDo Again? (y/n): ";
return static_cast(tolower (_getch ())) == 'y';
}
// Professor: Ronald J Hart
// Program Description: This program can be used to gather the statical data
// about the number of movies college students see in a month. The program
// asks the user on how many students were survayed and then taking that integer
// value, it dynamically allocates memory as directed by user as an array of
// that many elements (integers). It also validates the user input limiting
// them to enter negative values. It uses a seperate function to display what
// the user has input for each student's movie watch behaviour. Then, it uses
// a seperate function (with pointer as an argument to it) to calculate the
// average, median, and mode of the values entered.
// Date/Time: 4/2/2010 6:51:27 AM
//------------------------------------------------------------------------------
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include "extras.h"
#include "Generic_Class.h"
using namespace std;
//---------------------------------------------------------------------------
// Named Constants
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
// Structure Declarations
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
// Function Prototypes
double Get_Average(int*, int);
void Display_Array(int*, int);
double Get_Median (int*, int);
void Sort_Array (int*, int);
int Get_Mode (int*, int);
//---------------------------------------------------------------------------
bool Do_Again (void);
//---------------------------------------------------------------------------
int main (void) {
cout.setf (ios::fixed, ios::floatfield);
cout.setf (ios::showpoint);
cout.precision (2);
do {
int num_students = 0;
while (num_students <= 0) {
cout << endl << "Enter the number of students survayed: ";
cin >> num_students;
}
// Dynamically allocating pointer named student_array that holds
// num_students.
int* student_array = new int[num_students];
for(int i = 0; i < num_students; ++i) {
// Now storing the values in the student_array.
cout << endl << "Enter the number of movies seen by student " << i+1
<< " : ";
cin >> student_array[i];
while (student_array[i] < 0) {
// Validating the user's input to student array for non-zero entries.
cout << endl << "Invalid Entry!!"
<< "Re-enter the number of movies seen by student " << i+1
<< " : ";
cin >> student_array[i];
}
}
cout << endl;
Display_Array(student_array, num_students);
// This function displays the contents of the student_array.
double average = Get_Average(student_array, num_students);
// Average variable gets average from Get_Average function.
cout << endl << "The average number of movies college students see per month: "
<< average; // Displays the Average.
Sort_Array(student_array, num_students);
// Sorting the elements of student_array as we need the sorted array to
// calculate the median of the of the elements in the array.
cout << endl << endl << "The number of movies seen by each students (Sorted): ";
for (int i = 0; i < num_students; ++i) {
// Displaying the sorted array elements just for the shake of
// information to the users.
cout << student_array[i] << " ";
}
// The median variable gets the median from Get_Median().
double median = Get_Median(student_array, num_students);
cout << endl << "The median of number of movies college students see per month: "
<< median; // displays the median.
// The variable mode gets the mode from Get_Mode().
int Mode = Get_Mode (student_array, num_students);
// If it finds the mode then it will display the mode else it would disply
// that the mode doesn't exists.
if (Mode > 0) {
cout << endl << "The Mode of number of movies college students see per month: " << Mode;
} else {
cout << endl << "No Mode Exists";
}
} while (Do_Again ());
return 0;
}
//------------------------------------------------------------------------
// Function Definitions
double Get_Average(int* array_num_movies, int student_num) {
// This function calculates the average number of movies that the students
// saw by returning a double to main which is the average number of movies
// that the student who were survayed and stored in array saw.
double average_num_movies = 0.0;
for (int i = 0; i < student_num; ++i) {
average_num_movies += array_num_movies[i];
}
average_num_movies /= student_num;
return average_num_movies;
}
void Display_Array(int* dynamic_array, int num_elements) {
// This function uses the definations to display the contents of the array
// loaded dynamically.
cout << endl << "The number of movies seen by each students: ";
for(int i = 0; i < num_elements; ++i) {
cout << dynamic_array[i] << " ";
}
}
double Get_Median (int* in_array, int num_elements) {
// This function definations returns the median to main. If the num_elements is greater
// than 1 then it sorts the array so that it could give median for odd as well
// as even num_elements.
double median;
if (num_elements > 1) {
Sort_Array (in_array, num_elements);
}
if (num_elements % 2 == 1) {
// Odd number of elements.
median = static_cast
} else {
// Even number of elements.
median = (in_array[num_elements/2 - 1] +
in_array[num_elements/2]) / 2.0;
}
return median;
}
void Sort_Array (int* in_array, int num_elements) {
// This function definations would sort the array for further calculations
int temp;
bool swapped = true;
while(swapped) {
swapped = false;
for (int i = 0; i < num_elements; ++i) {
if (in_array[i] > in_array[i + 1]) {
temp = in_array[i];
in_array[i] = in_array[i + 1];
in_array[i + 1] = temp;
swapped = true;
}
}
}
}
int Get_Mode (int* in_array, int num_elements) {
// Function receives two parameters: first is the address
// on an integer array; second is the number of elements
// in the given array. This function returns the statistical
// mode of the given array of integers. (Note: We assume
// either one mode value exists or not; i.e., bi-modal and
// multi-modal situations are not considered.)
int frequency;
int greatest_frequency = 0;
int mode;
for (int i = 0; i < num_elements; ++i) { // For each array element...
frequency = 0;
for (int j = 0; j < num_elements; ++j) {
if (in_array[i] == in_array[j]) {
++frequency;
}
}
if (frequency > greatest_frequency) {
greatest_frequency = frequency;
mode = in_array[i];
}
}
if (greatest_frequency == 1) {
mode = -1;
}
return mode;
}
//------------------------------------------------------------------------
bool Do_Again (void) {
(void) fflush (stdin);
cout << endl << endl << "\aDo Again? (y/n): ";
return static_cast
}
I could not understand on the code given above. Need your assistant tosend me the full source code of "the number of movies college students see in a month"...you may send it to zuhair.jzu@gmail.com. your assistant are highly appreciated.
ReplyDeleteYou just need to copy and paste it in your compiler! It's working perfectly fine! Sorry for the late reply!
ReplyDelete