I am trying to figure out why my letterGrades and highest function aren't working.....I'm more clueless on the letterGrade portion, as I believe that my highest function is a simple fix that I'm just not seeing.

With the letterGrade function I am trying to assign a letter grade to each students avg.

With the highest value, I am trying to find the highest value of each test.

#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
#include <cmath>
using namespace std;

struct student
{
	int id;
	string fname;
	string lname;
	double gr[8];
	double avg;
	string letGr;
};

void print(student arr[], int cnt)
{
	cout << endl;
	cout << "Total Number of Students: " << cnt;
	cout << endl;
	cout << " ID      LName      FName          Q1   Q2   Q3   MT   Q4   Q5   Q6    F   Avg  Grade" << endl;
	cout << "-------------------------------------------------------------------------------------" << endl;
	for (int i=0;i<cnt;i++)
	{
		cout << setprecision(0);
		cout << setw(2) << arr[i].id << "   " << left << setw(12) << arr[i].fname 
			 << setw(12) << arr[i].lname << right;
		for (int j=0;j < 8;j++)
			cout << setw(5) << arr[i].gr[j];
		cout << setprecision(1);
		cout << "  " << arr[i].avg  << "    " << arr[i].letGr << endl;
	}
	cout << endl;
}
double studAvg(student rec)
{
	int s=0;
	int m=0;
	int f=0;

	for (int i=0; i < 8; i++)
		s += rec.gr[i];
	m = rec.gr[3];
	f = rec.gr[7];

	if (rec.gr[0] <= rec.gr[1] && rec.gr[0] <= rec.gr[2] && rec.gr[0] <= rec.gr[3] && rec.gr[0] <= rec.gr[4] && rec.gr[0] 
		<= rec.gr[5] && rec.gr[0] <= rec.gr[6] && rec.gr[0] <= rec.gr[7])
		return (((s - rec.gr[0]) + m + (2 * f)) / 11.0);
	else if (rec.gr[1] <= rec.gr[0] && rec.gr[1] <= rec.gr[2] && rec.gr[1] <= rec.gr[3] && rec.gr[1] <= rec.gr[4] && rec.gr[1] 
		<= rec.gr[5] && rec.gr[1] <= rec.gr[6] && rec.gr[1] <= rec.gr[7])
		return (((s - rec.gr[0]) + m + (2 * f)) / 11.0);
	else if (rec.gr[2] <= rec.gr[0] && rec.gr[2] <= rec.gr[1] && rec.gr[2] <= rec.gr[3] && rec.gr[2] <= rec.gr[4] && rec.gr[2] 
		<= rec.gr[5] && rec.gr[2] <= rec.gr[6] && rec.gr[2] <= rec.gr[7])
		return (((s - rec.gr[1]) + m + (2 * f)) / 11.0);
	else if (rec.gr[4] <= rec.gr[0] && rec.gr[4] <= rec.gr[2] && rec.gr[4] <= rec.gr[1] && rec.gr[4] <= rec.gr[4] && rec.gr[4] 
		<= rec.gr[5] && rec.gr[4] <= rec.gr[6] && rec.gr[4] <= rec.gr[7])
		return (((s - rec.gr[2]) + m + (2 * f)) / 11.0);
	else if (rec.gr[5] <= rec.gr[0] && rec.gr[5] <= rec.gr[1] && rec.gr[5] <= rec.gr[2] && rec.gr[5] <= rec.gr[3] && rec.gr[5] 
		<= rec.gr[4] && rec.gr[5] <= rec.gr[6] && rec.gr[5] <= rec.gr[7])
		return (((s - rec.gr[3]) + m + (2 * f)) / 11.0);
	else if (rec.gr[6] <= rec.gr[0] && rec.gr[6] <= rec.gr[1] && rec.gr[6] <= rec.gr[2] && rec.gr[6] <= rec.gr[3] && rec.gr[6] 
		<= rec.gr[4] && rec.gr[6] <= rec.gr[6] && rec.gr[6] <= rec.gr[7])
		return (((s - rec.gr[4]) + m + (2 * f)) / 11.0);
	else
		return ((s + m + (2 * f)) / 11.0);
}


