Author |
Message |
supadupamegaman
|
Posted: Tue Nov 06, 2012 1:19 pm Post subject: Help creating a program that converts decimal to octal with loops |
|
|
Can someone create a program that converts decimal to octal with loops for me pleasee?
Greatly appreciated ty |
|
|
|
|
![](images/spacer.gif) |
Sponsor Sponsor
![Sponsor Sponsor](templates/subSilver/images/ranks/stars_rank5.gif)
|
|
![](images/spacer.gif) |
Zren
![](http://compsci.ca/v3/uploads/user_avatars/1110053965512db6185954b.png)
|
Posted: Tue Nov 06, 2012 1:35 pm Post subject: RE:Help creating a program that converts decimal to octal with loops |
|
|
No.
What have YOU tried so far? Do you understand what the octal number system even is?
http://simple.wikipedia.org/wiki/Octal |
|
|
|
|
![](images/spacer.gif) |
supadupamegaman
|
Posted: Tue Nov 06, 2012 1:36 pm Post subject: Re: Help creating a program that converts decimal to octal with loops |
|
|
This is what i have so far
%comments
var dn, on : real
put "Input decimal number."
get dn
loop
exit when dn <= 0
dn:= dn div 8
on:= dn mod 8
end loop
put "The octal number is ", on
i dont under stand why it is not working |
|
|
|
|
![](images/spacer.gif) |
supadupamegaman
|
Posted: Tue Nov 06, 2012 1:49 pm Post subject: Re: Help creating a program that converts decimal to octal with loops |
|
|
How do i make the program print the octal numbers? I got the program to print the remainders.
%comments
var dn, on : real
put "Input decimal number."
get dn
loop
exit when dn <= 0
on:= dn mod 8
dn:= dn div 8
put on
end loop |
|
|
|
|
![](images/spacer.gif) |
Zren
![](http://compsci.ca/v3/uploads/user_avatars/1110053965512db6185954b.png)
|
Posted: Tue Nov 06, 2012 5:18 pm Post subject: RE:Help creating a program that converts decimal to octal with loops |
|
|
http://academic.evergreen.edu/projects/biophysics/technotes/program/int_conv.htm#dec2hex
> The first remainder is the least significant digit and the last remainder is the most significant digit.
You're printing the remainders out in the wrong order.
I'd suggest using a string to keep track of the digits, then prefix the digits as they come. Then when you're done converting, print the final thing. |
|
|
|
|
![](images/spacer.gif) |
supadupamegaman
|
Posted: Tue Nov 06, 2012 9:21 pm Post subject: Re: Help creating a program that converts decimal to octal with loops |
|
|
I don't really understand what you are trying to say....
Can you please explain in more detail?
How do you rearrange the MSD and LSD?
What do you mean by "string" and "prefix them"? |
|
|
|
|
![](images/spacer.gif) |
Zren
![](http://compsci.ca/v3/uploads/user_avatars/1110053965512db6185954b.png)
|
Posted: Tue Nov 06, 2012 9:50 pm Post subject: RE:Help creating a program that converts decimal to octal with loops |
|
|
By "string" I mean the datatype string, which I assumed you'd already have learned about.
By"prefix", take the following example into account:
Turing: |
var outputString : string := ""
for i : 1 .. 9
outputString := outputString + intstr(i ) % Can also be written as: outputString += intstr(i)
end for
put outputString % Output: 123456789
|
It would append (add onto the end) the stringified character(s) of the value of the counter of the for loop. Which would eventually have a string containing the number 123456789. Which is then outputed.
Now, how would you change this line so that it would output 987654321?
code: | outputString := outputString + intstr(i) |
After you've figured that out. Apply it to your current problem. |
|
|
|
|
![](images/spacer.gif) |
supadupamegaman
|
Posted: Tue Nov 06, 2012 10:09 pm Post subject: Re: Help creating a program that converts decimal to octal with loops |
|
|
We never really learned what an intstr was....
We learned what div, mod, loops, and for .
Is there a way to create this program with those commands? |
|
|
|
|
![](images/spacer.gif) |
Sponsor Sponsor
![Sponsor Sponsor](templates/subSilver/images/ranks/stars_rank5.gif)
|
|
![](images/spacer.gif) |
supadupamegaman
|
Posted: Tue Nov 06, 2012 10:14 pm Post subject: Re: Help creating a program that converts decimal to octal with loops |
|
|
Would this reverse the order?
This gives me an output of "987654321"
var outputString :string := ""
for i : 1 .. 1
outputString := outputString + intstr(987654321) % Can also be written as: outputString += intstr(i)
end for
put outputString % Output: 123456789 |
|
|
|
|
![](images/spacer.gif) |
Zren
![](http://compsci.ca/v3/uploads/user_avatars/1110053965512db6185954b.png)
|
Posted: Tue Nov 06, 2012 10:42 pm Post subject: RE:Help creating a program that converts decimal to octal with loops |
|
|
=.= Well. Yes that DOES output the correct answer, but I want you do so without hardcoding 987654321 in there.
intstr() is a function that will take an integer and convert to a string.
If you don't know what strings are, glance over this (http://compsci.ca/v3/viewtopic.php?t=9634) thread. It's part of the Turing Walkthrough.
for loops and other looping constructs are explained here (http://compsci.ca/v3/viewtopic.php?t=3678). Edit: Woops, mistook you for not understanding for loops. Perhaps you should step through each iteration of the for loop I posted in my example.
When it first enters the loop, outputString = "".
After the first iteration, outputString = "1". As:
code: | outputString := outputString + intstr(i)
outputString := outputString + intstr(1) [Since i = 1]
outputString := outputString + "1" [Since intstr() will convert the integer into it's string equivelent]
outputString := "" + "1" [Since outputString was "" before reaching this statement]
outputString := "1" [Since concatenating (combining) "" + "1" equals "1"] |
After the second iteration, outputString = "12". As:
code: | outputString := outputString + intstr(i)
outputString := outputString + intstr(2) [Since i = 2]
outputString := outputString + "2" [Since intstr() will convert the integer into it's string equivelent]
outputString := "1" + "2" [Since outputString was "1" before reaching this statement]
outputString := "12" [Since concatenating (combining) "1" + "2" equals "12"] |
... and so on and so forth until the loop exits. |
|
|
|
|
![](images/spacer.gif) |
|