Elements appearing in arrays that shouldn't exist

The problem I'm solving involves deleting an element of an array and making sure there are no empty spaces. The program works as intended but while testing it, there appears to extra elements in the array that I don't want to exist. Lets say the size of the array is 7. When I check arr4[7] (which shouldn't exist) it has the same value as the correct last element. However, even if the size is bigger than I think, the element should be 0 correct? then when I got past that, lets say arr4[10] it prints a huge number.

Basically I need the array to read that it changed what is inside and change the size accordingly. But I also want to understand why the size is bigger than it should be and why they aren't 0's.

1
2
3
4
5
6
7
8
9
10
11
12
  int arr4[] = {1, 2, 3, 4, 5, 6, 7, 8};
	int del;
	int size = 8;
	cout << "Enter a element in the array to be deleted. There are 8 elements in the array.: " << endl;
	cin >> del;
	for(int i = del; i < size; i++)
	{
		arr4[i] = arr4[i + 1];
	}

        //This is just to test why I am getting elements when I don't want them. The output for arr4[10] is "6422320"
	cout << "The new array is: " << arr4[10];
uninitialized variables can have any value. some tools in debug mode zero them, so don't. None do in optimal builds.

arrays cannot change size. you can make it too big and track a fake size yourself, or use <vector> (but, resizing is expensive to do, it copies everything if it needs to do that).

it isnt bigger than it should be: going out of bounds is a memory access violation. These typically do not crash if off by 1 or so (reason: its still memory the program owns, but its outside the array), but it will still mess things up, and you should not do it.

if you poke at some code, eg
int x[5]{}; //the{} will zero initialize it...
int oops{42};
x[5] = 12345;
x[6] = 12345;
cout << oops; //there is a solid chance this is 12345. Its not assured, but many compilers will put the variables in memory sequentially enough that you can see the problem. Accessing the array out of bounds can corrupt other bytes and damage other variables.
Last edited on
Okay. So if I just want to print out what the array should be (If the size is 8 originally but there should only be 7 elements after deleting one) then I should just have it print out up to the 7th element and that's not a wrong thing to do?
I should just have it print out up to the 7th element

Yep, that's the correct thing to do.

When you define an array with space for n elements, the computer locates a block of memory with space for exactly that many elements, no more, no less. Nothing you do later on in the program can change the size of the memory block the computer has already found for you.

Arrays correspond to blocks of memory with a fixed size.
Why do you use an array if you want to delete elements?
It's much easier to use std::vector.
L6 is incorrect anyhow. When i is 7, i + 1 is 8 which is out of bounds (bounds are 0 to 7).

For L6:
 
for(int i = del; i < size - 1; i++)

Last edited on
Hello MorganCx,

A different approach to your program.

Your prompt:

Enter an element in the array to be deleted.
There are 8 elements in the array.:

would lead people that the 1st element is (1) and the last element is (8) when it should be (0) and (7).

Your for loop to move elements to the left should start at (0)zero, but has a better chance to start at (1) which would never delete the 1st element of the array.

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
//  279502 Elements appearing in arrays that shouldn't exist MorganCx

// <--- Common includes.
#include <iostream>
//#include <iomanip>
#include <limits>
//#include <string>
//#include <vector>
//#include <cctype>

int main()
{
    constexpr unsigned MAXSIZE{ 8 };

    int arr4[MAXSIZE]{ 1, 2, 3, 4, 5, 6, 7, 8 };
    int del{};
    //int size{ 8 };  // <--- Not used and should be a constant.

    // <--- These commented lines used for testing.
    //std::cout << '\n';

    //for (unsigned idx = 0; idx < MAXSIZE; idx++)
    //{
    //    std::cout << (!(idx % 8) ? " " : ", ") << arr4[idx];
    //}

    std::cout << "\nEnter an element in the array to be deleted.\nThere are 8 elements in the array.: ";
    std::cin >> del;

    if(del)  // <--- If (0)zero is entered for "del" it skips the if statement.
        del--;

    for (int idx = del; idx < MAXSIZE - 1; idx++)
    {
        arr4[idx] = arr4[idx + 1];
    }

    arr4[7] = 0;

    //This is just to test why I am getting elements when I don't want them. The output for arr4[10] is "6422320"
    //cout << "The new array is: " << arr4[10];  // <--- 10 is past the end of the array.

    std::cout << "\nThe new array is:\n";

    for (unsigned idx = 0; idx < MAXSIZE; idx++)
    {
        std::cout << (!(idx % 8) ? " " : ", ") << arr4[idx];
    }


    // The next line may not be needed. If you have to press enter to see the prompt it is not needed.
    std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');  // <--- Requires header file <limits>.
    std::cout << "\n\n Press Enter to continue: ";
    std::cin.get();

    return 0;  // <--- Not required, but makes a good break point.
}


For deleting the 1st element by entering (1):

Enter an element in the array to be deleted.
There are 8 elements in the array.: 1

The new array is:
 2, 3, 4, 5, 6, 7, 8, 0

 Press Enter to continue:


For deleting the 1st element by entering (0)zero:


Enter an element in the array to be deleted.
There are 8 elements in the array.: 0

The new array is:
 2, 3, 4, 5, 6, 7, 8, 0

 Press Enter to continue:


See what you think.

Andy
@Handy Andi,

this is what happens when I enter 7.
Probably not what you expect.
Enter an element in the array to be deleted.
There are 8 elements in the array.: 7

The new array is:
 1, 2, 3, 4, 5, 6, 8, 0
@thmm,

No that is exactly what I expected.

#7 is in the 6th element, so you move element 7 1 left and make element 7 a (0)zero.

If not 7 would be == MAXSIZE - 1 and the for loop would fail.

Andy

I do see a problem is someone would enter the correct element number, but with out a proper prompt to know that the 1st element starts at (0)zero it should work for those who are not programmers.
Last edited on
Andy's code is based upon an index of 1 - 8 and not 0 - 7. He allows 0 to also mean 1!

Shawn of the comments and with the output formatting fixed, it's simply:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>

int main()
{
	constexpr unsigned MAXSIZE {8};

	int arr4[MAXSIZE] {1, 2, 3, 4, 5, 6, 7, 8};
	size_t del {};

	std::cout << "\nEnter an element index in the array to be deleted (1 - " << MAXSIZE << "): ";
	std::cin >> del;

	for (auto idx {del - (del > 0) }; idx < MAXSIZE - 1; ++idx)
		arr4[idx] = arr4[idx + 1];

	std::cout << "\nThe new array is:\n";

	for (size_t idx {}, mx {MAXSIZE - (del <= MAXSIZE)}; idx < mx; ++idx)
		std::cout << arr4[idx] << ((idx + 1) % mx ? ", " : "\n");
}

Last edited on
@Handy Andy, @seeplus,

I see. With a proper prompt it's much clearer.
Topic archived. No new replies allowed.