Homework Code Help


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.
Last edited on
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.
Here is all I can figure out.
Can someone walk me through how to apply this to the 3 instructions?

1
2
3
4
5
6
7
8
9
10
11
#include <iostream>
using namespace std;
void main()
{
char charac;
cout << "Enter your letter : " << endl;
cin>>charac;
cout << "The next letter is : " << endl;
cout << (char)(charac+1) << endl;
system("pause");
}
now you need to create the functions for nextCharByVal (), nextCharByRef(). and use the code you have written and place it within the functions.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>

using namespace std;

char nextCharByVal (char yourChar)
{
     return yourChar + 1;
}

int main()
{
     char yourChar = 'A';

     cout << "Your character equals " << yourChar << endl;

     yourChar =  nextCharByVal(yourChar);

     cout << "Your character now equals " << yourChar << endl;

     return 0;
}


now take the work that I have done for you and research how to pass a variable by reference since i did the by value
Sorry, I don't understand the difference between the functions nextcharbycal and nexcharbyref.
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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33

#include<iostream>

using namespace 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;
}

Last edited on
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.

one way is:
1
2
3
4
5
6
7
8

char yourChar = 'A';

for(int i = 1; i <= 94; i++)
{
    cout << (yourChar + i) << endl;
}

Topic archived. No new replies allowed.