Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

There are two types of arrays in C++, dynamic and static. Dynamic arrays can gro

ID: 3597156 • Letter: T

Question

There are two types of arrays in C++, dynamic and static. Dynamic arrays can grow in size while the

program is executing, whereas static arrays cannot. Their size must be known before runtime.

Because dynamic arrays can grow during runtime, it is unnecessary to estimate their maximum size

before compile time. You just grow their size as needed.

3. Definitions

We will define several terms that you need to know to understand arrays. They are as follows:

1. The name of a

dynamic array

is a pointer to the first element in the array.

2. The name of a

static array

is a

constant

pointer to the first element in the array.

3. The

size/capacity

is the number of memory cells allocated to an array.

4. An

index /subscript

is used to access the memory cells in an array.

5.

[ ]

is called the subscript operator.

6. The

index

is a non-negative integer.

7. The

range

of an index is between 0 and the size-1.

4. Declaration Syntax

To declare a dynamic array of type:

type *array_name; // e.g. char *ch;

Examples allocating memory for two Dynamic Arrays:

char *ch = new char[20];

int *p;

p = new int[5];

More information on dynamic arrays can be found in your course textbook and on the web.

P a g e |

3

5. Experiments

Step 1: In this experiment you will explain the output of a program that uses static and dynamic

arrays. Enter, save, compile and execute the following program in MSVS. Call the new

directory “DynamicArraysExp1” and the program “DynamicArrays1.cpp”. Answer the

question that follows.

#include <iostream>

using namespace std;

int main()

{

int static_Array[5];

int *dynamic_Array = new int[5];

int i;

for(i=0; i<5; i++)

{

static_Array[i]=i;

dynamic_Array[i]=i;

}

for (i=0; i<5; i++)

{

cout<<"static_Array["<<i<<"] = "<<static_Array[i]<<endl;

cout<<"dynamic_Array["<<i<<"] = "<<dynamic_Array[i]<<endl;

}

return 0;

}

Question 1: Please explain each line of code and the output.

P a g e |

4

Step 2: In this experiment you will explain the output of a program that uses static and dynamic

arrays. Enter, save, compile and execute the following program in MSVS. Call the new

directory “DynamicArraysExp2” and the program “DynamicArrays2.cpp”. Answer the

question that follows.

#include <iostream>

using namespace std;

int main()

{

int static_Array[5];

int *dynamic_Array;

dynamic_Array = new int[5];

int i;

for(i=0; i<5; i++)

{

static_Array[i]=i;

dynamic_Array[i]=5;

}

for (i=0; i<5; i++)

{

cout<<"static_Array["<<i<<"] = "<<static_Array[i]<<endl;

cout<<"dynamic_Array["<<i<<"] = "<<dynamic_Array[i]<<endl;

}

cout<<endl<<endl<<endl;

static_Array = dynamic_Array;

for (i=0; i<5; i++)

{

cout<<"static_Array["<<i<<"] = "<<static_Array[i]<<endl;

cout<<"dynamic_Array["<<i<<"] = "<<dynamic_Array[i]<<endl;

}

return 0;

}

Question 2: Please list and explain any error message(s).

P a g e |

5

Step 3: In this experiment you will investigate the output of a program that uses static and

dynamic arrays. Enter, save, compile and execute the following program in MSVS. Call the

new directory “DynamicArraysExp3” and the program “DynamicArrays3.cpp”. Answer

the question that follows.

#include <iostream>

using namespace std;

int main()

{

int static_Array[5];

int *dynamic_Array;

dynamic_Array = new int[5];

int i;

for(i=0; i<5; i++)

{

static_Array[i]=i;

dynamic_Array[i]=5;

}

for (i=0; i<5; i++)

{

cout<<"static_Array["<<i<<"] = "<<static_Array[i]<<endl;

cout<<"dynamic_Array["<<i<<"] = "<<dynamic_Array[i]<<endl;

}

cout<<endl<<endl<<endl;

dynamic_Array = static_Array;

for (i=0; i<5; i++)

{

cout<<"static_Array["<<i<<"] = "<<static_Array[i]<<endl;

cout<<"dynamic_Array["<<i<<"] = "<<dynamic_Array[i]<<endl;

}

return 0;

}

Question 3:

Why did the program in Step 3 compile and run without any errors, and the program in Step 2 did not? Explain your answer.

P a g e |

6

Step 4: In this experiment you will explain the output of a program that uses static and dynamic

arrays. Enter, save, compile and execute the following program in MSVS. Call the new

directory “DynamicArraysExp4” and the program “DynamicArrays4.cpp”. Answer the

question that follows.

#include <iostream>

using namespace std;

int main()

{

int *static_Array = new int[5];

int *dynamic_Array;

dynamic_Array = new int[5];

int i;

for(i=0; i<5; i++)

{

static_Array[i]=i;

dynamic_Array[i]=5;

}

for (i=0; i<5; i++)

{

cout<<"static_Array["<<i<<"] = "<<static_Array[i]<<endl;

cout<<"dynamic_Array["<<i<<"] = "<<dynamic_Array[i]<<endl;

}

cout<<endl<<endl<<endl;

static_Array = dynamic_Array;

for (i=0; i<5; i++)

{

cout<<"static_Array["<<i<<"] = "<<static_Array[i]<<endl;

cout<<"dynamic_Array["<<i<<"] = "<<dynamic_Array[i]<<endl;

}

return 0;

}

