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 11:30
Hassan is writing his master’s thesis, which is a thirty-page document. he received some feedback from his professor in the form of comments, but does not see where the comments are. what is the fastest way for hassan to find the feedback?
Answers: 3
question
Computers and Technology, 23.06.2019 07:00
What are three software programs for mobile computing?
Answers: 1
question
Computers and Technology, 23.06.2019 22:30
What would be the address of the cell, which is at the intersection of the second row and the third column in a worksheet?
Answers: 1
question
Computers and Technology, 24.06.2019 00:00
Visualizing a game of “tag” to remember the meaning of contagious
Answers: 3
You know the right answer?
Dynamic Arrays There is a close association between pointers and arrays. Recall that an array variab...
Questions
question
History, 15.04.2020 16:58
question
Mathematics, 15.04.2020 16:58
Questions on the website: 13722363