$29
Motivation
This project will give you experience in using basic C++ constructs including I/O, arithmetic operators, branch, and loop.
Introduction
Integers have many properties. For example, they can be odd, even, prime, or composite. In this project, we will test the following four properties of a given integer (in this project, we will assume that a valid given integer is positive):
1. Triangle number: an integer is called a triangle number if it equals ( + 1)/2, where n is an integer.
2. Palindrome: an integer is called a palindrome if the number reads the same both ways. For example, 121 and 4334 are palindromes.
3. Odious number: an integer is called an odious number if it has an odd number of 1 in its binary representation. For instance, 1, 2, and 4 are odious, but 3 is not.
4. Extravagant number: an integer is called an extravagant number if it has fewer digits than the number of digits in its prime factorization (including exponents). Note that 2 is
the smallest prime. Thus, 4 = 22 is extravagant (1 digit in the number versus 2 digits in its prime factorization); 6 = 2 × 3 is also extravagant. However, 125 = 53 is not. Note that for the number 1, it is not (please think about why).
Programming Assignment
You will implement a program that tests whether a given integer has a specific property.
Input/Output
Your program should first prompt:
“Please enter the integer and the test number: ”
You must use exactly this prompts. Don't forget the trailing single space!
Then your program will take two integers as inputs, separated by a white space. The first one is an integer to be tested and the second one is an integer between 1 and 4, denoting one of the above properties to be tested for. The first integer should be a positive integer and be no larger than 10 million. The second input should be in the range between 1 and 4, inclusively. If either input entered is outside its range, your program should prompt the above statement and take the inputs again. (You should not prompt anything other than the above statement.) You can assume that the user always enters integral values, not any other erroneous inputs (i.e., you can always read the value into a variable of int type). Assume the entered values are within the range from -20,000,000 to 20,000,000.
Your output will be either 0 or 1, where 1 indicates that the test succeeds and 0 indicates that the test fails.
Thus, the input and output will look like:
Please enter the integer and the test number: 4 1 0
Below is a situation where the first input attempt fails.
Please enter the integer and the test number: -1 1 Please enter the integer and the test number: 4 1 0
Note the prompt of the statement for the second time because the first value you input at the first time is illegal (negative).
Implementation Requirements
You should put all of the functions you write in a single file, called p1.cpp. You may only include <iostream>, <cmath>, <string>, and <cstdlib>. No other system header files may be included, and you may not make any call to any function in any other library.
Compiling and Testing
To compile, type the following Linux command:
g++ -Wall -o p1 p1.cpp
You should test your program extensively.
Submitting and Due Date
You only need to submit your source code file p1.cpp (name it exactly like this!). The source code file should be submitted via the online judgment system. See TAs’ announcement for additional details. The due date is 11:59 pm on May 31, 2019.
Grading
Your program will be graded along three criteria:
1. Functional Correctness
2. Implementation Constraints
3. General Style
An example of Functional Correctness is whether or not you produce the correct output. Implementation Constraints checks whether you stick to the implementation requirements. General Style speaks to the cleanliness and readability of your code. We don't need you to follow any particular style, as long as your style is consistent and clear. Some typical style requirements include: 1) appropriate use of indenting and white space, 2) program appropriately split into subroutines, 3) variable and function names that reflect their use, and 4) informative comments at the head of each function.