Some slides from Syracuse University
grade1
, grade2
, grade3
, ...
int
variables.
homework scores ┌───────┬───────┬───────┬───────┐ │ hw #1 │ hw #2 │ hw #3 │ hw #4 │ └───────┴───────┴───────┴───────┘
int grades[5];
#define MAX_NUM_STUDENTS 5
int grades[MAX_NUM_STUDENTS];
#define
allows us to define constants—variables that do not change.
#define
#define
keyword tells the compiler that the following “variable”
declaration/initialization will remain constant throughout the program.
#define PI 3.1415926 printf("%f\n", PI);
3.141593
#define PI 3.1415926 PI = 3.0;
c.c: In function 'main': c.c:2: error: lvalue required as left operand of assignment
#define
ints can be used as size parameters in the declaration of arrays.
This is useful for creating code that is easily modified.
a[n]
.
0
.
The last element in the array is at index
N-1
, where N
is the number of elements in the array.
#define MAX_NUM_STUDENTS 5 int grades[MAX_NUM_STUDENTS]; grades[0] = 98; grades[1] = 87; grades[2] = 92; grades[3] = 79; grades[4] = 85; ┌───────────┬───────────┬───────────┬───────────┬───────────┐ │ grades[0] │ grades[1] │ grades[2] │ grades[3] │ grades[4] │ └───────────┴───────────┴───────────┴───────────┴───────────┘
Three ways to do the same thing:
#define MAX_NUM_STUDENTS 5 int grades[MAX_NUM_STUDENTS] = {98, 87, 92, 79, 85}; int grades[] = {98, 87, 92, 79, 85}; int grades[5]; grades[0] = 98; grades[1] = 87; grades[2] = 92; grades[3] = 79; grades[4] = 85; ┌───────────┬───────────┬───────────┬───────────┬───────────┐ │ 98 │ 87 │ 92 │ 79 │ 85 │ └───────────┴───────────┴───────────┴───────────┴───────────┘ grades[0] grades[1] grades[2] grades[3] grades[4]
Uninitialized array elements contain unknown values, like all variables.
#define MAX_NUM_STUDENTS 5 int grades[MAX_NUM_STUDENTS]; grades[3] = 10+6+42; ┌───────────┬───────────┬───────────┬───────────┬───────────┐ │ ?? │ ?? │ ?? │ 58 │ ?? │ └───────────┴───────────┴───────────┴───────────┴───────────┘ grades[0] grades[1] grades[2] grades[3] grades[4]
#define MAX_NUM_STUDENTS 5 int grades[MAX_NUM_STUDENTS]; for (int i = 0; i < MAX_NUM_STUDENTS; i++) grades[i] = i*12; for (int i = 0; i < MAX_NUM_STUDENTS; i++) printf("%d ", grades[i]);
0 12 24 36 48
┌───────────┬───────────┬───────────┬───────────┬───────────┐ │ 0 │ 12 │ 24 │ 36 │ 48 │ └───────────┴───────────┴───────────┴───────────┴───────────┘ grades[0] grades[1] grades[2] grades[3] grades[4]
for
loops are often used to do things with the elements of an array.
// sum the elements of an array #define ARRAY_SIZE 5 int my_list[ARRAY_SIZE] = {1,2,3,4,5}; int sum = 0; for (int counter=0; counter < ARRAY_SIZE; ++counter) sum += my_list[counter]; printf("sum=%d\n", sum);
sum=15
#define SIZE 3 int main() { double a[SIZE]; printf("Enter numbers: "); for (int i = 0; i < SIZE; i++) scanf("%lf", &a[i]); return 0; } Enter numbers: 1.2 3.4 5.6
#define SIZE 3 int main() { double a[SIZE] = {1.2, 3.4, 5.6}; /* Display array elements */ for (int i = 0; i < SIZE; i++) printf("a[%d] = %.2lf\n", i, a[i]); return 0; } a[0] = 1.20 a[1] = 3.40 a[2] = 5.60
#define SIZE 3 int main() { double a[SIZE] = { 1.2, 3.4, 5.6 }; double max = 0.0; /* Find max value in array */ for (int i = 0; i < SIZE; i++) if (a[i] > max) max = a[i]; printf("max = %.2lf\n", max); return 0; } max = 5.60
#define SIZE 3 int main() { double a[SIZE]; double max = 0.0; printf("Enter the %d array elements\n", SIZE); for(int i = 0; i < SIZE; i++) /* Read the array elements */ scanf("%lf", &a[i]); for(int i = 0; i < SIZE; i++) /* Display array elements */ printf("a[%d] = %.2lf\n", i, a[i]); for(int i = 0; i < SIZE; i++) /* Find max value in array */ if (a[i] > max) max = a[i]; printf("max = %.2lf\n", max); return 0; }
int scores[] = {1,2,3}; for (int i=0; i<20; i++) printf("%d ", scores[i]);
1 2 3 3 4195824 0 -269273115 32597 -267849208 32597 1000319960 32767 0 1 4195734 0 0 0 -2134907816 -1169010567
Sometimes, you get away with breaking the rules:
int a[10]; a[12] = 456; printf("%d\n", a[12]);
456
Sometimes, you don't:
int a[10]; a[1000006] = 789; printf("%d\n", a[1]);
c.c: In function 'main': c.c:3: warning: 'a[1]' is used uninitialized in this function SIGSEGV: Segmentation fault
type function(type array name[])
#define MAXELEMENTS 5 double average(double a[], int numberOfElements) ; int main() { double numbers[MAXELEMENTS] = {1.2, 3.4, 5.6, 7.8, 9.0}; printf("%lf\n", average(numbers, MAXELEMENTS)); return 0; } double average(double a[], int numberOfElements) { double sum = 0.0; for (int i=0; i<numberOfElements; i++) sum += a[i]; return sum / numberOfElements; }
5.400000
int sum(int list[], int size) { int sum=0; for (int i=0; i<size; i++) sum += list[i]; return sum; } int main() { int a[] = {11, 22, 33, 44, 55}; printf("total: %d\n", sum(a, 5)); return 0; }
total: 165
A recursive function to add an array:
int sum(int list[], int size) { if (size == 1) return list[0]; return list[size-1] + sum(list, size-1); } int main() { int a[] = {11, 22, 33, 44, 55}; printf("total: %d\n", sum(a, 5)); return 0; }
total: 165
void change_list(int list[], int size) { list[0] = 10; } int main() { int foo[5] = {1,2,3,4,5}; printf("before: %d\n", foo[0]); change_list(foo, 5); printf("after: %d\n", foo[0]); return 0; }
before: 1 after: 10
change_list
function actually is a change in the my_list
array
in the main
function (based on previous slide example).
void foo(int oneval) { oneval = 99; } int main(){ int hula[] = {1,2,3,4,5}; printf("before: %d\n", hula[0]); foo(hula[0]); printf("after: %d\n", hula[0]); return 0; }
before: 1 after: 1
void print(int list[], int size) { for (int i=0; i<size; i++) printf("%d ", list[i]); printf("\n"); } void swapFirstLast(int list[], int size) { // What goes here? } int main() { #define LIST_SIZE 5 int my_list[LIST_SIZE] = {1,2,3,4,5}; print(my_list, LIST_SIZE); swapFirstLast(my_list, LIST_SIZE); print(my_list, LIST_SIZE); return 0; }
1 2 3 4
the list will change to 4 2 3 1
swapFirstLast
.