Write a program that can be used to gather statistical data about the number of
ID: 3695167 • Letter: W
Question
Write a program that can be used to gather statistical data about the number of movies college students see in a month. The program should perform the following steps: A) Ask the user how many students were surveyed. An array of integers with this many elements should then be dynamically allocated. B) Allow the user to enter the number of movies each student saw into the array. C) Calculate and display the average, median, and mode of the values entered. (Use the functions to calculate the median and mode.)
C++ form using visual studios PLEASE HELP!!!!!
Explanation / Answer
#include <iostream>
#include <iomanip>
using namespace std;
// Function prototypes
double calculateMean(int *, int);
double calculateMedian(int *, int);
int calculateMode(int *, int);
int main()
{
cout << "Programmer: Adam Day ";
int *nums; // To dynamically allocate an array
int num_students; // To hold the number of students
char repeat = ' '; // Does the user want to go again?
do
{
// Prompt the user to enter in how many students were surveyed.
cout << "Enter in how many students were surveyed: ";
cin >> num_students;
// Determine input validation.
while (num_students < 0)
{
cout << "Invalid number of students! ";
cout << "Enter in how many students were surveyed: ";
cin >> num_students;
}
// Dynamically allocate an array large enough to hold
// that many numbers of students.
nums = new int[num_students];
// Get the number of movies for each student;
for (int count = 0; count < num_students; count++)
{
cout << "Number of movies say by Person #" << (count + 1) << ": ";
cin >> nums[count];
// Determine input validation.
while (nums[count] < 0)
{
cout << "Invalid number. Please enter in a positive number: ";
cout << " Number of movies say by Person #" << (count + 1) << ": ";
cin >> nums[count];
}
}
// Set output formatting.
cout << fixed << showpoint << setprecision(1);
// Display the mean.
cout << " The mean is: ";
cout << calculateMean(nums, num_students) << endl;
// Display the median.
cout << " The median is: ";
cout << calculateMedian(nums, num_students) << endl;
// Display the mode.
cout << " The mode is: ";
cout << calculateMode(nums, num_students) << endl;
// Free dynamically allocated memory.
delete[] nums;
nums = 0; // Make nums point to null.
// Ask the user if he/she wants to go again.
cout << "Do you want to go again? ";
cin >> repeat;
} while (repeat == 'Y' || repeat == 'y');
cout << "Program ending. ";
return 0;
}
//********************************************************************
// Definition of calculateMean. The nums parameter is a pointer.
// The num_students parameter holds the number of students. The *
// function calculates and returns the mean back to the main function*
//********************************************************************
double calculateMean(int *nums, int num_students)
{
double total = 0;
double average;
for (int count = 0; count < num_students; count++)
{
total += nums[count];
}
average = total / num_students;
return average;
}
//**********************************************************************
// Definition of calculateMedian. The nums parameter is a pointer. *
// The num_students parameter holds the number of students. The *
// function calculates and returns the median back to the main function*
//**********************************************************************
double calculateMedian(int *nums, int num_students)
{
double median = 0.0;
cout << fixed << showpoint << setprecision(1);
// int answer = num_students % 2;
if (num_students % 2 == 0)
{
median = (nums[num_students / 2] + nums[(num_students / 2) + 1]) / 2.0;
// median = even_median;
}
else
median = nums[num_students / 2];
return median;
}
//**********************************************************************
// Definition of calculateMode. The nums parameter is a pointer. *
// The num_students parameter holds the number of students. The *
// function calculates and returns the mode back to the main function *
//**********************************************************************
int calculateMode(int *nums, int num_students)
{
int mode = 0;
int val = 0;
int index;
for (index = 0; index < num_students; index++)
{
if (*(nums + index) == *(nums + (index + 1)))
{
mode++;
val = *(nums + index);
}
}
if (val > 0)
return val;
else
return -1;
} }
#include <iostream>
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.