Question

In: Computer Science

Write a program (main.cpp) to test the following: 1) Static allocation a. Create two integer variables...

  • Write a program (main.cpp) to test the following:

1) Static allocation

a. Create two integer variables x and y, initialize them with different values.

b. Use static memory allocation, declare px and py as address of x and y separately.

c. Print out x, y, px, py, &x, &y, *px, *py.  

d. Let py = px, and *py = 100

e. Print out x, y, px, py, &x, &y, *px, *py.

g. Print out *px++, x, px

2) Dynamic allocation and memory leak

Repeat the same steps of (2), by dynamic allocation. Make sure you deallocate pointers to avoid memory leaks.

3)Swap values

a. Create three swap functions that implement pass-by-value, pass-by-reference, pass-by-pointer

b. In the main function, test each function if they are swapping the values or not.

  • Compile, run to generate output file

Solutions

Expert Solution

Here, we will discuss all the 3 questions one by one:

Note: For each question, I have explained everything in the comments of the program. So, please refer to it for understanding and clarity.

  • Here I have written all the three programs separately for better clarity. You can choose to merge if you need as per your requirement.

1.

Code(With explanation):

#include<bits/stdc++.h>
using namespace std;

int main()
{
    int x = 10, y = 50;  // Created two variables and declared it with different values

    int *px = &x, *py = &y; // Declared pointer as address of x and y separately

    //Print out x, y, px, py, &x, &y, *px, *py
    cout << "x: " << x << endl;
    cout << "y: " << y << endl;
    cout << "px: " << px << endl;
    cout << "py: " << py << endl;
    cout << "&x: " << &x << endl;
    cout << "&y: " << &y << endl;
    cout << "*px: " << *px << endl;
    cout << "*py: " << *py << endl;

    // py = px, and *py = 100
     py = px;
    *py = 100;

    // Print out x, y, px, py, &x, &y, *px, *py
    cout << "\nx: " << x << endl;
    cout << "y: " << y << endl;
    cout << "px: " << px << endl;
    cout << "py: " << py << endl;
    cout << "&x: " << &x << endl;
    cout << "&y: " << &y << endl;
    cout << "*px: " << *px << endl;
    cout << "*py: " << *py << endl;

    // Print out *px++, x, px
    cout << "\n*px++: " << *px++ << endl;
    cout << "x: " << x << endl;
    cout << "px: " << px << endl;


    return 0;
}

Output/Code-run:

2.
Memory leaks occur when a new memory is allocated dynamically and never deallocated.

Code(With explanation):

#include <iostream>
using namespace std;


// Declared a class for use
class Plant {
   public:
      Plant() {
         cout << "Constructor!" <<endl;
      }
      ~Plant() {
         cout << "Destructor!" <<endl;
      }
};

int main() {

    // Dynamic memory allocation with memory leak
   cout << "\With memory leak: " << endl;
   Plant* myPlantArray = new Plant[4];

   // Dynamic memory allocation without memory leak
   cout << "\nWithout memory leak: " << endl;
   Plant* myPlantArray2 = new Plant[4];
   delete[] myPlantArray2;

   return 0;
}

Output/Code-run:

3.

Code(With explanation):

#include<bits/stdc++.h>
using namespace std;

// Swap function - Pass by value
void swapPassByValue(int a, int b)
{
   int temp;

   temp = b;
   b = a;
   a = temp;
   cout << "After swap, value of a :" << a << endl;
   cout << "After swap, value of b :" << b << endl;
}

// Swap function - Pass by reference
void swapPassByReference(int &x, int &y) {
   int temp;
   temp = x; /* save the value at address x */
   x = y;    /* put y into x */
   y = temp; /* put x into y */

   return;
}

// Swap function - Pass by pointer
void swapPassByPointer(int* a, int* b) {
    int temp;
    temp = *a;
    *a = *b;
    *b = temp;
}

int main()
{
    // Declaring two variables which we want to swap
   int a = 100;
   int b = 200;

   // Pass by value

   cout << "Pass by value: \n" << endl;
   cout << "Before swap, value of a :" << a << endl;
   cout << "Before swap, value of b :" << b << endl;

   // After calling pass by value, the value of a = 100 and b = 200, for the next step and we have printed its in the function itself.

   /* calling a function to swap the values using pass by value.*/
   swapPassByValue(a, b);

   // Pass by reference

   cout << "Pass by reference: \n" << endl;
   cout << "Before swap, value of a :" << a << endl;
   cout << "Before swap, value of b :" << b << endl;

   /* calling a function to swap the values using pass by reference.*/
   swapPassByReference(a, b);

   cout << "After swap, value of a :" << a << endl;
   cout << "After swap, value of b :" << b << endl;

   // Pass by Pointer

   cout << "Pass by pointer: \n" << endl;
   cout << "Before swap, value of a :" << a << endl;
   cout << "Before swap, value of b :" << b << endl;


   /* calling a function to swap the values using pass by pointer.*/
   swapPassByPointer(&a, &b);

   cout << "After swap, value of a :" << a << endl;
   cout << "After swap, value of b :" << b << endl;

    return 0;
}


