473,765 Members | 2,061 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

issues with maximum and average value in functions

19 New Member
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(St udent[],int)
int maxgradestudent (Student [],int)


//classes declaration

class Person
{
public:
Person(void);
~Person(void);
void Setfirstname(ch ar []);
char * Getfirstname(vo id);
void Setlastname(cha r []);
char * Getlastname(voi d);

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<numStude nts;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(f irstName);
studentInfo[i].Setlastname(la stName);
studentInfo[i].Setnid(NID);
studentInfo[i].SetGrade(stude ntGrade);

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

ave= averagegrade(st udentInfo[i],ave);

//maximum call function :

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

cout << "\n\n";

for (i=0;i<numStude nts;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()<<"gra de:"<<studentIn fo[i].GetGrade()<<"\ n";


}

//functions

//constructor
}

Person::Person( void)
{
strcpy(Firstnam e,"");
}

//destructor

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

//function for first name

void Person::Setfirs tname(char firstname[])
{
strcpy(Firstnam e,firstname);
}

//function for Last name

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

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

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

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

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

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

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


//function for grade:

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

float Student::GetGra de(void)
{
return grade;
}

//function for average grade:

?
Dec 1 '06 #1
1 2133
DeMan
1,806 Top Contributor
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
28599
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
3143
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 all the fields from two tables, each with about 20 fields. I then have a statistics form that has the query as its source and the form has 273 textboxes on, each with a calculation based on the underlying query values. The form looks like a...
3
1495
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: http://www.douglas-county.com/employment/currentopenings2.asp Our old site uses classic ASP with JavaScript syntax. I'm in the process of developing a new site that uses XML/XSLT/CSS/ASP.NET/VB.NET, but in the meantime, we still have our old site up. It contains a lot of...
3
2317
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
3595
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 accomplish it. Thanks, Scott
3
17926
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?? (using Dev-C++ compiler) Please see task and attempt below: TASK : 1.8.1 Exercise 1A - Temperature tracker Write a program that tracks temperatures during an experiment that takes 24 hours. The user should input the temperature. This...
3
9039
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 performance monitoring subsystem and can be accessed via the System.Diagnostics.PerformanceCounter object. I haven't found an obvious way to get to the Minimum, Maximum, Average values for a counter. Thanks.
2
2643
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 the maximum value, minimum value and average of all elements. I'm stuck on the first one :/ I've practiced calling another function to find the maximum, and that seemed to work fine. I get a number in the millions when trying to do this all in one...
53
12130
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 breaks just to get it right. I find in general a prescriptive rule makes for markedly less readable code, which unfortunately is a subjective argument, however, the waste of time modifying code when it does not need to is not.
0
9568
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9399
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10163
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10007
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8832
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6649
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5423
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3924
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2806
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.