Monday, April 19, 2010

Lab and homework #15

1) Using a for loop, fill an array with the number 6.
2) Using a for loop, fill an array with numbers pulled from the keyboard.
3) Using a for loop, print out the contents of an array.
4) An array contains midterm scores for a class. Find the average of the scores on the midterm, and print that out. Then, print out every midterm score which was above average.
5) One array has midterm scores and another has final scores. Make a third array for semester average, which will store the average of the midterm and the final for each student.
6) An array has midterm scores. Apply a curve to these midterm scores, so that each new midterm score is square root of the grade times 10.

Wednesday, April 14, 2010

Lab and homework #14

1) Write a function exp, which returns x to the power of n, using iteration.

2) Write a function exp2, which returns x to the power of n, using recursion.

3) http://en.wikipedia.org/wiki/Pascal's_triangle
n is the row, k is the column.
P(0, 0) is 1
P(1,0) is 1
P(1,1) is 1
P(2,0) is 1
P(2,1) is 2, because it is P(1,0)+P(1,1)
P(2,2) is 1

Write P, recursively

4) Write a function max, which takes in x and y and returns whichever is bigger, by comparing each *digit* in turn. Do this using iteration. Do this using recursion.

Monday, April 12, 2010

Lab and homework #13

1) Using recursion, find the product of the digits of an integer.

2) Using iteration (= a loop), find the product of the digits of an integer.

3) Using iteration and using recursion, write a function which will find the sum of the numbers I through K.

4) You have a recursive reverse function. Write a recursive forward function which will print all the digits in the forward order. (So, 123 would print 123, by printing a 1, then a 2, then a 3.)

Wednesday, April 7, 2010

Lab and homework #12

pass by reference:
1) write a function putInOrder which takes two integers by reference, and puts them in ascending order.

2) write a function putInOrder3 which takes three integers by reference, and puts them in ascending order.

3) Write a function GenerateFullName which takes in three parameters: FirstName, LastName, and FullName. It should generate full names by concatenation of First Name with Last Name. Which should be passed by value, and which by reference?

4) Write a function GenerateFullName2 which takes in two parameters, First Name and Last Name, and *returns* the Full Name.

5) I will demonstrate a trace through a recursive function. Nothing to hand in for this one.

6) Write a recursive function Fib, which will generate the N'th fibonacci number.
See here:
http://en.wikipedia.org/wiki/Fibonacci_number

Wednesday, March 24, 2010

Lab and homework #11

1) Using functions you write yourself,
draw an American flag on the screen. Don't worry about setting the colors, although I will update with instructions of how to change the colors.

2) Write a function which will take a number and print out all the factors of that number.

3) Write a function factorial, which will take a number n and *return* n factorial. Hint: use a for loop for this, with an accumulator.

Monday, March 22, 2010

quiz 3

1) What will be the output of the following code? Explain.
for(int i=0; i<10; i++);
   cout << "i";

2) What will be the output of the following code? Explain.
for(int i=0; i<10; i++);
   cout << i;

3) What will be the output of the following code? Explain.
for(int i=10; i>0; i--)
   cout << i;

4) Given the following function:
int foo(int bar) { return bar+bar*2; }
How would you call this function from main?
main() {
___________________________;
}

Submit form for homeworks, labs, quizzes