Question

Parameter Passing:

Use the following parameter passing methods:

1) Call by reference

2) Call by value

Consider the following function: Check Sums (A,B,C) where A is a two-dimensional (2D) array of size [1:n, 1:n) and B and Care

0 0
Add a comment Improve this question Transcribed image text
Answer #1

// PassByValueCode.cpp

#include <iostream>
#include <array>
using namespace std ;

int CheckSums( array<int, 10> A, array<int, 10> B, array<int, 10> C)
{
   array <int, 10> arrD;
   int value = 0;
  
   for(int i = 0; i < 10 ; i++){
       arrD[i] = A[i]^B[i]^C[i];
   }
  
   for(int i = 0; i < 10; i++){
       value = value ^ arrD[i];
   }
   return value;
}

int main()
{
   array <int, 10> arrA = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 };
   array <int, 10> arrB = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 };
   array <int, 10> arrC = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 };

   CheckSums(arrA ,arrB, arrC);
   return 0;
}

// PassByRefCode.cpp

#include <iostream>
#include <array>
using namespace std ;

int CheckSums( int * A, int* B, int *C )
{
   int arrD[10];
   int value = 0;
  
   for(int i = 0; i < 10 ; i++){
       arrD[i] = A[i]^B[i]^C[i];
   }
  
   for(int i = 0; i < 10; i++){
       value = value ^ arrD[i];
   }
   return value;
  
}

int main()
{
   int arrA[10] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 };
   int arrB[10] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 };
   int arrC[10] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 };

   CheckSums(arrA ,arrB, arrC);
   return 0;
}

// Below is memory comparison with Linux OS x86 System

PassByValueCode : 17136 Byte
PassByValueRef :   17000 Bytes

// Below is execution time comparions:

PassByValueCode : 11 ms
PassByValueRef : 8 ms

Add a comment
Know the answer?
Add Answer to:
Parameter Passing: Use the following parameter passing methods: 1) Call by reference 2) Call by value...
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. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Call stack question! Long one... The call stack is part of main memory that is reserved...

    Call stack question! Long one... The call stack is part of main memory that is reserved for function calling. Like all r memory it is finite, so can be exhausted resulting in a stack overflow. Recursive functions allocate space on the stack for each recursive call: if there are many such recursive calls a stack overflow can result. The questions that follow ask you to investigate recursive functions and stack overflows. Note that when running programs in the Linux terminal...

  • T F a) Void Functions can use reference parameters. T F b) Arguments corresponding to reference p...

    T F a) Void Functions can use reference parameters. T F b) Arguments corresponding to reference parameters can be variables only T F c) The C++ statement retum "hello can be used with an integer value-retuming function T F d) Static variables maintain their value from function call to function call. T F e) The components of a C++ array must be homogeneous T F fA hierarchical structure has at least one component that is itself a structure. T F...

  • 1. Write a recursive function that computes the sum of all numbers from 1 to n,...

    1. Write a recursive function that computes the sum of all numbers from 1 to n, where n is given as parameter. Here is the method header: public static int sum (int n){...} 2. Write a recursive function that finds and returns the minimum value in an array, where the array and its size are given as parameters. Here is the method header: public static int minValue (int [] data, int size){...} 3. Write a recursive function that reverses the...

  • 1. All functions should be written AFTER the main procedure. 2. A function prototype should be...

    1. All functions should be written AFTER the main procedure. 2. A function prototype should be written for each function and placed BEFORE the main procedure. 3. Each function should have a comment explaining what it does. 4. Each function parameter should have a comment explaining the parameter. 5. Prompt for the number of elements of the list. 6. Allocate an array whose size equals the number of elements specified by the user. 7. Read into the array the elements...

  • Write a MATLAB function with the following specifications: Input parameter 1: A Input parameter 2: B...

    Write a MATLAB function with the following specifications: Input parameter 1: A Input parameter 2: B Input parameter 3: C Input parameter 4: D Output parameter: Sum of the input parameters Time interval: -2T lessthanorequalto t lessthanorequalto 2T Waveform 1 to plot: y(t) = A sin(B(t - c))+D Waveform 2 to plot: y(t) = A cos(B(t - c)) + D Display two plots in vertical fashion on one figure Include all labeling on both plots. Add a grid to both...

  • Question 1. (1 marks) The following procedure has an input array A[1..n] with n > 2...

    Question 1. (1 marks) The following procedure has an input array A[1..n] with n > 2 arbitrary integers. In the pseudo-code, "return” means immediately erit the procedure and then halt. Note that the indices of array A starts at 1. NOTHING(A) 1 n = A. size 2 for i = 1 ton // i=1,2,..., n (including n) 3 for j = 1 ton // j = 1,2,...,n (including n) 4. if A[n - j +1] + j then return 5...

  • C++,please help. I have no idea how to do that begining with zero parameter constructor. and...

    C++,please help. I have no idea how to do that begining with zero parameter constructor. and help me to test the method, Write a class template Square Matrix that implements a square matrix of elements of a specified type. The class template should be fully in the Square Matrix.h file. The class uses "raw" pointers to handle the dynamically allocated 2D-array. "Raw" pointers are the classic pointers that you've used before. In addition to it, you will need a variable...

  • Program Overview This brief exercise is designed for you to consider how reference variables behave when...

    Program Overview This brief exercise is designed for you to consider how reference variables behave when you are passing them as arguments by-value. Instructions Name your class References.java. 1. Implement the following methods. 1.1 Method name: readStudentNames Purpose: This method accepts an array of strings as a parameter. It loops through the array and asks the user to input names. It populates the array of strings with the names. Parameters: an array of Strings, stringArray Return type: void In the...

  • 1.You are to write a program name search.java that will do the following: 2.You are to...

    1.You are to write a program name search.java that will do the following: 2.You are to create 3 arrays - prompt the user for a number that is greater than 100 that will serve as the size for the arrays (all 3 arrays will have the same user input size). Call them A, B & C. 3.Generate this amount of random numbers to fill these arrays – the random numbers must range from 1 to 99. 4.Write 1 sequential search...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT