Question

In: Computer Science

write C++ program using functions (separate function for each bottom) Write a program to find if...

write C++ program using functions

(separate function for each bottom)

Write a program to find if a number is large word for two given bottom base - bottom1 and bottom2. You can predict that a number, when converted to any given base shall not exceed 10 digits. .

the program should ask from user to enter a number that it should ask to enter the base ranging from 2 to 16 after that it should check if the number is palindrom or not
Sample Input:

A number is called a word if it's represented in its bottom. e.g. Let  bottom1 = 6 and bottom2 = 2 as it is not a word in base 5(1010)(as the reciprocal of the number isnt a palindrome).
bottom is base of a number
take bases as input from user
bases can go from decimal-hexadecimal.

for bottom1 = 3 & bottom2 = 4, then the number 130 (in base 10) will be called a large_word, as it is word in bottom 3 (11211) as well as in bottom 4 (2002). However, it is not a large_word for bottom1 = 3 and bottom2 = 5 as it is not a word in bottom 5(1010).

Number: 51

bottom(base) 1: 6

bottom(base) 2: 2

Sample Output:
51 is not large word

hint:
bottom is basically base of a number

A large _word is a word, phrase, number, or other sequence of characters which reads the same reverse or straightward.

word==palindrome

use function in the program at all time

Solutions

Expert Solution

Code-

#include <stdio.h>

#include <iostream>

#include <string.h>

using namespace std;

char val(int n)// to return the charcter of the number accordingly

{

if(n>=0 && n<=9)

return (char)(n+'0'); // returns the normal charcter from 0-9

else

return (char)(n-10+'A');// returns char from A-F accordingly

}

string reverse(string s)// to reverse the string

{

string rev="";

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

rev+=s[i];

return rev;

}

int base1(int b1,int num)// to convert to base 1

{

string s1;

while(num>0)

{

s1+=val(num%b1);

num=num/b1;

}

if(s1==reverse(s1))// compare reverse with original

return 1;

else

return 0;

}

int base2(int b2,int num)// to convert to base 2

{

string s2;

while(num>0)

{

s2+=val(num%b2);

num=num/b2;

}

if(s2==reverse(s2))//compare reverse with original

return 1;

else

return 0;

}

int main()

{

cout<<"Number: ";

int num,b1,b2;

cin>>num;

cout<<"bottom base 1: ";

cin>>b1;

cout<<"bottom base 2: ";

cin>>b2;

if(base1(b1,num)==1 && base2(b2,num)==1)// checking for large word

cout<<num<<" is a large word";

else

cout<<num<<" is not a large word";

return 0;

}

Indentation-

Output-


Related Solutions

C++ program to perform each of the area calculations in separate functions. Your program will take...
C++ program to perform each of the area calculations in separate functions. Your program will take in the relevant information in the main (), call the correct function that makes the calculation, return the answer to the main () and then print the answer to the screen. The program will declare a variable called “choice” of type int that is initialized to 0. The program will loop while choice is not equal to 4. In the body of the loop...
Write a C++ program which consists of several functions besides the main() function. The main() function,...
Write a C++ program which consists of several functions besides the main() function. The main() function, which shall ask for input from the user (ProcessCommand() does this) to compute the following: SumProductDifference and Power. There should be a well designed user interface. A void function called SumProductDifference(int, int, int&, int&, int&), that computes the sum, product, and difference of it two input arguments, and passes the sum, product, and difference by-reference. A value-returning function called Power(int a, int b) that...
Can you solve this C program by using Function? Q1. Write a C program to ring...
Can you solve this C program by using Function? Q1. Write a C program to ring the computer bell at any number of times you specify. Use the system clock as a delay, you need to include the time header file.
Write a C++ program using separate void which asks the user to input side of a...
Write a C++ program using separate void which asks the user to input side of a square, radius of a circle , height and base of a triangle and finds the area of squares, circles and triangles. Then using main function display the area of square, circle and triangle
Using c++, write a program that will display your name as a void function then will...
Using c++, write a program that will display your name as a void function then will perform the following by user-defined functions: a. to compute for the sum of two numbers (n1, n2) using function.
Write a C or C++ program using the fork() system call function. You will need to...
Write a C or C++ program using the fork() system call function. You will need to create 3 processes – each process will perform a simple task. Firstly, create an integer "counter" initialized to a random value between 1 and 100. Print this number to the console. This can be done by: Including the stdio.h and stdlib.h libraries Using the rand() function to generate your randomly generated number The main thread consists of the parent process. Your job is to...
IN C++ Write a program to find the number of comparisons using binarySearch and the sequential...
IN C++ Write a program to find the number of comparisons using binarySearch and the sequential search algorithm as follows: Suppose list is an array of 1000 elements. 3 Search list for some items as follows: a. Use the binary search algorithm to search the list. (You may need to modify the algorithm given in this chapter to count the number of comparisons.) b. Use the binary search algorithm to search the list, switching to a sequentialsearch when the size...
IN C++ Write a program to find the number of comparisons using binarySearch and the sequential...
IN C++ Write a program to find the number of comparisons using binarySearch and the sequential search algorithm as follows: Suppose list is an array of 1000 elements. 2 Use any sorting algorithm to sort list.
In C++ Write a program to find the number of comparisons using binarySearch and the sequential...
In C++ Write a program to find the number of comparisons using binarySearch and the sequential search algorithm as follows: Suppose list is an array of 1000 elements. 5.1 Use a random number generator to fill list.
C++ program assignment asks to implement the following functions. Each function deals with null terminated C-strings....
C++ program assignment asks to implement the following functions. Each function deals with null terminated C-strings. Assume that any char array passed into the functions will contain valid, null-terminated data. The functions must have the signatures listed below. 1. This function returns the last index where the target char can be found in the string. it returns -1 if the target char does not appear in the string. For example, if s is “Giants” and target is ‘a’ the function...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT