// preprocessor commands

#include "c:\borlandc\include\vitodef.h"

// prototypes

void getinput(void);
void printray(void);
float mean(void);
int max(void);        // returns biggest value in scoray[].
float median(void);   // returns median or "middle" score in scoray[].
void sort(void);      // changes the order of values in scoray[] --->
		      // randomly without losing any or adding any new ones.
void swap (int, int); // swaps numbers within scoray[]. 

// global variables

int scoray[1000];
int class_size = 0;


// function definitions


void main(void)
// get the input, print it, output the mean
{
clrscr();
getinput();
printray();
cout << "mean:    "  << mean()   << endl;
cout << "max:     "  << max()    << endl;
cout << "median:  "  << median() << endl;
sort();
printray();
getche();
}

void getinput()
// get integers from keyboard into scoray[] until user inputs -1
// set class_size to number of inputs, -1 not included
// ignore inputs out of 0..1000 range; tell user when doing so
{
int innum;
cout << "type integer, then ENTER, etc.  To stop, type -1" << endl;
cout << "scores must be in range 0..100" << endl;
cin >> innum;
while (innum != -1)
	 {
	 if (innum < 0 or innum > 100)
		  cout << "input ignored, out of range" << endl;
	 else
		  {
		  scoray[class_size] = innum;
		  class_size++;
		  }
	 cin >> innum;
	 }
}

void printray()
// output contents of scoray[] to display screen in eight columns.
{
int ctr;
cout << endl;
for (ctr = 0;  ctr < class_size; ctr++)
	{
	printf("%-6d", scoray[ctr]);
	if ( (ctr+1) % 8 == 0)
	 cout << endl;
	}
cout << endl << endl;
}

float mean(void)
// return mean of all inputs
{
int count = 0, sum =0;
// first add them up
for (; count < class_size; count++)
	 sum+= scoray[count];
// then divide by the class size and output
return(((float) sum) / class_size);
}

int max(void)
// returns biggest value in scoray[].
{
int big = scoray[0], count = 0;
for (; count < class_size; count++)
	  if (scoray[count] > big)
	  big = scoray[count];
return(big);
}

float median(void)
// returns median or "middle" score in scoray[].
{
int sum=0;
if ((class_size % 2) == 0)
	{
	sum = ((scoray[class_size/2]) + (scoray[class_size/2-1]));
	return(((float) sum) / 2);
	}
else
	{
	sum = scoray[class_size/2];
	return(sum);
	}

}

void swap (int first, int second)
// swaps numbers within scoray[].
{
int temp;
temp = scoray[first];
scoray[second] = scoray[first];
scoray[first] = temp;
}

void sort(void)
// sorts scoray[] in descending order.
{
int x = class_size, ctr;
for (ctr=x; ctr = 0; ctr--)
   swap(x,x);
}


