GIDForums  

Go Back   GIDForums > Computer Programming Forums > CPP / C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 01-Feb-2005, 14:23
greensand greensand is offline
New Member
 
Join Date: Feb 2005
Posts: 2
greensand is on a distinguished road
Unhappy

base conversions c++ (please help)


my professor hasn't taught us how to use strings yet and we're supposed to create a prog that converts between bases without them.
I'm having a hard time figuring out how to convert from base 10 to base 2.

Please help. I've been trying different things for the past 2 hours and none work right.

(He wrote part of the code for us and we receive user input once then use a function to convert.)
  #2  
Old 01-Feb-2005, 15:17
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,627
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold
Quote:
Originally Posted by greensand
my professor hasn't taught us how to use strings yet and we're supposed to create a prog that converts between bases without them.
I'm having a hard time figuring out how to convert from base 10 to base 2.

Please help. I've been trying different things for the past 2 hours and none work right.

(He wrote part of the code for us and we receive user input once then use a function to convert.)

So what do you have so far?

Regards,

Dave
  #3  
Old 01-Feb-2005, 15:56
greensand greensand is offline
New Member
 
Join Date: Feb 2005
Posts: 2
greensand is on a distinguished road
Here's the function. I commented out a lot of stuff (in case I may need them later) to try others. Sorry if it looks confusing.
CPP / C++ / C Code:
int convert10to2(int n)
{
	// WRITE THIS
	int a=1;
	//int count1=0;
	//int count0=0;
	int one=1;
    int inc=1;
    while (a<n)
	{
	    a=a*2;
	    
	} 
 //a=a/2;	
 //n=n-a;
 //cout<<n<<a;
    do//a>=1)
    {
           
                  
        //if (n-a>=0.9)
        //{
             //n=n-a;  
             //cout<<n;          
            //count1++;
            /*if (one!=1 && one%2==0)
             {
             one = one+(one*9+1);
             inc*=10;  
              }   
             else //(one%2>0)
            {*/
            a=a/2;	
            n=n-a;
            one = one+(10*inc);
            inc*=10;
             //}
               
            //cout<<"1";
        //} 
       /* else if(n-a<0)
        {
            //n=n-a;
            //int zero=0;
            one = one+(one*9);//9999
            
            inc*=10;
            
            //n=n+a;
            cout<<n<<endl;
            
        }  */
 //inc*=10;
           
    } while (n!=0);  
    return one;    
}
Last edited by dsmith : 01-Feb-2005 at 20:19. Reason: Please use [c] & [/c] when posting c code
  #4  
Old 01-Feb-2005, 16:34
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,627
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold
Quote:
Originally Posted by greensand
Here's the function.


First of all, before you sit down at the computer and open the text editor to start typing C language statements: Ya gotta have a plan. Here's a way to approach this.

First: state the problem:

1. Describe the function in a few words.

2. Define the input(s) to the function.

3. Define the output(s) from the function: return value, if any; side effects, if any.

Now: decide how your program is going to implement the functionality. (Choose an algorithm.)

Decide how you will test the function. (A main() function that will invoke your function with various input values to test proper operation.)

I usually implement the main() function first, since it typically involves user input (console or file input), and I want to make sure I can feed the new function with what I want to.

Then implement the new function. Lots of times there are extra printf() statements that show intermediate results of the various operatons to make sure things are going OK. These extra print statements will be commented out or deleted before releasing the final product.

Test the function. Use inputs that you know the answer to. Use inputs that visit the "dark corners" of functionality where things may go wrong. (Can it handle negative inputs? Can it handle zero? Can it handle the largest integer that you can represent on the machine. Etc.)


I'll show you what I mean for the first few steps.


1. Describe the function: You have to tell me what your assignment is. Saying "convert decimal to binary" is not very descriptive, but maybe that's all you can think of for now.

2. Describe function input(s): An integer in a variable passed as an argument. (You tell me: is this for non-negative integers only, or what. Design and test of the program depend on knowing what you have to deal with.)

3. Describe the function output(s). Now, here's where we first get an inkling of the importance of knowing what we are supposed to do before actually trying to do it. Can the function return an integer? Well it could, but what's the point. The input is an integer,which is represented internally as a binary number in any machine that you or I are likely to have access to. How can we convert a decimal integer to a binary integer? Inside the computer, they are all binary numbers.

So, I can think of two possibilities (actually I can think of about a dozen, but I will pick the first two that came to mind). Your assignment should have completely specified inputs, outputs and functionality, but I am making up my own assignment. Here's a couple of possibilities:

(a) Print out the binary digits (bits) in human-readable form a bit at a time.
So, if the number is 123(decimal) the routine prints out "1111011" or something like that.

(b) Put the characters representing the '0' and '1' bits into an array. Maybe the array (as well as the integer to be converted) is an argument to the function. Then the calling function (main()) can print out the values of the array as a string, for example.

Let's suppose we choose (a).

Then the function has to do two things:

1. extract the bits (value 0 or 1) from the given number
2. print the character '0' or '1' for each bit.


Implementation time: For first test, we just pick a number to feed to the function; if your instructor has showed you how to get a user input number, you can use this instead:

CPP / C++ / C Code:
#include <stdio.h>

int main()
{
  voiid PrintBin(int n);
  int n;
  PrintBin(n);
  return 0;
}

int PrintBin(int n)
{
  /* extract the bits and print their values */
}


Your turn:

Regards,

Dave
  #5  
Old 01-Feb-2005, 20:06
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,627
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold
Quote:
Originally Posted by davekw7x
CPP / C++ / C Code:
#include <stdio.h>

int main()
{
  void PrintBin(int n);  /* edit:  corrected typographical error in "voiid" */
  int n;
  PrintBin(n);
  return 0;
}

void PrintBin(int n)  /* edit: corrected return type to agree with prototype */
{
  /* extract the bits and print their values */
}


Look at comments for corrections to my previous post.

Oops,

Dave
  #6  
Old 05-Mar-2005, 12:54
arikeri arikeri is offline
New Member
 
Join Date: Mar 2005
Posts: 10
arikeri is on a distinguished road
Question

base conversion


As a part of a larger program, I need a function whose input is an integer and it returns the corresponding binary number as a STRING of fixed length.

for eg: suppose the length of the string is 8(this is fixed outside the function)
and the input is 32
then the function should return 000100000
plz reply ASAP cuz it's quite urgent.



thanks in advance
Arikeri
  #7  
Old 05-Mar-2005, 21:23
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,234
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all
Quote:
Originally Posted by arikeri
As a part of a larger program, I need a function whose input is an integer and it returns the corresponding binary number as a STRING of fixed length.

for eg: suppose the length of the string is 8(this is fixed outside the function)
and the input is 32
then the function should return 000100000
plz reply ASAP cuz it's quite urgent.

Reply with what? A working function? Corrections to your existing function? Ideas on how to write it yourself?
Be specific in what you need, and don't ask us to write code for you -- it probably won't happen, but we'll be glad to help if you have specific questions. Also, Dave has graciously given greensand a pattern on how to figure it out. Even you are allowed to use what he suggested. You can also look at the Tutorial sub-board above -- I know the answer is there, too.
__________________

Cow: You're a lawyer too?
Mooseblood (mosquito): Ma'am, I was already a bloodsucking parasite. All I needed was a briefcase!
  #8  
Old 05-Mar-2005, 22:06
arikeri arikeri is offline
New Member
 
Join Date: Mar 2005
Posts: 10
arikeri is on a distinguished road
Quote:
Originally Posted by WaltP
Reply with what? A working function? Corrections to your existing function? Ideas on how to write it yourself?
Be specific in what you need, and don't ask us to write code for you -- it probably won't happen, but we'll be glad to help if you have specific questions. Also, Dave has graciously given greensand a pattern on how to figure it out. Even you are allowed to use what he suggested. You can also look at the Tutorial sub-board above -- I know the answer is there, too.


Iam sorry for not being specifc. Hereis a function I have written:
------------------------------------------------------------------------

CPP / C++ / C Code:
int dob(int integer,int BIN[14]){
        int saved_integer;// just save the input
        int index,temp,bin,n;
        int array[8]={1,2,4,8,16,32,64,128};
        int i;

        cout<<"\n\n";

                 while(index>0){
                        saved_integer=integer;

                        if(integer<0 || integer>255)
                        {
                                cout<<"The number must be between 0-255, inclusive.\n";
                        }
                        else
                        {
                        index=7;
                                while(index>=0)
                                {
                                        temp=0;
                                        if(integer>=array[index])
                                        {
                                                temp=array[index];
                                                integer=integer-temp;
                                                bin=1;
                                                index--;
                                        }
                                        else
                                        {
                                                bin=0;
                                                index--;
                                        }
                                        BIN[index+1]=bin;

                                           if(index==6)
                                        {
                                                cout<<"The decimal number "<<saved_integer<<" is the binary number ";

                                        cout<<bin<<"";

                                        }
                                cout<<"\n\n\n";
                                }
                         }
        cout<<"The binary number is\t";
        i=7;
        while(i>=0){cout<<BIN[i];i--;}
        cout<<"\n";

return 0;

}
------------------------------------------------------------------------------
I get the following error when I compile the program:

parse error at end of input

-------------------------------------------------------------------------
BIN is a global variable .Its an array of type int
Last edited by LuciWiz : 07-Mar-2005 at 07:54. Reason: Please insert your C code between [c] & [/c] tags
  #9  
Old 05-Mar-2005, 22:11
arikeri arikeri is offline
New Member
 
Join Date: Mar 2005
Posts: 10
arikeri is on a distinguished road
what Iam doing is the possibility '(b)' in DAVEs reply.
  #10  
Old 05-Mar-2005, 22:32
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,234
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all
Quote:
Originally Posted by arikeri
what Iam doing is the possibility '(b)' in DAVEs reply.
Use code tags as specified in the message at the top of the forum titled
Sticky: GIDForums enables New [C ] / [ C++] bbcode.
so your code can be read.

Usually what that message usually means is you have too many {'s and not enough }'s
__________________

Cow: You're a lawyer too?
Mooseblood (mosquito): Ma'am, I was already a bloodsucking parasite. All I needed was a briefcase!
 
 

Recent GIDBlog2nd Week of IA Training by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: Conversion: Binary, Decimal, Hexadecimal WaltP C Programming Language 1 10-May-2006 06:49
calling abstract base class method calls draw instead achoo FLTK Forum 1 19-Dec-2004 09:38
Help with Decimal to Hex Conversions jediboy C Programming Language 1 13-Dec-2004 00:11
Rebase - Must Get! BobbyDouglas Miscellaneous Programming Forum 0 18-Aug-2003 04:57

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 00:54.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.