Output/Code-run:

Please let me know in the comments in case of any confusion. Also, please upvote if you like.


Related Solutions

1) Dynamic Allocation (c++) a. Create two integer variables x and y, initialize them with different...
1) Dynamic Allocation (c++) a. Create two integer variables x and y, initialize them with different values. b. Use dynamic memory allocation, declare px and py as address of x and y separately. c. Print out x, y, px, py, &x, &y, *px, *py.   d. Let py = px, and *py = 100 e. Print out x, y, px, py, &x, &y, *px, *py. g. Print out *px++, x, px
Write a program that asks the user to type in two integer values. Test these two...
Write a program that asks the user to type in two integer values. Test these two numbers to determine whether the first is evenly divisible by the second and then display the appropriate message to the terminal. Objective C
Write a program to do the following. • Input an integer n. • Create a BST...
Write a program to do the following. • Input an integer n. • Create a BST S inserting the keys 1, 2, . . . , n in that order, which will result in a completely-skewed tree. • Measure the time to search for n + 1 in S. • Display the time taken for search. /** * Exception class for access in empty containers * such as stacks, queues, and priority queues. * @author Mark Allen Weiss */ public...
Write a program to do the following. • Input an integer n. • Create a BST...
Write a program to do the following. • Input an integer n. • Create a BST B containing the same items, but inserting them in an order that will yield the tree balanced. The following algorithm, known as pre-order traversal, gives you a strategy to produce that sequence. seq(low, high, T){    if (low <= high){        mid = (low+high)/2        T.insert(mid)        seq(low, mid-1, T)        seq(mid+1, high, T)    } } • Measure the...
C++ Memory Allocation: 1) Write a C++ program that allocates static, stack, & heap memory. Your...
C++ Memory Allocation: 1) Write a C++ program that allocates static, stack, & heap memory. Your program does not need to do anything else.  Indicate via comments where memory for at least one variable in each memory area is allocated. a) Write code that allocates static memory (only include one declaration): b) Write code that allocates stack memory (only include one declaration): c) Write code that allocates heap memory (only include one declaration): 2) Edit the C++ program below to include...
Write C++ program to do the following: 1. Create integer array size of 10 2. Ask...
Write C++ program to do the following: 1. Create integer array size of 10 2. Ask user input the values of the array's element using for loop 3. pass the array to void function. in void function do the following: a. Find the maximum of the array. b. Compute the element average c. Find out how many numbers are above the average d. Find out and print how many numbers are below the average e. find out how many numbers...
write a simple program to demonstrate the use of static type of variables in c++... use...
write a simple program to demonstrate the use of static type of variables in c++... use comments to explain plz
JAVA PROGRAMMING 1)BuildLists: Write a program to create an ArrayList<Integer>. Fill it with numbers from 1...
JAVA PROGRAMMING 1)BuildLists: Write a program to create an ArrayList<Integer>. Fill it with numbers from 1 to 1000. Then remove every even number. Then remove every multiple of 3 remaining Then remove every multiple of 5 Then remove every multiple of 7 Then sum the array, and print.
C++ Program 1. Declare an integer static array a[ ] with 100 elements. 2. Declare an...
C++ Program 1. Declare an integer static array a[ ] with 100 elements. 2. Declare an integer pointer p. 3. Let p pointing to the array a[ ]. 4. Use p (you have to use p) to put 0 into the first element of this array, 2 into the second element, 4 into the 3rd element, 6 into the 4th element, ... 198 into the 100th element of this array. 5. Use a (you have to use a) to display...
Write the null and alternative hypothesis for a two-tailed test for each of the following variables:...
Write the null and alternative hypothesis for a two-tailed test for each of the following variables: (10 points) Student Ht Wt Age (Yrs) Shoe Size Waist Size Pocket Change 01 64 180 39 07 36 018 02 66 140 31 09 30 125 03 69 130 31 09 25 151 04 63 125 36 07 25 011 05 68 155 24 08 31 151 06 62 129 42 06 32 214 07 63 173 30 08 34 138 08 60...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT