How to assign an array to every element of another array
Mar 29, 2014 at 7:26pm UTC
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
#define MAX 100
#include<iostream>
#include<stdlib.h>
#include<string.h>
using namespace std;
template <class T>
class Array
{
private :
T Names[MAX];
int size;
public :
Array();
void GetInfo();
void PrintInfo();
};
template <class T>
Array<T>::Array()
{
size=0;
}
template <class T>
void Array<T>::GetInfo()
{
int i;
do {
cout << "Enter total number of students: " ;
cin >> size;
}while (size<1 || size>MAX);
for (i=0; i<size; i++)
{
cout << "\n\nStundent #" <<i+1;
cin >> Names[i];
}
}
template <class T>
void Array<T>::PrintInfo()
{
int i;
if (size>0)
{
cout << "\n\n" ;
for (i=0; i<size; i++)
cout << '\t' << Names[i];
cout << "\n\n" ;
}
else
cout << "\n There are no students saved" ;
}
class Student
{
private :
int Grade;
char Name[64];
public :
Student();
Student(int , char *);
friend istream &operator >> (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<Student> school;
school.GetInfo();
system("cls" );
school.PrintInfo();
system("PAUSE" );
return EXIT_SUCCESS;
}
Mar 30, 2014 at 9:48pm UTC
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 :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
#include <iostream>
#include <string>
#include <vector>
#include <iomanip>
using std::string;
using std::vector;
class Student
{
private :
string name_;
vector<int > 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<int > 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<Student> 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.