473,406 Members | 2,467 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

issues with maximum and average value in functions

Well ...this is a real challenge .....i got everything else working OK...but ...

I have to get the average and maximum value out of a group of people thru two functions .I have problems passing value to function and processing it after is there ...
Any help will be very appreciated.

This is my code :


#include <iostream>
#include <cstdlib>
using namespace std;

//functions prototypes

float averagegrade(Student[],int)
int maxgradestudent(Student [],int)


//classes declaration

class Person
{
public:
Person(void);
~Person(void);
void Setfirstname(char []);
char * Getfirstname(void);
void Setlastname(char []);
char * Getlastname(void);

private:
char Firstname[30];
char Lastname [30];
};

class Student : public Person
{
public:
Student(void);
~Student(void);
void Setnid(char[]);
char * getNID(void);
void SetGrade(float);
float GetGrade(void);

private:
float grade;
char NID [30];


};

void main()
{
Student studentInfo[100];
float studentGrade;
int i,numStudents;
char firstName[30];
char lastName[30];
char NID [30];
float ave =0.0;
int maxgrade;

//check for errors

do
{
cout << "Enter the number of students:";
cin >> numStudents;
if ((numStudents < 1) || (numStudents > 100))
{
cout << "Incorrect value. Try again...\n";
}
} while ((numStudents < 1) || (numStudents > 100));

//Enter info for students:

for (i=0;i<numStudents;i++)
{
cout << "Student #"<<i+1 << endl;
cout << " Enter the Student's first name :";
cin >> firstName;

cout << " Enter the Student's last name :";
cin >> lastName;

cout << "Enter the student's nid:";
cin >> NID;


cout << "Enter the student's grade:";
cin >> studentGrade;

//send data to functions

studentInfo[i].Setfirstname(firstName);
studentInfo[i].Setlastname(lastName);
studentInfo[i].Setnid(NID);
studentInfo[i].SetGrade(studentGrade);

//average call fuction (i dont know if is correct):

ave= averagegrade(studentInfo[i],ave);

//maximum call function :

maxgrade= maxgradestudent(studentInfo[i],maxgrade);
}

cout << "\n\n";

for (i=0;i<numStudents;i++)
{
cout << "The average grade is " << studentInfo[i].GetGrade() << endl;

cout << "The student with the highest grade is :" << studentInfo[i].Getfirstname()<<" ";
cout << studentInfo[i].Getlastname()<< "NID:"<< studentInfo[i].getNID()<<"grade:"<<studentInfo[i].GetGrade()<<"\n";


}

//functions

//constructor
}

Person::Person(void)
{
strcpy(Firstname,"");
}

//destructor

Person::~Person(void)
{
strcpy(Firstname,"");
}

//function for first name

void Person::Setfirstname(char firstname[])
{
strcpy(Firstname,firstname);
}

//function for Last name

void Person::Setlastname(char lastname[])
{
strcpy(Lastname,lastname);
}

char * Person::Getfirstname(void)
{
return Firstname;
}

char * Person::Getlastname(void)
{
return Lastname;
}
//nid function

Student::Student(void)
{
strcpy (NID,"");
}

Student::~Student(void)
{
strcpy (NID,"");
}

void Student::Setnid(char Nid[])
{
strcpy (NID,Nid);
}

char * Student::getNID(void)
{
return NID;
}


//function for grade:

void Student::SetGrade(float studentGrade)
{
grade = studentGrade;
}

float Student::GetGrade(void)
{
return grade;
}

//function for average grade:

?
Dec 1 '06 #1
1 2111
DeMan
1,806 1GB
Hello,
I think the problem is that you are passing the same value that you try to store the result that is in
Expand|Select|Wrap|Line Numbers
  1.  ave= averagegrade(studentInfo,ave); 
you pass ave (at the time 0.0) into the function and ask for ave in return. This isn't really a problem, except passing ave in serves no purpose.
Secondly, the only way to pass anything other than a primitive (the basic types) is to pass reference to the object. That is, c doesn't (really) understand Objects. You have to pass a reference to the object and guarantee c you know what to do with it.
This is sort of irrelevant, because the way it is represented here you won't REALLY notice it, but be aware all you pass in the method call is a reference to where the array starts (more information try the tutorials here or search for "pointers c" on the internet)

While there are more than one ways to skin a cat, I would suggest you keep the same principle for your function protoypes, but use the second (int) variable to pass the size of the array (some will say this is pointless, but you have already calculated it and it is easier to use something you know without having to use new functions).

The prototype, then, should be something like:
Expand|Select|Wrap|Line Numbers
  1. float averageGrade(int studentInfo[], int size)
  2.  
;

Remember that the Array i smerely a representation, I'll try to keep it as such, but it pays to understand how c is dealing with this structure (because if you pass anything more complicated you have to understand that you are only passing a pointer). I'll again suggest you visit the pointer idea for a while.

Now we know we have a pointer to an integer array, and we have the size so we can try to implement:

Expand|Select|Wrap|Line Numbers
  1. float averageGrade(int studentScores[], int size)
  2. {
  3.   int total = 0; //variable to keep sum of all elements in array
  4.   for(int i=0; i<size; i++)
  5.   {
  6.     total=total+studentScores[i]; //add this students score to the total
  7.   }
  8.  
  9.  return  (float)total/size; 
  10. }
  11.  
the "return (float)" makes sure that we return a number with decimal places. C (quite reasonably, I think) says : an integer divided by an integer must be an integer (because after all we have been dealing with INTEGERS), so we CAST the result into a float (floating point number) so that c remembers to give us more than that (that's probably not clear, but i can clarify that later if you need).

you can implement maxgrade similarly, using the second int as array size. this timeyou will need a temporary variable (initialized at 0), and rather than adding to total, you will need to check whether the current item is bigger than the item in your temporary variable, and update the temporary varibale if it isn't.
You won't have to worry about casting either, as this time you only have to return the maximum value (which is an int)
Dec 1 '06 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Kums | last post by:
What is the maximum permissible size of a database? Is there any limitation. What is the maximum # of tablespace's allowed in a database? Thanks for your response.
11
by: ian.davies52 | last post by:
Is there anything I can do about the apparent limit on the number of textboxes that have calculations as their control source on a form or report in ms-access? I have a query that pulls together...
3
by: KWilliams | last post by:
I'd like to get some good advice about our old ASP site. You can see our home page at: http://www.douglas-county.com/ ....and an example application page at:...
3
by: C++Geek | last post by:
I need to get this program to average the salaries. What am I doing wrong? //Program to read in employee data and calculate the average salaries of the emplyees.
4
by: Scott | last post by:
In order to give a meaning average value and minimum and maximum values, I would like to have a formula for a group of data after taking out those extremes. Can someone share your way to...
3
by: Madmartigan | last post by:
Hello I have the following task but am battling with the final output. How do I keep two different vectors in sync and how would I retrieve the index for the maximum value of one of the vectors??...
3
by: Frank Rizzo | last post by:
I am trying to do some monitoring of some PerfMon counters and have a question. Does PerfMon figure out the Minimum, Maximum, Average values for each counter? Or are those values part of the...
2
choke
by: choke | last post by:
I need to write a function in devc++ that creates an array of n integers, each element is equal to n*n+i*i-n*i where i is from 0 to n-1 as the array index. Within the same function I need to find...
53
by: Gianni Mariani | last post by:
Do you have a preference on maximum line width for C++ code? I've seen the craziest debates on this most silly of topic. I have witnessed engineers spent oodles of time fiddling with line...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.