subject

Dynamic Arrays There is a close association between pointers and arrays. Recall that an array variable is actually a pointer variable that points to the first indexed variable of the array. Array elements can be accessed using pointer notation as well as array notion. Consider the following program that uses an array with the help of a for loop to read in 8 integers from the keyboard and then display the values in the odd indices, ie, the values in the indices 1, 3, 4,5 and 7. # include clostrean using nanespace std; int main) int nuntist[8]: lfread 8 integeres fron keyboard for (int ; e: )( coutec Enter value at tndex eclee cinxnunlist[i]: IIdisplay the numbers at odd indices for (int 1-1; 1es; t-1+2) coutec"Printing value at index "eetee"" coutecnunlist[tJeecendl; return 0: One problem with static arrays is that we must specify the size of the array when we write the program. This may cause two different problems: (1) we may create an array much larger than needed; or (2) we may create one that is smaller than what is needed In general, this problem is created because we do not know the size of the array until the program is run. This is where dynamic arrays can be used. The new expression can be used to allocate an aray on the freestore. Since array variables are pointer variables, you can use the new operator to create dynamic variables that are arrays and treat these dynamic array variables as if they were ordinary arrays. Array elements can also be accessed using pointer notation as well as array notation Now let's modify the above program to create the array dymamicaly and access the array using pointer notation. Recall that to access elenet i in array a we can use (arr) that is equivalent to arrli Specifically, modify the program as follows a. Prompt for and read in the size of the array b. Create the array numlist dynamically (using the new operator) of the size specified by the user c. Read in the values for the array using pointer notation (inside the first foz loop). Note you will also have to change the for loop to account for the variable size of the array d. Print out the values at the odd indices (indices 1.3,S etc) of the array using pointer notation (inside the second for loop). Note you will also have to change the for loop to account for the variable size of the array. And finally, delete the dynamic array once we are done with it e.

ansver
Answers: 2

Another question on Computers and Technology

question
Computers and Technology, 22.06.2019 06:00
What role do chromosomes play in inheritance?
Answers: 1
question
Computers and Technology, 22.06.2019 09:50
Assume that you have an sorted array of records. assume that the length of the array (n) is known. give two different methods to search for a specific value in this array. you can use english or pseudo-code for your algorithm. what is the time complexity for each algorithm and why?
Answers: 1
question
Computers and Technology, 22.06.2019 12:30
Some of the first computer games were created in the early 1970s by college students experimenting after hours to see what the were capable of doing.
Answers: 3
question
Computers and Technology, 22.06.2019 14:30
If the polar bear were taken out of the food chain what would happen to the seal population the seal population would diminish the seal population would grow dramatically the seal population would stay the same the seal population would decrease slightly
Answers: 1
You know the right answer?
Dynamic Arrays There is a close association between pointers and arrays. Recall that an array variab...
Questions
question
World Languages, 16.10.2020 17:01
question
Mathematics, 16.10.2020 17:01
question
Mathematics, 16.10.2020 17:01
question
Mathematics, 16.10.2020 17:01
Questions on the website: 13722363