Computer Science Canada

how do you square a number?

Author:  Tricia [ Fri Oct 01, 2004 6:50 pm ]
Post subject:  how do you square a number?

Write a program to output a table of values of the integers starting at 1 and their squares. Label the table at the top of the columns.

this came right out of one of the ruing textbooks and i wrote a code but it won't run though...what am i doing wrong?

code:
setscreen ("graphics:640;480")
var sum : string
for count : 1 .. 5
    put "Number     Square"
    put count, sum
    sum := count * count
end for


you might think i'm kind of stupid not to know this but i love computer programming and i am just not good at loops Embarassed

Author:  bugzpodder [ Fri Oct 01, 2004 6:55 pm ]
Post subject: 

try swapping your second-last and third last line

Author:  Tricia [ Fri Oct 01, 2004 6:57 pm ]
Post subject: 

i tried it's not working it seems that there was something wrong with my second count...on the sum := count * count part

Author:  wtd [ Fri Oct 01, 2004 7:01 pm ]
Post subject: 

bugzpodder wrote:
try swapping your second-last and third last line


In case that's ambiguous, he meant:

code:
setscreen ("graphics:640;480")
var sum : string
for count : 1 .. 5
    put "Number     Square"
    sum := count * count
    put count, sum
end for


Tricia, are you familiar with writing functions?

Author:  Tricia [ Fri Oct 01, 2004 7:07 pm ]
Post subject: 

um...i did do what bugzpodder was saying to me...but it's still not working it highlights the second count i put in

and honestly i am not all that expert in functions Embarassed

Author:  Tricia [ Fri Oct 01, 2004 7:14 pm ]
Post subject: 

can anyone help me please?

Author:  wtd [ Fri Oct 01, 2004 7:20 pm ]
Post subject: 

Well, I'm just guessing, but I think you want the header:

code:
put "Number     Square"


To only be printed once. As it is, it gets printed each time the loop repeats.

Author:  Tricia [ Fri Oct 01, 2004 7:26 pm ]
Post subject: 

i already know how to do the header but i also need to have numbers and square them. in the book it says....it should look lyk this

Number Square
1 1
2 4
3 9
4 16
5 25

so what code should i put in?

Author:  Tricia [ Fri Oct 01, 2004 7:29 pm ]
Post subject: 

except the squared numbers should be under the square heading

Author:  wtd [ Fri Oct 01, 2004 7:36 pm ]
Post subject: 

You could use tabs rather than spaces.

code:
setscreen ("graphics:640;480")
var sum : int
put "Number\tSquare"
for count : 1 .. 5
    sum := count * count
    put count, "\t\t", sum
end for


Try that.

Author:  Tricia [ Fri Oct 01, 2004 7:40 pm ]
Post subject: 

thanks it worked this time Very Happy

Author:  wtd [ Fri Oct 01, 2004 7:57 pm ]
Post subject: 

You're welcome.

Now, the real question is, do you understand why it works?

Author:  Tricia [ Fri Oct 01, 2004 8:33 pm ]
Post subject: 

yup...my main mistake was putting the put statement under the for statement it messes up my whole program Very Happy

Author:  wtd [ Fri Oct 01, 2004 9:55 pm ]
Post subject: 

Good. It's better to fail and know why than to succeed and think it's magic.

Author:  AsianSensation [ Fri Oct 01, 2004 11:02 pm ]
Post subject: 

Thinking I thought it was because in the original code, it was
code:
var sum : string
and then you tried to assign an integer value to a string. That's why it was wrong?

Author:  wtd [ Fri Oct 01, 2004 11:10 pm ]
Post subject: 

AsianSensation wrote:
Thinking I thought it was because in the original code, it was
code:
var sum : string
and then you tried to assign an integer value to a string. That's why it was wrong?


Well, that was wrong, but there were other problems too.

Can't believe I missed that, actually.

Author:  Jonny Tight Lips [ Sat Oct 02, 2004 12:24 pm ]
Post subject: 

Also just for other referance if you wanted to do squaring the easy way you can just use **2, the **2 is the sign for squaring in turing. Just so yah know. And **3 is cubed and so on. Well That doesn't really help with the probem but it is a good bit of info.

Author:  Cervantes [ Sat Oct 02, 2004 5:36 pm ]
Post subject: 

mind you, tony has told us before that that method is slower than actually writing out count * count. by slower we are talking about the program runs slower, not its slower to type.

Author:  Jonny Tight Lips [ Sat Oct 02, 2004 5:39 pm ]
Post subject: 

really, I did not know that. But in a such a small program would it really make that much of a difference? How much slower are we talking about?

Author:  wtd [ Sat Oct 02, 2004 5:49 pm ]
Post subject: 

Jonny Tight Lips wrote:
really, I did not know that. But in a such a small program would it really make that much of a difference? How much slower are we talking about?


No, it wouldn't, and it better conveys the intention of the programmer.

Premature optimization is evil.

Author:  Cervantes [ Sat Oct 02, 2004 7:04 pm ]
Post subject: 

Jonny Tight Lips wrote:
really, I did not know that. But in a such a small program would it really make that much of a difference? How much slower are we talking about?


Truly, it doesn't make any difference when outside of a loop or a really big for loop.
if you were to write your own Math.Distance(x1, y1, x2, y2 : int) : real function, for example, you would want to write it out rather than do **2.

says tony. 8)

Author:  gigaman [ Thu Oct 07, 2004 8:08 am ]
Post subject: 

so if i have something like a basic calculator program will it make a difference in the speed?

Author:  the_short1 [ Tue Oct 26, 2004 3:29 pm ]
Post subject: 

you can pass a intergert to a string variable.. u just cant do operations ..

like

var sum : string


yo ucan do


sum := 82342+11

but you cant

blah : = sum - 5 .. . that dont work unless u go

intstr or strint
...

and hmm thx for the info on ** being slow.. thats what i normally used.. but now i guess ill write it out long way..

Author:  djlenny_3000 [ Tue Oct 26, 2004 7:17 pm ]
Post subject: 

1 question, how did tony actually prove that the **2 is slower, i mean if its like 0.01 seconds longer, its not a diff, i mean what kind of a person can't spare 0.01 seconds, now that i know ill probably change it but can some1plz tell me the actual time difference

Author:  gigaman [ Wed Oct 27, 2004 11:40 am ]
Post subject: 

DJ i think the idea is that if you are making a large program or you do sumthn like
num:=2**100
num:=num**100
num:=num**100
etc.
it will make a difference. tell me if i'm right

Author:  Mazer [ Wed Oct 27, 2004 4:27 pm ]
Post subject: 

djlenny_3000 wrote:
1 question, how did tony actually prove that the **2 is slower, i mean if its like 0.01 seconds longer, its not a diff, i mean what kind of a person can't spare 0.01 seconds, now that i know ill probably change it but can some1plz tell me the actual time difference


I don't know how exactly tony proved it, but here's one way:
code:

var num := 5
for i : 1 .. 20
    put num ** 2
end for
put Time.Elapsed


code:

var num := 5
for i : 1 .. 20
    put num * num
end for
put Time.Elapsed


You can run those two programs and compare the last number that each outputs. Of course, you should run the programs several times and maybe get an average.
The first time I went to test it, **2 came out quite a bit slower, but when I went to try it again they really weren't far enough apart to worry about speed.

Author:  Andy [ Wed Oct 27, 2004 5:23 pm ]
Post subject: 

since turing is written in c++, they basicly switch the ** with pow(), and pow is kinda inefficient... thats why c++ programmer usually do num*num instead of pow(num,2)


: