![]() ![]() |
||||||||
| CMPS 012A/L - Week 8 Lab | |
|
|
|
Arrays in CFor the week of Feb 27 - Mar 3, 2006Arrays in C are pretty similar to arrays in Java. The [] placement during the initialization of variables is a bit different, but arrays follow the same basic concept. You will find that many different programming languages all have similar data structures (like arrays and strings) as well as similar ways of controlling program flow (like if/else, loops, and functions/methods). Things may look different in other languages, but under the differences are the same concepts you are learning about in Java and C. Additional InformationFor information on arrays, please check out Chapter 3 of C for Java Programmers: A Primer.ProcedureThere are two parts of this lab. Try to get as much done as possible. The first part is pretty easy, so almost everyone should be able to start on part two.Part 1
Part 2If you have time, try and complete the following program.Write a C program that repeatedly reads pairs of vectors of integers from standard input (the console/keyboard) and prints the sum of the two vectors. Each vector pair will be preceeded by the length of the two vectors (the two vectors must be the same length - you do not need to check this). The program ends when it reads in a vector length of 0. Ideally the program would include a function that is called to read in a vector. The prototype or signature of the function should be: void readVector(int vec[], int size);
Notice this is almost like 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.
The first line is the size of the vector to add, and the next two lines are the vectors to add. The element at position i in vector a is added to the element at position i in vector b, and printed by the computer on the line below the input. I strongly suggest you first try and get this program working assuming that there will be only three lines of input: the size, the first vector, and the second vector. When that it working, it should be easy to add a while loop that does the same thing until a 0 size is read. |
|
| 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 |