delete element in array

good day, i have array program which will select a specific element in array and will be replaced by the last element after that it will delete the last element.

here is my working program.

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
  #include<iostream>
using namespace std;

int main(){
	char array[4] = {'a','b','c','d'};
	char find,change;
	int count,mark;
	
	for(int x =0; x<4; x++){//display the array
	cout<<"[" <<array[x] <<"]";
	}					
	
	cout<<endl;
	cout<<"Delete Element" <<endl;
	cout<<"==========================================" <<endl;
	cout<<"Find the element to delete: ";
	cin>>find;
	
			for(int x = 0;x<4;x++){	
				if(find==array[x]){	//search for match, if found
				mark= x;	//store the value of position of your search
				find = 1;	//your counter if search found the element
			}
		}
	
		if(find==1){	//if find is = 1, execute below
			cout<<"Element found in position: " <<mark+1;
			cout<<endl;
			cout<<"Enter the element again to delete: ";
			cin>>change;
		
					//if the change variable found the element to delete, it will copy the last elemet
						for(int x = 0;x<4;x++){
							if(change==array[x]){	
							array[x]=array[4-1];
							break; //stop at the first found value
							}
						}
					
						cout<<"==========================================" <<endl;	
						cout<<"New Array " <<endl;
						cout<<"Delete successful and was replaced by the last element! " <<endl;
					//display new array and decrement by 1
							for(int x =0; x<4-1; x++){
								cout<<"[" <<array[x] <<"]";
							}						
			}
			
				//if find is 0, execute
			else
				{		
					cout<<"Element Not found! "; //message if not found element
				}
					cout<<endl;
					cout<<"==========================================" <<endl;	
		
	
}	


in line 29, i want to change it into yes and no condition. my problem is that if i add an if condition it ruins my program. it changes the program output and its just delete the last element. i wonder if the placement of if condition is the issue.

here my modified code

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
#include<iostream>
using namespace std;

int main(){
	char array[4] = {'a','b','c','d'};
	char find,change;
	int count,mark;
	
	for(int x =0; x<4; x++){//display the array
	cout<<"[" <<array[x] <<"]";
	}					
	
	cout<<endl;
	cout<<"Delete Element" <<endl;
	cout<<"==========================================" <<endl;
	cout<<"Find the element to delete: ";
	cin>>find;
	
			for(int x = 0;x<4;x++){	
				if(find==array[x]){	//search for match, if found
				mark= x;	//store the value of position of your search
				find = 1;	//your counter if search found the element
				}	
			}
	
		if(find==1){	//if find is = 1, execute below
			cout<<"Element found in position: " <<mark+1;
			cout<<endl;
			cout<<"Do you want to delete the element? (y/n?):  ";
			cin>>change;
		
				if(change=='y'){
					
							for(int x = 0;x<4;x++){
								if(change==array[x]){	
								array[x]=array[4-1];
								break; //stop at the first found value
								}
							}
						
							cout<<"==========================================" <<endl;	
							cout<<"New Array " <<endl;
							cout<<"Delete successful and was replaced by the last element! " <<endl;
						//display new array and decrement by 1
								for(int x =0; x<4-1; x++){
									cout<<"[" <<array[x] <<"]";
									}						
								}
					else
					{
						cout<<"Delete was canceled!";
					}
				}
				//if find is 0, execute
		else
				{		
					cout<<"Element Not found! "; //message if not found element
				}
					cout<<endl;
					cout<<"==========================================" <<endl;	
		
	
}	

The problem seems to be in this loop.

1
2
3
4
5
6
for(int x = 0;x<4;x++){
	if(change==array[x]){	
		array[x]=array[4-1];
		break; //stop at the first found value
	}
}

change contains the answer to the question "Do you want to delete the element? (y/n?)" so it doesn't make sense comparing it to the array elements. Instead I think you want to use the original value of find, but you have reused find for other purposes so you no longer have this value.

To avoid all this confusion I think you should try to use better variable names and don't reuse variables for different purposes. It makes it so much easier to write correct code that way.
Last edited on
1. Your indentation is not very systematic. That makes code harder to read.

2. Why do you compare element to 'y' on line 35? Why do you have a loop at all? Doesn't the mark mark the spot?

3. Your loop on lines 19-24 does not stop on first match. It will set the mark to the last location that has searched element. You could, for practice, change the loop to search from end (in reverse direction) and stop on first match. That yields the same element, but potentially does less work.
@peter thank you for pointing that out. i never actually noticed that and forgot that i already use that variable. thanks again for the help
Last edited on
@keskiverto


3. i see. so should i use break to stop at the first find?
Which one do you want to be "found"?


Lets seek 'b' from "abcbd" in four different ways.

1. Seek from begin:
a is not b
b is b, we have found something, mark=1
c is not b
b is b, we have found something, mark=3
d is not b
====
find==1, mark==3
after delete: "abcd"

2. Seek from begin and break:
a is not b
b is b, we have found something, mark=1, break
====
find==1, mark==1
after delete: "adcb"

3. Seek from end:
d is not b
b is b, we have found something, mark=3
c is not b
b is b, we have found something, mark=1
a is not b
====
find==1, mark==1
after delete: "adcb"

2. Seek from end and break:
d is not b
b is b, we have found something, mark=3, break
====
find==1, mark==3
after delete: "abcd"
ok that make sense. thanks again
Topic archived. No new replies allowed.