CSE100Lab12ManskiJosh

CSE100Lab12ManskiJosh - Lab Section Name Lab 12 2-D Arrays...

Info icon This 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
Image of page 1

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

View Full Document Right 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.
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 ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern