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

Username:   Password: 
 RegisterRegister   
 Variables/Values Help
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
-JP-




PostPosted: Sun Mar 06, 2005 4:20 pm   Post subject: Variables/Values Help

Well, I just started Turing and searched for any topics related to my problem, but no luck found.

There are a couple of problems I need of help.

1) Switching values contained in variables.
If I start with this:
Quote:
var x: int
var y: int
x:= 7
y:= 4
x:= y
y:= x

The result would be 4 for each variables, while y, after switching the values, is suppose to be 7. I need on how to solve this problem and getting the results for each variables (after switching the values) correctly.

2) Writing a program that asks the user for a three-digit number, find the sum of the digits of the number.

I can get up to the part where asking the user to input in a three-digit number, but I don't know how to get the sum of the digits of the number by coding. I have no experience on splitting the digit numbers as a variable, sorry ><;;

Thanks~
- JP
Sponsor
Sponsor
Sponsor
sponsor
Delos




PostPosted: Sun Mar 06, 2005 5:50 pm   Post subject: (No subject)

Hello and welcome. Good to see that you have some experience in posting well structured questions. That shall serve you well.

1)
What you need here is a third variable. Think about having three buckets, two with water. One of them has red water, the other one has blue water. Now, if you wanted to switch water from one bucket to the other, you wouldn't just pour it straight in would you?! You'd end up with purple water...and no one ever wants purple water...
So, you pour the red water into the empty bucket, then pour the blue water into the bucket you just used, then transfer the red water into the other bucket...simple eh?

2)
What you need to do here is understand the concept of counters. They're really really simple. A counter is just a fancy name for a numerical variable - given due to its function. Say you have a bucket. (Razz). You ask people for marbles. Each time, the person says, "Sure, I'll give you x marbles.", where 'x' is a number. So, you drop the marbles into the bucket. At anytime, you can look at the number of marbles in the bucket, and that will be your total.
Since you may not be aware of these commands, here's some simple syntax for you to learn:

code:

var counter : int := 0
% Notice that this was initialized straight away.  It'll become important soon.
var marbles : int

put "Can I have some marbles?"
get marbles

counter := counter + marbles
% In this line, the variable counter has had the contents of 'marbles' put into it.  Also, this line will not work if the variable 'counter' had not already been initialized!!!

put "Can I have some more please?"
get marbles

counter += marbles
% This line does exactly the same thing as the other one, but is written in condensed format.  Useful when you need to code quicikly.

put "My total now is ", counter


With respect to that note about initializing...try remove the initialization step from the declaration (the " := 0" part). Run it again and see when it crashes...

Ok, that's about it. Have fun.
person




PostPosted: Sun Mar 06, 2005 6:20 pm   Post subject: (No subject)

1) the thrid line makes x equal 4 (in simpler terms)

2) get the variables and add them together (isnt that wat u want)?
Cervantes




PostPosted: Sun Mar 06, 2005 7:29 pm   Post subject: (No subject)

Hi JP! Posted Image, might have been reduced in size. Click Image to view fullscreen.
-JP- wrote:

2) Writing a program that asks the user for a three-digit number, find the sum of the digits of the number.

I can get up to the part where asking the user to input in a three-digit number, but I don't know how to get the sum of the digits of the number by coding. I have no experience on splitting the digit numbers as a variable, sorry ><;;


What Delos said works fine, but it limits you the user to entering one digit at a time. If that's all that is required, cool. But, if the user needs to input the three digit number all at once, it's not so easy. Because you still have to determine the value of each digit.
There are two ways to do this. One involves a little bit of math, and the other involves string manipulation. Both will involve commands you've probably never seen before.

Here's the math method, shortened to just two digits:
code:

var num := 98 
%There's no need to do var num : int := 98.  Turing knows 98 is an integer.  This is just shorter.  :)
var hundreds, tens, ones : int
ones := num mod 10
tens := (num mod 100 - ones) div 10
put  tens, ones

the mod command takes the number before it (in our case, this is the variable, num) and divides it by the after it (in the case of the third line of code, that's a 10). So, that's 98 divided by 10. That's 9 remander 8. 8 is the final result of that line. So we just made ones = 8, which happens to be the number in the ones column! Next line. Since we're checking for tens, we have to mod by 100, instead of 10. 98 mod 100... that's 98 divided by 100, which is 0 remander 98. Okay, but that's not what we want. Hey! We could just get rid of that 8, because that's the ones digit from the original number. So we subtract the value we got for the variable 'ones'. So 98 - 8 = 90. Woot! Now, divide it by 10 and we'll get the number that fits in the tens column! Huzaa!

Wicked. Now, the second method. String Manipulation.
First, we need to know a little bit about strings. Know that if you have a string, you can get any character of that string by putting (#) after the name of the string variable. Code:
code:

var word := "The Beatles"
%once again, Turing knows this is a string.  Let's shorten it up. :)
put word (1) %output character 1 from word
put word (2)
put word (4)
put word (7)
put word (3 .. 8) %output all charcters between 3 and 8 from word, inclusive
put word (*) %output the last character of word
put word (6 .. *) %output all characters from 6 to the end, inclusive
put word (5 .. * -2) %output all characters from 5 to the third last character, inclusive

Cool, eh? It's going to come in handy.
So, we've got out three digit number. Only it's not an integer, it's a string.
code:

var number := "732"

We can output the hundreds digit, tens digit, and ones digit easily.
code:

put "Hundreds Digit: ", number (1)
put "Tens Digit: ", number (2)
put "Ones Digit: ", number (3)

Great. But, the thing is, we can't add the digits up, because their strings. (When you try to add strings, it will put one string after another. So the following would reproduce "732":
code:

put number (1) + number (2) + number (3)

.)
To add the digits up, we need to first convert them into integers. Because the string variable, numbers, is full of numbers, (as opposed to something like "The Beatles"), we can use the strint() function to change the value of numbers into an integer value.
code:

var numbers := "732"
var numbersAsInt : int
numbersAsInt := strint (numbers) %numbers being a string
put numbersAsInt

So now we can add the various digits up. Add the strint() value of each character of the numbers string to a counter variable. Then output the counter variable at the end.

I hope you followed all that.

EDIT: See how much help people get when they ask questions properly and intelligently? Smile
-JP-




PostPosted: Sun Mar 06, 2005 10:07 pm   Post subject: :O

Thanks for replying back. Most of my problems are solved ^^

Quote:
1)
What you need here is a third variable. Think about having three buckets, two with water. One of them has red water, the other one has blue water. Now, if you wanted to switch water from one bucket to the other, you wouldn't just pour it straight in would you?! You'd end up with purple water...and no one ever wants purple water...
So, you pour the red water into the empty bucket, then pour the blue water into the bucket you just used, then transfer the red water into the other bucket...simple eh?


