§ 01 — Computer Programming 1
An array is a fixed-size, sequenced collection of elements all of the same data type, stored in contiguous memory locations and accessed through a single name plus an index.
Key characteristics:
Element address formula:
element address = array address + (sizeof(element) × index)
The formula, worked on a real array:
data_type array_name[size];
int scores[20]; // 20-element integer array
char name[30]; // 30-element character array
float averages[10]; // 10-element float array
int first_array[5] = {5, 3, 2, 7, 9};
int second_array[] = {11, 21, 75, 24, 5}; // size inferred
int third_array[15] = {3, 7, 4, 6, 1}; // remaining 10 → 0
A string in C is a character array terminated by '\0' (null character). The null terminator is added automatically from string literals.
char college[6] = "CCMIT";
// college[0]='C', college[1]='C', ..., college[5]='\0'
scores[0] // first element
scores[9] // tenth element (for a 10-element array)
scores[i] // element at variable index i
Reading into an array:
for (i = 0; i < 10; i++)
scanf("%d", &scores[i]);
Printing:
for (i = 0; i < 10; i++)
printf("%d\n", scores[i]);
Copying (element by element — arrays cannot be assigned directly):
for (i = 0; i < 25; i++)
second[i] = first[i];
#include <stdio.h>
#include <conio.h>
void main() {
clrscr();
int num[3] = {5, 3, 7};
int h, i, temp;
for (h = 0; h < 3; h++)
for (i = 0; i < h; i++)
if (num[i] > num[i + 1]) {
temp = num[i];
num[i] = num[i + 1];
num[i + 1] = temp;
}
for (i = 0; i < 3; i++)
printf("%d\n", num[i]);
getch();
}
Output: 3, 5, 7
void main() {
int a[50], n, count_neg = 0, count_pos = 0, i;
printf("Enter the size of the array: ");
scanf("%d", &n);
for (i = 0; i < n; i++) scanf("%d", &a[i]);
for (i = 0; i < n; i++) {
if (a[i] < 0) count_neg++;
else count_pos++;
}
printf("Negative numbers: %d\n", count_neg);
printf("Positive numbers: %d\n", count_pos);
}
n runs from a[0] to a[n-1]. Using a[n] goes past the end.int b[] = {11, 21, 75}; lets the compiler count the size for you.char array that ends in '\0', so char college[6] holds the 5 letters of "CCMIT" plus the terminator. Read, print, and copy arrays with a loop: one array cannot be assigned to another with =.Add more scores to the array (update the size too) and watch the average and max update.
#include <stdio.h>
int main(void) {
int scores[] = {85, 90, 78, 92, 88};
int size = 5;
int sum = 0;
printf("Scores: ");
for (int i = 0; i < size; i++) {
printf("%d ", scores[i]);
sum += scores[i];
}
printf("\nSum: %d\n", sum);
printf("Average: %.2f\n", (float)sum / size);
int max = scores[0];
for (int i = 1; i < size; i++)
if (scores[i] > max) max = scores[i];
printf("Highest: %d\n", max);
return 0;
}Quick check
2 questions from the notes above. Answering beats rereading.
Pro3 reviewers locked
Part of this subject's paid reviewers. Not included in this printout.
Done with this module? Track it — your progress shows on the subject list.
Studying with blockmates? Send them this lesson.
Up next
Lesson 6: Functions→←Previous: Lesson 4: Program Control Structures