Arrays
Arrays
0 1 2 3 4 5
Creating an Array
<data type> name[<size>];
Example:
int temperature[3]; 0 1 2
temperature[0] = 65;
temperature[1] = 87;
temperature[2] = 30; 65 87 30
OR
0 1 2
65 87 30
for (int i = 0; i < 3; i++)
{
printf("%d\n",
temperature[i]);
}
#include <stdio.h>
#include <cs50.h>
#define CLASS_SIZE 30
int main(void)
{
// declare array
int scores_array[CLASS_SIZE];
// populate array
for (int i = 0; i < CLASS_SIZE; i++)
{
printf("Enter score for student %d: ", i)
scores_array[i] = GetInt();
}
}
Where's the bug?
char board[3][3];
board[1][1] = 'o';
x x
1,0 1,1 1,2
board[0][0] = 'x';
o
board[2][0] = 'o';
board[0][2] = 'x';
o
Accessing Multidimensional
Array Elements