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.
Wednesday, April 28, 2010
Monday, April 26, 2010
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.
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.
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);
}
{
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.
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.
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.
Subscribe to:
Posts (Atom)