answersLogoWhite

0


Best Answer
To remove any element from an array, you must overwrite every element from that point on with the element that follows it. That is, every element after the removed element must be copied to the preceding element. So to remove the 5th element, you copy the 6th element to the 5th element, the 7th to the 6th, and so on. Once all elements have been copied, you can reduce the array size by one element. Alternatively, you can leave the array size as is and simply maintain a count of the used elements (unused elements will always be at the end of the array).


Note that in C++, a vector encapsulates an array of any type and provides member methods for manipulating that array, including the removal of individual elements. While manipulating arrays is a worthwhile exercise in determining "how things work", it's always better to make use of efficient, tried-and-tested code, rather than constantly re-inventing wheels to provide such common functionality.

User Avatar

Wiki User

11y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: How do you write a program in c plus plus to remove fifth element in the array?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

How array elements are retrieved?

Array elements are retrieved by specifying the offset of the element you want. Since array elements are a fixed size, you can also retrieve elements using simple pointer arithmetic. In actual fact, this is the underlying mechanism used when you supply an offset.Consider the following array declaration:int myArray[5] = { 1, 2, 3, 4, 5 };Memory is allocated to myArray according to the type of the element specified. The type is int, therefore each element will be exactly 4 bytes in length. The subscript [5] allocates 5 elements to this array, thus 20 bytes (5 x 4 bytes) are allocated in total.myArray refers to the first address of the memory location where the memory was allocated. It therefore refers to the first element in the array, which is offset 0. To retrieve the value stored at offset 0 we specify the offset as a subscript:int x = myArray[0];This effectively copies the value 1 (the value we stored in the first element) and places it in the memory allocated to x.If we want to copy the last element, the fifth element, we specify the final offset, which is 4.int y = myArray[4];As before, this copies the value 5 and places it in the memory allocated to y.Note that to access the nth element of an array, we specify the offset n-1. Offsets are zero-based, so the last element of an n dimension array is also offset n-1.If we use pointers, we can better understand the underlying mechanism. myArray refers to the starting address of the entire array, so we can dereference the array by allocating a pointer to this memory location.int * p = myArray;This copies the address of myArray and stores it in the memory allocated to p. Therefore p points at the first element in the the array and we can retrieve the value of that element by using the indirection operator.int z = *p;This is the same as saying z = myArray[0].To access the next element (found at offset 1), we simply increment the pointer's value by 1.++p;Note that if the value in p was originally the memory location 0x00123456, it is easy to assume it must now be 0x00123457 -- a difference of 1 byte. But p was declared as a pointer to int, so we actually increment the memory address by 4 bytes. So, p now stores the address of myArray[1].So when we retrieve the element myArray[1], what we're really doing is accessing the value stored 1 x 4 bytes away (offset) from the memory location referred to by myArray. All this is done behind the scenes, but is no different to allocating a pointer to myArray, incrementing it by the specified offset, and finally returning the value through indirection.We can also use indirection to change the value of an element:*p = 10;This is the same as saying:myArray[1] = 10;The only problem to be aware of when working with pointers is that your pointer can end up pointing at memory outside of the array. However the same can be said of any pointer. Always make sure you know what you're pointing at before attempting to access the value stored at that location.


Arrays in c?

1>an array is a static data structure.after declaring an array it is impossible to change its size.thus sometime memory spaces are misused. 2>each element of array are of same data type as well as same size.we can not work with elements of different data type. 3>in an array the task of insertion and deletion is not easy because the elements are stored in contiguous memory location. 4>array is a static data structure thus the number of elements can be stored in it are somehow fixed.


What is an array and how do you use them in c and c plus plus?

#include<iostream> #include<vector> int main() { std::vector<int> v {1, 4, 8, 15, 23}; // initialise array with 5 elements v.push_back (42); // add a 6th element for (auto i : v) std::cout << i << std::endl; // print array elements }


Programming Logic and Design Fifth Edition solution manual?

Use single subscripted array(s) to solve the following problem. A company pays its sales people on a commission basis. The salespeople receive $200 per week plus 9 percent of their gross sales for that week. For example, a salesperson who grosses $3000 in sales in a week receives $200 plus 9% of $3000, or a total of $470. Design a program (using an array of counters) that determines how many of the salespeople earned salaries in each of the following ranges.$200-$299$300-$399$400-$499$500-$599$600-$699$700-$799$800-$899$900-$999$1000 and over


How do you create an array in visual basic?

If you are referring to the searching and sorting of strings, there are 2 main methods that Visual Basic uses most. If you want to search for a phrase in a string, you would probably use the InStr method, which would give you an integer which would indicate the place where the phrase was found. It might look something like this: Dim InputString as String Dim StringToFind as String = "Whatever text you want to find" Dim PositionInteger as Integer PositionInteger = InStr(InputString, StringToFind) If you wanted to sort the string, you could do it with the substring method using the split point you found with the previous code. To do that, you would make a variable that would contain the latter half of your string, and use the subtring method to extract it from the whold string. To do that, you must type "Substring" and in parenthasis, give it a starting position, and an ending position. If you want all the characters from the phrase to the end of the sting, you do not need to put in an end point. The starting point for this example will be the position of the start of the search phrase, plus thirty (since you want to start after the end of the phrase, which is thirty characters long). Your code might look like this: Dim NewString as String NewString = Substring(PositionInteger + 30) This would make the variable "NewString" contain all the characters following the search phrase.

Related questions

Which formula you will use to calculate The memory address of fifth element of an array?

a


In the film the fifth element wHaT is the fifth element?

"Life" was the fifth element...


Which formula you will to calculate The memory address of fifth element of an array?

Awais khanLOC(LA[K]) =base(LA)+w(k-lowerbound)


What is the duration of The Fifth Element?

The duration of The Fifth Element is 2.1 hours.


When was The Fifth Element created?

The Fifth Element was created on 1997-05-06.


When was The Fifth Element released?

The Fifth Element was released on 05/09/1997.


What is the fifth element called?

The fifth element on the periodic table is boron, which has the symbol B.


When was The Fifth Element - video game - created?

The Fifth Element - video game - was created in 1998.


When did The Fifth Element - video game - happen?

The Fifth Element - video game - happened in 1998.


Who played the girl in fifth element?

the girl who played the fifth element is milla jovovich who also played in resident evil; loved her hair


On howrse can you change the element of the fifth element on your horse?

no you get a random element and you dont change it


What is the thirty fifth element?

bromine