Question

your objective is to write a program in c++ to execute the requested task. ? Write...

your objective is to write a program in c++ to execute the requested task.

? Write a string function that takes two int parameters named num and base respectively. If num is nonnegative and the base is between 2 and 16 inclusively, the function returns a string of num converted to base; otherwise, the function returns '0'. You must create a stack class.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Code implemented using stack class which is created externally we can use in-built stack if we want

Just implemented normal base conversion

#include<bits/stdc++.h>
using namespace std;
#define SIZE 32

// Class for stack
class Stack
{
   char *arr;
   int top;
   int capacity;

public:
   Stack(int size)
   {
       arr = new char[size];
       capacity = size;
       top = -1;
   }  
   ~Stack()
   {
       delete arr;
   }
   void push(char x)
   {
       if (isFull())
       {
           cout << "OverFlow\nProgram Terminated\n";
       }

       //cout << "Inserting " << x << endl;
       arr[++top] = x;
   }
   char pop()
   {
       if (isEmpty())
       {
           cout << "UnderFlow\nProgram Terminated\n";
       }
       //cout << "Removing " << peek() << endl;
       return arr[top--];
   }
   char peek()
   {
       if (!isEmpty())
           return arr[top];
       else
           cout<<"Stack is Empty"<<endl;
   }
   int size()
   {
       return top + 1;
   }
   bool isEmpty()
   {
       return top == -1;
   }
   bool isFull()
   {
       return top == capacity - 1;
   }
};
string convert(int num,int base){
   int temp,l=0;
   while(temp){
       temp/=base;
       l++;
   }
   Stack st(l+1);
   if(base<10){
       while(num){
           st.push((char)((num%base)+'0'));
           num/=base;
       }
   }else{
       while(num){
           int r=num%base;
           if(r<=9){
               st.push((char)((num%base)+'0'));
           }else{
               st.push((char)((num%base)%10+'A'));
           }
           num/=base;
       }
   }
   string out="";
   while(!st.isEmpty()){
       out+=st.peek();
       st.pop();
   }
   return out;
  
}

int main(){
   int num,base;
   cin>>num>>base;
   if(base>=2 && base<=16){
       cout<<convert(num,base)<<endl;
   }else{
       cout<<"0"<<endl;
   }
}

Add a comment
Know the answer?
Add Answer to:
your objective is to write a program in c++ to execute the requested task. ? Write...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • C++ Write a recursive function named sumSquares that returns the sum of the squares of the...

    C++ Write a recursive function named sumSquares that returns the sum of the squares of the numbers from  to num, in which num is a nonnegative int variable. Do not use global variables; use the appropriate parameters. Also write a program to test your function.

  • C++ Write a recursive function named sumSquares that returns the sum of the squares of the...

    C++ Write a recursive function named sumSquares that returns the sum of the squares of the numbers from  to num, in which num is a nonnegative int variable. Do not use global variables; use the appropriate parameters. Also write a program to test your function.

  • C++ visual studio program...Write your own version of a class template that will create a dynamic...

    C++ visual studio program...Write your own version of a class template that will create a dynamic stack of any data type. The pop function must return a bool; it should return a false if it was not able to pop an item off the stack. Otherwise it returns true. The parameter to the pop function is passed by reference and should be the item on the list if it was able to pop something. Create a driver program (main) that...

  • Write a program that uses a stack to reverse its inputs. Your stack must be generic...

    Write a program that uses a stack to reverse its inputs. Your stack must be generic and you must demonstrate that it accepts both String and Integer types. Your stack must implement the following methods: push, pop, isEmpty (returns true if the stack is empty and false otherwise), and size (returns an integer value for the number of items in the stack). You may use either an ArrayList or a LinkedList to implement your stack. Also, your pop method must...

  • Write a C++ program that will count the number of words and vowels in a sentence....

    Write a C++ program that will count the number of words and vowels in a sentence. Create a bool function called isVowel that accepts a character as a parameter and returns a true if it’s a vowel (aeiou) and false otherwise. Create an int function named countVowels that will accept a string variable as a parameter and will return the number of vowels in it. Call the isVowel function as part of this process. Write an int function named countWords...

  • Please write MIPS program that runs in QtSpim (ex: MipsFile.s) Write a MIPS program that will...

    Please write MIPS program that runs in QtSpim (ex: MipsFile.s) Write a MIPS program that will read in a base (as an integer) and a value (nonnegative integer but as an ASCII string) in that base and print out the decimal value; you must implement a function (which accepts a base and an address for a string as parameters, and returns the value) and call the function from the main program. The base will be given in decimal and will...

  • Program must be in Python 3. Write a program that prompts the user to enter two...

    Program must be in Python 3. Write a program that prompts the user to enter two strings and then displays a message indicating whether or not the first string starts with the second string. Your program must satisfy the following requirements. 1. Your program must include a function called myStartsWith that takes two string parameters. This function returns True if the rst string starts with the second string and returns False otherwise. 2. Your program must include a main function...

  • help ASAP 3. Write a string C++ function named UnsignedPartialSum() that takes two string parameters and...

    help ASAP 3. Write a string C++ function named UnsignedPartialSum() that takes two string parameters and an int parameter. If both string parameters represent binary numbers and the int parameter is equal to a positive number less than or equal to the length of the longest string parameter, the function should return a binary string whose length is equal to two times the length of the maximum length of the two string parameters whose value is equal to the sum...

  • C# programming 50 pts Question 2:2 1- Create the base class Book that has the following instance variables, constructor, and methods title (String) isbn (String) authors (String) publisher (Strin...

    C# programming 50 pts Question 2:2 1- Create the base class Book that has the following instance variables, constructor, and methods title (String) isbn (String) authors (String) publisher (String) edition ( int) published year (int) Constructor that takes all of the above variables as input parameters. set/get methods ToString method// that return sting representation of Book object. 2-Create the sub class New_Book that is derived from the base class Book and has the following instance variables, constructor, and methods: title...

  • Game Development: Uno For this assignment you will be creating the game of Uno (See the...

    Game Development: Uno For this assignment you will be creating the game of Uno (See the accompanying pdf for the instructions of the game). Your version will adhere to all the rules except that only the next player can issue a challenge against the previous player in regards to penalties, and your games must have at least three (3) players and at most nine (9) players. To begin, you must create the class Card which contains: Private string field named...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT