Alright, here's another batch of practice problems. I added in some extra
"challenges" for those people who are interested. If you finish the other
problems early (or if you just feeling like doing the harder problems),
then try them.
Solutions will be posted at the bottom at 3pm on 2-16.
Problem 1: Fix my broken code
I wrote a code and linked it below. There are some
comments explaining what I wanted to do, but right now
it doesn't work as intended. I'd like you to fix the errors.
(there is more than one error)
Don't just try to eyeball it! Copy-paste it into to visual studio,
and write code to help you debug. Add in extra cout statements in
clever places to let you know what's going on. Change the numbers
around if that helps (run tests with small numbers so you can verify
the results by hand). Create as many small tests as you can to help
figure out which segments are working and which are not.
The correct final answer should be 171700 (so you can check your work).
Problem 2: Write some code
The first 7 Fibonacci numbers are
fib_1 = 1
fib_2 = 1
fib_3 = 2
fib_4 = 3
fib_5 = 5
fib_6 = 8
fib_7 = 13
In particular, if I already know fib_n and fib_(n+1), I get fib_(n+2) by adding
fib_(n+2) = fib_(n+1) + fib(n).
Find and print to the screen the 40th fibonacci number.
My idea is to write a function called incrementFib that takes in
two consecutive fibonacci numbers, and uses those to calculate the
next pair. (ex: 1, 2 -> 2, 3. Or 3, 5 -> 5,8).
I like calling my pair of numbers smallFib and bigFib. With those names,
your function should accomplish the following:
updated bigFib = old bigFib + old smallFib;
updated smallFib = old bigFib;
(of course, you will have to make this make sense in C++).
Once your function works (and you've tested it). Put it inside a loop and
run it many times to get the answer. The correct answer is 102334155.
Find and print to the screen the 50th Fibonacci number.
(What's different between the 50th and the 40th?)
Add up all the even Fibonacci numbers whose value does not exceed four million.
(so find 2 + 8 + 34 + .... + some_big_number_here)
Then submit your answer to Project Euler.
Problem 3: Write more code
Write a program that inputs a string (via getline), and finds
the position-number of the smallest character, as well as what charactter is
in that position. If there is more than one of the same smallest character,
you can pick any one.
(ex: if I input "aaaaaaabbbaa" you can pick any of the 'a's)
Side note: When I say 'smallest', I mean use < to compare two characters.
This makes sense in c++. (in computer memory, c++ stores characters as numbers.
Google unicode table if you're interested in learning more). If you
don't want to think about it, then make sure your strings just use lower
case letters (then it's obvious which ones are bigger and which are smaller).
Look through the characters of the string one at a time, and keep track of
the smallest out of the characters you've seen so far.
So at the start, you look only at the first character, and that one is
obviously the smallest. When you look at the second character, update
your smallest if necessary (if first is smaller than second, make no change).
Repeat this until you've looked at every character in the string.
Use the idea above to sort the string from smallest to biggest.
Find the smallest character in the string. Save it in a new location, and
delete it from the original string.
Now use your smallest character function again. You already deleted the
real smallest one, so it will now find the second smallest one. Add this
to your new location, and delete it from the original string.
Repeat till done.