Looking for help with programming.

Post » Tue May 17, 2011 4:59 am

Howdy. I am trying to understand programming and the book says to start by understanding pseudocode. I understood it in the first few chapters, but now stuff is getting pretty wacky.

An example problem would be...

"Find the sum of the squares of the integers from 1 to MySquare, where MySquare is input by the user."

So far I have come up with...



Declare sum as Interger

Declare MySquare as Interger

Repeat

Write "Enter an interger"

Input Mysquare


That is only less than half done. I am stuck about what to write after the user inputs MySquare.

Any help on this would be really appreciated. I really need an example of a completed version of this problem but the book offers none.

This is not for school, by the way. I am just trying to learn programming in my own time.
User avatar
NIloufar Emporio
 
Posts: 3366
Joined: Tue Dec 19, 2006 6:18 pm

Post » Tue May 17, 2011 2:13 pm

Pseudocode could be expressed in many ways as long as it's understandable. All you need to do is loop from 1 to the integer given by the user and inside the loop add the current square into the sum, 1+4+9+16+... Then you write out the resulting sum after you reach the end condition. I don't know how to respond to you by not giving you the whole answer..
In Basic I would start with something like this for the loop that does the actual work.
For i = 1 to MySquare
...
Next i
User avatar
Dean
 
Posts: 3438
Joined: Fri Jul 27, 2007 4:58 pm

Post » Tue May 17, 2011 9:05 am

I don't mind if you want to give the answer. I got a ton of these problems to try and an example of a completed problem would really help.

What you said is pretty helpful in itself as well, but still pretty confusing. Programming svcks for those bad at math.
User avatar
Trevi
 
Posts: 3404
Joined: Fri Apr 06, 2007 8:26 pm

Post » Tue May 17, 2011 1:35 am

You need two variables and you already have those. Ok, one more for the index. You can use the variable i that I used in the loop. That's the index or just some integer to keep count of where we are going. Generally, you should use longer variable names, but this will do.

init
Sum can probably be assumed to be initialized to 0. And MySquare is what you get from the user.

loop
Then comes the loop above, and inside it you just add the values. sum = sum + i*i.

When the end condition is reached, you have the sum that was asked for and you can just write the value what's stored in variable sum.

Note that the Basic For construct means that the loop is run for each value from 1 to MySquare including the 1 and MySquare, and "Next i" adds 1 to i and moves execution back to the start of the loop. I don't know how they express pseudocode in your book. In Pascal style it could be something like
"for i from 1 to MySquare step 1", or
"while i <= MySquare"
They are just different ways of constructing the loop.
User avatar
Leticia Hernandez
 
Posts: 3426
Joined: Tue Oct 23, 2007 9:46 am

Post » Tue May 17, 2011 12:01 pm

First you want to find MySquare. Since this is pseudo code, we can basically write what we want as long as we get the intention across:

MySquare <- getUserInput


Next we want to do http://rogercortesi.com/eqn/tempimagedir/eqn5301.png
(don't look if you haven't done sums in maths. If you have, it may help).

In english. For every number between 1 and MySquare, we need to square it and then add it to the sum.

So first we need to define the initial value of the sum, which will be 0.

Sum <- 0


Then for every number from 1 to MySquare, we square it and add it to the Sum:

From 1 to MySquare As k    Sum <-  Sum + k*k

User avatar
Silvia Gil
 
Posts: 3433
Joined: Mon Nov 20, 2006 9:31 pm

Post » Tue May 17, 2011 2:21 am

Thanks for the help, guys. Now let me just soak in this informaton.
User avatar
Matthew Aaron Evans
 
Posts: 3361
Joined: Wed Jul 25, 2007 2:59 am

Post » Tue May 17, 2011 9:36 am

Honestly, this sounds like a pretty bad book at explaining how to learn to program IMO.

Your ability to solve problems is key to learning to program well. First step is understanding what the problem is asking. Write it out in natural language if you need to, then create a flow chart if you need to.

Once you've got a solid foundation in your ability to solve problems and think through all the little details, THEN you can go to actual real programming. I think you should at least look at some languages before doing pseudocode, as if you don't you won't have any idea how real languages behave. For example X < Y < Z makes perfect sense to you and me, but a computer doesn't read it like you and I do.
User avatar
Dominic Vaughan
 
Posts: 3531
Joined: Mon May 14, 2007 1:47 pm

Post » Tue May 17, 2011 12:23 pm

Honestly, this sounds like a pretty bad book at explaining how to learn to program IMO.

Your ability to solve problems is key to learning to program well. First step is understanding what the problem is asking. Write it out in natural language if you need to, then create a flow chart if you need to.

It may be trying to teach that, first what you do when you encounter a problem is write what you need to do without worrying to much about the implementation. The easiest way to do it might be pseudo code as a slightly more formatted version of English. IDK how the book explains pseudo code.

It could be that it wants something like this:

set MySquare to the user inputset sum to 0for the number k between 1 and MySquare    set sum to sum + k*k



In any case, functional programming makes much more sense imho. Why do we need the loop? The order doesn't matter. Ug. Imperative programming seems so ugly sometimes.
User avatar
Skivs
 
