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;   }
whats the output of the following program? #include <iostream> using namespace std; int main () {...
whats the output of the following program? #include <iostream> using namespace std; int main () { int fistNum = 28; int secondNum = 25; cout << firstNum << " " << secondNum << end1; cout << (firstNum = 38-7) << end1; cout << (firstNum <= 75) << end1; cout << (firstNum > secondNum + 10) << end1; cout << (firstNum >= 3 * secondNum - 100) << end1; cout << (secondNum - 1 == 2 * firstNum) << end1; cout...
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; }
What will be the output of the following code? #include <iostream> using namespace std; int main()...
What will be the output of the following code? #include <iostream> using namespace std; int main() {    char *s = "hello";    char *p = s;    cout << *(p+3) << " " << s[1] << endl; }
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...
What is the exact output of the following program? using namespace std; const int MAX =...
What is the exact output of the following program? using namespace std; const int MAX = 9; int main() {     int list[MAX];     list[0] = 1;     list[1] = 2;     for (int i = 2; i < MAX; i++)     {         list[i] = list[i-1] * list[i-2];         if (i > 5)         {             list[i] = list[i] - list[i-1];         }     }     for (int i = 0; i < MAX; i=i+1)     {         cout...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT
Active Questions
  • describe the three fiscal policy tools at the government's disposal to stimulate or contract the economy....
    asked 10 minutes ago
  • i) Write the condensed electron configurations for the following ions: (a) Ca+ (b) S2- (c) V2+...
    asked 10 minutes ago
  • In Java a function can call itself(we may explore more about this later in the course).  This...
    asked 23 minutes ago
  • Write a short program,Java, that implements the Caesar substitution cipher with k=3. That is, each letter...
    asked 25 minutes ago
  • Design a function named findMax that accepts two integer values as arguments and returns the value...
    asked 31 minutes ago
  • In MIPS, I am trying to create an array of 7 words that can either be...
    asked 32 minutes ago
  • Cash, $2,000 Accounts Receivable, $1,250 Professional Equipment, $10,200 Office Equipment, $5,500 Accounts Payable, $3,500 P. Palmer,...
    asked 33 minutes ago
  • Add two more statements to main() to test inputs 3 and -1. Use print statements similar...
    asked 34 minutes ago
  • C++ program called that reads a string and check if it’s well-formed or not. ex== The...
    asked 45 minutes ago
  • 1: Describe five functions of nonverbal communication. (Creating and maintaining relationships, Regulating Interaction, Influencing others, Concealing/deceiving,...
    asked 58 minutes ago
  • Trucks that travel on highways have to stop at various locations to be weighed and inspected...
    asked 1 hour ago
  • Define and describe the PERT technique. For the toolbar, press ALT+F10 (PC) or ALT+FN+F10 (Mac).
    asked 1 hour ago