You are on page 1of 2

Week 1: Overview of C++ and Programs to demonstrate C++ Arrays and Classes,

call by value and call by reference.


scription: the objective is to review the concepts of C++ necessary in Data Structures

Resource: Visual C++ compiler.

1.1 Source Code:


1.3.1 Program 1:

#include <iostream>
using namespace std;
class student {
private:
char name[5];
int grade[3];
int max;
double avg;
public:
void getinfo(); //gets info from the user
void findmax();//finds the maximum number in the array
void findavg();//finds the average of numbers in the array
void printinfo();//prints the results
};
void student::getinfo() {
cout << "please enter your name: " << endl;
cin >> name;
cout << "please enter 3 grades: " << endl;
for (int i = 0; i<3; i++)
cin >> grade[i];
}
void student::findmax() {
max = grade[0];
for (int i = 0; i < 3; i++)
if (grade[i] > max)
max =grade[i];

}
void student::findavg() {
int sum = 0;
for (int i = 0; i < 3; i++)
sum += grade[i];
avg = (sum / 3);

}
void student::printinfo() {
cout << "student name is: " << name << endl;
cout << "student max grade is : " << max << endl;
cout << "student average is: " << avg << endl;
}
int main() {
student s1;
s1.getinfo();
s1.findmax();
s1.findavg();
s1.printinfo();
return 0;
}

1.3.2 Program 2:

#include <iostream>
using namespace std;
void by_ref(int &value)
{
value = 6;
}
void by_val(int value)
{
value = 16;
}
int main()
{
int value = 5;

cout << "value = " << value << '\n';


by_ref(value);
cout << "value = " << value << '\n';
int value2 = 10;
cout << "value2 = " << value2 << '\n';
by_val(value2);
cout << "value2 = " << value2 << '\n';
return 0;
}

You might also like