How to assign an array to every element of another array

Hello everyone, I have been given an assignment which I understand pretty well, but I have a problem, and haven't been able to find a clear solution anywhere. I'm sort of a beginner in all this, so it's hard to understand what some people say.

So basically what I wanna do is assign a set of grades to every element in an array of a set number of students. I already have the part where you ask for the number of students you want to enter and then ask for their names and grades, but it can only enter one grade, and I can't figure out how to assign several grades and then get the average.

Here's my code, the assignment said to do it like this.

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105`` ``````#define MAX 100 #include #include #include using namespace std; template class Array { private: T Names[MAX]; int size; public: Array(); void GetInfo(); void PrintInfo(); }; template Array::Array() { size=0; } template void Array::GetInfo() { int i; do{ cout << "Enter total number of students: "; cin >> size; }while (size<1 || size>MAX); for (i=0; i> Names[i]; } } template void Array::PrintInfo() { int i; if (size>0) { cout << "\n\n"; for (i=0; i> (istream &, Student &); friend ostream &operator << (ostream &, Student &); }; Student::Student() { } Student::Student(int Grd, char Nm[]) { Grade=Grd; strcpy(Name, Nm); } istream &operator >> (istream &Get, Student &ObjSt) { cout << "\n\nEnter name: "; Get >> ObjSt.Name; cout << "\nEnter grade: "; Get >> ObjSt.Grade; return Get; } ostream &operator << (ostream &Print, Student &ObjSt) { Print << "Student's information\n"; Print << "\nName: " << ObjSt.Name; Print << "\nGrade: " << ObjSt.Grade<<"\n\n\n"; return Print; } int main() { Array school; school.GetInfo(); system("cls"); school.PrintInfo(); system("PAUSE"); return EXIT_SUCCESS; }``````
I'm not evil, but your program is horrible. You are trying to create an array, your own? Pff. Use, at least, vectors! I wrote an simple program that do the same of your does, but with some C++11:
 ``12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576`` ``````#include #include #include #include using std::string; using std::vector; class Student { private: string name_; vector grades_; public: Student(string name__, unsigned size) : name_(name__), grades_(size){}; int size() const {return grades_.size();} string name() const { return name_; } float average() const { float ret = 0; for(auto i : grades_) ret += i; //C++11 return ret / grades_.size(); } int& grade(unsigned index) { if(index > grades_.size()-1) throw nullptr; return grades_[index]; } vector grades() { return this->grades_; } }; int main() { unsigned number; unsigned grade_numbers; std::cout << "Enter the number of students.\n> "; std::cin >> number; std::cout << std::endl << "How much grades?\n> "; std::cin >> grade_numbers; vector school; //Input for(unsigned i = 0; i < number; i++) { string name; std::cout << std::endl << "Enter student #" << i+1 << " name.\n> "; std::cin >> name; school.push_back(Student(name, grade_numbers)); for(unsigned a = 0; a < grade_numbers; a++) { unsigned grade; std::cout << std::endl << "Enter grade #" << a+1 << " for student #" << i+1 << ".\n> "; std::cin >> grade; school[i].grade(a) = grade; } } std::cout << std::endl; //Output - needs C++11 int instance = 1; for(auto i : school) { int inst = 1; std::cout << "Student #" << instance << " (" << i.name() << "):\n"; for(auto a : i.grades()) { std::cout << "Grade #" << inst << " : " << a << std::endl; inst++; } std::cout << "Average: " << std::setprecision(3) << i.average() << "\n\n"; instance++; } }``````
Topic archived. No new replies allowed.