Easy Programming

Void Functions with Arrays in C++:

Welcome to my 20th tutorial of Easy Programming in C++. In this tutorial, I show you how to use the Void Function a bit more by introducing Arrays into the mix as well as explain a bit more about how to reference variables through the parameter list.

I use the parallel arrays tutorial here as the base and work around that and convert that into a void function. I actually use two void functions and add on an extra equation to the program. It's very simple and easy. If you have any questions about the program or functions in general, feel free to ask.

I hope you enjoy the video and if you have any requests feel free to let me know.

Click here to go back

//Programmer:	Nazmus
//Program:		Void Function Tutorial #2 - Arrays in Functions
//Website:		www.EasyProgramming.net

#include <iostream>
#include <string>

using namespace std;

void calc(int test1[], int test2[], float avg[]);
void total(float avg[], float & totalavg);

int main()
{
	string name[3];
	int test1[3];
	int test2[3];
	float avg[3];
	float totalavg;
	int i;

	for(i=0;i<=2;i++){

		cout << "Please enter student's name: ";
		cin >> name[i];

		cout << "Please enter first test score: ";
		cin >> test1[i];

		cout << "Please enter second test score: ";
		cin >> test2[i];
	}

	calc(test1, test2, avg);
	total(avg, totalavg);

	cout << endl;

	cout << "Name" << "          " << "Average" << endl;

	for(i=0;i<=2;i++){
		cout << name[i] << "          " << avg[i] << endl;
	}	

	cout << endl << endl;

	cout << "The overall average of the grades is: " << totalavg << '.' << endl << endl;

	system("pause");
}

void calc(int test1[], int test2[], float avg[]){

	int i;
	for(i=0;i<=2;i++){
		avg[i] = (test1[i] + test2[i]) / 2;
	}

}

void total(float avg[], float & totalavg)
{
	int i;
	float total;
	total = 0;

	for(i=0;i<=2;i++)
	{
		total=total + avg[i];
	}

	totalavg = (total/3);
}