Posts: 3550
Joined: Sat Dec 01, 2007 10:06 pm

Post » Tue May 17, 2011 2:21 pm

It may be trying to teach that, first what you do when you encounter a problem is write what you need to do without worrying to much about the implementation.

Great. Then the wannabe programmer not only has no idea whether what (s)he has written is computable or not, (s)he even has absolutely no idea that it's possible that what (s)he has written is not computable.
User avatar
Kellymarie Heppell
 
Posts: 3456
Joined: Mon Jul 24, 2006 4:37 am

Post » Tue May 17, 2011 10:49 am

Great. Then the wannabe programmer not only has no idea whether what (s)he has written is computable or not, (s)he even has absolutely no idea that it's possible that what (s)he has written is not computable.

I didn't say it was a good method.

Anyway, most things are computable, unless you are asking silly questions like "Check if the program will terminate"
User avatar
Kill Bill
 
Posts: 3355
Joined: Wed Aug 30, 2006 2:22 am

Post » Tue May 17, 2011 5:25 am

I dont like this stuff either, I wish it started me off at basic or something.
User avatar
Lloyd Muldowney
 
Posts: 3497
Joined: Wed May 23, 2007 2:08 pm

Post » Tue May 17, 2011 10:09 am

Anyway, most things are computable...

...or so it seems at first.

However, a program (an algorithm) which takes k∈? input values is basically a function f: ?k → ?. For a fixed k there are ?0?0k = 2?0 = ?? such functions. Only ?0 of those are computable (which I'm too lazy to write a proof of at the moment), which leaves ?? non-computable functions. Conclusion: there are a lot more non-computable than computable functions.

Besides, http://en.wikipedia.org/wiki/List_of_undecidable_problems. (http://en.wikipedia.org/wiki/Post_correspondence_problem.)
User avatar
Samantha Jane Adams
 
Posts: 3433
Joined: Mon Dec 04, 2006 4:00 pm

Post » Tue May 17, 2011 12:00 pm

I wouldn't try learning form that book. I learned from Beginning Programming for Dummies. Goes through many of the basics of programming with a bunch of different languages.
User avatar
Josh Trembly
 
Posts: 3381
Joined: Fri Nov 02, 2007 9:25 am

Post » Tue May 17, 2011 6:15 am

...or so it seems at first.

How many of those are going to come up in a beginners programming book? I have been programming for years and not yet met a problem that I couldn't compute.

(Although cool links)
User avatar
Claire Mclaughlin
 
Posts: 3361
Joined: Mon Jul 31, 2006 6:55 am

Post » Tue May 17, 2011 1:14 pm

How many of those are going to come up in a beginners programming book?

Not many (probably none at all) but how can you know that the person learning to program won't attempt to do something non-computable in one of the exercises? Not all non-computable things are obviously non-computable and the terrible downside of pseudocode is that it's relatively easy to write something non-computable in it without even realising it, what with it being pseudocode which means you can basically write whatever the hell you want.
User avatar
Charlie Sarson
 
Posts: 3445
Joined: Thu May 17, 2007 12:38 pm

Post » Tue May 17, 2011 11:35 am

Not many (probably none at all) but how can you know that the person learning to program won't attempt to do something non-computable in one of the exercises? Not all non-computable things are obviously non-computable and the terrible downside of pseudocode is that it's relatively easy to write something non-computable in it without even realising it, what with it being pseudocode which means you can basically write whatever the hell you want.

I can't argue. If I was to write a beginning programming book, I would chose a language close to pseudo code, like Python, and use that rather than pseudo code.

Lets just say the book doesn't do it as I would.
User avatar
kennedy
 
Posts: 3299
Joined: Mon Oct 16, 2006 1:53 am

Post » Tue May 17, 2011 4:57 am

Hey. Thanks for the help so far, guys. It is starting to make more sense to me.

But I am still kind of confused what I should write after the user inputs the varible MySquare.
User avatar
Tha King o Geekz
 
Posts: 3556
Joined: Mon May 07, 2007 9:14 pm

Post » Tue May 17, 2011 5:20 am

Hey. Thanks for the help so far, guys. It is starting to make more sense to me.

But I am still kind of confused what I should write after the user inputs the varible MySquare.

You've got two choices:

1) Initialise an integer variable to 0, then loop from 1 to MySquare adding the square of the current loop value to the variable, or

2) calculate (MySquare*(MySquare+1)*(2*MySquare+1))/6.


No doubt the second choice would be the way to go in any serious program where you needed the sum of the squares of the first MySquare integers because the complexity is constant (versus the linear complexity of the first approach) but I presume the point of the exercise is the first approach.
User avatar
Elea Rossi
 
Posts: 3554
Joined: Tue Mar 27, 2007 1:39 am

Post » Tue May 17, 2011 4:21 pm

Just to give my opinion about pseudo-code.

I don't think that there is much danger in undecidable problems, but there is a very large downside when teaching beginners using pseudo-code: when writing pseudo-code one can never see code in action, and in my experience the greatest thing you can do to teach beginners is to show them why their programs don't work on concrete examples.
User avatar
noa zarfati
 
Posts: 3410
Joined: Sun Apr 15, 2007 5:54 am


Return to Othor Games