Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

help with arrays....

Status
Not open for further replies.

ting

Technical User
Apr 27, 2001
9
US
Hello everyone. I just have a little problem with this program I am trying to complete. I will post it. The program is a sample program that computes the average, median, variance, and standard deviation for a certain number of grades. I need it to display the passing grades and failing grades properly. For whatever amount of entries the user enters to be calculated, the program should execute saying which of those grades deserve an 'A', and which of the grades deserve an 'F'. For example the grades 90, 85, 22, 67, 75, 82, 57, 78, 81, 64, 67, 75, 37, and 98 should display all the numbers sorted in ascending order, the mean grades, the variance, the standard deviation, and at the end it should state the 'A' (passing) grades which is 98, and the 'F' (failing) grades which are 22 and 37. I want the display to look like this:
"The 'A' grades are: 98"
"The 'F' grades are: 22, 37"

I am having a problem displaying it like that. The problem is the way I am writing my for loops (in the main function).

If for any certain number of grades a message there are no failing grades AND no passing grades two messages should appear:
"There are no 'A' grades."
"There are no 'F' grades."

If there are A grades but NO failing grades:
"The 'A' grades are: ...." (providing the 'A' grades)
"There are no failing grades".
And vice versa.
Here is a copy of my program. Anyone who can help, your help will gladly be appreciated. Thank you.

#include<iostream.h>
#include <stdlib.h>
#include<iomanip.h>
#include<math.h>

double findaverage(double[], int);
void sortnums(double[], int);
double findmedian(double[], int);
double fstandard_dev (double[], int);

void main()
{
double maxstudents[40];
const double STANDEV = 1.28;
int grades = 0;
int entry;
double average;
double median;
double standard_dev;
int i;
double finalf = 0;
double finala = 0;

cout << &quot;Enter the grades to be processed.&quot;<<endl;
cout << &quot;When finished, press -1 and 'Enter'.&quot; << endl;
do
{
cout << &quot;Grades: &quot;;
cin >> entry;
if (entry > -1)
{
maxstudents[grades] = entry;
grades++;
}
}
while (entry > -1);

//*******clear screen******
system(&quot;cls&quot;);

cout << endl<<endl;
cout << &quot;The average of the grades is: &quot;;
average = findaverage(maxstudents, grades);
cout << endl;

cout << &quot;The sorted list of grades in ascending order is: &quot;<<endl;
sortnums(maxstudents, grades);
for (i = 0; i < grades; i++)
{
cout << &quot; &quot; << setprecision(0) << maxstudents;
}
cout << endl << endl;

cout <<&quot;The median of the grades is: &quot;;
median = findmedian(maxstudents, grades);

standard_dev = fstandard_dev (maxstudents, grades);

finalf = average - (STANDEV * standard_dev);
finala = average + (STANDEV * standard_dev);

cout<<&quot; The 'A' grades will be more than &quot; <<setprecision(2)<<finala<<endl;
cout<<&quot; The failing grades will be less than &quot;<<setprecision(2)<<finalf<<endl<<endl;


//int counter = 0;
//here are the for loops that are supposed to output the grades
for (i=0; i<grades; i++)
{
if((maxstudents) >= finala)
{
cout<<endl<<&quot; The 'A' grades are: &quot;<<setprecision(0)<<maxstudents<<endl<<endl;

if((maxstudents) < finala)
cout<<&quot; There are no 'A' grades.&quot;<<endl<<endl;
}
}

for (i=0; i<grades; i++)
{
if((maxstudents) <= finalf)
{
cout<<&quot; The failing grades are: &quot;<<setprecision(0)<<maxstudents<<endl<<endl;

//while(counter=-1)
if((maxstudents) > finalf)
cout<<&quot; There are no failing grades.&quot;<<endl<<endl;
//counter++;
}
}
}

double findaverage (double maxstudents[], int grades) //computes the average
{
int i;
double average;
double total = 0;

for (i=0; i < grades; i++)
{
total = total + maxstudents; //average is calculated as the total of entered
average = total / grades; //numbers divided by the amount of numbers
} //maxstudents represents each number

cout << setiosflags(ios::fixed) << setprecision(2) <<average << endl;

return average;
}

void sortnums (double maxstudents[], int grades) //computes the sort
{
int i, j, min1;
double k;
double min;

for (i=0; i < (grades - 1); i++)
{
min = maxstudents;
min1 = i;
for (j = (i + 1); j < grades; j++)
{
if (maxstudents[j] < min)
{
min = maxstudents[j];
min1= j;
}
}
if (min < maxstudents)
{
k = maxstudents; //the sort switches the lowest number with the highest
maxstudents = min; //number to output in ascending order. Here the switch
maxstudents[min1] = k; //is taking place.
}
}
}

double findmedian (double maxstudents[], int grades) //finds the median
{
int i;
double median;
double total=0;

switch (grades)
{
case 2:
{
for(i=0; i<grades; i++)
total = total + maxstudents;
median = total / grades;
cout<<setiosflags(ios::fixed)<<setprecision(2)<<median;
}break;

case 3:
{
for(i=0; i<grades; i++)
median = maxstudents[1];
cout<<median;
}break;

case 4:
{
for(i=0; i<grades; i++)
median = ((maxstudents[1] + maxstudents[2]) / 2);
cout<<setiosflags(ios::fixed)<<setprecision(2)<<median;
}break;

case 5:
{
for(i=0; i<grades; i++)
median = maxstudents[2];
cout << median;
}break;

case 6:
{
for(i=0; i<grades; i++)
median = ((maxstudents[2] + maxstudents[3]) / 2);
cout<<setiosflags(ios::fixed)<<setprecision(2)<<median;
}break;

case 7:
{
for(i=0; i<grades; i++)
median = maxstudents[3];
cout<<median;
}break;

case 8:
{
for(i=0; i<grades; i++)
median = ((maxstudents[3] + maxstudents[4]) / 2);
cout<<setiosflags(ios::fixed)<<setprecision(2)<<median;
}break;

case 9:
{
for(i=0; i<grades; i++)
median = maxstudents[4];
cout<<median;
}break;

case 10:
{
for(i=0; i<grades; i++)
median = ((maxstudents[4] + maxstudents[5]) / 2);
cout<<setiosflags(ios::fixed)<<setprecision(2)<<median;
}break;

case 11:
{
for(i=0; i<grades; i++)
median = maxstudents[5];
cout<<median;
}break;

case 12:
{
for(i=0; i<grades; i++)
median = ((maxstudents[5] + maxstudents[6]) / 2);
cout<<setiosflags(ios::fixed)<<setprecision(2)<<median;
}break;

case 13:
{
for(i=0; i<grades; i++)
median = maxstudents[6];
cout<<median;
}break;

case 14:
{
for(i=0; i<grades; i++)
median = ((maxstudents[6] + maxstudents[7]) / 2);
cout<<setiosflags(ios::fixed)<<setprecision(2)<<median;
}break;

case 15:
{
for(i=0; i<grades; i++)
median = maxstudents[7];
cout<<median;
}break;

case 16:
{
for(i=0; i<grades; i++)
median = ((maxstudents[7] + maxstudents[8]) / 2);
cout<<setiosflags(ios::fixed)<<setprecision(2)<<median;
}break;

case 17:
{
for(i=0; i<grades; i++)
median = maxstudents[8];
cout<<median;
}break;

case 18:
{
for(i=0; i<grades; i++)
median = ((maxstudents[8] + maxstudents[9]) / 2);
cout<<setiosflags(ios::fixed)<<setprecision(2)<<median;
}break;

case 19:
{
for(i=0; i<grades; i++)
median = maxstudents[9];
cout<<median;
}break;

case 20:
{
for(i=0; i<grades; i++)
median = ((maxstudents[9] + maxstudents[10]) / 2);
cout<<setiosflags(ios::fixed)<<setprecision(2)<<median;
}break;

case 21:
{
for(i=0; i<grades; i++)
median = maxstudents[10];
cout<<median;
}break;

case 22:
{
for(i=0; i<grades; i++)
median = ((maxstudents[10] + maxstudents[11]) / 2);
cout<<setiosflags(ios::fixed)<<setprecision(2)<<median;
}break;

case 23:
{
for(i=0; i<grades; i++)
median = maxstudents[11];
cout<<median;
}break;

case 24:
{
for(i=0; i<grades; i++)
median = ((maxstudents[11] + maxstudents[12]) / 2);
cout<<setiosflags(ios::fixed)<<setprecision(2)<<median;
}break;

case 25:
{
for(i=0; i<grades; i++)
median = maxstudents[12];
cout<<median;
}break;

case 26:
{
for(i=0; i<grades; i++)
median = ((maxstudents[12] + maxstudents[13]) / 2);
cout<<setiosflags(ios::fixed)<<setprecision(2)<<median;
}break;

case 27:
{
for(i=0; i<grades; i++)
median = maxstudents[13];
cout<<median;
}break;

case 28:
{
for(i=0; i<grades; i++)
median = ((maxstudents[13] + maxstudents[14]) / 2);
cout<<setiosflags(ios::fixed)<<setprecision(2)<<median;
}break;

case 29:
{
for(i=0; i<grades; i++)
median = maxstudents[14];
cout<<median;
}break;

case 30:
{
for(i=0; i<grades; i++)
median = ((maxstudents[14] + maxstudents[15]) / 2);
cout<<setiosflags(ios::fixed)<<setprecision(2)<<median;
}break;

case 31:
{
for(i=0; i<grades; i++)
median = maxstudents[15];
cout<<median;
}break;

case 32:
{
for(i=0; i<grades; i++)
median = ((maxstudents[15] + maxstudents[16]) / 2);
cout<<setiosflags(ios::fixed)<<setprecision(2)<<median;
}break;

case 33:
{
for(i=0; i<grades; i++)
median = maxstudents[16];
cout<<median;
}break;

case 34:
{
for(i=0; i<grades; i++)
median = ((maxstudents[16] + maxstudents[17]) / 2);
cout<<setiosflags(ios::fixed)<<setprecision(2)<<median;
}break;

case 35:
{
for(i=0; i<grades; i++)
median = maxstudents[17];
cout<<median;
}break;

case 36:
{
for(i=0; i<grades; i++)
median = ((maxstudents[17] + maxstudents[18]) / 2);
cout<<setiosflags(ios::fixed)<<setprecision(2)<<median;
}break;

case 37:
{
for(i=0; i<grades; i++)
median = maxstudents[18];
cout<<median;
}break;

case 38:
{
for(i=0; i<grades; i++)
median = ((maxstudents[18] + maxstudents[19]) / 2);
cout<<setiosflags(ios::fixed)<<setprecision(2)<<median;
}break;

case 39:
{
for(i=0; i<grades; i++)
median = maxstudents[19];
cout<<median;
}break;

case 40:
{
for(i=0; i<grades; i++)
median = ((maxstudents[19] + maxstudents[20]) / 2);
cout<<setiosflags(ios::fixed)<<setprecision(2)<<median;
}break;
}
cout<<endl<<endl;
return median;
}

double fstandard_dev (double maxstudents[], int grades)
/*computes the variance, standard deviation, and 1.28 standard deviation of grades that
passed and failed*/
{
int i;
double average;

double variance=0; //variance initialized to zero because when calculating variance,
//each value must be added up then divided by the total grades minus one
double standard_dev;
double k, k2;
double sum = 0;
double total = 0;

for (i=0; i < grades; i++)
{
total = total + maxstudents;
average = total / grades; //to find variance must calculate the average first
}
/*after the average is calculated I created an inner loop for the variance */
for (i=0; i < grades; i++)
{
k = maxstudents - average; //k represents each number minus the average.
k2 = pow(k,2); //k2 is the square of each answer derived from k
variance = variance + (k2 / (grades - 1)); //variance is calculated here
standard_dev = sqrt(variance); //standard deviation is the square root of the variance
}

cout<<&quot;The variance is: &quot;;
cout<<setiosflags(ios::fixed) << setprecision(2) <<variance<<endl<<endl;
cout<<&quot;The standard deviation of the grades is: &quot;;
cout << standard_dev<<endl<<endl; //output standard deviation

return standard_dev;
}
 
A couple of suggestions --

When accepting input, make sure your user can't enter more entries than your array can hold (or you get a runtime error)

When looping through the grades (like during the sort, or before), keep track of the number of A's and F's in two counter variables.

And in your findmedian function, see if you can find a better way to determine the median other than a huge switch statement. Try using the floor() and/or ceil() functions to find the middle grade.

Good luck on your assignment.

Chip H.
 
Hi Chip.
You see, that's exactly what I've been trying to do.
-keep track of the number of A's and F's in two counter variables.
But I'm having trouble just writing a simple counter to do that. Maybe I'm putting it in the wrong place, who knows.
And sorry about the find_median function. I know it's long, it can actually be done in two steps, but I did it the &quot;baby&quot; way. I'm not a very skilled programmer. Still learning. But thanks for the tip. Can I get another tip on how to fix this counter?? Please???
 
Well it looks like your &quot;finala&quot; and &quot;finalf&quot; variables are looking at the std deviation to do grading on a curve (i.e. the grading points aren't fixed at 90,80,70, etc).

You'll need two additional variables to count the number of grades above finala and below finalf. Do something like:
[tt]
for (i = 0; i < grades; i++) {
[tab]if maxstudents > finala
[tab][tab]CountA++;
[tab]if maxstudents < finalf
[tab][tab]CountF++;
}
[/tt]

Chip H.
 
I tried a counter and thats working fine. But I need the numbers to output with a comma after each number. Like say there are two or more passing - or two or more failing grades I need a comma to output after each number. I tried something like this, but it does not work quite well:

Code:
for (i=0; i<grades; i++)
	{
		if((maxstudents[i]) <= finalf)
			numf++;
		if(numf == 1)
			cout<<&quot; The failing grades are: &quot;<<setprecision(0)<<maxstudents[i];		
		else
			cout<<&quot;, &quot;<<setprecision(0)<<maxstudents[i];
	}
	if(numf == 0)
		cout<<&quot; There are no failing grades.&quot;<<endl<<endl;
	else
		cout<<endl<<endl;
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top