You are on page 1of 3

Lesson 1

A R R AYS
Array Concept
Imagine we have a problem that requires 20 integers to be processed. We need to read them, process them,
and print them. We must also keep these 20 integers in memory for the duration of the program. We can declare
and define 20 variables, each with a different name.
But having 20 different names creates another problem. How can we read 20 integers from the keyboard
and store them? To read 20 integers from the keyboard, we need twenty references, each to one variable.
Furthermore, once we have them in memory, how can we print them? To print them, we need another twenty
references.
Although this may be acceptable for 20 integers, it is definitely not acceptable for 200, 2,000 or 20,000
integers. To process large amounts of data we need a powerful data structure, such as array.
Definition
An array is a fixed-size, sequence collection of elements of the same data type.
Since an array is a sequenced collection, we can refer to the elements in the array as the first element, the
second element, and so forth until we get to the last element.
Declaration and Definition
An array must be declared and defined before it can be used. Declaration and definition tell the compiler
the name of the array, the type of each element, and the size or number of elements in the array.
Syntax:
data_type array_name[number of elements];
Examples:
int score[20];
float ave[20];
Accessing Elements in Array
We use a subscript to access individual elements in an array. The first subscript is 0. For example, if we
declare an array with 20 elements it has a subscript from 0 to 19.
Example:
int score[20];
score[0]= first element
score[1]= second element
.
.
score[19]= last element

Storing Values in Arrays


Declaration and definition only reserve space for the elements in the array. No values will be stored. If we
want to store values in the array, we must initialize the elements, read values from the keyboard, or assign values to
each individual element.

Initialization
Initialization of all elements in an array can be done at the time of declaration and definition, just as with variables.
For each element in the array we provide a value. The only difference is that the values must be enclosed in braces.
Examples:
int numberseries[5] = {2,4,6,8,10};
char name[10]={R, A, C,H,E,L,\ 0}

Note: C language doesnt check for array boundary

Inputting values
Another way to fill the array is to read the values from the keyboard or a file. This can be done using for loop.
Example:
int scores[10];
for(i=0;i<10;i++)
scanf(%d, &scores[i]);
Assigning values
Individual elements can be assigned values using the assignment operator.
Example:
int scores[10];
scores[5] = 95;

95 is the value of the sixth element in the array

Sample Program #1:


// Sorting an array of integers in ascending order
#include<stdio.h>
#include<conio.h>
int number[20];
int x, y, howmany, temp;
main(){
printf ("Enter how many will be sorted: " );
scanf ("%d", &howmany);
for(x=0; x<howmany; x++)
{ printf("Enter number %d: ", x+1);
scanf ("%d", &number[x]);
}
// Sorting
for(x=0; x<howmany; x++){
for(y=(x+1) ; y<howmany ; y++){
if (number[x]>number[y]){
temp= number[x];
number[x] = number[y];
number[y] = temp; }}}
printf ("\nOutput: \n");
for(x=0; x<howmany; x++)
printf("%d ", number[x]);
getch();
}

Sample Program #2:


/* Write a program that would store 10 integer values in an array. Store the sum of the two succeeding
pairs in another array. Hence, only 9 integer values will be stored in the second array.
Sample Run:
Enter 10 integers: 1 2 3 4 5 6 7 8 9 10
Output:
3 5 7 9 11 13 15 17 19
*/
#include<stdio.h>
#include<conio.h>
int arr1[10], arr2[9];
int z;
main() {
printf("Enter 10 integers: ");
for (z=0; z<10; z++)
scanf("%d", &arr1[z]);
for (z=0; z<9; z++)
arr2[z] = arr1[z] + arr1[z+1];
printf("Output: ");
for (z=0; z<9; z++)
printf("%d ", arr2[z]);
getch();
}
Sample Program #3: Write a program that will input numbers and would call a function to sort the
numbers in descending order.
#include<stdio.h>
#include<conio.h>
int number[20];
int x, y, howmany, temp;
void sort(int number[ ]);
main(){
printf ("Enter how many will be sorted: " );
scanf ("%d", &howmany);
for(x=0; x<howmany; x++)
{ printf("Enter number %d: ", x+1);
scanf ("%d", &number[x]);
}
sort(number); //pass the array
printf ("\nOutput: \n");
for(x=0; x<howmany; x++)
printf("%d ", number[x]);
getch();
}

void sort(int number[]) // accept the array


{
for(x=0; x<howmany; x++){
for(y=(x+1) ; y<howmany ; y++){
if (number[x]<number[y]){
temp= number[x];
number[x] = number[y];
number[y] = temp; }}}
}

Note:
In passing an array to a function you just use the
array name without any subscript.
e.g.
sort(number);
At the function declaration and definition, no need
to specify the number of element for the
declaration of array.
e.g.

void sort(int number[]);