Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

SET-252 C Programming #2 Homework: 2 New and Delete Readings: Chapter 4: Pointers and the Free Store. Passing Parameters By Reference In C++ In C++

SET-252 C Programming #2

Homework: 2 New and Delete

Readings: Chapter 4: Pointers and the Free Store.

Passing Parameters By Reference In C++

In C++ simple data types (e.g. short, int, long, etc) and structures are passed by value. Arrays are passed by reference. Its really challenging to pass an array by value but we are going to learn how to do that this semester (along with being able to use the assignment operator with strings).

To pass a simple data type or structure by reference in C++ you can use an ampersand (&) in front of the parameter name in the procedure definition (and prototype declaration). The ampersand modifies the parameter not the data type. For example:

void main( )

{

int intValue = 0;

PassByReference( intValue );

cout << The new value is: << intValue << endl; // Prints 5

}

void PassByReference( int &intValue )

{

intValue = 5; // Changes original

}

Isnt that great! So much easier than using pointers.

Homework:

Step 1

Write a single program that does the following.

Prompt the user for an array size. Use a small number when testing (e.g. 5 or 6).

Dynamically allocate an array of that size and store the address in a pointer.

palngValues = new long[ 5 ];

Write a for loop that reads in numbers from the user and stores them in the array.

Write a for loop that prints out the numbers in the array.

Delete the memory used by the array and set the pointer equal to 0.

Step 2

Modify the code from the previous step. I dont need the code from Step 1. Submit the code for just Step 2.

Make a procedure called MakeArray that takes two parameters by reference: palngValues, lngArraySize. In the procedure prompt the user to enter a size. The size must be from 1 to 100. Loop until a valid size is entered. Allocate an array of the size specified, initialize it and assign it to the pointer passed in. Call this procedure from main.

Make a procedure called PopulateArray that takes two parameters by value: a pointer to the array and the array size. In the procedure prompt the user to enter a value for each location in the array. Call this procedure from main.

Make a procedure called PrintArray that takes two parameters by value: a pointer to the array and the array size. In the procedure print out the value for each location in the array. For each value indicate the location. For example: Location [ 1 ] = 42. Call this procedure from main.

Make a procedure called DeleteArray that takes a pointer to the array by reference. In the procedure delete the memory allocated for the array and set the array pointer variable to 0. Call this procedure from main.

In the main print out the address stored by the array pointer to show that it was reset to 0 after calling the DeleteArray procedure

Double check your code to make sure it runs without errors and that you followed all the coding standards.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Application Development And Design

Authors: Michael V. Mannino

1st Edition

0072463678, 978-0072463675

More Books

Students also viewed these Databases questions

Question

Show that standard Brownian motion is a Martingale.

Answered: 1 week ago