Deleting number from array using a loop

This code returns:
array size is: 3
1 is present in the array
1 2 3
1 0

I want the code to return this:
array size is: 3
1 is present in the array
1 2 3
1 3

I've been messing with it for over an hour now and I can't figure out what is wrong exactly or how to fix it. I'm not allowed to use vectors or anything preprogrammed function to do it. I can't change the header or the main file either. So it has to be something with how I've done the loop in the removeNumber function. Any suggestions or help is appreciated!

Header:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#ifndef VARARRAY_H_
#define VARARRAY_H_

class varArray{
public:
	varArray(); // void constructor
	int arraySize() const { return size; } // returns the size of the array

	int check(int number); // returns index of element containing "number" or -1 if none
	void addNumber(int);    // adds number to the array
	void removeNumber(int); // deletes the number from the array
	void output();      // prints the values of the array

private:
	int *dArray; // pointer to the dynamically allocated array
	int size;   // array size
};

#endif /* VARARRAY_H_ */ 

Defintions:
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
#include <iostream>
#include "vararray.h"

using std::cout; using std::endl;


varArray::varArray() {
		// void constructor
	size = 0;
	dArray = new int[size];
	}

int varArray::check(int number){
		// returns index of element containg "number" or -1 if none
		for (int i = 0; i < size; i++) {
			if (dArray[i] == number) {
				return i;
			}
		}
		return -1;
	}
	
void varArray::addNumber(int number){
	// adds number to the array
	int a = check(number);
	if (a == -1) {
		int *p = new int[size + 1];
		for (int i = 0; i < size; ++i) {
			p[i] = dArray[i];
		}
		delete[] dArray;
		p[size] = number;
		size = size + 1;
		dArray = p;
	}
}

void varArray::removeNumber(int number){
			// deletes the number from the array
	bool found = false;
	for (int i = 0; i < size; i++) {
		if (dArray[i] == number) {
			found = true;
		}
	}
	if (found == true) {
		int b = check(number);
		size = size - 1;
		int *p = new int[size];
		for (int i = 0; i < size; i++) {
			if (i != b) {
				p[i] = dArray[i];
			}
		}
		delete[] dArray;
		dArray = p;
		}
	
}

void varArray::output(){
	// prints the values of the array
	for (int i = 0; i < size; i++) {
		cout << dArray[i] << " ";
	}
	cout << "\n";
}

Main:
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
#include <iostream>
#include "vararray.h"

using std::cout; using std::endl;

int main(){

	varArray a1;

	// testing regular member functions
	a1.addNumber(1);
	a1.addNumber(2);
	a1.addNumber(3);
	a1.addNumber(3); // trying to add duplicate, should not add it

	cout << "array size is: " << a1.arraySize() << endl;

	if (a1.check(1) != -1) // check() returns -1 if number not present
		cout << "1 is present in the array" << endl;

	if (a1.check(5) != -1)
		cout << "5 is present in the array" << endl;

	a1.output();
	a1.removeNumber(2);
	a1.output();
}
The result of line 47: if number is not in array, you will shrink anyway?

The result of line 48: you just forgot the size of the current array.

The error of line 52: you don't move elements that are after the removed element.
@keskiverto For your question regarding line 47, I used the boolean found to prevent that from happening. It only does that if found returns true.
For line 48 I don't know what forgetting the size of the current array does in regards to my problem.
For line 52 I have tried doing this here before and it doesn't work either.
1
2
3
4
5
6
7
8
for (int i = 0; i < size; i++) {
	if (i < b) {
		p[i] = dArray[i];
	}
        if (i > b) {
                p[i-1] = dArray[i];
        }
}
For line 48 I don't know what forgetting the size of the current array does in regards to my problem.

Lets illustrate. The array ha {2,4,8}, so size==3. Remove 4, so b==1
1
2
3
4
5
6
7
8
9
size = size - 1;
for (int i = 0; i < size; i++) {
	if (i < b) {
		p[i] = dArray[i];
	}
        if (i > b) {
                p[i-1] = dArray[i];
        }
}

Unroll loop:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
size = size - 1;
i = 0;
{
	if (i < b) {
		p[i] = dArray[i];
	}
        if (i > b) {
                p[i-1] = dArray[i];
        }
}
i = 1;
{
	if (i < b) {
		p[i] = dArray[i];
	}
        if (i > b) {
                p[i-1] = dArray[i];
        }
}
i = 2;
size==2
0 < 2, true

0 < 1, true
p[0] = dArray[0]

0 > 1, false



1 < 2, true

1 < 1, false


1 > 1, false



2 < 2, false

Do you now see why the new size never considers the last element of dArray?

line 47, I used the boolean found

I see. You loop the array first to see whether it contains the number and then you loop the same array again to find the same number again in order to store the index too. That is unnecessary code repetition and execution.
1
2
3
4
5
6
void varArray::removeNumber( int number ) {
  const auto b = check( number );
  if ( -1 < b ) {
    // remove element
  }
}


The i<b and i>b are mutually exclusive. Furthermore, 0<=b<size.
1. Loop range [0..b[ -- i<b is true
2. Loop range ]b..size[ -- b<i is true
3. --size;
Topic archived. No new replies allowed.