Question

In: Computer Science

Purpose To develop C++ code that incorporates: (1) generalized conversion from one number base to another;...

Purpose

To develop C++ code that incorporates: (1) generalized conversion from one number base to another; (2) string character extraction and insertion; and (3) a loop trifecta (while, for, and do loops). After this exercise you should better understand the difference between integer and string versions of a number, and the difference between integer and printable character versions of a single digit. Conversions between string and integer representations of a number are needed since you can't perform arithmetic directly on the string version of a number, and you can't index the printable digits directly with the integer version of a number.

What to do

Create a new Visual Studio console project named assignment06. Write C++ code inside the main() function that first prompts for two integers in the range 2 to 10. The first value is the base for numbers entered on the console (ibase), and the second value is the base for numbers displayed on the console (obase). Like the Linux bc program, your program allows the ibase and obase to be independently set. For example, ibase can be 2 and obase can be 10, or vice versa. The ibase and obase can be the same (not very useful but good for testing), or one could be 5 (the valid digits are 0 to 4) and the other could be 8 (the valid digits are 0 to 7).

While the ibase and obase can be read into integer variables, the number to be converted from the ibase to the obase should first be read in as a string, converted to an integer according to the ibase, and then be converted to a output string according to the obase. While the ibase and obase values are checked for validity, the number's input string does not have to be checked for validity.

Algorithm

Here is an appropriate algorithm expressed in pseudocode.

   prompt for ibase and obase
   if (ibase and obase are valid) {
     declare a string named input
     prompt for input 
     while (input != "x") {
       int value = 0
       for (i from 0 to (input.length() - 1)) {
         convert the input[i] printable character to an integer named digit_int
         value = (value * ibase) + digit_int
       }
       declare a string named output
       do {
         int remainder = value % obase
         convert the integer remainder to a printable character named digit_char
         prepend digit_char to output
         value = value / obase
       } while (value != 0)
       display ibase, input, obase, output
       prompt for a string named input
     }
   }
   else {
     display a message indicating that ibase and/or obase is invalid
   }

Converting the algorithm to C++

Pseudocode does not need to precisely specify how an operation is performed in a particular programming language. Examples in the above pseudocode are:

  1. The characters in a C++ string are indexed from 0 (leftmost character) to n-1 (rightmost character) where n is the length of the string. Strings are initially empty (n = 0). There is no string character with index n. Hence a string character index must range from 0 to n-1.
  2. Converting a printable (ASCII) digit to a binary integer value. Take a look at an ASCII table such as https://www.ascii-code.com/ (Links to an external site.) The binary code for the character '0' is 0x30 which is not the same as the binary value for 0 (0x00). The ASCII code for the character '1' is 0x31, '2' is 0x32, ... , '9' is 0x39. Hence one needs to subtract 0x30 (or just '0') to convert from printable ASCII to an integer. It may seem strange but the C/C++ char type is treated as an 8-bit integer allowing you to subtract '0' from another character. Using '0' is preferred over the constant 0x30 because it is clearer what is going on and you don't need to remember the code.
  3. Converting an integer (binary) digit value to a printable (ASCII) character is the inverse function, so just add '0' to the integer value and assign to a char variable.
  4. While a character can be appended to a string using the append() member function, the insert() member function can be used to prepend a character. You can read about how to use insert() at https://stackoverflow.com/questions/3223302/c-insert-char-to-a-string (Links to an external site.) As explained, output.insert(0, 1, digit_char) inserts 1 character (digit_char) at the beginning (index 0) of the string.
  5. There is no need for float or double variables or arithmetic.

here is what i have done so far but im sure there are some mistakes. plz help me complete it


#include <iostream>
#include <cstring>

int main()
{
int ibase, obase;
std::cout << "enter the value for the base of the input followed by the value of the base for the output";
std::cin >> ibase >> obase;
if (ibase <= 10 && ibase >= 2 && obase <= 10 && obase >= 2) {
std::string input;
std::cout << "enter a interger that you want to convert";
std::cin >> input;
while (input != "x") {
int value = 0;
int n;
n = size(input);
for (int i = 0; i < n; i++) {
int digit_int;
  
digit_int = input[i];


  
  

}


}

}
}

