Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last
• 10-05-2003
umutdogan
Good night:
I'm trying to make a homework but I couldn't create the algoritm.

Wanted is this: Entered as an input Binary(2), Octal(8), Hexadecimal(16) or Decimal(10), knowing which format is and converting to other formats...

For example; user enters 0100010.
program must give an output that says "this is a binary number".
and the equals in decimal, hexadecimal and octal.

EXAMPLE RUN:
Enter a number: 0100010
It is a binary number.
Octal of 0100010 is: XXXXXX.
Decimal of 0100010 ZZZZZ.

thank you..
• 10-05-2003
XSquared
Show us what code you have done so far.
• 10-05-2003
DrZoidberg
And how is that programm supposed to find out what kind of number I entered?
If I enter "1234" how does the program know if it is an octal, decimal or hex number?

First convert the number that was entered into an int value. Then convert that to the other formats.

An array like this will be helpfull:
Code:

`char num[]={'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};`
• 10-05-2003
Thantos
As DrZoidberg said, you can't tell 100% what the number base is just by the user inputing the number. What you can do is to rule out certain ones. So if the user inputed "157563" you know the lowest base is 8.

Also do post some code so we can help you better :)
• 10-05-2003
Sebastiani
And remember - we don't do the homework here, you do. Please be specific with your questions. ;)
• 10-05-2003
umutdogan
To Sebastiani : I'm not trying to take the code from you. I am a bginner type programmer and couldn't mke my algorithm. Just wanted some advices about walkthrough. Alright?

To DrZoidberg & Thantos : The input will be shown as the lecturers' told as: an octal number begins with 0O (for ex. 0O1254), and hexadecimal with 0X (like 0X13EA)...

To XSquared : I couldn't start. Once upon a time i think you were a novice programmer and nothing would come into your mind. I'm now in that position. I know, if i would start i would finish this.

Thanks all for advices, but more is neeed if available :]
• 10-05-2003
XSquared
Do you know how to convert numbers between the bases (by hand)?
• 10-05-2003
Thantos
General Tips based off of what you said:

1) Read the whole thing into a string
2) Look at the first two characters to determine the base
3) Convert the rest of the string into an integer
4) Conver the new integer into the other bases.

Other little tidbits:

If the number to be entered isn't of a fixed length (ie the number isn't left padded with zeros) you will need to find out how many more places there are after the first two.

How I'd approch it:
1) write a mini program to read the string and find the base
2) write another mini program that will convert a string (minus the base characters) into a number
3) write a third mini program to convert a number into different bases
4) Combine what you learn into a new program
• 10-05-2003
umutdogan
If the number to be entered isn't of a fixed length (ie the number isn't left padded with zeros) you will need to find out how many more places there are after the first two.

Yeah that's my problem. If there would be a finite string i would count and compare... but it is not finite. The input may be Ox1243545645456 or 10101101011011110 or anything else...

Do you know how to convert numbers between the bases (by hand)?:
Yes, for example for 0x12EF = F * 16exp0+E*16exp1+.....
0101 = 1*2exp0+0*2exp1+....
• 10-05-2003
Sebastiani
Before adding any 'intelligence' to the algorithm to 'guess' the base, first write the program to convert the number in different modes (use a menu or similar). Read a line, advance past whitespace, total up the array into an integer, then pass the result to each converter.
• 10-05-2003
Lynux-Penguin
umutdogan gave me an idea that might be able to help you with guessing bases. Have each base denoted by a specific prefix:
0b = binary
0x = hex
0o = octal
0d = decimal

and then handle it that way for input. Or do as you wish

-LC
• 10-05-2003
umutdogan
yeah that's true. we can form this way...:]
• 10-05-2003
Thantos
Here is a program I wrote for my pascal class. It takes a number from 0 to 32767 and converts it to every base from base 2 to base 62.

Yes I know you are asking for C but this should give you a good start. think of procedure convert as void convert(void) and begin as { and end as }.

And honestly it appears you aren't putting forth much effort into this program. I hope I'm wrong

Code:

```var   startmax, userinput, temp, numch: integer; const   MAX = 32767;   MAXBASE = 62;   BASE : integer = 2;   pow : integer = 1;   symbols : array [0..62] of Char = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwyxz'; procedure convert; var   num : integer;  begin   startmax := MAX div BASE;   num := UserInput;   pow := 1;   write (UserInput, ' in base ', BASE:2, ': ');   while ( pow < startmax ) do     begin       pow := pow * base;     end;   while pow > 0 do   begin     temp := 0;     while ( num >= pow) do       begin         temp := temp + 1;         num := num - pow;       end;     write (symbols[temp]);     pow := pow div BASE;     numch := numch + 1;   end;  end; { Main Program } begin   repeat     write ('Enter a non negetive number between 0 and ', MAX, ': ');     readln (userinput);     if ( UserInput < 0 ) then writeln('You entered an invalid number');   until UserInput >= 0;   while ( base <= MAXBASE ) do     begin       numch := 0;       convert;       BASE := BASE + 1;       if ( BASE mod 2 ) = 0         then           writeln         else           begin             numch := 16 - numch;             repeat               write (' ');               numch := numch - 1;             until numch = 0;           end;     end; writeln; end.```
• 10-05-2003
umutdogan
thanx. i don't know pascal but i can understand the manner from this code... :]
• 10-05-2003
umutdogan
-----------------------------------------------------------------------------------
Finally I wrote this code, but i could'nt give an answer to this questions:
1) How can i prevent user to enter a incorrect number.
For example if s/he enters 12433 in binary selection what can i do?

2) I couldn't do the transformation between bases via recursion.
I think i must declare four functions: transToBinary, transToDecimal, transToHexa, transToOcta
But how?

3) Is there anyone there who can solve this problem and help me to make this program. I must give
this homework on Tuesday...
Thank You.. :]
Code:

```#include <iostream.h> main() { char input; int number; cout<<"Please select the type of the number you want to enter:\n"; cout<<"******************************************\n"; cout<<" d  :\t decimal\n x  :\t hexadecimal\n b  :\t binary\n o  :\t octal\n"; cout<<"******************************************\n"; cin>>input; switch (input)         { case 'd': case 'D':         cout<<"Now you can enter your decimal number:";         cin>>number;         cout<<"You entered "<<number;         break; case 'x': case 'X':         cout<<"Now you can enter your hexadecimal number:";         cin>>number;         cout<<"You entered "<<number;         break; case 'b': case 'B':         cout<<"Now you can enter your binary number:";         cin>>number;         cout<<"You entered "<<number;         break; case 'o': case 'O':         cout<<"Now you can enter your octal number:";         cin>>number;         cout<<"You entered "<<number;         break; default:         cout<<"Error!";         } return 0; }```
Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last