Question

Please solve the following question using C++, without any library except for <iostream> and <string>. 3.5 Palindromi c numbers in other bases Write a function multibase which returns all the bases in which an integer is a palindrome Consider the number 21, which in base 10 is not a palindrome. However, if we write 21 in base 2, the result is 10101, which is a palindrome. In base 4, 21 is written as 111 In base 6, 21 is written as 33 In base 20, 21 is written as 11 Hence, the function multibase (21) returns the string 2 4 6 20 since these are the bases in which the number 21 is a palindrome. Here is the function signature string multibase(int x) The function multibase returns all the bases in which x is palindromic, from 2 to x-1 inclusive. Each palindromic base should be separated from the previous one by a space

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

C++ Code:

#include<iostream>

#include<string>

using namespace std;

string multibase(int x)

{

              string bases = ""; //to store all bases

             

              int i, n, j; //variables

              int result[x/2]; //array to store values in each base

              //loop 2 to x-1

              for(int i=2; i<x; i++)

              {

                             n = x; //set n to x

                             j = 0; //index of result

                             //convert to each base

                             while(n>0)

                            {

                                           result[j] = (n%i);

                                           n = n/i;

                                           j++; //increment j

                             }

                             int flag = 1; //assume value is palindrome

                             int l = 0, r = j - 1; //left and right indices of array result

                             //check palindrome or not

                             while(r>l)

                             {

                                           //if any corresponding values are not same break

                                           if(result[l++] != result[r--])

                                           {

                                                          flag = 0;

                                                          break;

                                           }

                             }

                             //if palindrome

                             if(flag == 1)

                             {

                                           string s = ""; //to convert int to string

                                           n = i; //base value

                                           while(n>0)

                                           {

                                                          int k = n%10;

                                                          n = n/10;

                                                          s.push_back((char)(k + '0')); //push each reminder

                                           }

                                           //add s in reverse to bases

                                           for(j=s.length()-1; j>=0;j--)

                                                          bases = bases + s[j];

                                           bases = bases + " "; //add space

                             }

              }

             

              return bases; //return bases

}

int main()

{

              cout<<"multibase(21) : "<<multibase(21)<<endl;

              return 0;

}

OUTPUT:

Add a comment
Know the answer?
Add Answer to:
Please solve the following question using C++, without any library except for <iostream> and <string>. 3.5...
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++ #include <iostream> #include <string> using namespace std; /* Write a function checkPrime such that input:...

    c++ #include <iostream> #include <string> using namespace std; /* Write a function checkPrime such that input: an int output: boolean function: Return true if the number is a prime number and return false otherwise */ //TO DO ************************************** /* Write a function checkPrime such that input: an array of int and size of array output: nothing function: Display the prime numbers of the array */ //TO DO ************************************** /* Write a function SumofPrimes such that input: an int output: nothing...

  • you may not use any of the C-string library functions such as strlen(), strcpy(), strstr(), etc....

    you may not use any of the C-string library functions such as strlen(), strcpy(), strstr(), etc. You must write your own. You might consider writing helper functions to do tasks that many of these functions require, e.g. finding the last character of the string, and then use those helper functions where convenient. This function finds all instances of the char ‘target’ in the string and replaces them with ‘replacementChar’. It also returns the number of replacements that it makes. If...

  • using basic c++ and use no functions from c string library b) Write a fragment of...

    using basic c++ and use no functions from c string library b) Write a fragment of code (with declarations) that reads a pair of words from the keyboard and determines how many letters in corresponding positions of each word are the same. The words consist only of the letters of the English alphabet (uppercase and lowercase). For example, if the words are coat and CATTLE, the following output should be generated: 012245 0122 matching letter in position 0 t is...

  • C++ A Program for Examining String (1) Ask the customer to type any string they like...

    C++ A Program for Examining String (1) Ask the customer to type any string they like and then print it to the console. Example: Type a sentence: The CIS161 Computer Science II course is so much fun! You typed: The CIS161 Computer Science II course is so much fun! (2) Write the remaining code for the CountOfLetters() function, which returns the number of characters in the string that was entered. To get full credit for this question, this should be...

  • Please note that I cannot use the string library function and that it must be coded...

    Please note that I cannot use the string library function and that it must be coded in C89, thank you! Formatting: Make sure that you follow the precise recommendations for the output content and formatting: for example, do not change the text in the first problem from “Please enter a string of maximum 30 characters:” to “Enter string: ”. Your assignment will be auto-graded and any changes in formatting will result in a loss in the grade. 2.Comments: Header comments...

  • C++ Only. Don't use other libraries other than #include <iostream> and using namespace std; Write a...

    C++ Only. Don't use other libraries other than #include <iostream> and using namespace std; Write a function maxTemp which takes a filename as string argument and returns the maximum temperature as float type for the week. Input file will contain temperature readings for each day of the week. Your function should read each line from the given filename, parse and process the data, and return the required information. Your function should return the highest temperature for the whole week. Empty...

  • hi can you please use simple c++, and write with using "Arrays" and include "ctype.h, string,iostream" a...

    hi can you please use simple c++, and write with using "Arrays" and include "ctype.h, string,iostream" and use "while" "||&& operators" "for" "if" statements, ,this should be written in c++101, thanks Function Return Type Description Return size size size t max_size size t Return maximum size (always match with size) Test whether array is empty bool empty Fill array with value fill void void Exchanges the content of the array by the content of x, which is another array object...

  • In C Programming Language In this lab you will implement 4 string functions, two using array...

    In C Programming Language In this lab you will implement 4 string functions, two using array notation and two using pointers. The functions must have the signatures given below. You may not use any C library string functions. The functions are 1. int my strlen (char s ) - This function returns the number of characters in a string. You should use array notation for this function. 2. int my strcpy (char s [], char t I)- This function overwrites...

  • C++ Programming question For this exercise, you will receive two string inputs, which are the names...

    C++ Programming question For this exercise, you will receive two string inputs, which are the names of the text files. You need to check if the input file exists. If input file doesn't exist, print out "Input file does not exist." You are required to create 4 functions: void removeSpace (ifstream &inStream, ofstream &outStream): removes white space from a text file and write to a new file. If write is successful, print out "Text with no white space is successfully...

  • PLEASE HELP!!! C PROGRAMMING CODE!!! Please solve these functions using the prototypes provided. At the end...

    PLEASE HELP!!! C PROGRAMMING CODE!!! Please solve these functions using the prototypes provided. At the end please include a main function that tests the functions that will go into a separate driver file. Prototypes int R_get_int (void); int R_pow void R Jarvis int start); void R_fill_array(int arrayll, int len); void R_prt_array (int arrayl, int len) void R-copy-back (int from[], int to [], int len); int R_count_num (int num, int arrayll, int len) (int base, int ex) 3. Build and run...

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
ADVERTISEMENT