OK I am having trouble with inline functions. Every website I go too either shows how to use void, or a variable, but never both.
The program is suppose to have two functions. 1 to calculate average of grades, and 2 display statistics of grade ranges.
In the code you see below, I first created the code to run, with only one function that calculates the average. Now I am trying to improve it by moving the statistical readout to a function, but I can't figure it out. please help
void someFunction( int &a, int &b , int &c , int &ect....)
{
//you are now passing the actual object and not a copy
//so when you modify here the one being passed is modified.
a = 10; //a is now 10 outside the function
};
Ok so I read about reference and and pointers..and I can't figure out how to apply eithe rof them to my code.. I'm more confused now. Can somebody give me a meaningful example that uses a function with more than one variable to output from function like my code above..
Can someone help who is willing to teach and not brush me off by saying go read about pointers or references..etc..
thank you
The examples I read that I couldn't figure out how to apply are the following
#include <iostream>
#include <ctime>
usingnamespace std;
// function to generate and retrun random numbers.
int * getRandom( )
{
staticint r[10];
// set the seed
srand( (unsigned)time( NULL ) );
for (int i = 0; i < 10; ++i)
{
r[i] = rand();
cout << r[i] << endl;
}
return r;
}
// main function to call above defined function.
int main ()
{
// a pointer to an int.
int *p;
p = getRandom();
for ( int i = 0; i < 10; i++ )
{
cout << "*(p + " << i << ") : ";
cout << *(p + i) << endl;
}
return 0;
}
#include <iostream>
usingnamespace std;
constint MAX = 3;
int main ()
{
int var[MAX] = {10, 100, 200};
int *ptr[MAX];
for (int i = 0; i < MAX; i++)
{
ptr[i] = &var[i]; // assign the address of integer.
}
for (int i = 0; i < MAX; i++)
{
cout << "Value of var[" << i << "] = ";
cout << *ptr[i] << endl;
}
return 0;
}
#include <iostream>
#include <ctime>
usingnamespace std;
double vals[] = {10.1, 12.6, 33.1, 24.1, 50.0};
double& setValues( int i )
{
return vals[i]; // return a reference to the ith element
}
// main function to call above defined function.
int main ()
{
cout << "Value before change" << endl;
for ( int i = 0; i < 5; i++ )
{
cout << "vals[" << i << "] = ";
cout << vals[i] << endl;
}
setValues(1) = 20.23; // change 2nd element
setValues(3) = 70.8; // change 4th element
cout << "Value after change" << endl;
for ( int i = 0; i < 5; i++ )
{
cout << "vals[" << i << "] = ";
cout << vals[i] << endl;
}
return 0;
}