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 13:00
We as humans write math expression in infix notation, e.g. 5 + 2 (the operators are written in-between the operands). in a computer’s language, however, it is preferred to have the operators on the right side of the operands, i.e. 5 2 +. for more complex expressions that include parenthesis and multiple operators, a compiler has to convert the expression into postfix first and then evaluate the resulting postfix.write a program that takes an “infix” expression as input, uses stacks to convert it into postfix expression, and finally evaluates it. it must support the following operations: + - / * ^ % (example infix expression: (7 - 3) / (2 + 2)postfix expression: 7 3 - 2 2 + /result: 1guidelines: 1. you will need to use stacks in three placesa. one for the parenthesis check [char stack]b. one during infix to postfix [char stack]c. one during evaluation [int stack]for a and b above, you can use same array and same push, pop method as both ofthem are char. but for evaluation you have int stack and you might consider to createanother push pop method to handle it. maybe push_int, pop_int, etc. or find otherstrategy to utilize existing push pop method2. you can create a function for obtaining operator priority. that function should take anoperator as input and return its priority as an integer. this function will you a lot andreduce repeated code3. during evaluation you will need to convert char into integer. example for single digit: char c = '5'; int x = c - '0';
Answers: 2
question
Computers and Technology, 22.06.2019 18:00
What is the first view you place in your drawing?
Answers: 1
question
Computers and Technology, 23.06.2019 17:00
What are the 12 colors of the spectrum called?
Answers: 1
question
Computers and Technology, 23.06.2019 18:40
How does is make you feel when you're kind to others? what are some opportunities in your life to be more kind to your friends and loved ones? imagine a world where kindness has be outlawed. how would people act differently? would your day-to-day life change significantly? why or why not?
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
Mathematics, 10.03.2021 19:30
question
Mathematics, 10.03.2021 19:30
question
Health, 10.03.2021 19:30
question
Mathematics, 10.03.2021 19:30
question
Mathematics, 10.03.2021 19:30
Questions on the website: 13722367