Solutions

Expert Solution

#include <iostream>
#include <cstring>

int main()
{
int ibase, obase;
std::cout << "enter the value for the base of the input followed by the value of the base for the output\n";
std::cin >> ibase >> obase;
if (ibase <= 10 && ibase >= 2 && obase <= 10 && obase >= 2) {
std::string input;
std::cout << "enter a interger that you want to convert\n";
std::cin >> input;
while (input != "x") {
int value = 0;
int n;
n = input.length();
int digit_int;
for (int i = 0; i < n; i++) {
digit_int = (int)input[i] - '0';
value=(value*ibase) + digit_int;
}
std::string output;
do{
int remainde = value % obase;
char digit_char = (char)remainde + '0';
output.insert(0, 1,digit_char);
value = value/obase;
}
while(value!=0);
std::cout <<ibase<<" "<<input<<" "<<obase<<" "<<output;
std::cout << "\nenter a interger that you want to convert\n";
std::cin >> input;
}
}
else
{
std::cout<<"Invalid";
}
}

I did code just as things were mentioned in the pseudocode and things that you have researched. One mistake that i saw was size(input). It will be input.length() to find the length of the string.

Hope all problems are solved by this code. For any doubts or questions comment below.


Related Solutions

Develop a C program that generates a random number from 1 to 100, then prompt the...
Develop a C program that generates a random number from 1 to 100, then prompt the user to guess the number until it is guessed correctly. Let the user know if the guess is too high, too low, or correct. The program should count the number of times the user guesses and display the number, along with their rank, after the number is guessed correctly. Rank the user as follows: Super Guesser: 1 to 4 guesses Excellent Guesser: 5 to...
c ++ program that converts from any base to a decimal number
c ++ program that converts from any base to a decimal number
The purpose of this assignment is to develop your ability to code and understand ArrayLists. We...
The purpose of this assignment is to develop your ability to code and understand ArrayLists. We will do this by taking a do-it-yourself (DIY) approach by building our own ArrayList-Like data structure called "ArrayBox". You must: Create a generic class called ArrayBox that uses an ARRAY to store its objects. Assume the initial size of the array is two(2). Your ArrayBox must automatically double the size of your elements array when it becomes full as described in class (see slides)....
Write a C++ program that accepts a positive integer number from the keyboard . The purpose...
Write a C++ program that accepts a positive integer number from the keyboard . The purpose of the program is to find and the display all the square pair numbers between 1 and that number. The user should be able to repeat the process until he/she enters n or N in order to terminate the process and the program. Square numbers are certain pairs of numbers when added together gives a square number and when subtracted also gives a square...
Develop a one-day food plan that incorporates the MIND diet characteristics . Discuss how close your...
Develop a one-day food plan that incorporates the MIND diet characteristics . Discuss how close your current eating habits reflect the food plan you created and 2 simple step you can take to include foods that meet the MIND diet criteria.
how to code in c# to get the total number of 1's as the numbers are...
how to code in c# to get the total number of 1's as the numbers are added in a Listbox in windows forms
Xi = the number of times carton i was transferred from one aircraft to another Yi...
Xi = the number of times carton i was transferred from one aircraft to another Yi = the number of ampules broken upon arrival i 1 2 3 4 5 6 7 8 9 10 Xi 1 0 2 0 3 1 0 1 2 0 Yi 16 9 17 12 22 13 8 15 19 11 i. Fit the Poisson regression model assuming Yi ∼ Poisson(λi) where λi = λ(x, β) = eβ0+β1Xi is the response function. ii. Estimate...
Explain how the number of producers in a market varies from one market structure to another...
Explain how the number of producers in a market varies from one market structure to another with relevant examples?
how can benefit from C++ to develop a simple system for diagnosing number of people whom...
how can benefit from C++ to develop a simple system for diagnosing number of people whom affected by Coronavirus during a period of two weeks depending on the temperature and Oxygen rate of each individual person. Note: the temperature on the normal person is (37) and the Oxygen rate is above (90).
Propose an algorithm in C to match numbers / tokens (words) from one array to another...
Propose an algorithm in C to match numbers / tokens (words) from one array to another array and pull out the matching numbers.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT