Forums: » Register « |  Free Tools |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support |

New Free Tools on Dev Shed!

#1
October 12th, 2009, 01:27 PM
 Reapersoul
Registered User

Join Date: Oct 2009
Posts: 1
Time spent in forums: 7 m 29 sec
Reputation Power: 0

Hey guys,

I'm having some problems writing out basic algorithms and was hoping some of you could (please) give me a helping hand ?

We have been assigned this question :

"Write algorithms which return true or false for the following tasks:
(i) Given as input an object x and a set S, return true if x ∈ S, and false if x ∉S
(ii) Given as input a set S and another set T, return true if S ⊆ T, and false if S ⊆ T
You can assume a command getNextElement(S) which returns the next element of a set S (i.e. the first element of S that has not been given to you before). So you can have a line in your algorithm which says, for example, y ← getNextElement(S). So if you had a set S = {1,2,3}, and you had the following lines in your algorithm
y ← getNextElement(S)
z ← getNextElement(S)
then y would have the value 1, and z would have the value 2. If S has no more elements, then getNextElement(S) returns the special value NULL."

Now, for part one, I am thinking an answer could be :

Input : Object X and set S.
 If x ∈ S
 Return a value of true.
 If x ∉ S
 Return a value of false.

I have a feeling I'm missing out on some basic stuff, but my lecturer has decided to just blaze on ahead with giving us examples, rather than explaining. Which is fair enough, its probably the best way to learn, its just that the course is continuous assessment and in order to qualify for my doctorate, I need a first class honors ! Hence I need to buck up, and quickly !

I have absolutely no idea how to tackle the second problem either.

Thanks a lot.

#2
October 12th, 2009, 03:42 PM
 requinix
Forgetful

Join Date: Mar 2007
Location: Washington, USA
Posts: 13,504
Time spent in forums: 5 Months 2 Weeks 2 Days 8 h 37 m 57 sec
Reputation Power: 9259
Quote:
 Originally Posted by Reapersoul Input : Object X and set S.  If x ∈ S  Return a value of true.  If x ∉ S  Return a value of false.

That's... not an algorithm. An algorithm is a step-by-step process you can follow to do something: what you've given doesn't describe how to do a ∈ or ∉.

If I gave you a bag of marbles and asked you if there was a blue and green striped marble in the bag, how would you go about finding the answer?

For part two, imagine I gave you two bags of marbles and asked if the second bag had an identical copy of all the marbles in the first bag. Hint: you'll be using the answer to part (i).
jwdonahue agrees!

#3
October 13th, 2009, 11:47 PM
 jwdonahue
Contributing User

Join Date: May 2004
Posts: 3,417
Time spent in forums: 3 Weeks 5 Days 12 h 51 m 9 sec
Reputation Power: 886
Quote:
 Originally Posted by Reapersoul the course is continuous assessment and in order to qualify for my doctorate, I need a first class honors ! Hence I need to buck up, and quickly !

Please tell me your not a graduate student. If you are working on a doctorate in information or computer science, this should already be old hat for you.

Look up Donald Knuth. Also try googling algorithm. The Wikipedia has some coverage as well.

Algorithm for getting to work on time:

1. Set alarm to 8am.
2. Sleep until alarm goes off.
3. Kill the bloody alarm!
4. Have a quicky (if she's in the mood).
5. If quicky, get dressed and run out the door, else have breakfast first.
6. Drive car to office, taking short-cuts and avoid traffic jams. Don't speed! Cops take forever to write tickets.
7. Don't chat up the cute secretary on the way in the door. Race to your office, fire up your computer and broadcast a "good morning" message to co-workers so everyone knows your their on time.
__________________
I no longer wish to be associated with this site.

 Viewing: Dev Shed Forums > Programming Languages - More > Software Design > Basic Algorithm help please ?