0% found this document useful (0 votes)
4 views10 pages

Lab 8 Array 1 D

The document is a lab manual for a Programming with C course at Fayoum International Technological University, focusing on arrays in C programming. It covers the definition, declaration, initialization, and accessing of one-dimensional arrays, along with sample programs for inserting, sorting, and searching elements in arrays. The manual is prepared by Abdullah Emam and is intended for first-year students in the Information Technology program for the academic year 2024/2025.

Uploaded by

hossamarpro
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)
4 views10 pages

Lab 8 Array 1 D

The document is a lab manual for a Programming with C course at Fayoum International Technological University, focusing on arrays in C programming. It covers the definition, declaration, initialization, and accessing of one-dimensional arrays, along with sample programs for inserting, sorting, and searching elements in arrays. The manual is prepared by Abdullah Emam and is intended for first-year students in the Information Technology program for the academic year 2024/2025.

Uploaded by

hossamarpro
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/ 10

Programming with C - Lab

Fayoum International Technological University

Student Name: ……………………………………………………………….…………


Student Group: …………………………………………………………………………

Lab Instructor : Abdullah Emam

Lab Performed In: School Lab

Programming with C - Lab


Semester - II

Fayoum International Technological University


Faculty of Technology
Information Technology Program
First Year – Essentials in C programming
Academic Year (2024/2025)

______________________
Course Instructor / Lab Engineer

Prepared by Abdullah Emam

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.

A particular value is indicated by writing a number called 'index or subscript' number in


brackets for after the array name.
Syntax:
type variablename[size];
The type specifies data type of element that will be contained in an array such as
int, float, char, and double.
The size indicates the maximum number of elements that can be stored inside the
array.

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

How to access elements?


An array contains multiple elements. But the total array is accessed using one name. So to
access array elements you have to use index or subscript and array name. The index is
nothing but the number of the element. It identifies the position of the element in the array.
In C array index starts with 0 and ends at number of elements - 1.
E.g. marks[5] = 30; /*will store value 30 into 6th element*/
Subscripts: Subscripts of an array is an integer expression, integer constant or integer
variables like 'i', 'n' etc. that refers to different elements in the array. Subscripts have a
range, starting from '0' upto the "size of array-1".
Subscripts can also be reflected in for loops that print the array elements.
Consider a part of program of for loop
int number[8], n;
for(n=0;n<10;n++)
{
printf("\nEnter Number: ");
scanf("%d",&number[n]);
}
In the above program, 'n' is a subscript and n starts from 0 in the loop. The value at
number[0] will be typed and stored in the first element of the array. This will continue until
'n' reaches the value of 10.
Initializing an array
It is possible to store values into an array during time of declaration.
int a[5] = {10, 20, 30, 40, 50};
Values given in { } are placed in corresponding element of an array. If number of values
given is more than the size, C displays an error "Too many Initializers in Function...". If number
of values given is less than the size, then the number of elements then only the
corresponding elements are filled.
In certain case, you can ignore the size of the array if you are initializing the array. C
determines the size of the array from the number of values given in the initialization.
int a[] = {20,30,40}; /*is an array of 3 elements*/

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

Example: Program to search an element in an array using Linear search (Sequential


Search)
Linear search or sequential search is a method for finding a particular value in a list that
checks each element in sequence until the desired element is found or the list is
exhausted.

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;

printf("Enter the size of an


array: "); scanf("%d",&n);
printf("Enter the elements of the
array: "); for(i=0;i<=n-1;i++){
scanf("%d",&a[i]);
}
printf("Enter the number to be
search: "); scanf("%d",&m);
for(i=0;i<=n-
1;i++){
if(a[i]==m){
c=1;
break;
}
}
if(c==0)
printf("The number is not in the list");
else
printf("The number is found");
return 0;
}
Sample output:
Enter the size of an array: 5
Enter the elements of the array: 4
6 8 0 3 Enter the number to be
search: 0
The number is found

Example: Program to search an element in an array using Binary search


The binary search begins by comparing the target value to value of the
middle element of the sorted array.
⚫ If the target value is equal to the middle element's value, the position is
returned.
⚫ If the target value is smaller, the search continues on the lower half
of the array, or if the target value is larger, the search continues on
the upper half of the array.
⚫ This process continues until the element is found and its position is returned,

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

// Input the number of elements


printf("Enter number of elements in the sorted array: ");
scanf("%d", &n);

// Input elements in sorted order


printf("Enter %d elements in sorted order:\n", n);
for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}

// Input the element to search


printf("Enter the element to search: ");
scanf("%d", &target);

// Initialize binary search bounds


low = 0;
high = n - 1;

// Binary search loop


while (low <= high) {
mid = (low + high) / 2;

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

You might also like