1,105,556 Community Members

Search & Remove items from an array (struct version)

Member Avatar
rhn94
Newbie Poster
11 posts since Dec 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Hey guys. I'm trying to create a program which adds, searches & removes items from an array & i've been messing around with the code, trying to find answers on the internet but so far no dice. Could any of you help out?

void removeString(List& s, string strKey, const int iARRAYSIZE, int result)
{
	cout << "Enter the string to be deleted" << endl << ":" ;
	cin >> strKey;
	cout << endl ;
	for (result = 0; result < iARRAYSIZE; result++ )
	{
		if ( s.strList[result] == strKey )
		{
			for (int i = s.iListIndex; i < s.iListLength ; i++)
			{
				s.strList[s.iListIndex] = s.strList[s.iListIndex+1];
				s.strList[s.iListLength-1] = 0; //more than one operator 
			}                                       //matches these operands
			cout << "Succesfully Deleted" << endl;
		}
		else
		{
			cout << "Error 404" << endl;
		}
	}
}

This is what i have so far. I know it's crap but that's why i need help! Thanks :D

Member Avatar
firstPerson
Industrious Poster
4,052 posts since Dec 2008
Reputation Points: 761 [?]
Q&As Helped to Solve: 634 [?]
Skill Endorsements: 24 [?]
 
0
 

Just some few comments/questions first.

1) you might want to think twice on that hungarian notation.
2) Does the order of the elements in the string matter? For example does the last element has to be the last ? So does the element position matters relative to its neighbors?

Member Avatar
rhn94
Newbie Poster
11 posts since Dec 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

1) The names are temporary, ill work on those later. What's wrong with them anyway? :S
2) not really, they are bubble sorted soon after they're added. Yes the array size is a constant so the user cannot add any more strings after the array is full and needs to delete to add anymore(obv) ..

Any hints?

Member Avatar
firstPerson
Industrious Poster
4,052 posts since Dec 2008
Reputation Points: 761 [?]
Q&As Helped to Solve: 634 [?]
Skill Endorsements: 24 [?]
 
0
 

If the order doesn't matter then what you can do is
1) find the element A
2) If A does not exist then exit
3) Else if A does exist, then swap A with the last element in the array
4) Then simply set the last element to 0, and decrease the list's size

Then your done.

For me that notation is just a little annoying and sometimes can be in the way rather than helping me.

Member Avatar
rhn94
Newbie Poster
11 posts since Dec 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

that did not really help me but what ever .. i figured it out .. i came up with this code to anyone reading who wants this

int linearSearch(List& aList,const string& strKey) 
{
	int result;
	if(aList.iListIndex <= 0)
		result = iERROREMPTY;
	else
	{
		int i;
		for (i = 0; i < aList.iListIndex && aList.strList[i] == strKey; i++){;}
		if(i == aList.iListIndex)
			result = iERRORNOTFOUND;
		else
			result = i;

	}
	return result;
}
Question Answered as of 2 Years Ago by firstPerson
You
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: