Page 1 of 1

# I need help getting output for my code! I can't understand my

Posted 10 December 2019 - 07:11 PM

Here's my question and the code below

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:

1. Ask the user how many students where surveyed. An array of integers with this many elements should then be dynamically allocated.
2. Allow the user to enter the number of movies each student saw into the array.
3. Calculate and display the average, median, and mode of the values entered.
* Input validation: Do not accept negative numbers for input.

```#include <iostream>
#include <iomanip>
using namespace std;

void getData(int *, int);
void selectionSort(int *, int);
double getAvg(int *, int);
double getMedian(int *, int);
int getMode(int *, int);

int main()
{
int *Movies, Stud, Mode;
double Avg, Med;

cout << "How many students were surveyed? ";
cin >> Stud;

Movies = new int[Stud];

getData(Movies, Stud);

selectionSort(Movies, Stud);

Avg = getAvg(Movies, Stud);

Med = getMedian(Movies, Stud);

Mode = getMode(Movies, Stud);

cout << "Statistical results for the number of movies\n"
<< "     college students see in a month.\n";
cout << fixed << showpoint << setprecision(2);
cout << "Average: " << Avg << endl;
cout << "Median:  " << Med << endl;
cout << "Mode:    " << Mode << endl;

delete[] Movies;
Movies = 0;

return 0;
}

void getData(int *array, int size)
{
cout << "Enter the number of movies each student has seen in the month.\n";
for (int i = 0; i < size; i++)
{
cout << "Student " << (i + 1) << ": ";
cin >> *(array + i);
}
}

void selectionSort(int *array, int size)
{
int scan, minIndex, minValue;

for (int scan = 0; scan < (size - 1); scan++)
{
minIndex = scan;
minValue = *(array + scan);
for (int i = scan + 1; i < size; i++)
{
if (*(array + i) < minValue)
{
minValue = *(array + i);
minIndex = i;
}
}
*(array + minIndex) = *(array + scan);
*(array + scan) = minValue;
}
}

double getAvg(int *array, int size)
{
double Sum = 0;
for (int i = 0; i < size; i++)
{
Sum += *(array + i);
}
return Sum / size;
}

double getMedian(int *array, int size)
{
int Mid = (size - 1) / 2;
double Med;

if (size % 2 == 0)
{
Med = (*(array + Mid) + *(array + (Mid + 1))) / 2;
}
else
Med = *(array + Mid);

return Med;
}

int getMode(int *array, int size)
{
int Mode, Most, Count;
Count = Most = 0;

for (int i = 0; i < size; i++)
{
Count++;
if (*(array + i) < *(array + i + 1))
{
if (Count > Most)
{
Mode = *(array + i);
Most = Count;
}
Count = 0;
}
}
return Mode;
}
``` This post has been edited by modi123_1: 10 December 2019 - 07:48 PM
Reason for edit:: In the future please use the [code] tag button in the editor

Is This A Good Question/Topic? 0

## Replies To: I need help getting output for my code! I can't understand my

### #2 jimblumberg Reputation: 5783
• Posts: 17,685
• Joined: 25-December 09

## Re: I need help getting output for my code! I can't understand my

Posted 10 December 2019 - 07:42 PM

Do you have a question or problem with that code?

Please learn to use code tags when posting code: Jim

## Re: I need help getting output for my code! I can't understand my

Posted 10 December 2019 - 07:49 PM

Here's my question and the code below

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:

1. Ask the user how many students where surveyed. An array of integers with this many elements should then be dynamically allocated.
2. Allow the user to enter the number of movies each student saw into the array.
3. Calculate and display the average, median, and mode of the values entered.
* Input validation: Do not accept negative numbers for input.

```#include <iostream>
#include <iomanip>
using namespace std;

void getData(int *, int);
void selectionSort(int *, int);
double getAvg(int *, int);
double getMedian(int *, int);
int getMode(int *, int);

int main()
{
int *Movies, Stud, Mode;
double Avg, Med;

cout << "How many students were surveyed? ";
cin >> Stud;

Movies = new int[Stud];

getData(Movies, Stud);

selectionSort(Movies, Stud);

Avg = getAvg(Movies, Stud);

Med = getMedian(Movies, Stud);

Mode = getMode(Movies, Stud);

cout << "Statistical results for the number of movies\n"
<< " college students see in a month.\n";
cout << fixed << showpoint << setprecision(2);
cout << "Average: " << Avg << endl;
cout << "Median: " << Med << endl;
cout << "Mode: " << Mode << endl;

delete[] Movies;
Movies = 0;

return 0;
}

void getData(int *array, int size)
{
cout << "Enter the number of movies each student has seen in the month.\n";
for (int i = 0; i < size; i++)
{
cout << "Student " << (i + 1) << ": ";
cin >> *(array + i);
}
}

void selectionSort(int *array, int size)
{
int scan, minIndex, minValue;

for (int scan = 0; scan < (size - 1); scan++)
{
minIndex = scan;
minValue = *(array + scan);
for (int i = scan + 1; i < size; i++)
{
if (*(array + i) < minValue)
{
minValue = *(array + i);
minIndex = i;
}
}
*(array + minIndex) = *(array + scan);
*(array + scan) = minValue;
}
}

double getAvg(int *array, int size)
{
double Sum = 0;
for (int i = 0; i < size; i++)
{
Sum += *(array + i);
}
return Sum / size;
}

double getMedian(int *array, int size)
{
int Mid = (size - 1) / 2;
double Med;

if (size % 2 == 0)
{
Med = (*(array + Mid) + *(array + (Mid + 1))) / 2;
}
else
Med = *(array + Mid);

return Med;
}

int getMode(int *array, int size)
{
int Mode, Most, Count;
Count = Most = 0;

for (int i = 0; i < size; i++)
{
Count++;
if (*(array + i) < *(array + i + 1))
{
if (Count > Most)
{
Mode = *(array + i);
Most = Count;
}
Count = 0;
}
}
return Mode;
}

```

My output doesn't give me the total no of movies watched by the students, the average movies watched and the median of movies watched. I need help executing my code to get the right display. Thanks

### #4 jimblumberg Reputation: 5783
• Posts: 17,685
• Joined: 25-December 09

## Re: I need help getting output for my code! I can't understand my

Posted 11 December 2019 - 07:04 AM

The program seems to output the average, median, and mode for me and you never try to print the "sum".

Jim

Page 1 of 1

 .related ul { list-style-type: circle; font-size: 12px; font-weight: bold; } .related li { margin-bottom: 5px; background-position: left 7px !important; margin-left: -35px; } .related h2 { font-size: 18px; font-weight: bold; } .related a { color: blue; }