"deleting" last int of array

I enter the numbers 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 into the array and choose a number to delete (like 2)
but then the output looks like this
{1, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 0, 0,}

I need it to look like this
{1, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 0,}

I've used arr[SIZE]=0 which does the above array bu I get run time error #2?


I cannot use vectors!

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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
 

#include <iostream>
#include <iomanip>
#include <cctype>

using namespace std;

/*Given Function prototypes*/
void PrintArray(const int arr[], const int size);
void FillArray(int arr[], const int size);


void Delete(int arr[], const int SIZE, int delete_this);

int main()
{
	const int SIZE = 15;
	int arr[SIZE];
	int delete_this;
	

	FillArray(arr, SIZE);

	PrintArray(arr, SIZE);

	cout << "\nEnter a value to delete: ";
	cin >> delete_this;
	
	Delete(arr, SIZE, delete_this);

	PrintArray(arr, SIZE);

	return 0;
}


void Delete(int arr[], const int SIZE, int delete_this)
{
	for (int i = 0; i < SIZE; ++i)
	{
		if (arr[i] == delete_this)

			while (i++ <= SIZE)
				arr[i - 1] = arr[i];
		arr[SIZE-1] = 0;
	}
}

void PrintArray(const int arr[], const int size)
// this function prints the contents of the array
{
	cout << "\nThe array:\n { ";
	for (int i = 0; i < size - 1; i++)	// print all but last item
		cout << arr[i] << ", ";

	cout << arr[size - 1] << " }\n";	// print last item
}

void FillArray(int arr[], const int size)
// this function loads the contents of the array with user-entered values
{
	cout << "Please enter " << size
		<< " integers to load into the array\n> ";

	for (int i = 0; i < size; i++)
		cin >> arr[i];			// enter data into array slot
}



Last edited on

I need to look like this

Then you need to shift the remaining numbers down to replace the "deleted" value.

I thought I was doing that with this

1
2
3
4
5
6
7
for (int i = 0; i < SIZE; ++i)
	{
		if (arr[i] == delete_this)

			while (i++ <= SIZE)
				arr[i - 1] = arr[i];
}
that looks slightly different from your original code...
1
2
3
4
5
6
7
8
9
10
11
void Delete(int arr[], const int SIZE, int delete_this)
{
	for (int i = 0; i < SIZE; ++i)
	{
		if (arr[i] == delete_this)

			while (i++ <= SIZE) //out of bounds access
				arr[i - 1] = arr[i];
		arr[SIZE-1] = 0; //always execute
	}
}
I posted this in response to jlb

1
2
3
4
5
6
7
for (int i = 0; i < SIZE; ++i)
	{
		if (arr[i] == delete_this)

			while (i++ <= SIZE)
				arr[i - 1] = arr[i];
}


I'm still not able to figure it out though, my original code prints out
{1, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 0, 0,}

how can i make it print out

{1, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 0,}
?
You trash your array when you do arr[SIZE-1] = 0; in the first iteration of the loop.
That should be done as the last step (or not at all, simply reduce the 'size')


And you should avoid the undefined behaviour of out of bounds access
what does "out of bounds access" mean exactly? when i remove any part of

1
2
while (i++ <= SIZE) 
	arr[i - 1] = arr[i]; 


my array no longer shifts to the left and doesn't delete delete_this


I've taken it out of the loop like you suggested:

1
2
3
4
5
6
7
8
9
10
11
12
void Delete(int arr[], const int SIZE, int delete_this)
{
	for (int i = 0; i < SIZE; ++i)
	{
		if (arr[i] == delete_this)

			while (i++ <= SIZE) 
				arr[i - 1] = arr[i];	
	}

	arr[SIZE - 1]=0;
}




and now it prints out what I want it to

{1, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 0,}


thanks for your help


do you think I'll have trouble with the rest of the code because of the out of bounds access?
you declare an array of size 'SIZE'
valid index for accessing that array would be from 0 to SIZE-1
Trying to access outside that range is a logic error (an out of bounds access), and the result is undefined.

In your code you've got
1
2
while( i++ <= SIZE)
   arr[i-1] = arr[i];
you use post-increment so the value used in the comparison is the old one, that is one less than the one used in the arrays.
i starts in 0, the comparison 0<=SIZE success and you do arr[0] = arr[1]
In the last iteration you've got SIZE<=SIZE and you do arr[SIZE] = arr[SIZE+1] which are two invalid access. (the previous iteration was also incorrect)
Topic archived. No new replies allowed.