Question

In: Computer Science

Question 3 A. Show the output of the following code. #include <iostream> using namespace std; void...

Question 3

A. Show the output of the following code.

#include <iostream>

using namespace std;

void magic (int &a, int b, int& c)

{

  a *= 2;

   b = b+2;

   c = c-2;

}

int main ()

{

int x=1, y=3, z=7;

   magic (x, y, z);

cout << "x=" << x << ", y=" << y << ", z=" << z;

   magic (z, y, x);

cout << "x=" << x << ", y=" << y << ", z=" << z;

   return 0;

}

What is the parameter passing scheme for a, b and c in magic( ) function ?

B. Show the output of the following code.

#include <iostream>

using namespace std;

void printarray (int arg[], int length) {

   for (int n=length-1; n>=0; n--)

cout << arg[n] << " ";

   cout << endl;

}

int main ()

{

int firstarray[] = {3, 15, 10};

   int secondarray[] = {2, 4, 6, 8, 10};

   printarray (firstarray,2);

   printarray (secondarray,3);

   return 0;

}

Solutions

Expert Solution

Question 3 A):

#include <iostream>
using namespace std;
void magic (int &a, int b, int& c)
{
a *= 2;
b = b+2;
c = c-2;
}
int main ()
{
   int x=1, y=3, z=7;
magic (x, y, z);
cout << "x=" << x << ", y=" << y << ", z=" << z;
magic (z, y, x);
cout << "x=" << x << ", y=" << y << ", z=" << z;
return 0;
}

Screesnhots:

Output:

The parameter passing mechanism is both call by value and call by reference too.

Since in magic() method x reference(address) and z reference(addess) is used, the function magic() for x and z is call by reference method.

Since in magic() method y is used directly (formal parameter) , the function magic() for y is call by value method.

Parameter scheme for x is call by reference.

Parameter scheme for y is call by value.

Parameter scheme for z is call by reference.

Quesiton 3 B):

#include <iostream>
using namespace std;
void printarray (int arg[], int length)
{
for (int n=length-1; n>=0; n--)
cout << arg[n] << " ";
cout << endl;
}
int main ()
{
   int firstarray[] = {3, 15, 10};
int secondarray[] = {2, 4, 6, 8, 10};
printarray (firstarray,2);
printarray (secondarray,3);
return 0;
}

Screeshots:

Output:


Related Solutions

