Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

In Code 5, the program can convert base 10 numbers to base 2 numbers. Write a pr

ID: 3796235 • Letter: I

Question

In Code 5, the program can convert base 10 numbers to base 2 numbers. Write a program that can convert base 10 numbers to base 3 numbers. Make a screen capture of the console output of the program.

source.cpp

/*--------------------------------------------------------------
   This program uses a stack to convert the base-ten representation
   of a positive integer entered as input to base two, which is
   then output.
---------------------------------------------------------------------*/

#include <iostream>
using namespace std;
#include "Stack.h"          // our own -- <stack> for STL version

int main()
{
unsigned number,         // the number to be converted
           remainder;      // remainder when number is divided by 2
Stack stackOfRemainders; // stack of remainders
char response;           // user response
do
{
    cout << "Enter positive integer to convert: ";
    cin >> number;
    while (number != 0)
      {
remainder = number % 2;
stackOfRemainders.push(remainder);
number /= 2;
      }
    cout << "Base-two representation: ";

    while ( !stackOfRemainders.empty() )
    {
      remainder = stackOfRemainders.top();
      stackOfRemainders.pop();
      cout << remainder;
    }
    cout << endl;
    cout << " More (Y or N)? ";
    cin >> response;
}
while (response == 'Y' || response == 'y');
}

Stack.cpp

/*-- Stack.cpp-------------------------------------------------------------
              This file implements Stack member functions.
--------------------------------------------------------------------------*/

#include <iostream>
using namespace std;

#include "Stack.h"

//--- Definition of Stack constructor
Stack::Stack()
: myTop(-1)
{}

//--- Definition of empty()
bool Stack::empty() const
{
   return (myTop == -1);
}

//--- Definition of push()
void Stack::push(const StackElement & value)
{
   if (myTop < STACK_CAPACITY - 1) //Preserve stack invariant
   {
      ++myTop;
      myArray[myTop] = value;
   }
   else
   {
      cerr << "*** Stack full -- can't add new value *** "
              "Must increase value of STACK_CAPACITY in Stack.h ";
      exit(1);
   }
}

//--- Definition of display()
void Stack::display(ostream & out) const
{
   for (int i = myTop; i >= 0; i--)
      out << myArray[i] << endl;
}

//--- Definition of top()
StackElement Stack::top() const
{
   if ( !empty() )
      return (myArray[myTop]);
   else
   {
      cerr << "*** Stack is empty -- returning garbage value *** ";
      return (myArray[STACK_CAPACITY - 1]);
   }
}

//--- Definition of pop()
void Stack::pop()
{
   if ( !empty() )
      myTop--;
   else
      cerr << "*** Stack is empty -- can't remove a value *** ";
}

Stack.h

/*-- Stack.h ---------------------------------------------------------------

This header file defines a Stack data type.
Basic operations:
    constructor: Constructs an empty stack
    empty:        Checks if a stack is empty
    push:         Modifies a stack by adding a value at the top
    top:          Retrieves the top stack value; leaves stack unchanged
    pop:          Modifies stack by removing the value at the top
    display:      Displays all the stack elements

Class Invariant:
    1. The stack elements (if any) are stored in positions
       0, 1, . . ., myTop of myArray.
    2. -1 <= myTop < STACK_CAPACITY
--------------------------------------------------------------------------*/

#include <iostream>

#ifndef STACK
#define STACK

const int STACK_CAPACITY = 128;
typedef int StackElement;

class Stack
{
public:
/***** Function Members *****/
/***** Constructor *****/
Stack();
/*------------------------------------------------------------------------
    Construct a Stack object.

    Precondition: None.
    Postcondition: An empty Stack object has been constructed (myTop is
        initialized to -1 and myArray is an array with STACK_CAPACITY
        elements of type StackElement).
   -----------------------------------------------------------------------*/

bool empty() const;
/*------------------------------------------------------------------------
    Check if stack is empty.
    Precondition: None
    Postcondition: Returns true if stack is empty and false otherwise.
   -----------------------------------------------------------------------*/

void push(const StackElement & value);
/*------------------------------------------------------------------------
    Add a value to a stack.

    Precondition: value is to be added to this stack
    Postcondition: value is added at top of stack provided there is space;
         otherwise, a stack-full message is displayed and execution is
         terminated.
   -----------------------------------------------------------------------*/

void display(ostream & out) const;
/*------------------------------------------------------------------------
    Display values stored in the stack.

    Precondition: ostream out is open.
    Postcondition: Stack's contents, from top down, have been output to out.
   -----------------------------------------------------------------------*/

StackElement top() const;
/*------------------------------------------------------------------------
    Retrieve value at top of stack (if any).

    Precondition: Stack is nonempty
    Postcondition: Value at top of stack is returned, unless the stack is
        empty; in that case, an error message is displayed and a "garbage
        value" is returned.
   -----------------------------------------------------------------------*/

void pop();
/*------------------------------------------------------------------------
    Remove value at top of stack (if any).

    Precondition: Stack is nonempty.
    Postcondition: Value at top of stack has been removed, unless the stack
        is empty; in that case, an error message is displayed and
        execution allowed to proceed.
   -----------------------------------------------------------------------*/

private:
/***** Data Members *****/
StackElement myArray[STACK_CAPACITY];
int myTop;
}; // end of class declaration

#endif

Explanation / Answer

#include <stdio.h>
#include <fstream>
#include <iostream>
using namespace std;

int convert(int number, int base)
{

if (number == 0 || base == 10)
{
return number;
}
return (number % base) + 10 * convert(number / base, base);


}
int switchToLetter(int number)
{
switch (number)
{
case A:
cout << 'A';
break;
case B:
cout << 'B';
break;
case C:
cout << 'C';
break;
case D:
cout << 'D';
break;
case F:
cout << 'E';
break;
case G:
cout << 'F';
break;
}
}
int main()
{
int number;
int base;
ifstream numbers("numbers.txt");
numbers.open("numbers.txt");
if (numbers.is_open())
cout << "open";
else
cout << "Can not open";
int inputvalue = number;
if (inputvalue > 2000000000)
{
cout << "Decimal number too large for this program" << endl;
}
else
{

do
{
convert(number, base);
cout << inputvalue << "in base" << base << "is" << number << endl;

} while (inputvalue >= 0);
}

return 0;   
}