An algorithm about C/C++

This is a discussion on An algorithm about C/C++ within the C++ Programming forums, part of the General Programming Boards category; Good night: I'm trying to make a homework but I couldn't create the algoritm. Wanted is this: Entered as an ...

  1. #1
    Registered User
    Join Date
    Oct 2003
    Posts
    10

    An algorithm about C/C++

    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.
    Hexadecimal of 0100010 is YYYYYY.
    Decimal of 0100010 ZZZZZ.

    thank you..

  2. #2
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    Show us what code you have done so far.
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  3. #3
    Registered User
    Join Date
    Oct 2003
    Posts
    49
    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'};

  4. #4
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    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

  5. #5
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,600
    And remember - we don't do the homework here, you do. Please be specific with your questions.
    Code:
    if( numeric_limits< byte >::digits != bits_per_byte )
        error( "program requires bits_per_byte-bit bytes" );
    24bbs.cpp

  6. #6
    Registered User
    Join Date
    Oct 2003
    Posts
    10
    Thank you for answers!
    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 :]

  7. #7
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    Do you know how to convert numbers between the bases (by hand)?
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  8. #8
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    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

  9. #9
    Registered User
    Join Date
    Oct 2003
    Posts
    10
    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. #10
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,600
    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.
    Code:
    if( numeric_limits< byte >::digits != bits_per_byte )
        error( "program requires bits_per_byte-bit bytes" );
    24bbs.cpp

  11. #11
    Comment your source code! Lynux-Penguin's Avatar
    Join Date
    Apr 2002
    Posts
    533
    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
    Asking the right question is sometimes more important than knowing the answer.
    Please read the FAQ
    C Reference Card (A MUST!)
    Pointers and Memory
    The Essentials
    CString lib

  12. #12
    Registered User
    Join Date
    Oct 2003
    Posts
    10
    yeah that's true. we can form this way...:]

  13. #13
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    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.

  14. #14
    Registered User
    Join Date
    Oct 2003
    Posts
    10
    thanx. i don't know pascal but i can understand the manner from this code... :]

  15. #15
    Registered User
    Join Date
    Oct 2003
    Posts
    10
    -----------------------------------------------------------------------------------
    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;
    }

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Implement of a Fast Time Series Evaluation Algorithm
    By BiGreat in forum C Programming
    Replies: 7
    Last Post: 12-04-2007, 01:30 AM
  2. Replies: 4
    Last Post: 12-10-2006, 06:08 PM
  3. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  4. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 09:33 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21