1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
|
// The program should calculate the average, median, and mode of the values entered.
//
//
// Yea I know I don't need all of these
// I just keep reusing the same form as I practice
// An I just remove the code and start new code
#include "stdafx.h"
#include<iostream>
#include <iomanip>
#include <stdio.h>
#include <stdlib.h>
using namespace System;
using namespace std;
int get_mode(int n[], int size, int max);
int main(array<System::String ^> ^args)
{
/*Console::WriteLine(L"Hello World");*/
/*Junk I leave in for testing when my system goes nuts*/
int * students, n, total, med, frank, f ;
float avg;
/*Message to the end user with two line breaks*/
cout << " This is a one month survey? \n\n";
/*We are sending a question to the screen*/
cout << " How many students were surveyed in one month? \n";
cout << " Please only whole numbers no zeros or fractions \n\n";
cout << " Enter value for N : ";
/*We are taking the input from the keyboard*/
cin >> n;
cout << "\n\n"; /*add a line break for visual effect */
/*Error Checker: Making sure that the user is not placing in a number zero or negative number*/
if(n <= 0)
{
/*Message output to the screen if boolean value is true*/
cout << "Invalid number\n";
/*Stopping the screen from closing so the user can see the output*/
system("PAUSE");
return 1;
}
/*Storing the value of the keyboard input for student as 'n' "dynamically allocate array" */
students = new int[n];
/*Starting the count loop */
for(int i = 0; i < n; i++)
//for(f=0; f < i; f++)
{
/*Sending message to the screen to collect values for the place holders set by the value of 'n'*/
cout << "How many movies did student #" << i +1 << " see?\n";
/*Taking the input value for each place holder set by 'n'*/
cin >> students[i];
total += students[i];
/*Adding all the values being entered storing them in the holder 'avg' "calculate the average " */
med += students[i];
}
/*Taking the total count of avg and finding the median value stored; in 'n' that is use for the loop count */
avg = total/n;
med /= n;
/*Sending output to the screen of all the values stored and then divided*/
cout << "\n" << endl;
cout << "\n" << "The value you entered in for N was " << n << "\n\n" << endl;
cout << " Sum of value " << total << "\n" << endl;
cout << " The average is " << avg << " \n" << endl;
cout << " The mode is " << get_mode << "\n" << endl;
/*cout << " get_mode" << get_mode << endl;*/
// Checking to see if it is a even number and moving it up from position zero to get a middle value
if ( n % 2 == 0)
{
n = (n+1)/2;
}
//Checking to see if it is a odd number
else
{
n = (n/2);
}
cout << "\n" << "The median is Number is " << students[n] << "\n" << endl;
// printf("Mode: %d\n", get_mode());
/*Stopping the screen from closing so the user can see the output*/
system("PAUSE");
/*Adding a line break just a habit I do for a good visual effect*/
cout << "\n";
/*Clearing the pointer "dynamically allocated memory holder"
for student */
delete [] students;
return 0;
}
int get_mode(
int n[], /* List of numbers */
int size, /* Size of list */
int max /* Upper range limit */
)
{
int *freq;
int i;
int mode;
// I saw this code and though I would borrow it to see if I could grab the mode of the values being entered
// One issue I know of is if there is not a repeted number then there is not a mode value but it will not compile now.
/* Allocate and zero fill a table */
freq = calloc(max, sizeof *freq); //error here//
/* Increment the appropriate element for each occurrence of a[i] */
for (i = 0; i < 10; i++)
{
++freq[ n[i] ];
}
/* Find the first largest element */
mode = 0;
for (i = 0; i < max; i++)
{
if (freq[i] > mode)
{
mode = i;
}
}
free(freq);
return mode;
}
|