CSE100Lab09ManskiJoah - Lab Section Name Lab 9 Arrays...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
Lab Section: _____ Name: __________________________________ Lab 9 Arrays Pre-Lab: Please read the pre-lab and answer the accompanying questions before your lab session. In this lab session, you will learn: What an array is and when to use them How to declare an array How to initialize an array How to pass an array and how to pass an element of an array Arrays are a special kind of data type. Unlike the variables we've been using so far (e.g., double, char, int) that can only hold one value at a time, arrays can hold multiple values. For the most part, arrays look and act like their "single-valued" cousins, just with some syntactic differences. Before, we'd declare a variable of type int like this: int arbitraryValue; Now, if we wanted an array of int values (say, 10 of them), this is how we'd declare the array: int arbitraryValues[10]; Aside from the difference in the name of the array, the only syntactic difference is that " [10] " bit. The brackets tell C++ that we want an array (instead of just a normal variable), and the value inside (10, in this case) tells C++ that we want to be able to hold ten things inside the array. For now, don't worry about how C++ allocates and manages the array, you can just assume that there are magical array gnomes (cousins of the ever-popular lawn gnomes) that set up special boxes in memory for your values. Speaking of values, if you want to initialize your array to have certain values from the get-go, the syntax is a little different from what you're used to for normal variables. Recall that with normal variables, this is how you'd declare and initialize them: int arbitraryValue = 42; Well, arrays are similar, but you need to specify all the values at once in a comma-delimited list encased in curly braces. Since that sentence wasn't written in English, here's an example that explains it better: int arbitraryValues[10] = {12, 18, 27, 96, 104, 15, -3, 0, 2, 88}; That line puts all those values in sequence into the "boxes" of the array. Note that this is the only time that you can use syntax like that to set values for an array. After an array is declared, you have to assign values to it using the "standard" fashion. For instance, say you wanted to make the 7th value in arbitraryValues equal to 7 (because it's lucky). Whereas you'd do it like this for a normal variable: arbitraryValue = 7; CSE 100 Lab 9 1 / 7
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
For an array you'd set it like this: arbitraryValues[6] = 7; Note that I used the number 6 in between the brackets to talk about the 7th number in the array. This is not a typo. In arrays, the first value in the array is referenced by the index 0, so whatever number is in between the brackets is actually talking about the entry one greater than that. The index of an array is a number describing the position of a particular element in the array. C++ arrays are zero-based, so the first index is always 0, and the last index is always one less than the number of elements in the array. For example, to access the last (the 10th) value in our array above (say, to output the value), this is how we'd
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

Page1 / 7

CSE100Lab09ManskiJoah - Lab Section Name Lab 9 Arrays...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online