Download Looping problems

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

Law of large numbers wikipedia , lookup

Mathematics of radio engineering wikipedia , lookup

Large numbers wikipedia , lookup

Elementary arithmetic wikipedia , lookup

Arithmetic wikipedia , lookup

Location arithmetic wikipedia , lookup

Proofs of Fermat's little theorem wikipedia , lookup

Collatz conjecture wikipedia , lookup

Halting problem wikipedia , lookup

Addition wikipedia , lookup

Elementary mathematics wikipedia , lookup

Transcript
Adapted from Copyleft 2003 Fred Swartz
Looping problems
(1)
Write a program which reads a sequence of integers and counts how many there are. Print
the count. For example, with input 55 25 1004 4 -6 55 0 55, the output should be 8
because there were eight numbers in the input stream. Your program should work for any
input, not just this example.
(2)
Write a program which reads a sequence of integers and counts how many times the
number 55 occurs. Print the count. For example, with input 55 25 1004 4 -6 55 0 55, the
output should be 3 because there were 3 55's in the input stream. Your program should
work for any input, not just this example.
End-Of-File (EOF) when reading from cin
// Programming Idiom
int i =0;
int sum =0 ;
while(cin >> i)
// will keep reading until EOF or error
{
sum += i;
}
cout << “ sum is “ << sum << endl;
When a program is reading from a disk file, the system "knows" when it gets to the end.
This condition is called End-Of-File (EOF). All systems also provide some way of
indicating an EOF when reading from the keyboard. This varies from system to system.
MS Visual C++ EOF
Type: ENTER Control-z ENTER ENTER
Dev-C++ EOF
Type: ENTER Control-z ENTER
. In the figure below I typed an EOF with ENTER Control-z ENTER ENTER. Notice the
display say ^Z when I did this. I could have achieved the same effect with ee or fgrgr etc
this causes cin to ‘fault’ by typing a simple string (e.g. dd) instead of an expected integer
etc
Adapted from Copyleft 2003 Fred Swartz
Looping problems
(3)
Write a program that reads daily temperatures, as floats. Read in a loop until an EOF.
After the input has been read, print the maximum and minimum values. You can assume
that there is at least one input data value.
Sample input values
10.0 11.3 4.5 -2.0 3.6 -3.3 0.0
The output should look something like the following. Formatting may vary.
Maximum = 11.3
Minimum = -3.3
NOTE:
Be careful in choosing the initial values for the maximum and minimum values. Check
your program works correctly when all numbers are negative!!
(4)
Write a program that reads daily temperatures, as floats. Read in a loop until an EOF.
After the input has been read, print the average value.
Sample input values
10.0 11.3 4.5 -2.0 3.6 -3.3 0.0
The output should look something like the following. Formatting may vary.
Average = Average is 3.44286
Hints
No input. If there is no input, your program might try to divide by zero, which is a very
bad idea. How will you prevent this problem?
(5)
Write a complete program which reads floating point numbers from cin and computes
two averages: the average of the negative numbers it reads and the average of the positive
numbers it reads.
For example, for input 9 -1 -1 -4 1 the positive average would be 5 (the sum of the
positive numbers (9+1) divided by the number of positive numbers (2). By a similar
process the average of the negative numbers would be -2 (ie, ((-1 + -1 + -4) / 3). You
don't need to write any functions for this problem.
Adapted from Copyleft 2003 Fred Swartz
Looping problems
(6)
More difficult problem and it requires nested loops
A mathematician named Ulam proposed generating a sequence of numbers from any
positive integer n ( where n > 0) as follows:
* If n is 1, stop.
* If n is even, the next number is n/2.
* If n is odd, the next number is 3*n + 1.
* Continue with this process until reaching 1.
Here are some examples for the first few integers.
2 -> 1
3 -> 10 -> 5 -> 16 -> 8 -> 4 -> 2 -> 1
4 -> 2 -> 1
5 -> 16 -> 8 -> 4 -> 2 -> 1
6 -> 3 -> etc as for 3 above.
7 -> 22 -> 11 -> 34 -> 17 -> 52 -> 26 -> 13 -> 40 -> 20 -> 10 -> 5 -> see 5 above.
Does every sequence stop?
It's unknown if all sequences end at 1. Perhaps there are sequences that get into a loop, or
increase indefinitely.
Write a program that reads a number (in a while loop of course) and prints the Ulam
sequence for it. Let the user enter -1 to finish.
Possible extensions
There are several ways to extend this program.
(6a) Instead of reading input, write a for loop that examines the length of the Ulam
sequence for the first 1,000 numbers. Which number produces the longest sequence, and
how long is it? Or do some sequences never terminate?
(6b)Some intermediate numbers become quite large. What is the largest number that is
ever encountered in the Ulam sequences for the first 1,000 numbers?
Adapted from Copyleft 2003 Fred Swartz
Looping problems
(7)
Write a program which reads an int, then prints a parallelogram with edges that are that
size. For example, when it reads the number 5, the output would be
*****
*****
*****
*****
*****
Hints
This is most easily solved using nested for loops (the outer loop for the row, and loops
inside for the column. Each row will has one more blank at the beginning than the
previous row. You can use a for loop to print the blanks, then a for loop to print the stars.
(8)
Write a program to read a number (min 2 and max 8) from the user (in a loop of course),
and print a multiplication table up to that number. Your program must verify that the
number is in the range 2 to 8 and tell the user to reenter a correct number if he/she enters
a number outside the range.
As usual, the best approach is to use incremental development. Here are three stages you
might want to go thru. To make the multiplication table look nice, the I/O manipulator
setw() can be used to set the minimum width of the converted form of the number.
#include <iomanip>
...
cout << setw(4) << n;
This will right-align the value of n in a field of four spaces.
In addition you could add column and row headers as well as separating lines to make it
look like the following table. Several additional loops are required to print the column
header and horizontal separating lines. Don't try to make all of these additions at once.
For example, first add the row header and vertical bars. Get that running then print the
separating lines. Lastly add the column header. The order of these additions isn't
important in this case, but to work on small increments in the program makes
development to more easily.
1
2
3
4
1
2
3
4
5
+-----+-----+-----+-----+-----+
|
1 |
2 |
3 |
4 |
5 |
+-----+-----+-----+-----+-----+
|
2 |
4 |
6 |
8 | 10 |
+-----+-----+-----+-----+-----+
|
3 |
6 |
9 | 12 | 15 |
+-----+-----+-----+-----+-----+
|
4 |
8 | 12 | 16 | 20 |
+-----+-----+-----+-----+-----+