f. Write the definition of function main that tests each of these functions. To test nextCharByVal
and nextCharByRef, initialize z to the space character (‘ ‘) then use a loop to print out the next 94 characters after the space character.
To help you get started think of the character as a numerical value such as the ability to add an integer value to a character.
I don't want to give the code to you right away since you need to at the least try to write your own code and submit it here if you have problems so that we can see where in your code you are struggling.
Try to write the program and submit it and I would be glad to help you break the code down.
To start you might just want to play the the algorithm in the main() first and then separate the code into the necessary functions.
When you pass by reference you pass the memory address of the variable so if the function that you pass by reference to changes the value of the variable in the argument then it changes the value of the original value passed.
When you pass by value you pass the value of the variable, so when the function that accepts the variable in its argument will have only the value. If the function changes the value of the variable in its argument than the original variable that was passed in the parameter list will not have its value changed.
#include<iostream>
usingnamespace std;
int byValue(int byValueVariable)
{
return byValueVariable + 10;
}
void byRef(int &byRefVariable)//the "&" means the variable used is passed by reference
{
byRefVariable = 100;
}
int main()
{
int yourNum = 10;
cout << "yourNum = " << yourNum << endl; ///yourNum should equal 10
int newNum = byValue(yourNum); //this will give newNum the value of 20 since yourNum = 10 and was passed to function that added 10 to it
cout << "newNum = " << newNum << endl;
cout << "yourNum = " << yourNum << endl; ///yourNum should equal 10 since it was passed by value
byRef(yourNum);
cout << "yourNum = " << yourNum << endl; ///yourNum should equal 100 since it was passed by reference
return 0;
}
Wow, OUJI thank you so much. You explained it in a way that I can actually understand.
I hate to ask for any more, but do you think you could show me how to print the next 94 characters after the main value, from part f of the problem?
I will give you a hint. Create a for loop to iterate 94 times and during each iteration you need to change the original value of your char or add the iterate variable you created to use in the for loop.