Here are some good questions taken from past EE150 finals that you can use as practice for the upcoming final. The real EE150 will be just like this sample final in terms of length, so you should be able to complete all of these questions in under two hours.
Below is a C function named replace. It doesn't work like it supposed to.
1. int replace(int x[], int old, int new, int stop)
2. {
3. int i;
4.
5. for (i = 0; x[i] > stop; i++)
6. if (x[i] != old)
7. x[i] = new;
8. return i;
9. }
What it is supposed to do is this: run through the first stop elements of array x, replacing each element that contains the value old with the value new. It is then supposed to return the number of elements it modified. So, for example, suppose tablewas array of ints that was initialized this way:
int table[] =
{31, 17, 26, 9, 6, 17, 19, 17, 21, 17, 96, 108, 61, 19, 17};
Then the code:
n = replace(table, 17, 40, 9);
printf("%i\n", n);
should have written a 3 and changed table to look like:
That is, it should have run through the first 9 elements of table, changing each 17 to a 40. But it doesn't!31, 40, 26, 9, 6, 40, 19, 17, 21, 17, 96, 108, 61, 19, 17
So what does it do? Trace through the code using the "boxes" method shown in class.
What does this code actually write for the value of n?
What does table look like what the function is all done?
What do we have to do to fix replace? Either show us a fixed version of the function or tell us exactly what lines we must change and what the changes are.
Write a program tha tprints its input onto its output, but with a twist. Its input consists of a series of digit/character pairs, such as 2x or 4y. Each time your program reads a pair, it writes the character the digit numbre of times. So 2x turns into xx on the output. If it encounters a character without a digit preceding it, it writes the character once. Here's some sample I/O:
3x4yt5zHINT: Read the input a character at a time and do different things depending on whether or not the character is a number.
xxxyyyytzzzzz
2a7u
aauuuuuuu
char s[30] = { ' ', ' ', 'a', 'l', 'e', 'x', ' ', ' ', ' ', ' ',
'w', 'a', 's', ' ', ' ', 'h', 'e', 'r', 'e', ' ',
' ', '\0' };
These lines:
t = removeBlanks(s);
printf("%i, %s\n", t, s);
would produce this output:
HINT: Think about how you would copy one array into another. Then think about how you would avoid copying certain characters.10, alexwashere
struct entry
{
char *word;
char *meaning;
};
struct entry dictionary[MAXWORDS] =
{ {"phat", "excellent"},
{"chill", "relax"},
{"wack", "lame"},
{"props", "praise"},
{"fly", "attractive"},
{"dis", "disrespect"} }
Also assume that there's a function called isSameString that
takes two strings as parameters and returns a 1 if they are the same
and a 0 if they are different. (That is, feel free to use this function
if you are overcome with the desire to compare strings.)
Write a function, findWord that is passed the dictionary, the number of entries it contains, and a string containing a word in "rap". The function should return the subscript of the entry containing that word or -1 if it can't find it. Here's a template for findWord:
int findWord(struct entry table[], int n, char *rapword)
{
}
Write a program to read input words (in "rap"), one per line, and print their formal English equivalent on the next line. If it can't find a word, it should print "unknown word!". Here's some sample I/O.
chillYou MUST use the findWord function from the previous part. You MAY use any function that exists in the text that you find useful for solving this problem.
relax
phat
excellent
killer
unknown word!
fly
attractive
it's output would beA B D D F C B B C D D A
A B C D E F
* * * * *
* * * *
* *
*
Do not panic. You will do this problem as a series of functions
and then put them together in a short main program at the end.
Write a function, makeCounts, that takes an array of counters and the number of counters, and updates the appropriate counter for each letter. It should ignore other characters. If counters is an array of 6 elements, and we have the input above, the call makeCounts(counters) will wind up with the array having the value 2 3 2 4 0 1 (there are 2 A's, 3 B's, 2 C's, 4 D's, 0 E's and 1 F in the same intput).
HINT: Read the input one character at a time. Ignore any character that isn't a grade.
void makeCounts(int counters[], int n)
{
}
Write a function, blankOut that takes a two-dimensional array of characters containing r rows and c columns and sets every one of its elements to a blank. HINT: A two-dimensional array of characters is exactly like a two-dimensional array of integers, except that its elements are characters rather than integers.
void blockOut(char table[MAXROWS][MAXCOLS], int r, int c)
{
}
Write a function, placeStars, that taeks a two-dimensional array of characters, an array of counters, and the number of counters. It puts stars in the array in the appropriate place. The rows of the two-dimensional array correspond to rows of the output (the first row of the array corresponds to the first row of the histogram's output). So for the input above, if the two-dimensional array is named table:
table[0][0] and table[1][0] gets stars because there are 2 A's.
table[0][1], table[1][1], and table[2][1] gets stars because there are 3 B's.HINT: Draw a picture of the two-dimensional array you want to create for the input above.
void placeStars(char table[MAXROWS][MAXCOLS], int counters[], int n)
{
}
Write a function to print the two-dimensional array in the form of a vertical histogram. Its passed the two-dimensional array of blanks and stars, along with the number of rows and columns in that array.
void printHisto(char table[MAXROWS][MAXCOLS], int r, int c)
{
}
#define MAXROWS 20 /* # of rows in two-dimensional array */
#define MAXCOLS 6 /* # of columns in two-dimensional array */
main()
{
char output_table[MAXROWS][MAXCOLS];
int grade_counters[MAXCOLS];
}
[EE150 Home Page | EE150 Exam Information Page | Top Of Page]