CSE100Lab12ManskiJosh

CSE100Lab12ManskiJosh - Lab Section Name Lab 12 2-D 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 12 2-D Arrays Pre-lab: Please read the pre-lab and answer the accompanying questions before your lab session. In this lab, you will learn: what 2-D arrays are how they differ from 1-D arrays how to declare, initialize, and use 2-D arrays how to pass 2-D arrays as parameters to functions why it's important to keep track of your booty Now that you've had a little fun with one-dimensional (1-D) arrays, it's time to up the ante a bit. Just as a review, say you were a pirate who wanted to keep track of how much treasure (in doubloons) you found per month (so you knew how much rum you could afford). As you (hopefully) remember, we could accomplish this with the help of a 1-D array, which we would declare like this: int treasureFound[12]; Then we could use a for loop like this to initialize the array: for(int i = 0; i < 12; i++) { treasureFound[i] = 0; } We could set how much treasure we found in November (the 11th month of the year) like this: // Some guy hid his money in 16 sacks, 16 coins apiece! What a nerd. treasureFound[10] = 256; And if we had a function like this that summed up all of our year's earnings: int totalTreasure(int monthlyBooty[]) { int sum = 0; for(int i = 0; i < 12; i++) sum += monthlyBooty[i]; return sum; } We could call it like this: int allOurBooty = totalTreasure(treasureFound); And then we'd know how much rum we could buy (so we'll never have to say, "Why's the rum gone?")! But, what if we had a neurotic accountant on our crew who insisted that we keep track of our daily hauls? We could use a bunch of 1-D arrays, like this: CSE 100 Lab 12 1 / 7
Background image of page 1

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

View Full DocumentRight Arrow Icon
int januaryTreasure[31]; int februaryTreasure[29]; // Filthy landlubber leap years! int marchTreasure[31]; ... int decemberTreasure[31]; But any self-respecting pirate would get bored typing all of that somewhere around June (just think: you'd probably need specialized code to handle each month!). Instead, you could use a 2-D array, and make everything a lot cleaner. Don't worry, 2-D arrays work almost exactly like 1-D arrays. Here's how we might declare the array: int dailyTreasureFound[12][31]; See? It's almost the same thing, except we added an extra set of brackets. That extra " [31] " means that for each entry in the first dimension of the array ( dailyTreasureFound[0] , dailyTreasureFound[1] , etc.), instead of just having one value (like we had before), now we have an array of 31. If it still isn't making sense, think of the dimensions as rows and columns in a table (1st dimension makes the rows, 2nd dimension makes the columns). Initializing our 2-D array isn't too different from initializing our 1-D array either: for(int i = 0; i < 12; i++) { for(int j = 0; j < 31; j++) { dailyTreasureFound[i][j] = 0; } } Now, instead of one for loop, we nest a second for loop inside. The second for loop handles the second dimension of the array. Let me stop for a second and talk about something that you might have noticed.
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.

This note was uploaded on 09/20/2010 for the course CSE 100 taught by Professor Na during the Spring '09 term at Arizona.

Page1 / 7

CSE100Lab12ManskiJosh - Lab Section Name Lab 12 2-D 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