
-----------------------------------
Tricia
Fri Oct 01, 2004 6:50 pm

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?

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  :oops:

-----------------------------------
bugzpodder
Fri Oct 01, 2004 6:55 pm


-----------------------------------
try swapping your second-last and third last line

-----------------------------------
Tricia
Fri Oct 01, 2004 6:57 pm


-----------------------------------
i tried it's not working it seems that there was something wrong with my second count...on the sum := count * count part

-----------------------------------
wtd
Fri Oct 01, 2004 7:01 pm


-----------------------------------
try swapping your second-last and third last line

In case that's ambiguous, he meant:

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?

-----------------------------------
Tricia
Fri Oct 01, 2004 7:07 pm


-----------------------------------
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  :oops:

-----------------------------------
Tricia
Fri Oct 01, 2004 7:14 pm


-----------------------------------
can anyone help me please?

-----------------------------------
wtd
Fri Oct 01, 2004 7:20 pm


-----------------------------------
Well, I'm just guessing, but I think you want the header:

put "Number     Square"

To only be printed once.  As it is, it gets printed each time the loop repeats.

-----------------------------------
Tricia
Fri Oct 01, 2004 7:26 pm


-----------------------------------
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?

-----------------------------------
Tricia
Fri Oct 01, 2004 7:29 pm


-----------------------------------
except the squared numbers should be under the square heading

-----------------------------------
wtd
Fri Oct 01, 2004 7:36 pm


-----------------------------------
You could use tabs rather than spaces.

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.

-----------------------------------
Tricia
Fri Oct 01, 2004 7:40 pm


-----------------------------------
thanks it worked this time :D

-----------------------------------
wtd
Fri Oct 01, 2004 7:57 pm


-----------------------------------
You're welcome.

Now, the real question is, do you understand why it works?

-----------------------------------
Tricia
Fri Oct 01, 2004 8:33 pm


-----------------------------------
yup...my main mistake was putting the put statement under the for statement it messes up my whole program  :D

-----------------------------------
wtd
Fri Oct 01, 2004 9:55 pm


-----------------------------------
Good.  It's better to fail and know why than to succeed and think it's magic.

-----------------------------------
AsianSensation
Fri Oct 01, 2004 11:02 pm


-----------------------------------
:think: I thought it was because in the original code, it was var sum : string and then you tried to assign an integer value to a string. That's why it was wrong?

-----------------------------------
wtd
Fri Oct 01, 2004 11:10 pm


-----------------------------------
:think: I thought it was because in the original code, it was 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.

-----------------------------------
Jonny Tight Lips
Sat Oct 02, 2004 12:24 pm


-----------------------------------
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.

-----------------------------------
Cervantes
Sat Oct 02, 2004 5:36 pm


-----------------------------------
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.

-----------------------------------
Jonny Tight Lips
Sat Oct 02, 2004 5:39 pm


-----------------------------------
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?

-----------------------------------
wtd
Sat Oct 02, 2004 5:49 pm


-----------------------------------
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.

-----------------------------------
Cervantes
Sat Oct 02, 2004 7:04 pm


-----------------------------------
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)

-----------------------------------
gigaman
Thu Oct 07, 2004 8:08 am


-----------------------------------
so if i have something like a basic calculator program will it make a difference in the speed?

-----------------------------------
the_short1
Tue Oct 26, 2004 3:29 pm


-----------------------------------
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..

-----------------------------------
djlenny_3000
Tue Oct 26, 2004 7:17 pm


-----------------------------------
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

-----------------------------------
gigaman
Wed Oct 27, 2004 11:40 am


-----------------------------------
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

-----------------------------------
Mazer
Wed Oct 27, 2004 4:27 pm


-----------------------------------
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:

var num := 5
for i : 1 .. 20
    put num ** 2
end for
put Time.Elapsed



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.

-----------------------------------
Andy
Wed Oct 27, 2004 5:23 pm


-----------------------------------
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)
