ARRAYS
IN C PROGRAM
<p> COMSCI 1101 LECTURE </p>
WHAT IS AN ARRAY?
▪ An array is a group of memory locations related by
the fact that they have the same identifier and
data type.
▪ Just like variables, arrays occupy space in memory.
▪ C language provides the capability for the
programmer to define a set of ordered data items.
An array is just the same to a list.
Array Size = 8
Elements
0 1 2 3 4 5 6 7
Indexes
Array Size = 8
Elements
5 3 2 6 8 10 62 15
0 1 2 3 4 5 6 7
Indexes Index: 3
Element: 6
WHAT IS AN ARRAY?
▪ Array must be declared before they are used.
▪ Giving a name to an array takes also the rules of
naming convention in identifiers.
▪ Declaration of an array involves declaring the data
type of values that will be stored (int, char,
float, double) and indicating also the maximum
number of elements (dimension) that will be stored.
DECLARING ARRAYS
▪ Syntax:
data_type array_name[size];
▪ Example:
int grade[10]; //Array of 10 integers
char name[50]; //Array of 50 characters
float volume[5]; //Array of 5 floating values
DECLARING ARRAYS
▪ Each element of the array can be referenced by means
of a number called the index or subscript.
▪ The index must be an integer or an integer
expression.
▪ In C, the first element is indexed with 0 (zero).
ACCESS ARRAY ELEMENTS
▪ To access or reference a single element of an array,
we state the array name followed by a number in
square brackets.
int num[4];
num[0] = 100;
num[1] = 101;
num[2] = 102;
num[3] = 103;
ACCESS ARRAY ELEMENTS
▪ If an array uses an expression as a subscript, the
value of the expression is evaluated first to
determine the subscript.
int num[4];
int x = 3;
num[4 – 3] = 101;
num[x * 3 – 6] = 103;
ACCESS ARRAY ELEMENTS
▪ An individual array element can be used just like
variable.
▪ Example:
num[0] = 100;
num[1] = num[0];
printf(“%d”,num[1]);
ACCESS ARRAY ELEMENTS
▪ Array elements can also be used in calculations.
They are also operands.
▪ Example:
sum = num[1] + num[2];
total = values[9] + sum;
num[3]++;
--num[2];
#include <stdio.h>
#include <conio.h>
void main(){
clrscr();
int num[3], x = 2, y = 1, sum;
num[0] = x;
num[1] = y;
sum = (num[0] + y) * (x + num[1]);
num[2] = sum;
printf(“Value of index 2 is %d”,num[2]);
getch();
}
Value of index 2 is 9
_}
INITIALIZING ARRAYS
▪ Like variables, arrays can also be initialized.
▪ This is done by listing the initial values
(initializers) of the array.
▪ The values in the list are separated by commas, and
the entire list is enclosed by curly braces.
INITIALIZING ARRAYS
▪ Example:
int numbers[6] = {2, 4, 6, 8, 10, 12};
char word[6] = {‘H’, ‘E’, ‘L’, ‘L’, ‘O’, ‘\0’};
float data[5] = {3.14, 4.15, 1.55555};
double average[3] = {95.00, 81.25, 75.60};
INITIALIZING ARRAYS
▪ If there is not enough values, elements are set to 0
int num[5] = {1, 2};
//num[0]=1,num[1]=2,num[2]=0,num[3]=0,num[4]=0
int numbers[5] = {0};
//num[0]=0,num[1]=0,num[2]=0,num[3]=0,num[4]=0
INITIALIZING ARRAYS
▪ C allows to define an array without specifying the
number of elements (no dimension).
▪ When this is done the size of the array is
determined automatically, based on the number of
initialized elements.
char word[] = {‘W’,‘H’,‘A’,‘T’,‘\0’}; //size = 4
int numbers[] = {1, 3, 4, 5, 6, 2}; //size = 6
#include <stdio.h>
#include <conio.h>
void main(){
clrscr();
char word[] = {‘W’, ‘H’, ‘A’, ‘T’, ‘\0’};
printf(“Index 2 = %c”,word[2]);
getch();
}
Index 2 = A
_}
ARRAY OUT OF BOUNDS
▪ C has no array bounds checking to prevent the
computer from referring to an element that does not
exist.
▪ So, an executing program can walk off the end of an
array without any warning.
▪ It could be working without any issues but it is
suppose to be illegal use or not good practice in
using arrays.
#include <stdio.h>
#include <conio.h>
void main(){
clrscr();
int numbers[5];
printf(“Enter a number: ”);
scanf(“%d”,&numbers[100]);
printf(“Value of index 100 is %d\n”,numbers[100]);
getch();
}
Enter a number: 199
Value of index 100 is 199
_}
ARRAYS & LOOPS
▪ Storing individual values in each array element
requires accessing individual array elements.
▪ What if we have a total array size of 1000? Trying
to store values in each array element without using
an iterative statement is not an ideal choice.
ARRAYS & LOOPS
▪ Example:
int num[10]
num[0] = 1;
num[1] = 2;
num[2] = 3;
num[3] = 4;
…
ARRAYS & LOOPS
▪ Example:
int num[10];
for(int x = 0; x < 10; x++){
num[x] = x + 1;
}
ARRAYS & LOOPS
▪ Print the values of the elements of num[10]
for(int y = 0; y < 10; y++)
printf(“%d”,num[y]);
▪ Fill the elements of num[10] with user input
for(int z = 0; z < 10; z++)
scanf(“%d”,&num[z]);
#include <stdio.h>
#include <conio.h>
void main(){
clrscr();
int num[10], sum = 0;
for(int x=0; x<=9; x++){
printf(“Enter number [%d]: ”,(x+1));
scanf(“%d”,&num[x]);
}
for(int y=0; y<=9; y++){
sum += num[y];
}
printf(“The sum of the 10 integers is %d\n”,sum);
getch();
}
Enter number [1]: 5
Enter number [2]: 9
Enter number [3]: 3
Enter number [4]: 2
Enter number [5]: 7
Enter number [6]: 7
Enter number [7]: 2
Enter number [8]: 5
Enter number [9]: 6
Enter number [10]: 3
The sum of the 10 integers is 49
_}
PASS ARRAYS TO FUNC
▪ Specify the name of the array without any brackets
▪ Size of array is usually passed to a function
void myFunction(int x[], int size){…}
void main(){
int num[5];
myFunction(num, 5);
}
#include <stdio.h>
#include <conio.h>
void printArray(int numbers[], int size){
for(int x = 0; x < size; x++){
printf(“%d ”,numbers[x]);
}
}
void main(){
clrscr();
int num[] = { 1, 2, 3, 4};
printArray(num, 4);
getch();
}
1 2 3 4 _
#include <stdio.h>
#include <conio.h>
int oddOrEven(int number){
if(number % 2 == 0)
return 1;
else
return 0;
}
void main(){
clrscr();
int num[10], even = 0, odd = 0;
for(int x=0; x<=9; x++){
printf(“Enter number [%d]: ”,(x+1));
scanf(“%d”,&num[x]);
}
for(int y=0; y<=9; y++){
if(oddOrEven(num[y]))
even++;
else
odd++;
}
printf(“There are %d even and %d odd numbers”,even, odd);
getch();
}
Enter number [1]: 5
Enter number [2]: 9
Enter number [3]: 3
Enter number [4]: 2
Enter number [5]: 7
Enter number [6]: 7
Enter number [7]: 2
Enter number [8]: 5
Enter number [9]: 6
Enter number [10]: 3
There are 3 even and 7 odd numbers_}
THANK
YOU!