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;   }
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...
Please write variables and program plan (pseudocode) of the C++ programming below: #include <iostream> #include <cmath>...
Please write variables and program plan (pseudocode) of the C++ programming below: #include <iostream> #include <cmath> using namespace std; void divisors(int num); int main () {    char repeat;    int num;       while (repeat !='n')    {        cout << "Enter a number: ";        cin >> num;        divisors(num);        cout << "Continue? (y or n): ";        cin >> repeat;    }    return 0; } void divisors(int num) {   ...
I have an error but i can't correct it #include <iostream> using namespace       std; long reverse...
I have an error but i can't correct it #include <iostream> using namespace       std; long reverse (long       num, long   equation,long reverse = 0); int       main() {               long       num, reverse = 0;        cout << "Enter       the       num:       ";        cin >> num;        cout << "Reverse       num       is       =       "               << reverse << endl;        return       0; } long reverse(long       num, long equation, long reverse = 0) {        while (num)        {               equation...
Please write variables and program plan (pseudocode) of the C++ programming code below: #include <iostream> #include...
Please write variables and program plan (pseudocode) of the C++ programming code below: #include <iostream> #include <cmath> using namespace std; int getWeight(); float deliveryCharge(int weight); void displayCharge(int weight); int main () {    displayCharge(getWeight());    return 0; }   int getWeight() {    int weight;    cout << "Enter package weight (oz): ";    cin >> weight;    return weight; } float deliveryCharge(int weight) {    if (weight > 16)    {        return (((weight - 16) / 4) *...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT