Programming C++ involving averaging using voiding?

closed account (172v0pDG)
I don't quite get how to use void function in order to organize a program, and not sure how I can use it with this one. Basically I need to to create a program to gather the averages of students and their tests that the user requests, take that average and make it a grade character. One function needs to be called "newgrade" which would apparently use a "pass by reference" (I would like a explanation of what that means) that contains the total number of points for one student at a time, then has a "pass by value" (again I don't know what that means) which has the number of scores to that point, adds the new test score to a total and creates a numerical average. After that make a new void function that takes the average and makes a it into a grade character and prints it out in the program. I would like to know what the point of using void functions is and what those terms and mean, and how to complete this problem.

Here's what I got so far for my solution

#include <iostream>
#include <iomanip>
using namespace std;


int main()
{
int numstudents, numtests;
double total, average;


cout << fixed << showpoint << setprecision(1)

cout << "This program will tell you the averages of the/n";
cout << "number of students that you entered in/n";
cout << "How many students are there/n";
cin >> numstudents;
while (numstudents <= 0)
{
cout >> "Please enter a postive number";
cin >> numstudents;
}

cout << "How many grades are there for each student/n";
cin >> numtests;
while (numtests <= 0)
{
cout >> "Please enter a postive number";
cin >> numtests;
}

for (int test = 1; test <= numstudents; test++)
{
total = 0;
for (int test = 1; test <= numtests; test++)
{
double score;
cout << "Enter score " << test << " for ";
cout << "student " << numstudents << ": ";\
cin >> score;
total += score;
}
average = total / numtests;
cout << "The average score for student " << numstudents;
cout << " is " << average << ".\n\n";
}


system("pause");
return 0;
}
Tackling your separately, the 'point' of a void function could be many things. Maybe an output function that output's to the screen or a file. Also, some things, like arrays can't be returned via the return statement. (Though arrays are special cases.)

As far as passing-by-reference goes...

There a two ways of passing values to a function, "pass by reference" and "pass by value".

Pass-by-value is a one-way trip into your function. You can take that value, do whatever you want with it, and that value stays inside your function.

When you pass a variable-by-reference into your function, when that function ends, the value gets sent back to the calling function.

Here is an example of a useful pass-by-reference function: Swap:
1
2
3
4
5
6
void swap( int &x, int &y)
{
        int tmp = x;
        x = y;
        y = tmp;
}


swap takes two integers, and with the help of a temporary int variable, swaps x and y. Then, since we are passing by reference, x and y return back to the calling function.

So here with this main calling swap
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
using namespace std;

void swap( int &x, int &y)
{
        int tmp = x;
        x = y;
        y = tmp;
}

int main() {
        int val1 = 10;
        int val2 = 20;
        cout << val1 << " " <<  val2 << endl;
        swap(val1, val2)
        // val1 = 20 and val2 = 10
        cout << val1 << " " <<  val2 << endl;
}


Your output is this:

10 20
20 10
Last edited on
Topic archived. No new replies allowed.