how to remove an integer from a array

so i have this program
and in my removeAt function i need a new way to remove an integer out of the array
this way works i jus need another way to do it please help


#include <iostream>
#include <iomanip>
using namespace std;
void printIt (int numbers[],int length);
void removeAt (int numbers[], int length, int& index);
void insertAt (int numbers[], int length, int insertItem, int& index);
int main()
{
int numbers[] = {4,23,65,34,82,37,12,17,24,36,82,51};
int length = 12;
int index;
int insertItem;
cout<<"Removing an item from the list..."<<endl;
cout<<endl;
printIt(numbers,length);
removeAt(numbers,length,index);
insertAt(numbers,length,insertItem,index);
// printIt(numbers,12);
system ("PAUSE");
return 0;
}
void printIt (int numbers[],int length)
{
for (int i = 0; i<length; i++)
{
cout<<numbers[i]<<" ";
}
cout<<endl;
}
void removeAt (int numbers[], int length, int& index)
{
cout<<endl;
cout<<"There are "<<length<<" item(s) in the list (position 0 through 11)"<<endl;
cout<<"Enter the position of the item to be removed."<<endl;
cout<<"Enter 0 for the first item and so on: ";
cin>>index;
while (index > length)
{
if (index > length)
{
cout<<endl;
cout<<"!!!!!!!!!!!!!!!!!! ERROR !!!!!!!!!!!!!!!!!!"<<endl;
cout<<endl;
cout<<"The current array..."<<endl;
printIt(numbers,length); cout<<endl;
cout<<endl; cout<<"!!!! Index out of Range !!!!"<<endl;
cout<<"There are "<<length<<" item(s) in the list (position 0 through 12)"<<endl;
cout<<"You entered position "<<index<<", which is OUT OF RANGE."<<endl;
cout<<"Enter the position of the item to be removed."<<endl;
cout<<"Enter 0 for the first item and so on: ";
cin>>index;
cout<<endl;
cout<<"After removing the item at position "<<index<<", array is..."<<endl;
cout<<endl;
cout<<"The current array is..."<<endl;
printIt(numbers,length);
}

}
for (int i = 0; i < length; i++)
{
if (i != index)
{
cout<<numbers[i]<<" ";
}
}
cout<<endl;
}
void insertAt (int numbers[], int length, int insertItem, int& index)
{
cout<<endl;
cout<<"****************************************************"<<endl;
cout<<endl;
cout<<"Inserting an item in the list..."<<endl;
cout<<endl;
cout<<"The current array..."<<endl;
for (int i = 0; i < length; i++)
{
if (i != index)
{
cout<<numbers[i]<<" ";
}
}
cout<<endl;
cout<<endl;
cout<<"There are 10 items(s) in the list (position 1 through 10)"<<endl;
cout<<"Enter item to be inserted and its position"<<endl;
cout<<"Position of the first element is 1,"<<endl;
cout<<"so if you want the #5 at the front type in: "<<endl;
cout<<"5 (space) 1 "<<endl;
cin>>insertItem;
cin>>index;
if (index > length)
{
cout<<endl;
cout<<"!!!!!!!!!!!!!!!!!! ERROR !!!!!!!!!!!!!!!!!!"<<endl;
cout<<endl;
printIt (numbers, length);
cout<<endl;
cout<<endl;
cout<<"!!!! Index out of Range !!!!"<<endl;
cout<<"There are "<<length<<" item(s) in the list (position 1 through 10)"<<endl;
cout<<"You entered position "<<index<<", which is OUT OF RANGE. Please try again."<<endl;
cout<<endl;
cout<<"Enter item to be inserted and its position"<<endl;
cout<<"Position of the first element is 1,"<<endl;
cout<<"so if you want the #5 at the front type in: "<<endl;
cout<<"5 (space) 1 "<<endl;
cin>>insertItem;
cin>>index;
}
cout<<endl;
cout<<"After inserting the item at position "<<insertItem<<", array is..."<<endl;
cout<<endl;
numbers[index-1] = insertItem;
for (int i = 0; i < length; i++)
{
if (i != index)
{
cout<<numbers[i]<<" ";
}
}
}
Last edited on
ps also i cant use vectors
Hmmm ok. Terrible code, but what you get for 2mins work.

Notes: I went down the dynamic memory path. Perhaps this is overkill for your problem, but meh.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include <iostream>
#include <string>

using namespace std;

void removeAt(int index, int **array, int &arrayLength) {

  int *temp  = new int[arrayLength-1];
  int offset = 0;

  for (int i = 0; i < arrayLength; ++i) {
    if (i == index)
      offset++;
    else
      temp[i-offset] = (*array)[i];
  }


  delete [] *array;
  arrayLength--;
  *array = temp;
}

int main() {

  int arraySize = 10;
  int *array = new int[arraySize]; // Create Array

  // Populate With Crap Vars
  for (int i = 0; i < arraySize; ++i)
    array[i] = i;

  printf("Original Values:");
  for (int i = 0; i < arraySize; ++i)
    printf("%i", array[i]);
  printf("\n");


  removeAt(3, &array, arraySize);

  printf("New Values:");
  for (int i = 0; i < arraySize; ++i)
    printf("%i", array[i]);
  printf("\n");

  delete [] array;

  return 0;
}


Last edited on
any help will be great
Updated the code to a simpler solution.
i have a question isnt that basically the same thing i have in mine i skipped over the interger i wanted & printed everything but that interger
im a little lost on this im a beginner
You simply don't print the number. I actually delete the original array and make a new one with that element gone.

So mine is actually a removal. You have simply missed it when printing.
thanks
No worries. I do suggest using something like a vector though.
i cannot use a vector .
can i import this directly into my original program if so how
I would study my code until you understand it. Copy+Paste is only going to introduce errors into your code.

But you could just copy and paste the removeAt function. However, You have to pass in an allocated array and free that array later with delete[]
Topic archived. No new replies allowed.