Thread: C++ Assignment Help Please!

  1. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    11

    C++ Assignment Help Please!

    I have a C++ midterm due tomorrow that I can't figure out. Here is the assignment:

    Code:
    // Use the program BtoDsol.cpp located at:
    BtoDSol.cpp
    // which converts any byte to decimal
    // enhance the driver program to test an additional function DtoB with a prototype of:
    // string DtoB (int Decimal);
    // DtoB will convert any decimal number <= 255 to binary
    // Here is a possible partial code:
    string DtoB (int Decimal)
    {
    string Bin ="00000000"; // declare a model binary number
    // you will need to develop the rest of the code here
    //
    return Bin;
    }


    Now, the .cpp referred to, BtoDSol.cpp:

    Code:
    // This program will test the function BtoD
    // int BtoD (string Binary); is the prototype
    // will convert any byte (8bits) from binary to decimal
    #include <iostream>
    #include <string>
    using namespace std;
    void BaseGen(int Base[], int Size)
    {
    Base[0]=1; // the first element is always 1
    int Index=1; // start with the second value
    while(Index<Size) // must load all Base elements
    {
    Base[Index]=Base[Index-1]*2; // left = right *2
    Index=Index+1; // Bump the Index
    }
    return ;
    }
    int BtoD(string Binary)
    {
    unsigned int Index=1; //loop control variable
    Index=0; // Initialize to zero
    int Dv=0; //Dv is the decimal value for the binary number in Binary
    int Base[8]; // Base array to store binary base values
    BaseGen(Base,8); // Load Base
    while(Index<Binary.size()) // loop to check all bits
    {
    if (Binary[Index]=='1') // if the value is 1
    Dv=Dv+Base[Binary.size()-1-Index]; // add the corresponding base value to Dv
    Index=Index+1; // Bump the loop control variable
    }
    return Dv; // Return the decimal value
    }
    
    // The driver program follows
    
    int main()
    
    {
    string Bin,Anychar;
    int Decimal;
    // Get the binary number
    cout << " Please type any valid Binary Number of eight or less bits"<< endl;
    cin>> Bin;
    // Convert A to decimal
    Decimal=BtoD( Bin);
    cout <<" The decimal value of "<< Bin << " is = "<<Decimal<<endl;
    cin >> Anychar;
    return 0;
    }




    I have no idea what to do on this one. Any help greatly appreciated, being how my grade depends on it and all.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    If I gave you the number 150, do you know what the answer should look like?

    How do you know?

    Can you write down how you did it?

    Can you write it down even more carefully?

    That's your program.

  3. #3
    Registered User
    Join Date
    Oct 2008
    Posts
    11
    I don't understand what you're saying or what this program is even trying to do.

    All I know is that the BtoDSol.cpp takes an 8 digit binary number and turns it into a decimal number. His explanation of what he wants this new program to do is entirely unclear to me and so is what you said.

    I'm so lost on this one.

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    DtoB will convert any decimal number <= 255 to binary
    Do you really not know what this means?

  5. #5
    Registered User
    Join Date
    Oct 2008
    Posts
    11
    So that's the only line of any importance? What's the rest of that then? Filler? I hella don't pay attention in this class...

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Maybe I should back up a bit: Do you even speak English?
    // Use the program BtoDsol.cpp located at:
    BtoDSol.cpp
    // which converts any byte to decimal
    Sentence 1 tells you that you need the program BtoDsol.cpp and (presumably) where it is located, and what it does. (I'm guessing this was developed in class.)

    // enhance the driver program to test an additional function DtoB with a prototype of:
    // string DtoB (int Decimal);
    Sentence 2 tells you that you are going to "enhance" the program -- enhance means to add extra features to -- so that you can "test", which means make sure it works, another ("additional") function, which is called DtoB. The prototype is given; in C++ the prototype of a function tells you its name ("DtoB"), the parameters that are going to be passed in ("int Decimal") and the return type ("string").

    // DtoB will convert any decimal number <= 255 to binary
    Sentence 3 tells you what DtoB is supposed to do.

    // Here is a possible partial code:
    Sentence 4 tells you that the following code is partial code, meaning that it is only somewhat done. It is given because the teacher's experience has shown him or her that many students such as yourself do not remember, especially under pressure, what form a function takes, how to return values from a function, and other similar housekeeping details.

  7. #7
    Registered User valaris's Avatar
    Join Date
    Jun 2008
    Location
    RING 0
    Posts
    507
    Looks like you are supposed to finish the function to convert a base 10 number into binary. The algorithm to do this is on the net, basically you will divide by 2, saving the remainder until you cant divide anymore, then read it backwards, this will be the number in binary. That doesn't include floating point numbers though.

  8. #8
    Registered User
    Join Date
    Oct 2008
    Posts
    11
    So if I add this part in, what should be displayed when the program first runs?

    I mean, with BtoD, since there is only one capability, it simply asks to enter a binary number and it will be converted to decimal... if the program can do both, do I have to make it so that it can automatically detect either a binary or decimal was entered and then convert it?

  9. #9
    Registered User valaris's Avatar
    Join Date
    Jun 2008
    Location
    RING 0
    Posts
    507
    It can only convert binary to decimal right now it looks like. Your task it looks like is to write the inverse function.

  10. #10
    Registered User
    Join Date
    Oct 2008
    Posts
    11
    The inverse, converting a decimal to binary, was one of the first assignments we ever did and was a much simpler code than this. It looks like this BtoD code was written way more complicated than needed...

  11. #11
    Registered User
    Join Date
    Oct 2008
    Posts
    11
    It says test an "additional function" so I'm thinking that he wants these both in the same program, not just the opposite.

  12. #12
    Registered User
    Join Date
    Jan 2008
    Posts
    290

  13. #13
    Registered Abuser
    Join Date
    Jun 2006
    Location
    Toronto
    Posts
    591
    I'll say it again, where the hell is Mac and his sig when you need 'em...

  14. #14
    Registered User
    Join Date
    Oct 2008
    Posts
    11
    No I won't lie, I'm indeed extremely desperate. This is basically my entire grade and I have no idea what is expected of me.

  15. #15
    Registered User
    Join Date
    Oct 2008
    Posts
    11
    Seriously, can someone just tell me what to do? I am not getting anywhere, I've been staring at this and messing with it for hours and I keep getting errors and errors and I'm not making any progress. I don't know where to even insert the new code, how to make it capable of detecting whether it needs to be DtoB or BtoD...I can't figure out any of this.

    Man this sucks assssssss.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Menu
    By Krush in forum C Programming
    Replies: 17
    Last Post: 09-01-2009, 02:34 AM
  2. Assignment Operator, Memory and Scope
    By SevenThunders in forum C++ Programming
    Replies: 47
    Last Post: 03-31-2008, 06:22 AM
  3. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  4. Help with a pretty big C++ assignment
    By wakestudent988 in forum C++ Programming
    Replies: 1
    Last Post: 10-30-2006, 09:46 PM
  5. Replies: 1
    Last Post: 10-27-2006, 01:21 PM