Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Create a C++ Program that does the following: Purpose of this lab is to familiarize yourself with dynamic 1D and 2D arrays. One Dimensional Dynamic

Create a C++ Program that does the following:

Purpose of this lab is to familiarize yourself with dynamic 1D and 2D arrays.

One Dimensional Dynamic Array Creation:

1. Prompt the user for the number of elements (n) in the 1D array of pointers. 2. Dynamically create the 1d array of int pointers that can hold n elements (pointers). Examples:
  • 2 elements would result in a array with 2 rows..
  • 3 elements would result in a array with 3 rows.
  • 16 elements would result in a array with 16 rows ... and so on. 

Two Dimensional Dynamic Array Creation:

3. Prompt the user for the number of elements (n) in the 2D array, then dynamically create an [m][2] array of int pointers that can hold n elements (pointers). Do the math for m Examples:
  • 2 elements would result in a [1][2] array, therefore m = 1.
  • 3 elements would result in a [2][2] array, therefore m = 2.
  • 16 elements would result in a [8][2] array, , therefore m = 8 ... and so on.

Populate both One and Two Dimensional Dynamic Arrays by creating NEW ints:

4. create a function int* createInt(). This function will prompt the user for a int value and dynamically a create a new integer and return a pointer to it. Note: This function will be used later to populate both 1d and 2d arrays.

5. create a function void populate1D( pass the array of pointers). This function will loop through the entire array, calling createInt(); storing the pointer returned from createInt() into the 1d Array.

6. create a function void populate2D( pass the array of pointers). This function will loop through the entire array, calling createInt(); storing the pointer returned from createInt() into the 2d Array.

7. create a function void print1D( pass the array of pointers). This function will loop through the entire array and print the integer that is pointed to by each element (i.e. print the int).

8. create a function void print2D( pass the array of pointers). This function will loop through the entire array and print the integer that is pointed to by each element (i.e. print the int).

9. Finally, call both print methods within your int main.

Use the below integer values for both arrays.

1 2 3 4 5 6 7 8

Output:

1D Array: [1,2,3,4,5,6,7,8] 2D Array: [1,2] [3,4] [5,6] [7,8]

Upload .cpp/.h files, along with two screenshots (1) showing input of 2D array, (2) showing the output of both print methods.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered 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

Students also viewed these Databases questions