PDA

View Full Version : help plz, Any idea!


djnaf
03-10-04, 05:52 PM
i'm writing a c++ program
that takes an information on N students (N<=100)
Calculates the average of each one: the weight of the different exams are ( First Exam 25%, seconf exam 25%, final exam 50%)
find the name of the student wo received the best average
calculate the average of teh class

i must use a structer called STUDENT with 4 fields.
i'll paste what i've written unil now, the program is not fully done
so i need ut help in it plz. tell me what i forgot to add.

and i want u to help me how to add two fuctions:
-A function Find_Max with two parameters: an array of struct student and an integer representing the number of the students in the class.
-A function Find_Average with two parameters an array of struct student and an integer representing the number of students in the class.
-A function Display_Class with two parameters: a pointer to the first item in an array of struct student and an integet representing the number of students in the class.

=======================================
#include<iostream.h>
struct student
{
int number;
char name[10];
float grades[3];
float avg;
};

void main()
{
int n;
cout<<"enter the number of students: ";
cin>> n;

}
void Get_Student(student *s)
{
cout<<"Name: ";
cin>> s->name;
cout<<"Number: ";
cin>> s->number;
cout<<"First Grade: ";
cin>> s->grades[0];
cout<<"Second Grade: ";
cin>> s->grades[1];
cout<<"Final Grade: ";
cin>> s->grades[2];
}
void Get_Class(student s[], int n)
{
int i;
for (i=0;i<n;i++)
{
Get_Student(&s[i]);
}
}
void Display_Student(student s[], int i)
{
cout<<"student no:"<<s[i].number<< endl;
cout<<"student name:"<<s[i].name << endl;
cout<<"student 1st grade:"<<s[i].grades[0] << endl;
cout<<"student 2nd grade:"<<s[i].grades[1] << endl;
cout<<"student 3rd grade:"<<s[i].grades[2] << endl;
cout<<"the avrege for student:"<<s[i].avg << endl;
}
void Display_Class(student *s, int n) /* i think this function is wrong*/
{
for(int i=0;i<n;i++){
Display_Student(&s[i]);
}
}

wtd
03-11-04, 11:39 PM
That is a weird bastardized C/C++, not C++.

#include <iostream>
#include <string>
#include <vector>
#include <stdlib.h>

struct student
{
int number;
std::string name;

student(int init_number, std::string init_name = "")
: number(init_number), name(init_name) { }
};

struct grade_set : public std::vector<float>
{
float sum() const
{
float accumulator = 0;
for (iterator i = begin(); i != end(); i++)
accumulator += *i;
return accumulator;
}

float average() const
{
return sum() / size();
}

float max_grade()
{
float max = 0.0;
for (iterator i = begin(); i != end(); i++)
if (max < *i) max = *i;
return max;
}
};

struct gradable_student : student
{
grade_set grades;

gradable_student(int init_number, std::string init_name = "", grade_set init_grades = grade_set())
: student(init_number, init_name), grades(init_grades) { }

void get_info(std::ostream& out, std:istream& in)
{
float temp_grade;

out << "Student #? ";
in >> number;
out << "Student Name? ";
in.getline(name);
out << "First Grade? ";
in >> temp_grade;
grades.push_back(temp_grade);
out << "Second Grade? ";
in >> temp_grade;
grades.push_back(temp_grade);
out << "Third Grade? ";
in >> temp_grade;
grades.push_back(temp_grade);
}

friend std::ostream& operator<<(std::ostream& out, const gradable_student& gs);
};

std::ostream& operator<<(std::ostream& out, const gradable_student& gs)
{
out << "Student #: " << gs.number
<< "Student Name: " << gs.name;
int count = 1;
for (typename grade_set::iterator i = gs.grades.begin(); i != gs.grades.end(); i++)
out << "Grade " << count << ": " << *i << std::endl;
return out;
}

struct gradable_student_collection : std::vector<gradable_student>
{
int number_of_students() const
{
return size();
}

gradable_student student_with_max_grade() const
{
gradable_student gs = gradable_student(0);
for (iterator i = begin(); i != end(); i++)
if (gs.grades.max_grade() < i->grades.max_grade())
gs = *i;
return gs;
}

gradable_student student_with_max_average() const
{
gradable_student gs = gradable_student(0);
for (iterator i = begin(); i != end(); i++)
if (gs.grades.average() < i->grades.average())
gs = *i;
return gs;
}

float sum_of_averages() const
{
float sum= 0.0;
for (iterator i = begin(); i != end(); i++)
sum += i->grades.average();
return sum;
}

float average() const
{
return sum_of_averages() / number_of_students();
}

friend std::ostream& operator<<(std::ostream& out, const gradable_student_collection& gsc);
};

std::ostream& operator<<(std::ostream& out, const gradable_student_collection& gsc)
{
for (typename gradable_student_collection::iterator i = gsc.begin(); i != gsc.end(); i++)
out << *i;
return out;
}

int main(int argc, char ** argv)
{
int number_of_students;
std::cout<<"enter the number of students: ";
std::cin>> n;
}

hyjacked
03-19-04, 05:00 PM
edit: posted before reading wtd's post...

Well for starters your main should do more than just get the number of students. It should then call the function to get in the student information, and then call any other functions that you want. I would also store the student struct as an array in your program. that way you can deal with more than one at a time. Any other data structure(ie: linked list) would also serve the purpose of this.

Hope this gives you some help.

i'm writing a c++ program
that takes an information on N students (N<=100)
Calculates the average of each one: the weight of the different exams are ( First Exam 25%, seconf exam 25%, final exam 50%)
find the name of the student wo received the best average
calculate the average of teh class

i must use a structer called STUDENT with 4 fields.
i'll paste what i've written unil now, the program is not fully done
so i need ut help in it plz. tell me what i forgot to add.

and i want u to help me how to add two fuctions:
-A function Find_Max with two parameters: an array of struct student and an integer representing the number of the students in the class.
-A function Find_Average with two parameters an array of struct student and an integer representing the number of students in the class.
-A function Display_Class with two parameters: a pointer to the first item in an array of struct student and an integet representing the number of students in the class.

=======================================
#include<iostream.h>
struct student
{
int number;
char name[10];
float grades[3];
float avg;
};

void main()
{
int n;
cout<<"enter the number of students: ";
cin>> n;

}
void Get_Student(student *s)
{
cout<<"Name: ";
cin>> s->name;
cout<<"Number: ";
cin>> s->number;
cout<<"First Grade: ";
cin>> s->grades[0];
cout<<"Second Grade: ";
cin>> s->grades[1];
cout<<"Final Grade: ";
cin>> s->grades[2];
}
void Get_Class(student s[], int n)
{
int i;
for (i=0;i<n;i++)
{
Get_Student(&s[i]);
}
}
void Display_Student(student s[], int i)
{
cout<<"student no:"<<s[i].number<< endl;
cout<<"student name:"<<s[i].name << endl;
cout<<"student 1st grade:"<<s[i].grades[0] << endl;
cout<<"student 2nd grade:"<<s[i].grades[1] << endl;
cout<<"student 3rd grade:"<<s[i].grades[2] << endl;
cout<<"the avrege for student:"<<s[i].avg << endl;
}
void Display_Class(student *s, int n) /* i think this function is wrong*/
{
for(int i=0;i<n;i++){
Display_Student(&s[i]);
}
}