UCSCBaskin School of Engineering  
General Information Events, News & Organizations Degrees & Departments Research Classes Admissions & Advising People & Jobs Administration
CMPS 012A/L - Week 8 Lab

Arrays in C

For the week of Feb 27 - Mar 3, 2006

Arrays 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 Information

For information on arrays, please check out Chapter 3 of C for Java Programmers: A Primer.

Procedure

There 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

  • Download (Save As...) arrayParams.c onto a Unix or Mac computer. Windows users should log on to one of the Unix servers, and do this lab there. Unless you have additionalnsoftware on your computer, you will be unable to compile the C file.
  • Modify the program so that it uses a separate function to print out the values of the array instead of doing it inline inside of main(). After you finish you should have only four lines in main(), ignoring the comments:

    int smallArray[10];
    fillIt(smallArray);
    printIt(smallArray);
    return 0;

Part 2

If 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 fillIt() from arrayParams.c but with the additional size parameter. You can assume that the vectors 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.

os-prompt>vectorAdd
5
5 4 1 3 3
2 10 0 -2 100
7 14 1 1 103
10
100 101 102 103 104 200 210 220 230 500
2 2 2 2 2 -1 -1 -1 100 -5
102 103 104 105 106 199 209 219 330 495
0
os-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.


General info · News · Events · Degree Programs · Research · Classes · Admissions · Advising · People · Jobs · Administration
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