0% found this document useful (0 votes)
5 views

02_Arrays

Uploaded by

dorotheagrey25
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

02_Arrays

Uploaded by

dorotheagrey25
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 40

INTRODUCTION

TO
ARRAYS
Arrays
A collection of same type elements under the
same variable identifier(variable name)
referenced by index number.

Arrays are widely used within programming for


different purposes such as sorting, searching and
etc.
Arrays
a data structure which hold multiple variables (element/s) of the
same data type.

IN SHORT, ARRAY IS
–Structures of related data items
–Static entity – same size throughout program
Types of Arrays
1.SingleDimensional
2.Multidimensional
Syntax of Array Declaration (Single
Dimensional)
Data_type array_name[size];

Where:
data_type – valid data type
array_name – any user-defined identifier
size – number of elements in the array
Example: int num[5];
To refer to an array element:
Element/s
–Item/s of the array
–The variables of the array
Specify
–Array name
–Position number/index number/subscript
Index Number
–The ‘ID’ of the array element
–Also called Position number or Subscript
–Starts from 0 to size-1
–Ex: if size = 5, index numbers are 0 – 4
Refer to an element in the Ar
arrayname[index number]
–First element at position 0 (must always be put in
mind)

Example: The elements in array c with the size n


are:

c[ 0 ], c[ 1 ]...c[ n – 1 ]
Example
Consider an integer array num with size 5:
int num[5];
It will have five integer elements from num[0] to num[4].
elements value(int)
First Element → num[0] Subscript/Index number
num[1] – the position number
num[2] contained within square
brackets.
num[3] - must be an integer or
an integer expression.
Last Element → num[4]
Array Manipulation
You can use loops to manipulate each element of the array.
Note: When you declare an array, each elements are NOT initialized to
zero, but is instead given a trash value by your compiler.
int num[5];

}
num[0] 342332

num[1] 8372 These are values randomly


assigned by your compiler upon
num[2] 421
array declaration.
num[3] 14454

num[4] 123
Initialization of Arrays
We can initialize the elements in the array in the same way
as the ordinary variables when they are declared.

The general form of initialization of arrays

data_type array_name[size]={list of values};

Note: The values in the list care separated by commas, for example
the statement
Initialization of Arrays

int number[3]={0,0,0};

This will declare the array size as an array of size 3 and will
assign zero to each element.
Initialization of Arrays
What you should remember:
1. If the number of values in the list is less than the number of
elements, then only that many elements are initialized. The
remaining elements will be set to zero automatically.
Ex: int num[5] = {1};

}
num[0] 1
num[1] 0 num[0] is initialized to 1, while
the rest are initialized to 0.
num[2] 0
num[3] 0
num[4] 0
Initialization of Arrays
Therefore: int num[3] = {0, 0, 0}; can also be written as:
int num[3] = {0}; //this initialize all elements to 0.

num[0] 0
num[1] 0
num[2] 0
Initialization of Array Elements
More examples:
int num[5] = {1,2}; int num[5] = {1, 1, 2, 2};

num[0] 1 num[0] 1
num[1] 2 num[1] 1
num[2] 0 num[2] 2
num[3] 0 num[3] 2
num[4] 0 num[4] 0
Initialization of Arrays
What you should remember:
2. If too many elements are initializers:
Example:
int num[3] = {0, 0, 0, 0};

This will produce a syntax error.


Initialization of Arrays
What you should remember:
3. In the declaration of an array the size may be omitted, in such cases
the compiler allocates enough space for all initialized elements.
Example:
int counter[]={1,1,1,1};

Will declare the array to contain four elements (size = 4) with initial
values 1. this approach works fine as long as
Initialization of Array Elements: Using loops
int n [ 10 ] , i ; //declare an array n with size 10.
for (i = 0; i < 10; i ++) //index i starts with 0 and ends with 9.
{
n [ i ] = 0 ; //n[0] = 0; n[1] = 0; n[2]=0; and so on…
}

The initialization above is the same as:


int n [ 10 ] = { 0 } ; //I honestly prefer this type of initialization.
Arrays Elements
The same as normal variables.

c[ 0 ] = 3;
printf( "%d", c[ 0 ] );

You can also perform operations in subscript.


If x = 3:
c[ 5 - 2 ] == c[ 3 ] == c[ x ]
Declaring multiple arrays of the same data
type:
Syntax:

int arr1[ 100 ], arr2[ 27 ];

Snippet above declared two integer arrays arr1 of size 100 and
arr2 of size 27.
Example

Make a C program to create an array of 5


numbers. Output the numbers.
Sol’n 1 (Constant Values for the elements)
#include <stdio.h>
main()
{
int x, num[5]; //declare the array
num[0]=1; //input value for num[0]
num[1]=2; //input value for num[1]
num[2]=3; //input value for num[2]
num[3]=4; //input value for num[3]
num[4]=5; //input value for num[4]
for(x=0; x<5; x++)
{
printf("The contents of num[%d] is --> %d\n",x, num[x]);
}
}
Example 1 – Sol’n 1 Sample Output
Sol’n 2 (Ask the user for the values of the
elements)
#include <stdio.h>
main()
{
int x, num[5] = {0}; //declare the array
for(x=0; x<5; x++) //input values to each element of the array
{
printf("Enter a number%d:",x);
scanf("%d", &num[x]);
}
for(x=0; x<5; x++) //print the elements of the array
{
printf("The content of num[%d] is %d\n",x, num[x]);
}
}
Example 1 – Sol’n 2 Sample Output
Other Application of Array

The following are the most common


applications of array
Searching Arrays
Searching – the process of finding a particular element
of an array.
Two Types:
Linear search – compares each element of the array
with the search key.
Binary search – Can only be used in a sorted array.
Locates the middle element and compares it to the
search key. If they are equal, the search key is found
and the array subscript is returned. Otherwise, it
eliminates one half of the elements in the array.
Linear Search
Examples:
1.Create an array of N integers. Determine if 13 is
within the array.
2.Create an array of N integers. Input an integer to be
searched in the array. Output whether the integer to
be searched exist or not within the array.
3.Create an array of N scores. Determine the average
and output all the scores that are greater than or the
average scores.
Problem 1 Solution
Problem 1 Sample Outputs
Problem 2 Solution
Problem 2 Sample Outputs
Problem 3 Solution
Problem 3 Sample Output
Notes:

This will cause an error because n does not have a value yet. Meaning it
can be any random value set by the compiler. Including hexadecimal
trash values.
Sorting Arrays
Sorting data – placing the data into a particular order such as ascending
or descending.

Example:
Write a program that sorts the values in the elements of the 5-
element array num into ascending order.
int num[5] = {4, 3, 1, 5, 2};
Compare element at index [0] with the rest of the elements in the array:
int num[5] = {4, 3, 1, 5, 2};
Compare element at index [1] with the rest of the elements in the array except index [0]:
int num[5] = {4, 3, 1, 5, 2};
Compare element at index [2] with the rest of the elements in the array except index [0] & [1]:
int num[5] = {4, 3, 1, 5, 2};
Compare element at index [3] with last element at index [4]:

Array num is now sorted in ASCENDING ORDER.

You might also like