samedi 7 mars 2015

Mean, Median, and Mode incorrect in c++ program


Title says it. The program basically is supposed to do as follows; Ask someone who surveyed students how many they surveyed, enter the number of movies those students watched, then find the average, mean, and mode. It does use pointers, and dynamic arrays. Code is below, and help is appreciated. For input, I'm using 1-10, which gives me an average of 5.5, median of 6.5, and -1 for the mode.


EDIT Forgot to mention this, but it appears as if my logic is flawed here. If someone can point out why, that would be great...


EDIT 2 Updated the code with a bubble sort, to help fix some of the issues I'm having. Here's the updated code below:



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

double calculateMean(int *, int);
double calculateMedian(int *, int);
int calculateMode(int *, int);
void bubbleSort(int *, int);

int main()
{
int *nums;
int num_students;
char repeat = ' ';

do
{


cout << "Enter in how many students were surveyed: ";
cin >> num_students;


while (num_students < 0)
{
cout << "Invalid number of students!\n";
cout << "Enter in how many students were surveyed: ";
cin >> num_students;
}


nums = new int[num_students];


for (int count = 0; count < num_students; count++)
{
cout << "Number of movies say by Person #" << (count + 1) << ": ";
cin >> nums[count];

while (nums[count] < 0)
{
cout << "Invalid number. Please enter in a positive number: ";
cout << "\nNumber of movies say by Person #" << (count + 1) << ": ";
cin >> nums[count];
}

}
bubbleSort(nums, num_students);

cout << fixed << showpoint << setprecision(1);

cout << "\nThe mean is: ";
cout << calculateMean(nums, num_students) << endl;

cout << "\nThe median is: ";
cout << calculateMedian(nums, num_students) << endl;

cout << "\nThe mode is: ";
cout << calculateMode(nums, num_students) << endl;

delete[] nums;
nums = 0;

cout << "Do you want to go again? Y for Yes, N for No.";
cin >> repeat;


} while (repeat == 'Y' || repeat == 'y');
cout << "Program ending.\n";

return 0;
}

void bubbleSort(int *nums, int num_students)
{
int temp;
for (int i = 0; i < num_students; i++){
for (int j = i + 1; j < num_students; j++){
if (nums[i] > nums[j])
{
temp = nums[i];
nums[i] = nums[j];
nums[j] = temp;
}
}
}
}


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


double calculateMedian(int *nums, int num_students)
{
double median = 0.0;
cout << fixed << showpoint << setprecision(1);


if (num_students % 2 == 0)
{
median = (nums[num_students / 2 - 1] + nums[(num_students / 2)]) / 2.0;
}
else
median = nums[num_students / 2];

return median;
}


int calculateMode(int *nums, int num_students)
{
int mode = 0;
int val = 0;
int index;


for (index = 0; index < num_students - 1; index++) // note the - 1 here
{
if (nums[index] == nums[index + 1])
{
mode++;
val = *(nums + index);
}
}
if (val > 0)
return val;
else
return -1;

}



Aucun commentaire:

Enregistrer un commentaire