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.
#include<iostream>
usingnamespace 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.
#include<iostream>
usingnamespace 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;
}
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.
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.
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"