Click here to Skip to main content
15,888,351 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
In this revision guide (not homework btw just me trying to learn more) there is a question on a pseudo-code and a trace table based on it. I can't interpret it because I don't understand the pseudo-code, and I would appreciate it if you could explain it to me fully. Please be aware that I am new to this and do not know a lot. Thanks.

Pseudo-code:

SET list TO [3, 6, 9, 13, 17, 21]
SET found TO False
RECEIVE item FROM (INTEGER) KEYBOARD
FOR index FROM 0 TO LENGTH (list) - 1 DO
IF item = list[index] THEN
SET found TO True
SEND 'Found' TO DISPLAY
END IF
END FOR

What I have tried:

I have re-read it, yet all of it; especially the list, index and found part (which is basically the whole code) still doesn't make sense to me. Thanks.
Posted
Updated 14-Feb-18 3:11am
v2

Here is this code step by step:
SET list TO [3, 6, 9, 13, 17, 21]
You have a list of things you are going to do something with. The numbers you are going to do something with are 3,6,9,13,17 and 21 in that order.
SET found TO False
Set up a variable to signal (flag) to you when you have found something that you are looking for.
RECEIVE item FROM (INTEGER) KEYBOARD
Accept some input from the user. It must be an integer (whole number) and we are going to call it "item"

I'm going to pause here as I think this is the bit that you are struggling with.
Each number in that list we set up at the beginning has a "position" in the list.
3 is 1st, 6 is 2nd, 9 is 3rd etc.
We refer to that position as it's "index". In many programming languages that index starts at 0. I.e.
3 is 1st, has index 0
6 is 2nd, has index 1
9 is 3rd, has index 2
...
21 is last, has index 5
The list also has a concept of "length" - or the number of entries in the list. In this case we have 6 entries in the list. But remember, the index of each item started at 0! If we tried to look at the list's item at position index 6 we would get an error ... remember the index of the last number (21) is 5. So this line:
FOR index FROM 0 TO LENGTH (list) - 1 DO
means "step through the list items one-by-one starting at the beginning (0) right through to the end (length - 1)
if item = list[index] THEN
This line is checking to see if the item that the user entered via the keyboard is the same as (=) the number that is at position index in the list. So the first time through it will check item = list[0] then the next loop will check item = list[1]If the numbers do match then perform the actions that follow up to the END IFI.e.
SET found TO True
SEND 'Found' TO DISPLAY
The first line is setting that "flag" or "signal" to True, which means "yes, we have found a match". You then let the user know it's been found by displaying the word 'Found'.

Does that help?

As OriginalGriff has pointed out this is not the way to learn. I'm hoping that this detailed explanation is going to be sufficient to let you carry on on your own now. I'm afraid you will get short shrift if you keep posting questions like this. We are not a tutorial site.
 
Share this answer
 
Comments
tgspython 14-Feb-18 8:52am    
Thank you for your help. I know this is not specifically a tutorial site yet it's a place where you can teach, learn and help other whilst having fun. I am posting questions like this because I need help and want to learn more. However, thank you for your help and time. I very much appreciate your kindness.
Us explaining each and every piece of code you find Can you please explain this pseudo-code to me?[^] isn't goign to help you learn.

Read the pseudo code, read the task it is supposed to perform, and work out what it is doing from that - this is pretty obvious stuff, if you have followed your course at all you should understand this - especially if as you say these are not your homework but your revision guides.
If you are already revising, it's a bit late to not understand the stuff which was probably covered at the start of your course...
 
Share this answer
 
Comments
tgspython 14-Feb-18 8:58am    
Okay, but you saying that also doesn't help me. I have a bad teacher who doesn't know himself. And like I said I am new to this and the course just started. I did read it and tried to work it out myself, for someone like you who knows and has been taught well - whether self taught or not - it's pretty obvious. But please take into consideration that it's not the same for others. I came here for help and to learn more and have fun, which is what CodeProject is about. We all learn differently and actually the solution(s) I received which were proper helped me understand it more and now I know those functions for next time, so yes it does help me learn. Thank you for your time but if you can't help people with their questions please don't waste your time answering and making assumptions that people are "folling their course" etc.
OriginalGriff 14-Feb-18 10:38am    
We are always willing to help people - but those people have to be trying to help themselves. And you aren't showing any signs of that, you are just looking at a question, looking at the answer and going "how does that work"?
And if you think that is a good way to learn how to do it, you are very, very wrong. Coding is a skill, not a rote-memory thing, and like all skills you learn by doing, not by reading. Think about it: you can read about how to skateboard, or surf all you like, but that doesn't help when you first get on one and find that you don't know how to balance because your body hasn't learned that yet. Practice "doing skateboarding" or "doing surfing" and your skill improves, it becomes second nature.
But watching someone surf doesn't prepare you in any way for actually doing it!
Seriously: stop looking at the answers, and try doing the questions. If you don't you will never learn how to do them.
tgspython 14-Feb-18 11:09am    
Okay, thank you for the advice. Sorry for any misunderstandings but I did try myself and wasn't a 100% so I just wanted to know the correct thing before I could move on. Next time I will add more to what I have tried and my thoughts into the 'What I have tried:' box to make it more clear what I have tried to do to help myself. Thank you for your time and advice though! Have a nice day.
OriginalGriff 14-Feb-18 11:15am    
:thumbsup:
You're welcome!
Please read Code Project Quick Answers FAQ[^]. If you really do not understand the basics then you should get hold of a book, or try some online tutorials. For Python you can start at The Python Tutorial — Python 3.4.8 documentation[^].
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900