3 Replies - 435 Views - Last Post: 11 December 2019 - 07:04 AM Rate Topic: -----

#1 rk137   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 10-December 19

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;
}
:code:

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   User is offline

  • member icon

Reputation: 5783
  • View blog
  • 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:

:code:


Jim
Was This Post Helpful? 0
  • +
  • -

#3 rk137   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 10-December 19

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
Was This Post Helpful? 0
  • +
  • -

#4 jimblumberg   User is offline

  • member icon

Reputation: 5783
  • View blog
  • 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
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1