What is the output of the following program? #include <iostream> using namespace std; void showDouble(int); //Function...
What is the output of the following program? #include <iostream> using namespace std; void showDouble(int); //Function prototype int main() { int num; for (num = 0; num < 10; num++) showDouble(num); return 0; } // Definition of function showDouble void showDouble(int value) { cout << value << ‘\t’ << (value * 2) << endl; } Please do the following Program and hand in the code and sample runs. Write a program using the following function prototypes: double getLength(); double getWidth();...
Starting with the following C++ program: #include <iostream> using namespace std; void main () { unsigned...
Starting with the following C++ program: #include <iostream> using namespace std; void main () { unsigned char c1; unsigned char c2; unsigned char c3; unsigned char c4; unsigned long i1 (0xaabbccee); _asm { } cout.flags (ios::hex); cout << "results are " << (unsigned int) c1 << ", " << (unsigned int) c2 << ", " << (unsigned int) c3 << ", " << (unsigned int) c4 << endl; } Inside the block denoted by the _asm keyword, add code to...
--- TURN this Code into Java Language --- #include <iostream> #include <string> using namespace std; //...
--- TURN this Code into Java Language --- #include <iostream> #include <string> using namespace std; // constants const int FINAL_POSITION = 43; const int INITIAL_POSITION = -1; const int NUM_PLAYERS = 2; const string BLUE = "BLUE"; const string GREEN = "GREEN"; const string ORANGE = "ORANGE"; const string PURPLE = "PURPLE"; const string RED = "RED"; const string YELLOW = "YELLOW"; const string COLORS [] = {BLUE, GREEN, ORANGE, PURPLE, RED, YELLOW}; const int NUM_COLORS = 6; // names...
I need the following code completed: #include <iostream> #include <time.h> #include "CSVparser.hpp" using namespace std; //============================================================================...
I need the following code completed: #include <iostream> #include <time.h> #include "CSVparser.hpp" using namespace std; //============================================================================ // Global definitions visible to all methods and classes //============================================================================ // forward declarations double strToDouble(string str, char ch); // define a structure to hold bid information struct Bid { string bidId; // unique identifier string title; string fund; double amount; Bid() { amount = 0.0; } }; // FIXME (1): Internal structure for tree node struct Node {    Bid data; Node *left; Node...
#include <iostream> #include <cmath>using namespace std; const int A_CONSTANT = 3; void functionA(int a[], int aNumber);...
#include <iostream> #include <cmath>using namespace std; const int A_CONSTANT = 3; void functionA(int a[], int aNumber); void functionB(int a[], int anotherNumber); void functionC(const int anArray[], int aNumber); void functionD(int& sum); int functionE(double number); void functionF(int n); int main( ){ int production[A_CONSTANT]; cout << "This program displays a graph showing\n" << "production for each factory in the company.\n"; functionA(production, A_CONSTANT); functionB(production, A_CONSTANT); functionC(production, A_CONSTANT); return 0;} void functionA(int a[], int aNumber){ for (int someNumber = 1;someNumber <= aNumber; someNumber++) { cout...
Array based application #include <iostream> #include <string> #include <fstream> using namespace std; // Function prototypes void...
Array based application #include <iostream> #include <string> #include <fstream> using namespace std; // Function prototypes void selectionSort(string [], int); void displayArray(string [], int); void readNames(string[], int); int main() {    const int NUM_NAMES = 20;    string names[NUM_NAMES];    // Read the names from the file.    readNames(names, NUM_NAMES);    // Display the unsorted array.    cout << "Here are the unsorted names:\n";    cout << "--------------------------\n";    displayArray(names, NUM_NAMES);    // Sort the array.    selectionSort(names, NUM_NAMES);    //...
In C++, assuming you have the following incomplete code: #include<iostream> #include <unistd.h> using namespace std; //...
In C++, assuming you have the following incomplete code: #include<iostream> #include <unistd.h> using namespace std; // Structure for storing the process data struct procData { char pname[5]; // Name of a process int arrivt; //Arrival time of a process int pburst; // Burst time of a process int endtime; // Exit time/ Leaving time of a process int remburst; // Remaining burst time of a process int readyFlag; // boolean, Flag for maintaining the process status }; // Global variable...
write the algorithm for this the code?!. #include<iostream> using namespace std; #include<string.h> int main() { char...
write the algorithm for this the code?!. #include<iostream> using namespace std; #include<string.h> int main() { char plain[50], cipher[50]="", decrypt[50]=""; int subkeys[50], len;       cout<<"Enter the plain text:"<<endl; cin>>plain;    cout<<"Enter the first subkey:"<<endl; cin>>subkeys[0];    _strupr(plain);    len = strlen(plain);    /**********Find the subkeys**************/    for(int i=1; i<len; i++) { if ((plain[i-1]>='A') && (plain[i-1]<='Z')) { subkeys[i] = plain[i-1]-65; } }    /****************ENCRYPTION***************/       for(int i=0; i<len; i++) { if ((plain[i]>='A') && (plain[i]<='Z')) {    cipher[i] = (((plain[i]-65)+subkeys[i])%26)+65; }...
#include<iostream> #include<cmath> using namespace std; int p = 7; void main() { extern double var ;...
#include<iostream> #include<cmath> using namespace std; int p = 7; void main() { extern double var ; int p = abs(-90); cout << ::p + p - var << endl; system("pause"); } double var = 5.5;
Complete the C++ code #include <iostream> #include <stdlib.h> #include <time.h> using namespace std; struct Cell {...
Complete the C++ code #include <iostream> #include <stdlib.h> #include <time.h> using namespace std; struct Cell { int val; Cell *next; }; int main() { int MAX = 10; Cell *c = NULL; Cell *HEAD = NULL; srand (time(NULL)); for (int i=0; i<MAX; i++) { // Use dynamic memory allocation to create a new Cell then initialize the // cell value (val) to rand(). Set the next pointer to the HEAD and // then update HEAD. } print_cells(HEAD); }
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT