![]() ![]() |
||||||||
| CMPS 012A/L - Week 9 Lab | |
|
|
|
Writing a Program in CFor the week of Mar 6 - 10, 2006Now that we've done some work with C and C arrays, you should be able to use your experiences in Java and your knowledge of how to Java and C differ to make a C program. If you have a hard time with making this program in C, start with making it in Java, then use the basic framework introduced in part 2 of lab 7 to convert your program to C. Additional InformationPlease refer to C for Java Programmers: A Primer for general help with C. If you haven't already done so, please do the first part of lab 7 and the first part of lab 8 for basic introductions to C and C arrays.ProcedureWrite a C program that repeatedly reads pairs of arrays of integers from standard input (the console/keyboard) and prints 0 if the first array of the pair is a prefix of the second and prints -1 otherwise. Each array will be preceeded by its length. The length need not be on a separate line but it might be. Of course, if the first array is longer than the second array then it cannot possibly be a prefix and thus the output should be -1. The program ends when it reads in an array length of 0 for the first array of a pair. Your program must include a function that is called to read in an array. The prototype or signature of the function should be:
You can assume that the arrays will never be longer than 100 elements. Here is a sample execution. The lines in bold are the computer output, the other lines are the user input. Notice the program does not generate a prompt, though you may add a prompt and additional output if you wish.
OptionalTry to define and use a function that takes two integer arrays as parameters and their lengths, and returns the index of where the first array appears within the second array, if at all. If the first array does not appear anywhere in the second then return -1, otherwise return the index of where the first array starts in the second. Notice that this will give the same result as the original program if the array is a prefix or doesn't appear at all. Also, this is an integer equivalent of the Java method indexOf() for Strings. If you complete this method your program should print both the result of the original prefix test (0 or -1) followed by a space and then the result of calling the indexOf() method. Here is what your output should be on the same test case as above.
|
|
| SOE Webmail · SOE SSH · SOE Wiki · Search
· Sitemap · Contact us · Driving
directions
· Privacy · UCSC © Baskin School of Engineering, University of California, Santa Cruz 1156 High St., Santa Cruz, CA 95064 · (831) 459-2158 · webmaster@soe.ucsc.edu |