Question

Analyze the following program and write down the output. # include <iostream> using namespace std;    void...

Analyze the following program and write down the output.

# include <iostream>

using namespace std;

   void modifyArray( int [ ], int );

   void modifyElement ( int );  

   int main( )

{

  const int arraySize = 8;

  int a[arraySize] = { 2, -2, 10, -3, -1 ,0, 10, -5 };

     modifyArray ( a, arraySize);

     for ( int i =0; i < arraySize; i++)

              cout << a[i] << ‘  ’;

  

     modifyElement ( a[4] );

   

     for ( int i =0; i < arraySize; i++)

            cout << a[i] << ‘  ’;

     

    cout << endl;

     

     return 0;

}

void modifyArray ( int b[ ], int sizeOfArray )

{

  for ( int k = 0; k < sizeOfAarray; k++)

         b[k] *=2;

}

void modifyElement ( int e )

{

  e *= 2;

}

Homework Answers

Answer #1

4 -4 20 -6 -2 0 20 -10 4 -4 20 -6 -2 0 20 -10

Explanation:

void modifyArray ( int b[ ], int sizeOfArray )

{

  for ( int k = 0; k < sizeOfAarray; k++)

         b[k] *=2;

}

modifyArray method recieves an array b and and integer sizeofArray . In this method we iterate fro 0th index to sizeofArray and multiply each element by 2. So when modifyArray ( a, arraySize); is called in main, array a gets modified such that each element of array is now twice of itself.

void modifyElement ( int e )

{

  e *= 2;

}

modifyElement recieves an integer e and multipplies it by 2 and assigns it to e. But when modifyElement ( a[4] ); is called, a[4] doesnt get modified. Here is why

modifyArray ( a, arraySize): Here a is passed, which means base address(address of first element). is passed. After that each element is accessed by computing address( base + offset ) This is nothing but Pass by reference. In Pass by reference, if value gets changed in the called function, it will reflect in caller function also.So when array elements are modified in modifyArray , it gets eflected in main.

modifyElement ( a[4] ); Here a[4] is passed, which means a copy of the a[4] is passed to modifyElement. This is Pass by value. In Pass by value, if value gets changed in the called function, it will not reflect in caller function also.So when a[4] is mdified in modifyElement it will not reflect in main.

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
in C++ Need a heap-sort function #include <iostream> #include <stdlib.h> #include <string> using namespace std; void...
in C++ Need a heap-sort function #include <iostream> #include <stdlib.h> #include <string> using namespace std; void MyFunc ( int *array ) { // Your code here ----------------- } int main(int argc,char **argv) { int *Sequence; int arraySize; // Get the size of the sequence cin >> arraySize; // Allocate enough memory to store "arraySize" integers Sequence = new int[arraySize];    // Read in the sequence for ( int i=0; i<arraySize; i++ ) cin >> Sequence[i]; // Run your algorithms to...
Quick sort func in C++ #include <iostream> #include <stdlib.h> #include <string> using namespace std; void MyFunc...
Quick sort func in C++ #include <iostream> #include <stdlib.h> #include <string> using namespace std; void MyFunc ( int *array ) { // Code here } int main(int argc,char **argv) { int *Sequence; int arraySize; // Get the size of the sequence cin >> arraySize; // Allocate enough memory to store "arraySize" integers Sequence = new int[arraySize];    // Read in the sequence for ( int i=0; i<arraySize; i++ ) cin >> Sequence[i]; // Run your algorithms to manipulate the elements...
Analyze the following programs and write down the output of the program. Please print every character...
Analyze the following programs and write down the output of the program. Please print every character (including whitespace character) clearly!       # include <iostream> using namespace std;   int fun( int a ) {       int b = a * 2;       return b;   }   int main()   {       int y = 5;       cout << fun(y) << endl;       cout << fun(-- y) << endl;       cout << fun(y--) << endl;       cout << y <<endl;           return 0;   }
Rewrite the following program using switch statements. //Set 7.1 #include <iostream> #include <iomanip> using namespace std;...
Rewrite the following program using switch statements. //Set 7.1 #include <iostream> #include <iomanip> using namespace std; int main() {        int x;        cout << "Selection option " << endl;        cin >> x;        if (x == 1)               cout << "You select option 1" << endl;        else if (x >= 2 || x <= 4)               cout << "You select options 2 or 3 or 4" << endl;               else if (x == 10)               cout << "You select option 10" << endl;               else...
Take the following program and translate it into PEP/9 assembly language: #include <iostream> using namespace std;...
Take the following program and translate it into PEP/9 assembly language: #include <iostream> using namespace std; int theArray[] = { 5, 11, -29, 45, 9, -1}; void sumPos(int ary[], int len, int &sum) {    sum = 0;    for (int i = 0; i < len; i++)            if (ary[i] > 0)                sum = sum + ary[i]; } int main() {    int total;    sumPos(theArray, 6, total);    for (int k=0; k < 6; k++)      cout...
How to trace a c++ program by hand #include<iostream> using namespace std;    class Test {...
How to trace a c++ program by hand #include<iostream> using namespace std;    class Test {     int value; public:     Test(int v); };    Test::Test(int v) {     value = v; }    int main() {     Test t[100];     return 0; } _______________ #include <iostream> using namespace std; int main() { int i,j; for (i=1; i<=3; i++) { for(j=1; j<=i; j++ ) { cout<<"*"; } cout << "\n";   } return 0; }
Consider the following program: #include #include #include using namespace std; int main() { int num1; int...
Consider the following program: #include #include #include using namespace std; int main() { int num1; int num2; cout << fixed << showpoint << setprecision(2); cout << "Enter two integers: "; cin >> num1 >> num2; cout << endl; if (num1 != 0 && num2 != 0) cout << sqrt(abs(num1 + num2) + 0.5) << endl; else if (num1 != 0) cout << floor(num1 + 0.5) << endl; else if (num2 != 0) cout << ceil(num2 + 0.5) << endl; else...
This code it's not working, fix it for me please #include <iostream> using namespace std; class...
This code it's not working, fix it for me please #include <iostream> using namespace std; class People {    string name;    double height; public:    void setName(string name)    {        this->name = name;    }    void setHeight(double height)    {        this->height = height;    }    double getHeight() {        return height;    }    string getName()    {        return name;    } }; int main() {    const int size...
C++ #include<iostream> #include<string> #include<fstream> #include<cstdlib> using namespace std; const int ROWS = 8; //for rows in...
C++ #include<iostream> #include<string> #include<fstream> #include<cstdlib> using namespace std; const int ROWS = 8; //for rows in airplane const int COLS = 4; void menu(); //displays options void displaySeats(char[][COLS]); void reserveSeat(char [ROWS][COLS]); int main() { int number=0; //holder variable char seatChar[ROWS][COLS]; for (int i = 0; i < ROWS; i++) { for (int j = 0; j < COLS; j++) { seatChar[i][j] = '-'; } } int choice; //input from menu bool repeat = true; //needed for switch loop while (repeat...
1. Type in the following program. Compile and run. #include <iostream> using namespace std; int main()...
1. Type in the following program. Compile and run. #include <iostream> using namespace std; int main() { ​cout << "Hello, world." << endl; } Open a Word Document and copy and paste the program and the output. 2. Create a new program: #include <iostream> using namespace std; int main() { ​float num1; ​float num2 ​float avg; ​cout << "Enter two numbers" << endl; ​cin >> num1 >> num2; ​avg = num1 + num2/2; ​cout << "The average is " <<...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT