Question

(C++) In your main, create a bunch of character variables and use them to print out...

(C++) In your main, create a bunch of character variables and use them to print out a word (similar to the last on-paper exercise you did with “google”). Now write a function that uses call by value, call by reference, and call by pointer (it doesn’t have to be in that order) to modify the characters to new characters. Back in main, print out your new word. Note that you can’t use the examples used in the last on-paper exercise.

Homework Answers

Answer #1

// Code

#include <stdio.h>
#include <iostream>
using namespace std;

// call by reference
void callByRefer(char& a, char& b, char& c, char& d, char& e)
{
a = 'a';
b = 'n';
c = 'g';
d = 'e';
e = 'l';
  
cout<<"value inside callByRefer: "<<a<<b<<c<<d<<e << endl;
}

// call by pointer
void callByPointer(char* a, char* b, char* c, char* d, char* e)
{
char p = *a;
*a = *b;
*b = p;
}

// call by reference
void callByValue(char a, char b, char c, char d, char e)
{
a = 'h';
b = 'u';
c = 'm';
d = 'a';
e = 'n';
cout<<"value inside callByValue: "<<a<<b<<c<<d<<e << endl;
}

int main()
{
char d = 'd';
char e = 'e';
char v = 'v';
char i = 'i';
char l = 'l';
  
cout<<"Initial State :"<<endl;
cout <<d<<e<<v<<i<<l << endl;
cout<<"\ncall by reference"<<endl;
callByRefer(d,e,v,i,l);
cout <<d<<e<<v<<i<<l << endl;
  
cout<<"\ncall by value"<<endl;
callByValue(d,e,v,i,l);
cout <<d<<e<<v<<i<<l << endl;

cout<<"\ncall by pointer"<<endl;
callByPointer(&d,&e,&v,&i,&l);
cout <<d<<e<<v<<i<<l << endl;
  
  
return 0;
}

// Output:

/*

Initial State :
devil

call by reference
value inside callByRefer: angel
angel

call by value
value inside callByValue: human
angel

call by pointer
nagel


*/

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Convert this C++ program exactly as you see it into x86 assembly language: // Use the...
Convert this C++ program exactly as you see it into x86 assembly language: // Use the Irvine library for the print function #include <iostream> // The string that needs to be printed char word[] = "Golf\0"; // Pointer to a specific character in the string char * character = word; //NOTE: This main() function is not portable outside of Visual Studio void main() { // Set up a LOOP - See the while loop's conditional expression below int ecx =...
IN MIPS ASSEMBLY Macro File: Create macros to print an int, print a char, print a...
IN MIPS ASSEMBLY Macro File: Create macros to print an int, print a char, print a string, get a string from the user, open file, close file, read file, and allocate heap memory. You can use more macros than these if you like. Main Program File: Allocate 1024 bytes of dynamic memory and save the pointer to the area. The main program is a loop in which you ask the user for a filename. If they enter nothing for the...
C++ Create a program that will use pointers to determine the average (to 1 decimal place)...
C++ Create a program that will use pointers to determine the average (to 1 decimal place) of a series of grades entered into an array. You can assume a maximum of 15 students and the user will end input with a sentinel value. Make sure to use pointer increment and a pointer comparison while loop, and not array notation or array index numbers or offsets. You will use a function called getgrades. Send the array name (a pointer constant) to...
The goal in this exercise is to develop a program that will print out a list...
The goal in this exercise is to develop a program that will print out a list of student names together with other information foreach. The tab character (an escape sequence) is helpful in getting the list to line up nicely. A program with only two names is in the file Names.java. // ************************************************************ // Names.java // // Prints a list of student names with their hometowns // and intended major // ************************************************************ public class Names { // -------------------------- // main...
C++ See the provided specification files. Complete the implementation for each as a separate file. void...
C++ See the provided specification files. Complete the implementation for each as a separate file. void seen(std::string); If there is already a Word object in the Words list, then the number of occurrences for this word is incremented. If there is no Word object for this word already, create a new word object with occurrence =1, and insert this object into the list of Word objects. std::string getNextWord(); Returns the next word of the list and sets the currentItem pointer...
Assignment 1. Console Application IN PYTHON For this assignment you are to create a class called...
Assignment 1. Console Application IN PYTHON For this assignment you are to create a class called Lab1. Inside the class you will create three methods that will modify a three-digit whole number: sumNums - This method takes as input a three digit int and returns the sum of the three numbers. For instance, 123 would return 6 because 3 + 2 + 1 is 6 reverseNums - This method takes as input a three digit whole number and returns the...
Complete this in C++ and explain what is being done. 1      Introduction The functions in the...
Complete this in C++ and explain what is being done. 1      Introduction The functions in the following subsections can all go in one big file called pointerpractice.cpp. 1.1     Basics Write a function, int square 1(int∗ p), that takes a pointer to an int and returns the square of the int that it points to. Write a function, void square 2(int∗ p), that takes a pointer to an int and replaces that int (the one pointed to by p) with its...
Use the following algorithm: (1) Create three stacks of characters: stk1, stk2, stk3. (2) Read the...
Use the following algorithm: (1) Create three stacks of characters: stk1, stk2, stk3. (2) Read the input infix expression one character at a time and push every character read ( other than ')' ) onto stk1. Do not push the character read if it is ')'. (3) As long as stk1 is not empty, do the following:            Fetch the top character ( call it c ) of stk1 and pop stk1.            if c is an alphabetic character (a-z),...
Leave comments on code describing what does what Objectives: 1. To introduce pointer variables and their...
Leave comments on code describing what does what Objectives: 1. To introduce pointer variables and their relationship with arrays 2. To introduce the dereferencing operator 3. To introduce the concept of dynamic memory allocation A distinction must always be made between a memory location’s address and the data stored at that location. In this lab, we will look at addresses of variables and at special variables, called pointers, which hold these addresses. The address of a variable is given by...
using System; public static class Lab5 { public static void Main() { // declare variables int...
using System; public static class Lab5 { public static void Main() { // declare variables int inpMark; string lastName; char grade = ' '; // enter the student's last name Console.Write("Enter the last name of the student => "); lastName = Console.ReadLine(); // enter (and validate) the mark do { Console.Write("Enter a mark between 0 and 100 => "); inpMark = Convert.ToInt32(Console.ReadLine()); } while (inpMark < 0 || inpMark > 100); // Use the method to convert the mark into...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT