|
CS256 - Principles of Structured Design
Fall 2021
|
Displaying ./code/cs256su21code/jun14/arrays.c
#include <stdio.h>
int main()
{
int ar[5]; //declaring an integer array that can store 5 elements
int ar2[] = {9, 64, 225, 87, 150};
ar[0] = 32; //first element of this array is index 0
ar[1] = 65;
ar[2] = 87;
ar[3] = 101;
ar[4] = 43; //last element of this array is index 4
printf("%d\n", ar[4]);
/*
arrays are structured as follows
return_type arrayName[arraySize];
return_type - the type of data we're storing, like int, char, double, etc
arrayName - the name we're giving the array
arraySize - inside the brackets, we tell the array how many elements we're going to store.
inside the array
other key things to remember:
index - the location within the array
the first index you can store things in would be at index 0
the last index you can put things in would be at index SIZE - 1
where SIZE is the number of elements the array can store
element - whatever value is inside the array at the given index
*/
//print the array, first element is at index 0, last index is at index 4
printf(" Printing array ar\n");
for(int i = 0; i < 5; i++){ // since the first element is at index 0
printf("%d\n", ar[i]); //
}
return 0;
}
|