Well, I understand about adding another variable. However, that variable won't have a value, resulting to errors. Even if I put the new variables a value and do this:

Quote:
var z: int :=0
x:= z
y:= x
x:= y


The value of x and y will be zero. Did I do something wrong? O.o;

Oh right..And I have this problem (Well...sounds minor, but not sure ><)

"The current deficit of a company is $47,000,000 and it is estimated that the deficit will increase by 4.5% in each of the next two years. Write a program that will find the estimated deficit in these two years, rounded correctly to the nearest million $."

I understand rounding the digit. Does this mean I have to make a program on asking the user to give a deficit and then calculate it, then rounding the number. (I know this is a little off of my problems...;_; )

- JP
Cervantes




PostPosted: Sun Mar 06, 2005 10:20 pm   Post subject: (No subject)

-JP- wrote:

I understand rounding the digit. Does this mean I have to make a program on asking the user to give a deficit and then calculate it, then rounding the number. (I know this is a little off of my problems...;_; )

Well, given the nature of the assignment, I don't think the user has to input anything. Sounds like the deficit of the company is fixed at $47 000 000. Mind you, you could get the user to input the deficit just the same.
Calculating should be easy. Just don't fall into the trap of saying, "Two years... 4.5% each year... that's 9%!" Because that's wrong. Razz

As to the swapping variables question:
your_turing_code:

var z: int :=0
x:= z
y:= x
x:= y

Let's think about it. You make x equal to z, so now x = 0. Then make y = x = 0. Then make x = y = 0. All is zero. Uh oh.
What you want to do is transfer the red water to the extra bucket, then transfer the blue water to the red bucket. Finally, transfer the red water that is in the extra bucket into the blue bucket.
In your code, you made x = z. If z is your extra bucket, you probably want to make z equal to x (transfer red water to extra bucket).

Cheers
-Cervantes
-JP-




PostPosted: Sun Mar 06, 2005 11:16 pm   Post subject: (No subject)

Ohh I get it now!~ (Silly me XD...) Thanks for all your help ^^b~
-JP-




PostPosted: Sun Mar 06, 2005 11:33 pm   Post subject: (No subject)

Actually..there IS one more thing ><;;

...How do I round the deficit to the nearest MILLION!? I mean, I can round the decimal points, but uh...Help?
Sponsor
Sponsor
Sponsor
sponsor
Bacchus




PostPosted: Mon Mar 07, 2005 7:05 am   Post subject: (No subject)

divide the number by 1000000 then round it and multiply it by 1000000 to get the nearest million
person




PostPosted: Mon Mar 07, 2005 3:54 pm   Post subject: (No subject)

[/quote]divide
Quote:


to clarify he meant to use the "div" command
ssr




PostPosted: Mon Mar 07, 2005 8:05 pm   Post subject: (No subject)

yes yes
hello and welcome
I had the similar questions before
hopefully we can help with ur questions
yepyep
welcome
haha new people
multi
Flikerator




PostPosted: Mon Mar 07, 2005 8:33 pm   Post subject: (No subject)

Difference between div and "/" (Incase it wasn't covered here).

div divides and then rounds automaticly.
"/" divides and doesnt round. (May result in decimal answer).
Bacchus




PostPosted: Mon Mar 07, 2005 9:06 pm   Post subject: (No subject)

and as i learned as i gave a post quite similar to the one above, div rounds down. thats y its better to use / and roun() Razz
Flikerator




PostPosted: Mon Mar 07, 2005 9:30 pm   Post subject: (No subject)

Bacchus wrote:
and as i learned as i gave a post quite similar to the one above, div rounds down. thats y its better to use / and roun() Razz


Oh didn't know it always rounded down. I just knew it rounded ^^

Div or /+round are nessisary for drawing as you cannot have 0.232131 of a pixel, or a colour.

If you use /, use round.
Delos




PostPosted: Tue Mar 08, 2005 11:30 am   Post subject: (No subject)

You could always use floor() or ceil() to specify which way to round...?
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 1  [ 15 Posts ]
Jump to:   


Style:  
Search: