Lab 8 Array 1 D
Lab 8 Array 1 D
______________________
Course Instructor / Lab Engineer
Page 1
Lab Manual for Programming in C Lab by. Abdullah Emam
Programming with C - Lab
WEEK - 8
1 Arrays
2 One Dimensional Array: Declaration and Initialization
3 Accessing Array Elements
4 Sample C Programs
Page 2
Lab Manual for Programming in C Lab by. Abdullah Emam
------------------
What is an Array?
An Array is a collection of related data items which will share a common name. It is a
finite collection of values of similar data type stored in adjacent memory locations that is
accessed using one common name. Each value of the array is called as an element.
Elements are accessed using index, which is a number representing the position of the
element.
By finite we mean that, there are specific number of elements in an array and by similar
we mean that, all elements in the array are of same type.
Declaring an array
An array is declared just like an ordinary variable but with the addition of number of
elements that the array should contain.
E.g. The following declaration declares n array of 20 elements where each element is of
int type.
int marks[20];
Now, marks is the name of the array of 20 integers. Marks occupies 40 bytes, if int
occupies 2 bytes (20*2).
Page 3
Lab Manual for Programming in C Lab by. Abdullah Emam
Programming with C - Lab
Array can be of any variable type. The ability to use a single name to represent a
collection to refer to an item by specifying the item number enables us to develop
efficient programs.
TYPES:
Arrays in C Programming are of 3 types:
(1) One [or] Single Dimensional Array
(2) Two [or] Double Dimensional Array
(3) Multi Dimensional Array
ONE-DIMENSIONAL ARRAY
A list of items can be given one variable name using only one subscript and such a
variable is called a single subscripted variable or one dimensional array.
For Example: If we want to represent a set of 5 numbers 35, 40, 20, 57, 19 by an array
variable 'number' then we may declare the variable number as follows:
int number[5]; and computer reverses 5 storage locations are shown below:
number[0]
number[1]
number[2]
number[3]
number[4]
The values of an array element can be assigned as follows:
number[0] = 35;
number[1] = 40;
number[2] = 20;
number[3] = 57;
number[4] = 19;
This could cause the array name to store the values as follows:
35 number[0]
40 number[1]
20 number[2]
57 number[3]
19 number[4]
The elements can be used in programs just like as any other 'C' variable.
It is not possible to either add new elements or delete existing elements after the array is
created. In other words the size of the array is static and it cannot be changed at runtime.
Page 4
Lab Manual for Programming in C Lab by. Abdullah Emam
Programming with C - Lab
Sample C Programs
Example: Program to insert elements into an array and print the array elements.
#include<stdio.h>
main()
{
Page 5
Lab Manual for Programming in C Lab by. Abdullah Emam
Programming with C - Lab
int a[5],i;
printf("Enter 5 elements into an array:\n");
for(i=0;i<=4;i++) {
scanf("%d",&a[i]);
}
printf("The 5 elements are:\n");
for(i=0;i<=4;i++)
printf("%d\n",a[i]);
}
Output:
Enter 5 elements into an array:
35
23
45
67
11
The 5 elements are:
35
23
45
67
11
Example: Take an array of 10 integers and accept values into it. Sort the array in
descending order.
#include<stdio.h>
main()
{
int ar[10];
int i, j, temp;
for (i = 0 ; i < 10 ; i++)
{
printf("Enter number for [%d] element: ",i);
scanf("%d", &ar[i]);
}
/* sort array in descending order */
for ( i = 0 ; i < 9 ; i++)
{
for ( j = i+1; j < 10 ; j ++)
{
if ( ar[i] > ar[j])
Page 6
Lab Manual for Programming in C Lab by. Abdullah Emam
Programming with C - Lab
{
/* interchange */
temp = ar[i];
ar[i] = ar[j];
ar[j] = temp;
}
} /* end of j loop */
} /* end of i loop */
/* display sorted array */
printf("\nSorted Numbers \n");
for ( i = 0 ; i < 10 ; i++)
{
printf("%d\n", ar[i]);
}
}
Output:
Enter number for [0] element: 35
Enter number for [1] element: 67
Enter number for [2] element: 98
Enter number for [3] element: 12
Enter number for [4] element: 6
Enter number for [5] element: 58
Enter number for [6] element: 49
Enter number for [7] element: 23
Enter number for [8] element: 71
Enter number for [9] element: 85
Sorted Numbers
6
12
23
35
49
58
67
71
85
98
Page 7
Lab Manual for Programming in C Lab by. Abdullah Emam
Programming with C - Lab
#includ
e<stdio
.h> int
main(){
int a[10],i,n,m,c=0;
Page 8
Lab Manual for Programming in C Lab by. Abdullah Emam
Programming with C - Lab
or
there are no more elements left to search for in the array and
a "not found" indicator is returned.
#include <stdio.h>
int main() {
int arr[100], n, target;
int low, high, mid;
int found = 0; // Flag to track if element is found
if (arr[mid] == target) {
printf("Element found at index %d (position %d).\n", mid,
mid + 1);
found = 1;
break;
} else if (arr[mid] < target) {
low = mid + 1;
} else {
high = mid - 1;
}
}
if (!found) {
printf("Element not found in the array.\n");
}
return 0;
}
Page 9
Lab Manual for Programming in C Lab by. Abdullah Emam
Programming with C - Lab
Page 10
Lab Manual for Programming in C Lab by. Abdullah Emam