Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 how do you square a number?
Index -> Programming, Turing -> Turing Help
Goto page 1, 2  Next
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Tricia




PostPosted: 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
Sponsor
Sponsor
Sponsor
sponsor
bugzpodder




PostPosted: Fri Oct 01, 2004 6:55 pm   Post subject: (No subject)

try swapping your second-last and third last line
Tricia




PostPosted: 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




PostPosted: 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




PostPosted: 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 Embarassed
Tricia




PostPosted: Fri Oct 01, 2004 7:14 pm   Post subject: (No subject)

can anyone help me please?
wtd




PostPosted: 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




PostPosted: 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
Sponsor
sponsor
Tricia




PostPosted: Fri Oct 01, 2004 7:29 pm   Post subject: (No subject)

except the squared numbers should be under the square heading
wtd




PostPosted: 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




PostPosted: Fri Oct 01, 2004 7:40 pm   Post subject: (No subject)

thanks it worked this time Very Happy
wtd




PostPosted: 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




PostPosted: 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 Very Happy
wtd




PostPosted: 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




PostPosted: Fri Oct 01, 2004 11:02 pm   Post subject: (No 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?
Display posts from previous:   
   Index -> Programming, Turing -> Turing Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 2  [ 27 Posts ]
Goto page 1, 2  Next
Jump to:   


Style:  
Search: