Worksheet IV

advertisement
HiLCoE
School of Computer Science and Technology
CS221: Computer Programming I (Using C++)
Autumn 2015
Worksheet IV
1. Count the factors of 2
Write a C++ code that inputs a positive integer and prints
out the number of factors of two in it. For example, an odd
number has no factors of two, 2 has one factor of 2, 4 = 2
× 2 has 2 factors of 2, 6 = 3 × 2 has 1 factor of two, 8 =
2 × 2 × 2 has 3, 10 = 5 × 2 has 1 factor of 2, 12 = 3 × 2 ×
2 has 2 factors of 2 and so on.
2. List the Common Factors
Write a C++ code that counts all the least common
multipliers of a positive integer including 1 and the
number itself. For example 8 = 1 × 2 × 2 × 2, 17 = 1 × 17,
123456789 = 1 × 3 × 3 × 3607 × 3803. Here are sample uses
of the program:
Input any positive int: 8
8 = 1 x 2 x 2 x 2
Input any positive int: 17
17 = 1 x 17
Input any positive int: 123456789
123456789 = 1 x 3 x 3 x 3607 x 3803
3. Fix the bugs
Consider the following C++ code. It is an attempt to do the
sum:
S = 1− x2 + x4 − x6 + x8 − x10
The code has errors that the compiler detects as indicated
in the comment lines. Fix them. The code also has
“conceptual” errors in some lines, also indicated by
comments. Conceptual errors are those that compile without
error but do not provide correct answers upon execution.
Fix these as well.
HiLCoE
School of Computer Science and Technology
CS221: Computer Programming I (Using C++)
Autumn 2015
#include <iostream>
using namespace std;
int main(void) {
float x;
cout << "Input x: ";
cin >> x;
float Sum, p; // Conceptual errors on this line
int N = 10;
do {
i = i + 2; // Compiler detects error on this line
p = p*x; // Conceptual errors on this line
Sum = Sum + p;
}while(i <= N);//Compiler detects error on this line
cout << "Sum = " << Sum << "\n";
return 0;
}
4. Write a C++ program that will:
(a)
Prompt the user for a positive odd integer within a
do-while construct. If the integer is 0, negative or
even, prompt the user again. The algorithm goes to
the next step when an odd positive integer is
received. Call this odd integer N.
(b)
Output an inverted triangle of asterisks that looks
like the following example. The first row has N *’s.
Each following row has two *’s less than the one
above it and is centered with respect to the row
above.
Here’s an example of how the program should work:
Input
Input
Input
Input
an
an
an
an
odd
odd
odd
odd
positive
positive
positive
positive
int:
int:
int:
int:
-1
0
2
11
HiLCoE
School of Computer Science and Technology
CS221: Computer Programming I (Using C++)
Autumn 2015
***********
*********
*******
*****
***
*
You
You
You
You
must use a do{}while(); loop to obtain N.
must use for-loops to make the drawing.
may not use arrays.
must only use cout statements that have only single
character strings, for example, cout << "*";. Something
like cout << "**"; or cout << " *"; is not permitted.
Spaces are counted as a character.
Your program must work for arbitrary odd, positive N.
5. Computation of the factorial
Consider the following C++ program:
#include <iostream>
using namespace std;
int main(void)
{ cout << "Input N: "; int N; cin >> N;
if (0 > N)
cout << "N < 0. Stopping.\n";
else if (0 == N)
cout << "0! = 1\n";
else
{ int factorial = 1;
for (int i = 1 ; i <= N; i = i +1)
factorial = factorial*i;
cout << N << "! = " << factorial << "\n";
}
return 0;
}
HiLCoE
School of Computer Science and Technology
CS221: Computer Programming I (Using C++)
Autumn 2015
This program computes
input by the user. The
mathematical symbol N!,
N! = N × (N − 1) × (N −
the “factorial” of the integer N,
factorial of N, represented by the
is calculated by:
2) × (N − 3) · · ·3 × 2 × 1
By definition, 0! = 1. Other factorials are 1! = 1, 2! = 2,
3! = 6, 4! = 24, 5! = 120 and so on. Factorials are used a
lot in many branches of mathematics.
Your problem is to find out for what values of N input by
the user, does the program above not give correct answers.
(Here’s a hint: The maximum signed integer has a value 231 −
1 or 2147483647.)
6. This question is all about loops.
A solution for this may involve all of the loops we have
encountered: the do{}while() loop, the for(){} loop, and
the while(){} loop.
A solution can also be facilitated with the use of the
modulus operator (%).
A prime number is a positive whole number that can be
divided perfectly only by itself and one. The first 26
prime numbers are 1, 2, 3, 5, 7, 11, 13, 17, 19, 23, 29,
31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89,
and97. You will write a C++ code that will accept two
integers from its user. You will restrict the range of
inputs to between 4 and 1,000,000 (one million).
Once you have two integers you can work with, you will find
all the prime numbers between those two numbers, including
the starting and ending numbers themselves. You will output
the prime numbers as you find them to the screen, and as a
last step, print out how many of them you found.
Make sure that the primes your program gives agree with the
ones in the list above.
There are 78,496 prime numbers between 4 and one million.
To check if a number N is prime or not, it is not necessary
to do as many as N–2 divisions to verify. In fact we need to
check only up to the square root of N or √N.
HiLCoE
School of Computer Science and Technology
CS221: Computer Programming I (Using C++)
Autumn 2015
7. Reverse an integer
Given an integer n such as 1367, write a program that
computes the inverse of this integer as 7631 and also the
double of the inverse 15262. Also, when the input changes
to a different number like 2467, it should compute the
inverse 7642 and twice that as 15284, and when the input
changes to 5 digits number 12345, it computes 54321 and
108642.
Using arrays is not allowed, arrays are not covered yet at
this moment.
Download