Download HiLCoE School of Computer Science and Technology CS221

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

Positional notation wikipedia , lookup

Proofs of Fermat's little theorem wikipedia , lookup

Addition wikipedia , lookup

Halting problem wikipedia , lookup

Elementary mathematics wikipedia , lookup

Transcript
HiLCoE
School of Computer Science and Technology
CS221: Computer Programming I (Using C++)
Autumn 2015
Worksheet III
1. Your first programming problem
You have to create an entire code that will compile
correctly, accept inputs and provide outputs and stops.
Your program will do the following:
(a) Ask the user to input two non-negative integers.
(b) If the user inputs a negative integer, the program
issues
an error message and stops.
(c) If the two numbers are equal, output is provided to
inform the user. Otherwise the user is informed which
number is larger and which is smaller.
(d) The program outputs the ratio of the larger number to
the smaller number unless the smaller number is zero. If
the smaller number is zero, a message to this effect is
output.
(e) The program outputs the quadrature sum of the two
numbers,
that
is:
firstInteger
*
firstInteger
+
secondInteger * secondInteger.
(f) If the smaller number is zero, the program stops.
Otherwise, the program checks if the smaller number is a
perfect divisor of the larger number. This means there
is no remainder after division. For example, 2 is a
perfect divisor of 8, but not 7. Output is provided to
inform the user whether or not a perfect divisor was
found.
(g) Program stops.
The above set of instructions is really a coarse pseudocode
for your program. Read the instructions very carefully.
Here are some example uses of the program. Your program
should work exactly the same.
> a.out
Input the 1st
Input must be
> a.out
Input the 1st
Input the 2nd
Input must be
integer: -1
non-negative. Try again!
integer: 1
integer: -1
non-negative. Try again!
HiLCoE
School of Computer Science and Technology
CS221: Computer Programming I (Using C++)
Autumn 2015
> a.out
Input the 1st integer: 1
Input the 2nd integer: 1
The two numbers are the same!
The ratio is 1, the quadrature sum is 2
1 is a perfect divisor of 1
> a.out
Input the 1st integer: 10
Input the 2nd integer: 0
The bigger number is 10, the smaller number is 0
The smaller number is 0, so no ratio is calculated, the
quadrature sum is 100
> a.out
Input the 1st integer: 0
Input the 2nd integer: 10
The bigger number is 10, the smaller number is 0
The smaller number is 0, so no ratio is calculated, the
quadrature sum is 100
> a.out
Input the 1st integer: 7
Input the 2nd integer: 4
The bigger number is 7, the smaller number is 4
The ratio is 1, the quadrature sum is 65
4 is not a perfect divisor of 7
> a.out
Input the 1st integer: 4
Input the 2nd integer: 7
The bigger number is 7, the smaller number is 4
The ratio is 1, the quadrature sum is 65
4 is not a perfect divisor of 7
> a.out
Input the 1st integer: 8
Input the 2nd integer: 2
The bigger number is 8, the smaller number is 2
The ratio is 4, the quadrature sum is 68
2 is a perfect divisor of 8
> a.out
Input the 1st integer: 2
Input the 2nd integer: 8
The bigger number is 8, the smaller number is 2
The ratio is 4, the quadrature sum is 68
2 is a perfect divisor of 8
HiLCoE
School of Computer Science and Technology
CS221: Computer Programming I (Using C++)
Autumn 2015
2. Leap year calculation
The rules to calculate if it is a leap year are:
(a) A year divisible by 4 is a leap year (2004, 2008...),
unless
(b) It is also divisible by 100 (2100, 2200...) in which
case it is not a leap year.
(c) There is an exception. A year divisible by 400 is a leap
year (2000, 2400...).
Write a program that will accept a positive int, the year
to be tested, from a user.
If the user inputs a 0 or negative number, the program
quits without doing
anything. Otherwise, it will print a
message saying whether or not the year is a leap year. Here
is an example of how the program works.
Unix-prompt> a.out
Input a number for the year (>= 1): -1
Sorry, your year must be greater than 1.
Unix-prompt> a.out
Input a number for the year (>= 1): 2001
The year 2001 is not a leap year
Unix-prompt> a.out
Input a number for the year (>= 1): 2004
The year 2004 is a leap year
Unix-prompt> a.out
Input a number for the year (>= 1): 2100
The year 2100 is not a leap year
Unix-prompt> a.out
Input a number for the year (>= 1): 2400
The year 2400 is a leap year
HiLCoE
School of Computer Science and Technology
CS221: Computer Programming I (Using C++)
Autumn 2015
3. When in Rome...
The following table shows how Roman numerals are formed:
A Roman number is written in decades starting with the largest
decade on the left and then going to the right. For example,
the number 1999 would be represented as
MCMXCIX. The largest number that can be represented by Roman
numerals is 3999, or MMMCMXCIX. Write a program that will
accept a positive int, between 1 and 3999 inclusive. If the
user inputs something outside this range, the program quits
without doing anything. Otherwise, it will print the
conversion to Roman numerals.
Here is an example of how the program works.
Unix-prompt> a.out
Input an integer between
Sorry, your integer must
Unix-prompt> a.out
Input an integer between
Sorry, your integer must
Unix-prompt> a.out
Input an integer between
MCCCXXXVIII
Unix-prompt> a.out
Input an integer between
MMMDCCCLXXXVIII
Unix-prompt> a.out
Input an integer between
LII
1 and 3999 inclusive: 0
be between 1 and 3999 inclusive.
1 and 3999 inclusive: 4000
be between 1 and 3999 inclusive.
1 and 3999 inclusive: 1338
1 and 3999 inclusive: 3888
1 and 3999 inclusive: 52
HiLCoE
School of Computer Science and Technology
CS221: Computer Programming I (Using C++)
Autumn 2015
4. Base 10 to binary conversion
Write a C++ program that will accept any integer between 0
and 127, inclusive, and convert it to binary. You may not
use loops of any kind and your solution must NOT resemble
the following:
if (decimal == 0) cout << "0";
else if (decimal == 1) cout << "1";
else if (decimal == 2) cout << "10";
else if (decimal == 3) cout << "11";
.
.
.
else if (decimal == 126) cout << "1111110";
else if (decimal == 127) cout << "1111111";
Here’s an example of how it should work for an input of
126:
Unix-prompt> a.out
Input a positive int between 0 and 127 inclusive: 126
Decimal 126 converted to base 2 is 1111110
Hint: Depending on your algorithm, you may find it useful to
use the escape sequence “\b” which causes the cursor to move
backwards one space.
5. Something loopy
How would you write a C++ program that could convert any
unsigned integer to any base between 2 and 9? Using loops,
this is actually easier to do than the solution to the
previous problem!
Here’s an example of how it should work for an input of
126:
Unix-prompt> a.out
Input the base to convert to between 2 <= b <= 9: 7
Input any positive unsigned int: 126
Decimal 126 converted to base 7 is 240