Question 4: Compare the programs that were presented in Steps 1, 2, 3 and 4. What are your observations?

C++. Spam answers will be reported otherwise, 5 stars :)

Explanation / Answer

Answer 1: coments inline with code

#include <iostream>
using namespace std;
int main()
{
int static_Array[5]; //declare a variable static_Array which can hold upto 5 int, This is static allocated array
int *dynamic_Array = new int[5]; //assigns a dynamically allocated array to hold 5 ints to the pointer dynamic_array
int i;

//iterate over both arrays and assign integers 0, 1, 2, 3, 4 to both
for(i=0; i<5; i++)
{
static_Array[i]=i;
dynamic_Array[i]=i;
}

//display the contents of the static_Array and dynamic_Array. [ ] can be used to access elements from static or dynamic array

//will display static_Array[0] = 0 dynamic_Array[0] = 0, static_Array[1]= 1 dynamic_Array[1]=1 etc...
for (i=0; i<5; i++)
{
cout<<"static_Array["<<i<<"] = "<<static_Array[i]<<endl;
cout<<"dynamic_Array["<<i<<"] = "<<dynamic_Array[i]<<endl;
}
return 0;
}

Answer 2:

#include <iostream>
using namespace std;
int main()
{
int static_Array[5];
int *dynamic_Array;
dynamic_Array = new int[5];
int i;
for(i=0; i<5; i++)
{
static_Array[i]=i;
dynamic_Array[i]=5;
}
for (i=0; i<5; i++)
{
cout<<"static_Array["<<i<<"] = "<<static_Array[i]<<endl;
cout<<"dynamic_Array["<<i<<"] = "<<dynamic_Array[i]<<endl;
}
cout<<endl<<endl<<endl;
static_Array = dynamic_Array;
for (i=0; i<5; i++)
{
cout<<"static_Array["<<i<<"] = "<<static_Array[i]<<endl;
cout<<"dynamic_Array["<<i<<"] = "<<dynamic_Array[i]<<endl;
}
return 0;
}

The bold underlined line above is incorrect. It is not possible to assign any other array to static array name. A static array variable can never been changed / assigned a different array's address

Answer 3:

using namespace std;
int main()
{
int static_Array[5];
int *dynamic_Array;
dynamic_Array = new int[5];
int i;
for(i=0; i<5; i++)
{
static_Array[i]=i;
dynamic_Array[i]=5;
}
for (i=0; i<5; i++)
{
cout<<"static_Array["<<i<<"] = "<<static_Array[i]<<endl;
cout<<"dynamic_Array["<<i<<"] = "<<dynamic_Array[i]<<endl;
}
cout<<endl<<endl<<endl;
dynamic_Array = static_Array;
for (i=0; i<5; i++)
{
cout<<"static_Array["<<i<<"] = "<<static_Array[i]<<endl;
cout<<"dynamic_Array["<<i<<"] = "<<dynamic_Array[i]<<endl;
}
return 0;
}

Here the variable dynamic_Array is pointer variable and can point to any int array whether it is static array or dynamic array. So it is possible to assign the static_Array to dynamic_Array variable. Now both refer to the elements in static_Array

Answer 4:

#include <iostream>
using namespace std;
int main()
{
int *static_Array = new int[5]; //although the name of the variable is static_Array, it is a pointer to dynamically alocated array of 5 ints
int *dynamic_Array; //declare a pointer to an array
dynamic_Array = new int[5]; //dynamically allocate memory
int i;
for(i=0; i<5; i++)
{
static_Array[i]=i; //static_Array is asssigne values 0 , 1, 2, 3, 4
dynamic_Array[i]=5; //dynamic_Array is always assingned 5 to all elements
}

//display both the arrays
for (i=0; i<5; i++)
{
cout<<"static_Array["<<i<<"] = "<<static_Array[i]<<endl;
cout<<"dynamic_Array["<<i<<"] = "<<dynamic_Array[i]<<endl;
}
cout<<endl<<endl<<endl;
static_Array = dynamic_Array; //assigning dynamic_Array to pointer static_Array, both are pointing to same array, the one pointed by dyanmic_Array , containing all elements as 5

//here both are pointing to same array having all elements as 5 and bothe array contents are displayed twice.
for (i=0; i<5; i++)
{
cout<<"static_Array["<<i<<"] = "<<static_Array[i]<<endl;
cout<<"dynamic_Array["<<i<<"] = "<<dynamic_Array[i]<<endl;
}
return 0;
}

Comments inline with code.

output

static_Array[0] = 0
dynamic_Array[0] = 5
static_Array[1] = 1
dynamic_Array[1] = 5
static_Array[2] = 2
dynamic_Array[2] = 5
static_Array[3] = 3
dynamic_Array[3] = 5
static_Array[4] = 4
dynamic_Array[4] = 5

static_Array[0] = 5
dynamic_Array[0] = 5
static_Array[1] = 5
dynamic_Array[1] = 5
static_Array[2] = 5
dynamic_Array[2] = 5
static_Array[3] = 5


dynamic_Array[3] = 5
static_Array[4] = 5
dynamic_Array[4] = 5

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote