// preprocessor commands

#include "g:\victor\vitodef.h"

// prototypes

void getinput(void);
void printray(void);
float mean(void);
int max(void);             // returns biggest value in scoray[].
int min(void);             // returns smallest value in scoray[].
float median(void);        // returns median or "middle" score in scoray[].
void sort(void);           // sorts scoray[].
int findbigsub(int);       // returns the subscript of the max number in scoray[].
void swap(int,int);        // exchanges values in scoray between [sub1] and [sub2].
void makecountray(void);   // creates countray[].
int mode(void);            // returns most frequent score, using countray[].
int midrange(void);        // returns the midway points between highest & lowest.
float successrate(void);   // returns percentage of students with scores of 60 or above.


// global variables

int scoray[1000];
int class_size = 0;
int countray[101];


// function definitions


void main(void)
// get the input, print it, output the mean
{
clrscr();
getinput();
makecountray();
printray();
sort();
printray();
cout << "maximum:    "  << max()           << endl;
cout << "minimum:    "  << min()           << endl;
cout << "mean:       "  << mean()          << endl;
cout << "median:     "  << median()        << endl;
cout << "mode:       "  << mode()          << endl;
cout << "midrange:   "  << midrange()      << endl;
cout << "successrate "  << successrate()   << endl;
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);
}

int min(void)
// returns smallest value in scoray[].
{
int small = scoray[0], count = 0;
for (; count < class_size; count++)
     if (scoray[count] < small)
     small = scoray[count];
return(small);
}

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 sort(void)
// sorts scoray[] in descending order.
{
int ctr;
for (ctr=0; ctr < class_size; ctr++)
   swap(findbigsub(ctr), ctr);
}

int findbigsub(int start)
// returns the subscript of the max number in scoray[].
{
int ctr=0, big;
for (start=0; start < class_size-1; start++)
      if ((scoray[start]) < (scoray[ctr+1]))
	 big = ctr;
	 ctr++;
}
    
void swap(int sub1, int sub2)
// exchanges values in scoray between [sub1] and [sub2].
{
int temp;
temp = scoray[sub1];
scoray[sub1] = scoray[sub2];
scoray[sub2] = temp;
}

void makecountray()
// creates countray[].
{
int ctr, ctr1;
for (ctr=0; ctr < 101; ctr++)
   countray[ctr] = 0;

for (ctr1=0; ctr1 < class_size; ctr1++)
  {
  for (ctr=0; ctr < 101; ctr++)
     {
     if ((scoray[ctr1]) == ctr)
	countray[ctr]++;
     }
  }
}

int mode()
// returns most frequent score, using countray[].
{
int ctr = 0, num = countray[0], tie1, tie2, frequent;
for (; ctr < 101; ctr++)
    if (countray[ctr] > num)
	num = countray[ctr];
    if (countray[ctr+1] == num)
        tie1 = num;
for (ctr=0; ctr < 101; ctr++)
    if (countray[ctr] == num)
        frequent = ctr; 
return(frequent);
}	

int midrange()
// returns the midway points between highest & lowest.
{
int big = scoray[0], small = scoray[0], count = 0;
int midway = 0;
for (; count < class_size; count++)
     if (scoray[count] > big)
     big = scoray[count];
for (count = 0; count < class_size; count++)
     if (scoray[count] < small)
     small = scoray[count];
midway = ((big-small)/2)+small;
return(midway);
}

float successrate()
// returns percentage of students with scores of 60 or above.
{
int ctr, total=0, div, accum = 0;
for (ctr=0; ctr < class_size; ctr++)
     if (scoray[ctr] >= 60)
	 accum++;
return(((float)100/class_size)*accum);

}