C++ Midterm Help

Discussion in 'C++' started by oceanicblack, Oct 18, 2008.

  1. oceanicblack

    oceanicblack New Member

    Joined:
    Oct 18, 2008
    Messages:
    5
    Likes Received:
    0
    Trophy Points:
    0
    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:
    
    
    // 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.
     
    Last edited by a moderator: Oct 18, 2008
  2. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,009
    Likes Received:
    203
    Trophy Points:
    63
    Occupation:
    Senior Support Engineer
    Location:
    England
    Oh dear. You've done the whole course, you've tackled assignments yourself, asked the tutor questions about what you don't understand, this is D-day and you've no idea at all what to do with the assgnment?

    If your tutor is that bad then don't worry, everyone will fail and it will be the tutor's problem, not yours.

    OTOH, if you've no idea because you've been getting other people to do your work for you, e.g. by posting assignments on internet forums and handing the answers in without working them yourself and figuring out why you couldn't do it yourself, then a fail on this course is not an unreasonable expectation. A programming course is intended to teach you how to program, not how to get others to do tasks for you; for that, you should be on a Management course.

    But I'm not completely without mercy. It looks like a simple enough task. Complicated explanation, but very well documented code and by the looks of it you already have 75% of what you need to complete the task, basically you just have to reverse the code you've been given so it converts D to B instead of B to D.

    Where are you stuck? Could you outline in English basically what DtoB() will do? Do you know how to convert decimal to binary manually, on paper? (If you haven't then you have no chance of completing the assignment; you can't program a task you can't do yourself.)
    Do you think you might need to call BaseGen()?
    Do you think you might need to loop over something, and if so, what?
    If you're keeping "string Bin", how do you think you might need to modify it within the loop?
     
  3. oceanicblack

    oceanicblack New Member

    Joined:
    Oct 18, 2008
    Messages:
    5
    Likes Received:
    0
    Trophy Points:
    0
    You've lost me on that one.

    I know how to convert decimal to binary, both on paper and in a program. It was one of the first assignments we ever did. However, when I look at his partial code and I look at the assignment we first did, it looks nothing alike.

    Second, I don't really understand loops that well. My loops always return with errors and I'm never sure how to correct them.

    Third, does this seem more like he wants two different prompts, first asking the user for a decimal and converting to binary, followed by a prompt asking to enter a binary and convert it to decimal? That wouldn't be too difficult I don't think, but if he wants the program to automatically detect which was entered and convert it, I don't have the slightest clue how to do that.
     
  4. oceanicblack

    oceanicblack New Member

    Joined:
    Oct 18, 2008
    Messages:
    5
    Likes Received:
    0
    Trophy Points:
    0
    Here's what I have come up with for the code on converting a decimal number to binary using a string:

    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;
    
    
    string dec2bin(int Decimal);
    
    
    string dec2bin(int Decimal)
    {
    int steps[] = {128, 64, 32, 16, 8, 4, 2, 1};
    string binary;
    
    if(Decimal > 0)
    {
    int stepLen = sizeof(steps) / sizeof(int);
    for(int x = 0; x < stepLen; x++)
    {
    if(Decimal >= steps[x])
    {
    Decimal -= steps[x];
    binary += "1";
    }
    else
    binary += "0";
    }
    }
    else
    binary = "0";
    
    return binary;
    
    }
    
    
    
    // The driver program follows
    
    
    int main()
    {
    cout << "Enter an integer no larger than 255: ";
    int Decimal;
    cin >> Decimal;
    if (Decimal > 255)
    {
    cout << "Please follow instructions" << endl;
    }
    else
    {
    cout << "Binary: " << dec2bin(Decimal) << endl;
    }
    system("PAUSE");
    return 0;
    }
    
    After this, how would I go about making the program capable of distinguishing between a binary or decimal input and then converting it?
     
  5. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,009
    Likes Received:
    203
    Trophy Points:
    63
    Occupation:
    Senior Support Engineer
    Location:
    England
    I don't know if he wants one prompt that distinguishes between decimal and binary - you could do it if binary numbers were always 8 digits and decimals were always 1-3 digits, but given 101 as input and no length restrictions there's no way to tell if this is binary or decimal. To play safe you could always convert it both ways.

    Rereading "enhance the driver program to test an additional function DtoB"...if "the driver program", which is an odd way of saying "the main() function", currently tests BtoD, this could mean either modify it to take just a single binary input and give decimal output, or using the strict definition of "enhance", which means to add new features, could mean to take one of each and convert both. It's not clear.

    Or you could prompt the user for which way he wants to convert, then take input and call BtoD or DtoB depending on the result. Personally I'd be inclined to clarify this with the tutor, which may well be part of the exercise, because often a program specification won't be precise enough and you will need to go back to the customer to clarify what exactly he wants.

    I don't know how you make a loop that returns errors. Loops don't return anything; they just modify a variable and repeat a section of code until a particular end condition has been met. Can you give an example of a loop that "returns errors" and I'll see if I can see what the problem is? Give input and anything else I'm going to need to reproduce the problem.

    Here's an example loop:
    Code:
    for (int i=0; i<10; i++)
    {
      printf("Hello world %d\n",i);
    }
    
     
  6. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,009
    Likes Received:
    203
    Trophy Points:
    63
    Occupation:
    Senior Support Engineer
    Location:
    England
    As for distinguishing D from B, if we're talking about 8-bit numbers, which of the following are decimal and which are binary?
    1
    10
    256
    101
    27
    100110
    How can you tell? The answer will outline the code.
     
  7. oceanicblack

    oceanicblack New Member

    Joined:
    Oct 18, 2008
    Messages:
    5
    Likes Received:
    0
    Trophy Points:
    0
    I'm thinking the best thing to do with what he's asking is to just have the program on startup ask if the user wants to convert decimal to binary or binary to decimal and then do as asked. I know goto commands are frowned upon in C++, what would be an alternate way to do this since I'm not real familiar with the language?
     
  8. oceanicblack

    oceanicblack New Member

    Joined:
    Oct 18, 2008
    Messages:
    5
    Likes Received:
    0
    Trophy Points:
    0
    Well that's that. He never specifies he wants any type of error detection, so other than decimal numbers greater than 255, it will not be included. This is the final code for both D2B and B2D. Any suggestions are welcome but this seems to work just fine.

    Code:
    /*
    
    Craig Wilhelm
    18.October.2008
    Midterm Assignment
    CISP301, TTH, 19:00
    
    18.October.2008
    ---------------
    Initial Scripts
    
    */
    
    #include <iostream>
    #include <string>
    using namespace std;
    
    /* CONVERT DECIMAL TO BINARY */
    
    string DtoB(int Decimal);
    
    string DtoB(int Decimal)
    {
    	int steps[] = {128, 64, 32, 16, 8, 4, 2, 1};
    	string binary;
    
    		if(Decimal > 0)
    		{
    			int stepLen = sizeof(steps) / sizeof(int);
    			for(int x = 0; x < stepLen; x++)
    			{
    			if(Decimal >= steps[x])
    				{
    				Decimal -= steps[x];
    				binary += "1";
    				}
    			else
    				binary += "0";
    			}
    		}
    	else
    		binary = "0";
    
    return binary;
    }
    /* END DECIMAL TO BINARY CONVERSION*/
    
    /* CONVERT BINARY TO DECIMAL */
    
    void BaseGen(int Base[], int Size)
    {
    Base[0]=1;
    int Index=1;
    while(Index<Size)
    	{
    	Base[Index]=Base[Index-1]*2;
    	Index=Index+1;
    	}
    	return ;
    }
    
    int BtoD(string Binary)
    
    { 
    	unsigned int Index=1;
      	Index=0;
    	int Dv=0;
    	int Base[8];
    	BaseGen(Base,8);
    	while(Index<Binary.size())
    	{
    		if (Binary[Index]=='1')
    			Dv=Dv+Base[Binary.size()-1-Index];
    			Index=Index+1;
    	}
    		return Dv;
    }
    
    /* END BINARY TO DECIMAL CONVERSION */
    
    /* BEGIN DP. WHAT SAY YOU? */
    
    int main()
    {
    	string Bin;	
    
    		cout << "Please type any valid integer no larger than 255: " << endl;
    		int Decimal;
    		cin >> Decimal;
    
    		if (Decimal > 255)
    			{
    			cout << "Game Over." << endl;
    			system("PAUSE");
    			return(0);
    			}
    		else
    			{
    			cout << "Binary: " << DtoB(Decimal) << endl;
    			}
    	
    		cout << "Please type any valid binary number of eight or less bits: " << endl;
    		int Decimal2;
    		cin >> Bin;
    
    		Decimal2=BtoD(Bin);
    		cout << "Decimal: " << Decimal2 << endl;
    		system("PAUSE");
    	
    	return(0);
    }
    
    /* END DP. WHAT SAY YOU? */
    
     
  9. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,009
    Likes Received:
    203
    Trophy Points:
    63
    Occupation:
    Senior Support Engineer
    Location:
    England
    You can easily avoid gotos:
    Code:
    cont=1;
    while (cont)
    {
      prompt: 1: B->D, 2: D->B, 3: exit?
      switch answer
      {
      case 1: convert B->D; break;
      case 2: convert D->B; break;
      case 3: cont=0;
      }
    }
    
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice