Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Description: Write a program that can be used to gather statistical data about t

ID: 674846 • Letter: D

Question

Description:

Write a program that can be used to gather statistical data about the number of hours college students spend watching streaming videos on Youtube in one month.

Assignment Instructions:

MAIN FUNCTION:

1.Ask the user how many students were surveyed.

2.Call a function called makeArray to define an array of integers with the number of elements equal to the number of students surveyed.

3.Call a function called getStudentData to allow the user to enter the number of hours each student spent watching Youtube into the array.

4.Call a function called getAverage to calculate and display the average of the hours entered.

5.Call a function called selectionSort to sort the hours in ascending order. This function is provided for you.

6.Call a function called printArray to print out the hours in the array.

7.Call a function called getMedian to calculate and display the median of the hours entered.

8.Call a function called getMode to calculate and display the mode of the hours entered.

9.Print out a tab and then the header “YOUTUBE STATISTICS” in all capital letters, then skip down to the next line and then print out the average, median & mode.

a.The labels “Average:”, “Median:”, and “Mode:” should all have a colon “:” after the word and have a field width of

b.The value for average & median should have two numbers after the decimal point. Mean should not have a decimal point because the value should reflect the number of hours a student watched streaming videos.

c.The labels and the values should all be left aligned.

makeArray Function

Define an array of integers with the number of elements equal to the number of students surveyed. The function will accept as arguments the following: An integer that indicates the number of elements in the array.
The array should be dynamically allocated.
Use pointer notation instead of array notation in this function.

getStudentData Function

Allow the user to enter the number of hours each student watched Youtube into the array. The function will accept as arguments the following:

1.An array of integers

2.An integer that indicates the number of elements in the array.

Input Validation: Do not accept negative numbers for input.

Use pointer notation instead of array notation in this function.

printArray Function

This function should print out the text “Number of hours each student watched Youtube in ascending order: “. Then, the function should print out each element in the array with a space between each element. The function will accept as arguments the following:

1.An array of integers

2.An integer that indicates the number of elements in the array.

Use pointer notation instead of array notation in this function.

getAverage Function

The average of a set of values is calculated by adding all the values and then dividing the sum by the number of values in the set. Write a function that accepts as arguments the following:

1.An array of integers

2.An integer that indicates the number of elements in the array.

The function should determine the average of the array. The average is the value the function should return. Use pointer notation instead of array notation in this function.

getMode Function

In statistics, the mode of a set of values is the value which occurs most often or with the greatest frequency. Write a function that accepts as arguments the following:

1.An array of integers

2.An integer that indicates the number of elements in the array.

The function should determine the mode of the array. That is, it should determine which value in the array occurs most often. The mode is the value the function should return. If the array has no mode (none of the values occur more than once), the function should return -1. (Assume the array will always contain non-negative values.)
Use pointer notation instead of array notation in this function.

getMedian Function

In statistics, when a set of values is sorted in ascending or descending order, its median is the middle value. If the set contains an even number of values, the median is the mean, or average of the two middle values. Write a function that accepts as arguments the following:

1.An array of integers

2.An integer that indicates the number of elements in the array

The function should determine the median of the array. This value should be returned as a double. (The values in the array should already be sorted.)
Use pointer notation instead of array notation in this function.

Program should be like this.

Example:

How many students were surveyed? 3

Enter the number of hours each student spent watching Youtube.

Student 1: 4

Student 2: 9

Student 3: 5

Hours each student watched Youtube in ascending order: 4 5 9

YOUTUBE STATS

Average: 6.00 hrs

Median 5.00 hrs

Mode: There is no mode.

Hope you can help theese arrays are killing me.

This is due on November 4th of 2015

Explanation / Answer

#include<iostream>

using namespace std;

int* makeArray(int num) {

return (int *)malloc(sizeof(int)*num);

}

void getStudentData(int *arr, int n) {

cout<<"Enter the number of hours each student spent watching Youtube."<<" ";

  

for (int i=0; i<n; i++) {

cout<<"Student "<<(i+1)<<" :";

cin>>arr[i];

}

}

double getAverage(int *arr, int n) {

float sum = 0;

for (int i=0; i<n; i++) {

sum +=arr[i];

}

  

return sum/n*1.0;

}

void selectionSort(int *arr, int n) {

for (int i= n - 1; i > 0; i--)

{

int first = 0;

for (int j=1; j<=i; j++)

{

if (arr[j] > arr[first])

first = j;

}

int temp = arr[first];

arr[first] = arr[i];

arr[i] = temp;

}

}

void printArray(int *arr, int n) {

cout<<"Hours each student watched Youtube in ascending order: ";

for (int i=0; i<n; i++) {

cout<<arr[i]<<" ";

}

cout<<" ";

}

double getMedian(int arr[], int n) {

double med = 0.0;

if ((n % 2) == 0) {

med = (arr[n/2] + arr[(n/2) - 1])/2.0;

} else {

med = arr[n/2];

}

return med;

}

double getMode(int arr[], int n) {

int* ipRepetition = new int[n];

for (int i = 0; i < n; ++i) {

ipRepetition[i] = 0;

int j = 0;

bool bFound = false;

while ((j < i) && (arr[i] != arr[j])) {

if (arr[i] != arr[j]) {

++j;

}

}

++(ipRepetition[j]);

}

int iMaxRepeat = 0;

for (int i = 1; i < n; ++i) {

if (ipRepetition[i] > ipRepetition[iMaxRepeat]) {

iMaxRepeat = i;

}

}

delete [] ipRepetition;

  

if(ipRepetition[iMaxRepeat] == 1) {

return -1;

}

  

return arr[iMaxRepeat];

}

int main() {

int numOfStudents;

cout<<"How many students were surveyed? ";

cin>>numOfStudents;

  

int *arr = makeArray(numOfStudents);

  

getStudentData(arr, numOfStudents);

  

float avg = getAverage(arr, numOfStudents);

  

selectionSort(arr, numOfStudents);

  

printArray(arr, numOfStudents);

  

cout<<" YOUTUBE STATS ";

  

double median = getMedian(arr, numOfStudents);

double mode = getMode(arr, numOfStudents);

  

printf("Average: %4.2f ", avg);

printf("Median: %4.2f ", median);

if(mode == -1) {

printf("Mode: There is no mode. ");

} else {

printf("Mode: %4.2f ", mode);

}

  

  

  

cout<<" ";

return 0;

}