Practice Problems 2-16

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. brokenCode.cpp 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.

Challenge #1)

Find and print to the screen the 50th Fibonacci number. (What's different between the 50th and the 40th?)

Challenge #2)

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).


Use the idea above to sort the string from smallest to biggest.