Write a recursive a c++ code that checks if a number is Palindrome. A palindrome number is a number that reads the same from beginning to end and from end to beginning, in other words, a palindrome number remains the same when its digits are reversed. For example, 13431 is a palindrome number. 2332 is another one. (Note: Your algorithm should define and work with an integer number) The functionality of your code should be commented to explain what you do in each part of the code. You need to run your code for the following test cases and show the result:
1) 0 (output: yes)
2) 1234554321 (output: yes)
3) 123454321 (output: yes)
4) 1221 (output: yes)
5) 1234 (output: no)
6) 7676 (output: no)
7) -121 (output: yes)
8) -456 (output: no)
What is the time complexity of your algorithm? Cleary justify your answer.
In: Computer Science
Modify the insertionSort() method in insertSort.java so it counts the number of copies and the number of comparisons it makes during a sort and displays the totals. To count comparisons, you will need to break up the double condition in the inner while loop. Use this program to measure the number of copies and comparisons for different amounts of inversely sorted data. Do the results verify O(N2 ) efficiency? Do the same for almost-sorted data (only a few items out of place). What can you deduce about the efficiency of this algorithm for almost-sorted data?
// insertSort.java
class ArrayIns
{
private long[] a; // ref to array a
private int nElems; // number of data items
public ArrayIns(int max) // constructor
{
a = new long[max]; // create the array
nElems = 0; // no items yet
}
public void insert(long value) // put element into array
{
a[nElems] = value; // insert it
nElems++; // increment size
}
public void display() // displays array contents
{
for(int j=0; j<nElems; j++) // for each element,
System.out.print(a[j] + " "); // display it
System.out.println("");
}
public void insertionSort()
{
int in, out;
for(out=1; out<nElems; out++) // out is dividing line
{
long temp = a[out]; // remove marked item
in = out; // start shifts at out
while(in>0 && a[in-1] >= temp) // until one is
smaller,
{
a[in] = a[in-1]; // shift item to right
--in; // go left one position
}
a[in] = temp; // insert marked item
} // end for
} // end insertionSort()
} // end class ArrayIns
class InsertSortApp
{
public static void main(String[] args)
{
int maxSize = 100; // array size
ArrayIns arr; // reference to array
arr = new ArrayIns(maxSize); // create the array
arr.insert(77); // insert 10 items
arr.insert(99);
arr.insert(44);
arr.insert(55);
arr.insert(22);
arr.insert(88);
arr.insert(11);
arr.insert(00);
arr.insert(66);
arr.insert(33);
arr.display(); // display items
arr.insertionSort(); // insertion-sort them
arr.display(); // display them again
} // end main()
} // end class InsertSortApp
In: Computer Science
Write a program that prompts the user to input a decimal number and outputs the number rounded to the nearest integer.
In: Computer Science
Write a recursive a Java code that checks if a number is
Palindrome. A
palindrome number is a number that reads the same from beginning to
end and
from end to beginning, in other words, a palindrome number remains
the same
when its digits are reversed. For example, 13431 is a palindrome
number. 2332
is another one. (Note: Your algorithm should define and work with
an integer
number)
The functionality of your code should be commented to explain what
you do in
each part of the code.
You need to run your code for the following test cases and show the
result:
1) 0 (output:
yes)
2) 1234554321 (output: yes)
3) 123454321 (output: yes)
4) 1221 (output: yes)
5) 1234 (output:
no)
6) 7676 (output: no)
7) -121 (output: yes)
8) -456 (output: no)
What is the time complexity of your algorithm? Cleary justify
your
answer.
In: Computer Science
Complete the attached code so that if the user enters the number 22 for the number of items purchased and 10.98 for the price of each, the following results are displayed:
The total bill is $241.56
Hint: It must include cin and variables.
// This program will read the quantity of an item and its price
// Then print the total price.
// The input will come from the keyboard and the output will go to the monitor.
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
int quantity; // number of item purchased
float itemprice; // price of every article
float totalbill; // total account
cout << setprecision(2) << fixed << showpoint;
cout << “please, enter number of items purchased” << endl;
// indicate the instruction to record the amount
// indicate the instruction to ask the price
// indicate the instruction to record the price of each item
// indicate the assignment instruction to determine the total account
// indicate the instruction to show the total on the monitor
return 0;
}
In: Computer Science
In the United States, a Social Security number (SSN) is a
nine-digit number that
is a national identification number for taxation purposes. Write a
program that
asks the user for their social security number in DDDDDDDDD format
(where
D is digit). The program first performs a basic check if the number
corresponds
to actual SSN by making sure it has 9 characters in it. If yes, it
creates a string
variable new ssn with SSN in user-friendly DDD − DD − DDDD format
and
prints it. If not, it prints an error message Incorrect SSN
length.
use python
In: Computer Science
Design an algorithm to prompt user to enter a number. Determine the number is odd or even with Case Logic.
In: Computer Science
Assignment - Number Guessing with a Class
For this assignment you will revisit the number guessing game in which the user picks a number, and your program tries to guess the number.
For review, a sample run of the program is printed below. In the example the user picks the value 72 for the first game, and then 25 for the second game. The user's input is in bold. Here is how the output might look (exact numbers may vary) :
> Think of a number between 1 and 100
> Is the number 50? (h/l/c): h
> Is the number 75? (h/l/c): l
> Is the number 62? (h/l/c): h
> Is the number 68? (h/l/c): h
> Is the number 71? (h/l/c): h
> Is the number 73? (h/l/c): l
> Is the number 72? (h/l/c): c
> You picked 72? Great pick.
> Do you want to play again? (y/n): y
> Think of a number Between 1 and 100
> Is the number 50? (h/l/c): l
> Is the number 25? (h/l/c): c
> You picked 25? Great pick.
> Do you want to play again: (y/n): n
> Good bye.
The point of interest for us is not necessarily the game - but rather the design of the program. The essential part of this assignment is writing the NumberGuesser class. This class will contain all of the logic for guessing.
Write your NumberGuesser class as if it is going to be used in many different guessing games, created by different developers. You want to create a class that will be a useful tool in different contexts.
When a new instance of a NumberGuesser class is instantiated the upper and lower bounds of the possible values should be passed into its constructor. From that point on a NumberGuesser object will always return the mid-point of the possible values when the getCurrentGuess() method is called.
If the higher() or lower() methods are invoked, the NumberGuesser object should adjust its state to represent the new possible range of values. For example, if a NumberGuesser is created with the following line of code then the range will be the numbers from 1 to 100:
NumberGuesser guesser = new NumberGuesser(1, 100);
If the getCurrentGuess() method is called it should return 50, which is the midpoint between 1 and 100. If the higher() method is invoked then the object should adjust its state accordingly so that it knows that the correct value is between 51 and 100. If the lower() method is invoked then it should adjust its state to represent that the possible values are between 1 and 49.
After that, the getCurrentGuess() should return the value that is in the middle of the new range of possible values. (Either 75 or 25). By following this strategy the number guesser should be able to eventually guess the proper value.
Here is the basic design of the NumberGuesser class that you should write. The instance variables have been left up to you.
NumberGuesser Class
|
Private Instance Variables ?? |
|
Public Methods and Constructors NumberGuesser(int lowerBound, int upperBound) void higher(); void lower(); int getCurrentGuess(); void reset(); |
The reset() method should return the return a NumberGuesser to the state that it was in when it was constructed. In order to do this your class will need to be able to remember its original state. You can use two additional instance variables to store the original upper and lower bounds.
Write your number guess class and test it until you are sure that it is working properly. After it is working, write the rest of the program so that it plays the game by using an instance of your NumberGuesser class.
Note: Your NumberGuesser class should not use a Scanner object or System.out. It is only responsible for handling the indicated methods. All of the input and output work should be handled elsewhere in your program.
Testing your NumberGuesser class
You might find it useful to test your number guesser class with the main method in this program. This is not required. Testing NumberGuesser with Random Numbers
What to submit
Submit two .java files in a compressed directory:
For this assignment you will revisit the number guessing game in which the user picks a number, and your program tries to guess the number.
For review, a sample run of the program is printed below. In the example the user picks the value 72 for the first game, and then 25 for the second game. The user's input is in bold. Here is how the output might look (exact numbers may vary) :
> Think of a number between 1 and 100
> Is the number 50? (h/l/c): h
> Is the number 75? (h/l/c): l
> Is the number 62? (h/l/c): h
> Is the number 68? (h/l/c): h
> Is the number 71? (h/l/c): h
> Is the number 73? (h/l/c): l
> Is the number 72? (h/l/c): c
> You picked 72? Great pick.
> Do you want to play again? (y/n): y
> Think of a number Between 1 and 100
> Is the number 50? (h/l/c): l
> Is the number 25? (h/l/c): c
> You picked 25? Great pick.
> Do you want to play again: (y/n): n
> Good bye.
The point of interest for us is not necessarily the game - but rather the design of the program. The essential part of this assignment is writing the NumberGuesser class. This class will contain all of the logic for guessing.
Write your NumberGuesser class as if it is going to be used in many different guessing games, created by different developers. You want to create a class that will be a useful tool in different contexts.
When a new instance of a NumberGuesser class is instantiated the upper and lower bounds of the possible values should be passed into its constructor. From that point on a NumberGuesser object will always return the mid-point of the possible values when the getCurrentGuess() method is called.
If the higher() or lower() methods are invoked, the NumberGuesser object should adjust its state to represent the new possible range of values. For example, if a NumberGuesser is created with the following line of code then the range will be the numbers from 1 to 100:
NumberGuesser guesser = new NumberGuesser(1, 100);
If the getCurrentGuess() method is called it should return 50, which is the midpoint between 1 and 100. If the higher() method is invoked then the object should adjust its state accordingly so that it knows that the correct value is between 51 and 100. If the lower() method is invoked then it should adjust its state to represent that the possible values are between 1 and 49.
After that, the getCurrentGuess() should return the value that is in the middle of the new range of possible values. (Either 75 or 25). By following this strategy the number guesser should be able to eventually guess the proper value.
Here is the basic design of the NumberGuesser class that you should write. The instance variables have been left up to you.
NumberGuesser Class
|
Private Instance Variables ?? |
|
Public Methods and Constructors NumberGuesser(int lowerBound, int upperBound) void higher(); void lower(); int getCurrentGuess(); void reset(); |
The reset() method should return the return a NumberGuesser to the state that it was in when it was constructed. In order to do this your class will need to be able to remember its original state. You can use two additional instance variables to store the original upper and lower bounds.
Write your number guess class and test it until you are sure that it is working properly. After it is working, write the rest of the program so that it plays the game by using an instance of your NumberGuesser class.
Note: Your NumberGuesser class should not use a Scanner object or System.out. It is only responsible for handling the indicated methods. All of the input and output work should be handled elsewhere in your program.
Testing your NumberGuesser class
You might find it useful to test your number guesser class with the main method in this program. This is not required. Testing NumberGuesser with Random Numbers:
https://www.google.com/url?q=https://docs.google.com/document/d/1WtbkpOZ3OoKuf94HaBCC5or8-dUjClNey6C0wKUNi28/pub?embedded%3Dtrue&sa=D&ust=1571212772373000
What to submit
Submit two .java files in a compressed directory:
In: Computer Science
A complex number is a number with two components, namely, the real part (rel) and the imaginary part (img). For example the complex number (a+bi) has a as the real part and b as the imaginary part. In developing such a class, assume that both of the real and imaginary parts to be floats. The symbol i represent . A declaration for the Complex class is presented next.
class Complex {
public:
Complex (float r = 0.0, float g= 0.0);
Complex (const Complex & c); //The copy
constructor
void print( ) const; // Print out the class data
Complex & negate ( ); // Multiplies the data members of a
complex object by a
// negative one.
Complex addComplex (const Complex & b);
// add two complex numbers, the calling
//object and b and return a new complex //one
Complex subComplex (const Complex &b);
// subtract two complex numbers, the
// calling object and b and return a new
// complex one
Complex multipComplex ( const Complex &c) const; // multiply
two complex
// numbers and return a complex one
Complex assginComplex ( const Complex & c); // assign one
complex number to
// another one and return the result in a complex object.
bool isEqual (const Complex & c) const; //compares one complex
number to
// another and returns a Boolean value
private:
float rel;
float img;
};
Implement and test the class Complex. Write a driver program to
test your Complex class. Instantiate several Complex objects. Test
that all your member functions work properly.
Hint
• Addition of two complex numbers: (a+bi) +(c+di) =
(a+c) + (b+d) i
• Subtraction: (a+bi) (c+di) = (a-c) + (b-d) i
• Multiplication: (a+bi) * (c+di) = (ac-bd) + (ad+bc)
i
A sample run is given below:
Please enter the first complex number C1(r,img) ; 1 -5
Please enter the second complex number C2(r,img) ; 2 11
Testing the copy constructor C3= C2 = 2+11i
Testing the negate function C4 = C3.negate() = -2 -11i
Testing the assign function C5 = C3.assignComplex(C4) = -2 -11i
Testing the isEqual function C4.isEqual(C5) = 1
Testing the Add Complex function C6 = C1.addComplex(C2) = 3+6i
Testing the Sub Complex function C7 = C1.subComplex(C2) = -1 -16i
Testing the multiply Complex function C8 = C1.multipComplex(C2) = 57+1i
In: Computer Science
A palindromic number is a number that remains the same when its digits are reversed. For examples, 1, 11, 99, 121 are palindromic. Write a program that takes one array of 10 positive integers as input and output all the palindromic numbers in this array. We assume that each input number is valid, i.e., a positive integer.
Hint: You can consider the following steps to check whether a number (e.g., 121) is palindromic • Store this number to one variable, e.g., temp = 121. • Calculate the value of the number when all the digits in temp are reversed, e.g., extracting each digit in temp from the right-hand side to left-hand side, and calculating the value at the same time. • Check whether the original value equals to this calculated value.
write it in c++
In: Computer Science