double average(student arr[], int cnt, int i)
{
	double s=0;
		
	for (int j=0; j < cnt; j++)
		if (i > 0)
			s += arr[j].gr[i];
		else
			s += arr[j].avg;
	
	return s/cnt;
}

double highest(student arr[], int cnt, int i)
{
	double max = 0;

	for (int j=0;j<cnt;j++)
		if (i > max)
			max = arr[j].gr[i];

	return max;
}

string letterGrade(double studAvg)
{
	if (studAvg > 95.0) return "A";
	else if (studAvg > 90.0  && studAvg <= 95.0) return "A-";
	else if (studAvg > 85.0  && studAvg <= 90.0) return "B";
	else if (studAvg > 80.0  && studAvg <= 85.0) return "B-";
	else if (studAvg > 75.0  && studAvg <= 80.0) return "C";
	else if (studAvg > 70.0  && studAvg <= 75.0) return "C-";
	else if (studAvg > 65.0  && studAvg <= 70.0) return "D";
	else if (studAvg > 60.0  && studAvg <= 65.0) return "D-";
	else return "F";
}

void sort(student arr[], int cnt)
{
	student t;

	for (int i=cnt-1; i < 1; i--)
		for (int j=1; j < i; j++)
			if (arr[j+1].fname < arr[j].fname)
			{
				t = arr[j]; arr[j] = arr[j+1]; arr[j+1] = t;
			}
}

double stdDev(student arr[], double avg, int cnt, int i)
{
	double s = 0;

	for (int j=0; j < cnt ; j++)
		if (i>0)
			s += pow(arr[j].gr[i] - avg, 2);
		else
			s += pow(arr[j].avg - avg, 2);
	return sqrt(s / (cnt-1));
}


void main()
{
	ifstream inF;
	student myStuds[73];
	int cnt=0;
	int i;
	double classAvg;
	string fname, lname, id;

	cout << fixed << setprecision(1);

	inF.open("students.txt");


	inF >> myStuds[cnt].id;
	while (!inF.eof())
	{
		inF >> myStuds[cnt].fname;
		inF >> myStuds[cnt].lname;
		for (i = 0; i < 8; i++)
			inF >> myStuds[cnt].gr[i];
		myStuds[cnt].avg = studAvg(myStuds[cnt]);
		myStuds[cnt].letGr = letterGrade(myStuds[i].avg);

		cnt++;
		inF >> myStuds[cnt].id;
	}
	inF.close();
	classAvg = average(myStuds, cnt, i);
	print(myStuds, cnt); 
	cout << "                       Average: ";
	for (int i=0; i < 8; i++)
		cout << setw(5) << average(myStuds, cnt, i);
	cout << setw(6) << classAvg <<  endl;
	cout << "            Standard Deviation: ";
	for (int i=0; i < 8; i++)
		cout << setw(5) << stdDev(myStuds, average (myStuds, cnt, i), cnt, i);
	cout << setw(6) << stdDev(myStuds, classAvg, cnt, 0) << endl;
	cout << "                       Highest: ";
	for (int i=0; i < 8; i++)
		cout << setw(5) << highest(myStuds, cnt, i);
}

This is what I'm thinking of using for the largest value:

double highest(student arr[], int cnt, int i)
{
	double max = arr[0].gr[0];

	for (int j=0;j<cnt;j++)
		if (arr[j].gr[i] > max)
			max = arr[j].gr[i];

	return max;
}

Edited 7 Years Ago by PDB1982: n/a

Your highest function should return an INT, and that int should be
the index of the highest element in that array. Change that first.

Next, your highest function is almost correct, except that your max should be the index to the highest value, and your if statement should check every element against the array content of the max value.
So it should be something like this :
//returns the index to the highest element in arr

int highest(student arr[], int cnt, int i)
{
	int max = 0;
 
	for (int j=0;j<cnt;j++){
		if (arr[j].gr[i] > arr[max].gr[i])
			max = j;
             }

	return max;
}
This question has already been answered. Start a new discussion instead.