Help with Min/Max

Help with min max function for average in array of classes.

Page 1 of 1

5 Replies - 1052 Views - Last Post: 18 November 2009 - 08:29 PM Rate Topic: -----

#1 techtiger  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 18-November 09

Help with Min/Max

Post icon  Posted 18 November 2009 - 02:10 PM

#include<iostream>
#include<fstream>
#include<cctype>
#include<iomanip>
#include<cstring>
#include<cstdlib>
	
using namespace std;

class Student
{
	  private:
		   char name[20];
		   int project[7];
		   int exam[2];
		   int quiz[10];
		   double project_avg;
		   double exam_avg;
		   double quiz_avg;
		   int final;
		   double average;
		   char grade[3];

	  public:   Student();
			void get_student_scores();
			void compute_student_stats();
			double student_average();
			void determine_student_grade();
			void display_student_name_gpa_grade();
};

class Course
{
	  private:
		   Student student[6];
		   int index_of_highest;
		   int index_of_lowest;

	  public:
		   Course();
		   ~Course();
		   void get_grades();
		   void evaluate_class();
		   void determine_index_of_highest_lowest();
		   void display_highest();
		   void display_lowest();
		   void display_stats();
};

//Initialize Program
int main()
{
 Course  aCourse;
 
 aCourse.get_grades();
 aCourse.evaluate_class();
 
 system("PAUSE");
 return 0;
}


Student::Student()
{
	//Clear Variables
	strcpy(name, "");

	// Loops to clear the arrays
	for (int i = 0; i < 7; i++)
	{
	project[i] = 0;
	}

	for (int i = 0; i < 2; i++)
	{
	exam[i]=0;

	}
	for (int i = 0; i < 10; i++)
	{
	quiz[i]=0;
	}


	//Variable Set to Zero
	project_avg=0;
	exam_avg=0;
	quiz_avg=0;
	final = 0;
	average = 0;

	//Clear Grades
	strcpy(grade, "");


return;
}


void Student::get_student_scores()
{
	
	cout<<"Enter student's name:";
	cin.getline(name, 20);

	for(int i = 0; i<7;i++)
	{
   	cout<<"Please enter the grade that "<<name<<" received on project "<<i+1<<":";
	cin>>project[i];
	}

	for(int i = 0; i<10; i++)
	{
	cout<<"Please enter the grade that "<<name<<" received on quiz "<<i+1<<":";
	cin>>quiz[i];
	}

	for(int i = 0; i<2;i++)
	{
	cout<<"Please enter the grade that "<<name<<" received on exam "<<i+1<<":";
	cin>>exam[i];
	}
	cout<<"Please enter the grade that "<<name<<" received on the final ";
	cin>>final;
	
	cin.ignore();
	
return;		  
}

void Student::compute_student_stats()
{
	//Compute Student Averages
	//Project Average
	for(int i = 0; i<7;i++)
	{
	project_avg += project[i];
	}
	project_avg = (project_avg/7);
	
	//Quiz Average
	for(int i = 0; i<10; i++)
	{
	quiz_avg+=quiz[i];
	}
	quiz_avg=(quiz_avg/10);

	//Exam Average
	for(int i = 0; i<2;i++)
	{
	exam_avg+=exam[i];  
	}
	exam_avg=(exam_avg/2);

	 //Weight Constants
	 const double project_wt = 0.40,
				  quiz_wt = 0.10,
				  exam_wt = 0.30,
				  final_wt = 0.20;
	 
	 //Compute course average
	 average =   (project_avg *project_wt) +
				 (quiz_avg * quiz_wt) +	
				 (exam_avg*exam_wt)+
				 (final*final_wt);
	 
	 average=static_cast<int>(average*10+0.5);
	 average= static_cast<double>(average/10);
return;
}

//Calculate Student Average
double Student::student_average()
{   
  return average;
}


void Student::display_student_name_gpa_grade()
{
	 ofstream outs;
	 outs.open("StudentGrades.txt",ios::app);
	 outs<<endl;
	 cout<<endl;
	 //Output to External File
	 outs<<"Student Name: "<<name<<endl;
	 outs<<setprecision(2);
	 outs<<name<<"'s Average: "<<average<<endl;
	 outs<<name<<"'s Grade: "<<grade<<endl<<endl;
	 
	 //Display Grades on Screen
	 cout<<"Student Name: "<<name<<endl;
	 cout<<setprecision(2);
	 cout<<name<<"'s Average: "<<average<<endl;
	 cout<<name<<"'s Grade: "<<grade<<endl<<endl;
	 outs.close();
	 
	 

return;
}

//Determines Letter Grade for Students
void Student::determine_student_grade()
{
	   if(average>=93)
	   {
			grade[0]='A';
			grade[1]=' ';
			grade[2]='\0';
	   }
	   else if(average>=89)
	   {
			grade[0]='A';
			grade[1] = '-';
			grade[2]='\0';
	   }
	   else if(average>=87)
	   {
			grade[0] = 'B';
			grade[1] = '+';
			grade[2]='\0';
	   }
	   else if(average>=83)
	   {
			grade[0] ='B';
			grade[1] = ' ';
			grade[2]='\0';
	   }
	   else if(average>=79)
	   {
			grade[0] = 'B';
			grade[1] = '-';
			grade[2]='\0';
	   }
	   else if(average>=77)
	   {
			grade[0] = 'C';
			grade[1] = '+';
			grade[2]='\0';
	   }
	   else if(average>=73)
	   {
			grade[0] = 'C';
			grade[1] = ' ';
			grade[2]='\0';
	   }
	   else if(average>=69)
	   {
			grade[0] = 'C';
			grade[1] = '-';
			grade[2]='\0';
	   }
	   else if(average>=67)
	  {
			grade[0] = 'D';
			grade[1] = '+';
			grade[2]='\0';
	  }
	  else if(average>=63)
	  {
			grade[0] = 'D';
			grade[1] = ' ';
			grade[2]='\0';
	  }
	  else if(average>=57)
	  {
			grade[0] = 'D';
			grade[1] = '-';
			grade[2]='\0';
	  }
	  else
	  {
		   grade[0] = 'F';
		   grade[1] = ' ';
		   grade[2]='\0';
	  }
return;
}

void Course::get_grades()
{
	 for(int i=0;i<6;i++)
	 {
		 student[i].get_student_scores();
		 student[i].student_average();
	 }
return;
}

void Course::display_highest()
{
	 ofstream outs;
	 outs.open("StudentGrades.txt",ios::app);
	 outs<<"Highest Average Was: "<<index_of_highest;
	 cout<<"Highest Average Was: "<<index_of_highest;
	 outs.close();
	 
return;
}

void Course::display_lowest()
{
	 ofstream outs;
	 outs.open("StudentGrades.txt",ios::app);
	 outs<<"Highest Average Was: "<<index_of_lowest;
	 cout<<"Highest Average Was: "<<index_of_lowest;
	 outs.close();
	 
return;
}

//Determine minimum average and max average
void Course::determine_index_of_highest_lowest()
{
	for (int i = 0; i < 6; i++)
	{
	  if (student[i].average > max)
		{
		  index_of_highest = student[i].average;
		}
	  else if (student[i].average < min)
		{
		  index_of_lowest = student[i].average;
		}
	}

return;
}


//Evaluate Class for Each Student	 
void Course::evaluate_class()
{
	 for(int i=0;i<6;i++)
	 {
		 student[i].compute_student_stats();
		 student[i].determine_student_grade();
		 student[i].display_student_name_gpa_grade();
	 }

return;
}


//Default Constructor
Course::Course()
{
	Student student[6];
	index_of_highest=0;
	index_of_lowest=0;

return;
}


//Deconstructor
Course::~Course()
{
	cout<<"Object going out of existence!";

return;
}



Is This A Good Question/Topic? 0
  • +

Replies To: Help with Min/Max

#2 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6039
  • View blog
  • Posts: 23,436
  • Joined: 23-August 08

Re: Help with Min/Max

Posted 18 November 2009 - 03:04 PM

Are you receiving any errors? Does this code not work that way you intended it? When asking for help there are a couple items that are vital in order for someone to properly help you:
  • Post the code you're having problems with
  • Post the exact error you're receiving, if you are receiving one
  • If no error explain what the code is doing versus what you want it to do
  • Post your question in the body of your post, not the description field

Was This Post Helpful? 0
  • +
  • -

#3 techtiger  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 18-November 09

Re: Help with Min/Max

Posted 18 November 2009 - 03:28 PM

When i take out for (int i = 0; i < 6; i++)
{
if (student[i].average > max)
{
index_of_highest = student[i].average;
}
else if (student[i].average < min)
{
index_of_lowest = student[i].average;
}
}

in void Course::determine_index_of_highest_lowest()'
it will compile and run and give me the values. i just need to know how to get min and max values from the function

void Course::determine_index_of_highest_lowest()
Was This Post Helpful? 0
  • +
  • -

#4 Oler1s  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1395
  • View blog
  • Posts: 3,884
  • Joined: 04-June 09

Re: Help with Min/Max

Posted 18 November 2009 - 04:35 PM

Quote

i just need to know how to get min and max values from the function
Are you more interested in being given enough guidance to write the code yourself,

or would you be happy if we just fixed your code for you and gave you working code?
Was This Post Helpful? 0
  • +
  • -

#5 techtiger  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 18-November 09

Re: Help with Min/Max

Posted 18 November 2009 - 04:51 PM

something that would work so i can remember how its done in the long run.
Was This Post Helpful? 0
  • +
  • -

#6 techtiger  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 18-November 09

Re: Help with Min/Max

Posted 18 November 2009 - 08:29 PM

Basically option 2
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1