Arrays

Notes

Arrays are data structures that allow us to store data of the same type in contiguous memory locations.

That was a pretty dense statement! To better understand the concept of an array, think back to the last time you picked up mail in the Science Center basement or your house's mailroom.

An array is simply a block of contiguous space in memory (the mail center) that has been partitioned into identically-sized chunks (mailboxes). Each chunk can store a certain amount of data (mail) that can be accessed by an index number (mailbox number).

Note that array indices in C always start at 0!

Declare an array by specifying the data type it will store, its name, as well as its size.

Here, we declare an array of 3 ints called temperature and load it with values.

Alternatively, you can declare and initialize the array in a single step (in which case stating the size is optional).

Because arrays store data contiguously in memory, array size is fixed after array declaration. You are effectively asking the OS to reserve the appropriate number of contiguous chunks of memory for the array's elements. There's no guarantee that more memory adjacent to your array will be available for use later, so arrays cannot easily grow.

Because each element is associated with an array index, we have random access to all of the elements in an array.

In other words, we can access any element in a single step by indexing into the array. This is a big deal because algorithms like binary search depend on random access.

This code prints out all elements of the temperature array.

Here's an example of the usage of an array to keep track of student scores.

This code prompts the user to enter a score for each student. Scores are stored in scores_array.

What's the index number of the last element in an array of size n?

Arrays can be multidimensional.

You can think of multidimensional arrays as "arrays of arrays."

Here, we declare and initialize a 2-dimensional array tic-tac-toe board. Note that dimensions must be declared explicitly for multidimensional arrays!

This code uses nested for loops to print out all elements of the tic-tac-toe board array.

Slides ( / )

study50 slide
study50 slide
study50 slide
study50 slide
study50 slide
study50 slide
study50 slide

Pokemon

Prerequisites:

Write a program that prompts the user to input the names of five Pokemon. Store those Pokemon in an array, and randomly select one to print out.
HINT: Don’t reinvent the wheel — a function already exists that will return a random number!

jharvard@run.cs50.net (~): ./a.out
Give me a Pokemon: Butterfree
Give me a Pokemon: Clefairy
Give me a Pokemon: Diglett
Give me a Pokemon: Growlithe
Give me a Pokemon: Rapidash
Clefairy, I choose you!

Try out some pseudocode here!
#include <cs50.h>
#include <stdlib.h>
#include <stdio.h>
#include <time.h>

#define MAX 5

int main(int argc, string argv[])
{
    // TODO
}

Mean

Prerequisites:

Write a program prompts the user to input 5 integers. Store the integers in an array, and print out the mean of their values.

jharvard@run.cs50.net (~): ./a.out
Give me an integer: 4
Give me an integer: 1
Give me an integer: 5
Give me an integer: 10
Give me an integer: 5
Average: 5

Try out some pseudocode here!
#include <cs50.h>
#include <stdio.h>

#define N 5

int main(int argc, string argv[])
{
    // TODO
}

Videos

study50 video thumbnail

Wednesday, Week 2

An introduction to arrays.
study50 video thumbnail

Rob's Arrays Short

Rob gives a general overview.