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 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205
|
# include <iostream>
# include <cstring>
#include <iomanip>
#include <cmath>
using namespace std;
class Course // Creating the class Course
{private:
public:
char CourseName[10]; // Array of size 10, 9 characters and 1 null terminator
int StudentNumber; // Number of students enrolled in the class
double Grade[30]; //Grade Array of size 30, the maximum number of students that can be in the class
double Average;
Course ()
{ //Default constructor
CourseName[0]='\0';
StudentNumber=8;
for(int i=0; i<StudentNumber;i++)
Grade[i]=0.0;
Average=0.0;
;}
Course(char CName[],int SNumber) //Counstructor: CNAME for Course Name and SNumber for Student Number
{
setCourseName (CName); // Function setCourseName takes as argument the array CName
setStudentNumber (SNumber); // Function setStudentNumber takes as argument the integer value of SNumber provided in the main function
for(int i=0; i<StudentNumber;i++) // Initializing the whole array to 0.0 (double)
Grade[i]=0.0;
Average=0.0; // Defaulting average to 0.0 (double)
}
void setCourseName(char CName[]) //Function setCourseName takes an argument of type array of chars
{ CourseName[0]='\0'; //Defaulting the string to empty
strncpy_s(CourseName,CName,9); //Copying from the tring provides to the data member CourseName[]. Only 9 characters
CourseName[9]='\0'; //Putting the null terminator in the 10 case of the string
}
void setStudentNumber (int SNumber) //Function definition
{
StudentNumber=(SNumber<8 || SNumber>30?8:SNumber); // Using the conditional terminator to set the StudentNumber value to 8 if the conditions were true, and to take the value provided if false
}
char* getCourseName (void) //Return type is a pointer to an array, (to be able to point to the whole string)
{
return CourseName;
}
int getNumberStudent (void) //Returns StudentNumber and takes no argument
{
return StudentNumber;
}
double getAverage (void) //Returns a double value of Average and takes no argument
{
return Average;
}
void GradeFiller (void) // SNumber is the number of students in the class=number of grades
{
int i=0; // Initializing i to 0 to use it in the While loop
while (i<StudentNumber) // StudentNumber would change if we ever change the value through the program, making the change easier to apply
{
Grade[i]=rand()%101; // Rand will provide numbers between 0 and 100 in each cell of the array
i++;
}
}
void GradeChanger(int k, double grade) // k is the index and grade is the grade provided
{
int main; // main will take the value of k
double note; // note will take the value of grade
if(k>=0 && k<=29) //Checking for the validity of the index
main=k;
else main=0;
note=(grade>=0 && grade<=100?k:0.0); //Checking for the validity of the grade
Grade[main]=note;
}
double AverageCalculator (void)
{
double sum=0.0; // Creating a variable sum and assigning 0.0 (double) to it
for(int i=0;i<StudentNumber;i++)
sum+=Grade[i]; // Calculating the value of sum
return (static_cast<double> (Average)=sum/StudentNumber); // returning the value of Average being double
}
double StandardDeviation (void)
{
double StdDvtn; // The formula being too complex to calculate, I separated it into many pieces.
double top=0.0; // The top represents the Numerator of the function under the Square Root
if (Average>=0 && Average <=100) // Checking for Average calculation
{
for (int i=0;i<StudentNumber;i++) // for loop, to induce a loop to make the program run over again incrementing i by 1, and thus getting the value of top
top= top +pow((Grade[i]-Average),2) ;
StdDvtn= sqrt(top/(StudentNumber-1));
return StdDvtn;
}
else return 0;
}
int GradeChecker (void) // Doesnt take any argument, as it is a member function it has direct access to the data members
{
int counter=0;
for (int i=0;i<StudentNumber;i++)
{
if (Grade[i]>75) // Checking each grade's value if it is > then 75 to incrememnt the counter if true
counter++;
return counter; // The counter is the number of grades above 75
}
}
void printClass (void)
{
cout<<"Name of the course: "<<CourseName<<endl;
cout<<"Number of Students: "<<StudentNumber<<endl;
cout<<"The Grades of the students are: "<<endl;
for (int i=0;i<StudentNumber;i++)
{
cout<<Grade[i]<<endl;
}
cout<<"The average of the class is: "<<Average;
cout<<"The standart deviation is: "<<StandardDeviation();
}
};
int main ()
{
Course Class[3] = {
{"Bio", 13, 0.0, 0.0 }, { "Chimio", 23, 0.0, 0.0 }, { "Phisio", 22, 0.0,0.0 }
};
Course a1;
for (int i=0;i<3;i++)
{
Class[i].setCourseName("Hello");
Class[i].setStudentNumber(10);
Class[i].GradeFiller();
}
Course *Ptr=Class;
cout<<"Informations about the 2nd course: "<<endl;
(Ptr+1)->printClass();
cout<<"Informations about the 3rd course: "<<endl;
(Ptr+2)->printClass();
cout<<"The Average of class 1 is: "<<Ptr->AverageCalculator();
cout<<"The Average of class 2 is: "<<(Ptr+1)->AverageCalculator();
cout<<"The Average of class 3 is: "<<(Ptr+2)->AverageCalculator();
cout<<"The Std Deviation for the class 3 is: "<<(Ptr+2)->StandardDeviation();
a1.Grade[23]=30;
cout<<"The Course Name of class 1 is : "<<Ptr->getCourseName();
cout<<"Number of students in class 1 is : "<< Ptr->getNumberStudent();
cout<<"Number of grades >75: "<<Ptr->GradeChecker();
return 0; // Indicates the end of the program
}
|