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, 21.06.2019 23:00
Describe in pseudocode an algorithm that given an integer n and a linked list of elements increases the linked list by a factor of n by replacing each element in the original list with n copies of that element. for example, if l: [18, 7, 4, 24, 11] and n = 3 the resulting list should be l: [18, 18, 18, 7, 7, 7, 4, 4, 4, 24, 24, 24, 11, 11, 11]. if the value of n is less than or equal to 0, the list should be empty after the call. what’s the running time of your algorithm?
Answers: 3
question
Computers and Technology, 22.06.2019 09:30
Is a string of code written to hurt others by damaging or destroying
Answers: 1
question
Computers and Technology, 22.06.2019 10:20
Shown below is the start of a coding region within the fist exon of a gene. 5'--3' 3'--5' how many cas9 pam sequences are present?
Answers: 1
question
Computers and Technology, 22.06.2019 12:30
What characteristic of long period comets suggest they come directly from the oort cloud?
Answers: 2
You know the right answer?
Dynamic Arrays There is a close association between pointers and arrays. Recall that an array variab...
Questions
question
Spanish, 18.03.2021 18:50
question
History, 18.03.2021 18:50
question
Mathematics, 18.03.2021 18:50
question
Mathematics, 18.03.2021 18:50
Questions on the website: 13722367