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
|
#include <iostream>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
using namespace std;
class Student
{
private:
double grades[];
public:
char name[64];
int numgrades;
Student(){};
void input(const char namea[], const int lenname, double gradea[], int lengrade, int counter)
{
memcpy(Student::name, namea, lenname+1);
for (int i = 0; i<lengrade; i++)
{
grades[i] = gradea[i];
}
numgrades = counter;
}
double mean()
{
double sum = 0.0;
for (int i = 0; i<numgrades; i++)
{
sum += grades[i];
}
return sum/((double)(numgrades));
}
void disp();
};
void Student::disp()
{
cout<<"name: "<<Student::name<<endl;
cout<<"average: "<<mean()<<endl;
}
int main()
{
char namelong[256];
cout<<"Enter name: ";
gets(namelong);
char name1[strlen(namelong)];
memcpy(name1, namelong, strlen(namelong)+1);
double *gradesa;
gradesa = (double *)malloc(sizeof(double));
cout<<"Enter grades: \n";
int counter = 0;
double currgrade = 0.0;
while (currgrade != -1.0)
{
double *temp = (double *)realloc(gradesa, 8*(counter+1)*sizeof(double));
gradesa = temp;
cin>>currgrade;
if (currgrade != -1.0)
{
gradesa[counter] = currgrade;
counter++;
}
}
Student dakota;
dakota.input(name1, strlen(namelong), gradesa, counter, counter);
dakota.disp();
return 0;
}
| |