Home c++ Deleting an array element at index

Deleting an array element at index

Author

Date

Category

It is very simple but it is impossible.

# include & lt; Cstdlib & gt;
#Include & lt; iostream & gt;
#Include & lt; math.h & gt;
Using Namespace STD;
Int Main (int argc, char * argv [])
{
  int a [3000];
  int i, n, nm;
  COUT & LT; & LT; "Press the number" & lt; & lt; "\ n";
  CIN & GT; & GT; n;
  for (i = 1; i & lt; = n; i ++) {
    a [i] = rand ()% 401 - 200;
    COUT & LT; & LT; a [i] & lt; & lt; "";
  }
  COUT & LT; & LT; "\ n";
  SYSTEM ("PAUSE");
  Return EXIT_SUCCESS;
}

It is necessary from this array to make the removal of the index of the array element.


Answer 1, Authority 100%

Once it is all very sad.

The dynamically allocate memory is, STL do not use eye and cutting the call system ( "PAUSE");

.

do not have to remove a lot of the elements seems, and therefore simply shifts all elements except deleted, 1 left and decrease the length of the array 1.

For example:

k - required index
for (long i = k; i & lt; n; ++ i)
{
 a [i] = a [i + 1];
}
--n;

Once it so.

p.s. In C / C++ array indexing goes from zero. The future;)


Answer 2

If the array size is not large, you can do easier.
You create another array and adds the elements except removed
further assigns the first value of the second array.

He did so on GDScript it works


Answer 3

it looks like this. above answer is wrong, because we go to the index of the element to which you want to delete to the length of the array-1 (because if we went to the length of the array would have arisen RT).

int length = 5;
int array [5] = {23, 3.51, 87.90};
int index = 3;
COUT & LT; & LT; "Source array: ";
for (int i = 0; i & lt; length; ++ i)
   COUT & LT; & LT; array [i] & lt; & lt; "";
COUT & LT; & LT; Endl;
** index--; //t.k. indexing from scratch
for (int i = index; i & lt; length-1; ++ i)
  array [i] = array [i + 1];
length -; **
COUT & LT; & LT; "Array, after removal of the third element";
for (int i = 0; i & lt; length; ++ i)
   COUT & LT; & LT; array [i] & lt; & lt; "";

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions