In school - I'm not a CS major, and I'm STRUGGLING with psuedocode and algorithms. Would someone please explain this question/answer from my homework?
Posted by Cognitive_Realm57@reddit | learnprogramming | View on Reddit | 32 comments
Hi all,
As stated, I'm not any kind of coding/computer major, I'm a math major, but I can't seem to wrap my head around this. They say everything is supposed to be pseudocode, which is not a specific language, but it seems pretty specific to me! This is the homework problem: Devise an algorithm that finds the first term of a sequence of positive integers that is less than the immediately proceeding term of the sequence.
I'll add the book's answer in a picture below. I would greatly appreciate if anyone would explain would explain what it means, and why you would use it in pseudocode. And how you know you need all those parts!
HelpfulFriend0@reddit
There's a few parts that you could be struggling with
Do you understand what the question is asking? Do you know what "sequence of positive numbers" "proceeding term" etc mean? If not - then maybe work on wrote learning some definitions. If you do understand the words but can't come up with what it's asking - maybe talk to your professor and see if they can help. Unfortunately this part is kinda just "getting it". We can't really help you "get it"
Now that you know what's being asked, do you know how you would solve this as a human? Have you tried drawing any diagrams? Have you tried just explaining how you'd do things to a friend or a family member?
Once you have a list of steps as a human, are any of these "too high level" for pseudo code? Do you need to flesh any of this out more?
Once you have an idea of where you're stuck you'll be able to ask your prof better qns
Cognitive_Realm57@reddit (OP)
yes, I totally understand the question and what it's asking. You have a string of numbers and you want the location of the first one that's smaller than the proceeding. My initial answer was roughly:
location integers 1-n
for value 2<value 1 return location
else compare next values, continue to n
This is the book's answer: https://imgur.com/a/LBUbaoN
Which seems much more like an actual language to me. Is my understanding of pseudocode wrong? What does the i indicate? And what is the purpose of defining it that way?
danzerpanzer@reddit
You need to be more verbose and explicit with your pseudo-code, enough so that another programmer could understand what you want the computer to do even if they did not see the question. With your solution, the reader has to fill in too many details, like where do values 2 and 1 come from.
HelpfulFriend0@reddit
i is the index in the array you are looking at, have you learned about arrays?
And yeah it's psuedo code relative to c++ and c, but honestly I'd just write the python code for this. Pseudo code just means code that doesn't necessarily need to compile but sound about correct. So honestly "bad" python code fits the description for pseudo code as long as you're not really using language features like decorators and objects and stuff
Timanious@reddit
i is for index usually because you would use it inside the loop to itterate trough the length of an array using it as the index.
carcigenicate@reddit
What are you having difficulty with? Those instructions seem pretty plain and straightforward.
And you use pseudocode when you want to convey an algorithm but don't want to tie it to a particular language. A language like Python is probably a better choice a lot of the time because it's minimal and easy to write, but if they want you to use pseudocode, use pseudocode.
Cognitive_Realm57@reddit (OP)
This is the book's answer: https://imgur.com/a/LBUbaoN
I'm having difficulty with understanding all the parts of the code, and why are they necessary if I'm not writing in a specific language. What does the i indicate? also is the else statement saying go to the next integer?
nderflow@reddit
The book answer also has a well defined behaviour for sequences of less than 2 items.
willywillycow@reddit
it could be simplified to:
let i = 0
while a[i] < a[i+1] then
i += 1 #or i = i+1
return i
danzerpanzer@reddit
What happens if each integer in the sequence is equal to or greater than the one before it? What gets you out of the while loop?
arcticslush@reddit
You've introduced a bug - you have to return the first term that decreases.
Cognitive_Realm57@reddit (OP)
Maybe it's just the redundancy that's confusing me. Your code makes perfect sense
willywillycow@reddit
also the answer is quite unnecessarily redundant
willywillycow@reddit
for loop is different from while loop, but could be done both ways
carcigenicate@reddit
Like I said, the purpose of pseudocode is to convey algorithms. You need to include everything that would be required when the algorithm is translated to a particular language. If you wrote this same algorithm in Python, the code would look very similar.
And for
i, note howiis used. It's even using math-y notation there, which I'm assuming you're familiar with.context_switch@reddit
As you've probably seen in the other comments, pseudocode is not a specific syntax that anyone agrees on. You'll need to figure out what your professor expects. It should be easy to translate into an actual programming language syntax.
Using your problem as an example, I would start with describing the algorithm in plain language:
(note that the answer in the book is slightly different in that at step 3 it stores the index, then the loop condition checks if there is a stored value.)
This can be converted to code many ways. I'll use C# syntax since that's what I'm most comfortable with, but it would be nearly the same in any language that uses C-like syntax:
The pseudo-code used in the book is somewhere in between these two. It uses a mix of English words and common programming syntax (such as
:=for assignment, which was probably borrowed from the programming language Pascal;:=for assignment was used to differentiate from=used for testing equality).How you represent the actions in pseudo-code are largely up to you, but they should be simple steps like what you would put in code. Any complex logic should be broken into separate steps, and if you refer to them multiple times, consider if a loop or a sub-function would make it easier to organize.
PhilNEvo@reddit
It's because you're not being specific enough. Yes, pseudocode is supposed to be specific, not specific with regards to a particular programming language, but specific as to the exact steps that has to go on, if you wanted to convert it to some programming language.
What does "Location integers" mean? why is it 1-n?
"For value 2 < value 1 return location" is this supposed to be a conditional statement, in that case the usual terminology is "If value 2 < value 1
return location"
"For" is usually used for loops. Besides, you haven't described how you're iterating or what you are iterating through? What is "value 2" and "Value 1" supposed to be? and is the "location" you're returning the "1-n"? how does "1-n" tell you which location you're currently at?
And when you write "Else continue to n"... how are we continuing? What is the next step?
Pseudocode is supposed to be easily readable and convertable to code, but I'm pretty sure if I tried to convert your pseudocode into code, I wouldn't have anything functional.
Gennwolf@reddit
Your answer has statements with implicit meaning that you only know because of the context of the problem. The book solution can be read and understood without the context.
Going by what you posted, e.g. without context it's unlear what "value 2" references. It could be the literal number "2" or always the second number of the range. Also "continue" seems to imply incrementing the "location", but again only in the context.
For pseudocode, writing something like "increment location by one" should be valid, but "location := location + 1" is just closer to real programming languages and is easier to parse, at least for people used to reading code.
mxldevs@reddit
Pseudo code is intended to be language-agnostic, such that it describes the algorithm without relying on knowing any particular language, but your school or prof may have certain conventions, or even their own pseudocode syntax.
If your prof is following whatever the book says, you need to learn how to write in that syntax.
If you're asking why you need to do that, the answer is "because he's the one dictating right or wrong"
If you're asking how someone knows to use a loop or a variable or a condition, that's basically what programming is all about: how to use those programming concepts to create an algorithm that will solve the problem.
If you're actually asking, what exactly are all of these loops, functions, conditions, etc programming concepts, did you skip an introductory programming course? Or do they expect that you already know how to program?
mxldevs@reddit
Pseudo code is a higher level than a specific language, but your school or prof may have certain conventions, or even their own pseudo code syntax.
If your prof is following whatever the book says, you need to learn how to write in that syntax.
ffrkAnonymous@reddit
The main problem is that "gist" is not good enough. I'm reading your instructions and they're meaningless.
As a math major, would you write equations this sloppily?
Integral 1-n 2, 1 location n
Cognitive_Realm57@reddit (OP)
I totally get what you are saying, and no I wouldn't - that's part of what's driving me crazy. I think I'd do better just learning the actual structure of a language.
willywillycow@reddit
I think the reason you got wrong is because you used a for loop and that's not how for loop works, according to your original answer
Cognitive_Realm57@reddit (OP)
So a while loop would be more correct?
ItemAdept7759@reddit
Okay we can see some immediate differences here.
First, the book gives the procedure a name and lists the arguments that must be supplied to it.
It explicitly initialises the variables (and describes the intended meaning after the procedure).
I have no idea what n is, because it's not declared or initialised anywhere.
The book also explicitly returns from the procedure.
So, in short your code is a bit too vague, you need to be a little bit more formal, declare your variables, declare your procedure signature (name + arguments) and explicitly return.
Light_Matter_@reddit
I think pseudocode is one of the most useless thing to teach in schools. It just make everything more confusing for some people. Better to use Python and code things.
In the book they basically write the code (algorithm) which you have in English in the question. The code is defining the variables (position, i) and the looping (while) over the row of numbers (integers). Until it find the result or reach the end (n). The if/else is a condition to check the current state.
Not sure if it's understandable better though. 😎😄
willywillycow@reddit
for i in myArray do if myArray[i] < myArray[i + 1] then return i ahh
willywillycow@reddit
pseudocode is really just words, it's English
just "how would you wire this in your brain"
like if I'm to pay with card then it's like: if cardInserted and passwordInputted then completeTransaction()
DoubleOwl7777@reddit
pseudocode is just something you write down to understand the logic of a program, its not a standart or anything.
Cognitive_Realm57@reddit (OP)
I keep getting marked off because it's not right. But I can't figure out why it's not right, if there's no standard. This is the books answer: https://imgur.com/a/LBUbaoN
DoubleOwl7777@reddit
maybe its too vague, the book is closer to actual code than you are.
Cognitive_Realm57@reddit (OP)
https://imgur.com/a/LBUbaoN