Due Thursday June 7 at 6 p.m.
Your code will be graded on both its correctness and its elegence. So please take the time to try to come up with short and easy to understand solutions. Please read the programming guides on the Assignments page.
Please consult the directory ~hsc/classes/270/examples on CDF. Some of the examples may prove useful in solving these problems.
Example: (1, 4, 2) * (3, 5, -1) = 1*3 + 4*5 + 2*(-1) = 3 + 20 - 2 = 21
Here, each vector is stored as an array of integers.
I have started the program for you: dotproduct.c
Please place your code into a file called dotproduct.c.
Example: word1 = "ABCABCD", word2 = "BBADBGD"
The matching characters are the 2nd, 5th and 7th so the result will be "BBD".
Use pointers and not array indices to write intersection();
I have started the program for you: intersect.c
Sample execution:
eddie% intersect ABCABCD BBADBGD
Intersection string is BBD
eddie% intersect HowIHateUnix HowILoveWindows
Intersection string is HowIe
Note: The words can be separated by spaces or by tabs ('\t').
Hint:If you place your input into a file and pipe the file to your executable (remember the first lecture), then the EOF character will be sent at the end of the file. If you type the input in yourself, you can send and EOF character by typing control-D.
Call the program flipwords.c
Sample execution:
eddie% flipwords
Mary had a little lamb lamb little a had Mary Whose fleece was white as snow. snow. as white was fleece WhoseHint: Try to break the problem up into pieces and solve each piece separately. It may be easier than trying to solve the whole problem. Each piece will then become a separate function in your program.
As for problem 3, if any of the lines in the file exceed 80 characters, you can ignore all characters past the 80th.
Call the program countchars.c
Sample execution:
eddie% countchars
"Look out!", shouted Steve as the rock just missed my head. "Wow, that was close," I said. : 15 times, 16.48 percent s: 8 times, 8.79 percent o: 7 times, 7.69 percent e: 7 times, 7.69 percent t: 7 times, 7.69 percent a: 5 times, 5.49 percent d: 4 times, 4.40 percent ": 4 times, 4.40 percent h: 4 times, 4.40 percent ,: 3 times, 3.30 percent u: 3 times, 3.30 percent k: 2 times, 2.20 percent m: 2 times, 2.20 percent .: 2 times, 2.20 percent : 2 times, 2.20 percent c: 2 times, 2.20 percent i: 2 times, 2.20 percent w: 2 times, 2.20 percent l: 1 times, 1.10 percent L: 1 times, 1.10 percent S: 1 times, 1.10 percent r: 1 times, 1.10 percent W: 1 times, 1.10 percent !: 1 times, 1.10 percent j: 1 times, 1.10 percent v: 1 times, 1.10 percent I: 1 times, 1.10 percent y: 1 times, 1.10 percent
Note that special characters like "return" and "tab" will mess up the output. That is ok, you do not have to handle these characters specially.
Hint: See the hint for Problem 3. This problem can also be solved by breaking it up into pieces. Also, think about what data structures you will need to solve this problem.
You can use any sorting method you wish, but here is a simple sorting algorithm which works well when sorting a small number of items.
for i = 1 to end for j = i+1 to end if ith element is less than the jth element swap the ith and jth elementsThis method is called "bubble sort" because the largest elements will slowly move to the top.