Author |
Message |
Tricia
|
Posted: 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 |
|
|
|
|
|
Sponsor Sponsor
|
|
|
bugzpodder
|
Posted: Fri Oct 01, 2004 6:55 pm Post subject: (No subject) |
|
|
try swapping your second-last and third last line |
|
|
|
|
|
Tricia
|
Posted: Fri Oct 01, 2004 6:57 pm Post subject: (No subject) |
|
|
i tried it's not working it seems that there was something wrong with my second count...on the sum := count * count part |
|
|
|
|
|
wtd
|
Posted: Fri Oct 01, 2004 7:01 pm Post subject: (No 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? |
|
|
|
|
|
Tricia
|
Posted: Fri Oct 01, 2004 7:07 pm Post subject: (No 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 |
|
|
|
|
|
Tricia
|
Posted: Fri Oct 01, 2004 7:14 pm Post subject: (No subject) |
|
|
can anyone help me please? |
|
|
|
|
|
wtd
|
Posted: Fri Oct 01, 2004 7:20 pm Post subject: (No 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. |
|
|
|
|
|
Tricia
|
Posted: Fri Oct 01, 2004 7:26 pm Post subject: (No 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? |
|
|
|
|
|
Sponsor Sponsor
|
|
|
Tricia
|
Posted: Fri Oct 01, 2004 7:29 pm Post subject: (No subject) |
|
|
except the squared numbers should be under the square heading |
|
|
|
|
|
wtd
|
Posted: Fri Oct 01, 2004 7:36 pm Post subject: (No 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. |
|
|
|
|
|
Tricia
|
Posted: Fri Oct 01, 2004 7:40 pm Post subject: (No subject) |
|
|
thanks it worked this time |
|
|
|
|
|
wtd
|
Posted: Fri Oct 01, 2004 7:57 pm Post subject: (No subject) |
|
|
You're welcome.
Now, the real question is, do you understand why it works? |
|
|
|
|
|
Tricia
|
Posted: Fri Oct 01, 2004 8:33 pm Post subject: (No subject) |
|
|
yup...my main mistake was putting the put statement under the for statement it messes up my whole program |
|
|
|
|
|
wtd
|
Posted: Fri Oct 01, 2004 9:55 pm Post subject: (No subject) |
|
|
Good. It's better to fail and know why than to succeed and think it's magic. |
|
|
|
|
|
AsianSensation
|
Posted: Fri Oct 01, 2004 11:02 pm Post subject: (No subject) |
|
|
I thought it was because in the original code, it was and then you tried to assign an integer value to a string. That's why it was wrong? |
|
|
|
|
|
|