Wednesday, April 28, 2010

Lab and homework #18

1) Write a function that takes an array and will *return* the maximum. (You have code for minimum.)
2) Write a function that takes an array and will *print out* the maximum.
3) Write a function that takes an array and will *print out* all the elements of the array.
4) Write a function that takes an array and will return the sum of all elements of the array.
5) Write a function that takes an array and will return the product of all elements of the array.

Monday, April 26, 2010

Tutoring schedule link

http://www.cs.qc.edu/tutors.html

Lab and homework #17

1) Fill a 2-D array with the number 6
2) Print a 2-D array, with each element separated by a comma and rows separated by newlines.
3) Find the sum of all the elements in a 2-D array.
4) Given a 2D array A, and a 2D array B, add each corresponding element and store it in array C.

Wednesday, April 21, 2010

Lab and homework #16

1) A C-string, as opposed to a C++ string, is implemented
as a NULL-terminated array of characters. Thus, "hello" is 6 characters long, 'h', 'e', 'l', 'l', 'o', '\0'.
char s[6] = "hello";
Write a function stringlen which takes in a C-string and returns the string length of 5.
Write a function stringcopy which takes in two C-strings, source and destination, and makes destination = to source.
Write a function stringreverse which will reverse a C-string.

2) Write a function intToString which will take in a C-string and an integer, and write the digits of that number to the C-string.

3) Modify the above so that it takes in a 3rd parameter, which is the base. Passing in a 2 will make the string contain the number in binary. Passing in a 3 will make the string contain the number in ternary. Passing in a 10 will make the string contain the number in decimal.

Monday, April 19, 2010

int sum(int I, int K)
{
int retval = 0;
for(int i = I; i <=K; i++)
retval = retval + i;
return retval;
}

int sum2(int I, int K)
{
if (I == K)
return I;
else
return I + sum2(I+1, K);
}

int max1(int I, int K)
{
int revI = reverse(I);
int revK = reverse(K);
retval = max(revI, revK);
return reverse(retval);
}

int max(int revI, int revK)
{
if (revI % 10 > revK % 10)
return revI;
else if (revI % 10 < revK % 10)
return revK;
else // they are equal
return revI % 10 + 10 * max(revI/10, revK/10);
}

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