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

Gagan Deep: Arrays

This document provides information about arrays in C programming. It defines an array as a linear list of homogeneous elements stored in consecutive memory locations. It notes that arrays in C start at index 0 and end at size-1. It describes one-dimensional and multi-dimensional arrays. It provides examples of one-dimensional array declarations and definitions. It also gives examples of C programs that demonstrate input/output of arrays, finding the largest element in an array, linear search, insertion in an array, and bubble sort. Finally, it briefly describes multi-dimensional arrays and provides an example of adding two matrices.

Uploaded by

Amit sinha
Copyright
© © All Rights Reserved
Available Formats
Download as PPSX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
66 views

Gagan Deep: Arrays

This document provides information about arrays in C programming. It defines an array as a linear list of homogeneous elements stored in consecutive memory locations. It notes that arrays in C start at index 0 and end at size-1. It describes one-dimensional and multi-dimensional arrays. It provides examples of one-dimensional array declarations and definitions. It also gives examples of C programs that demonstrate input/output of arrays, finding the largest element in an array, linear search, insertion in an array, and bubble sort. Finally, it briefly describes multi-dimensional arrays and provides an example of adding two matrices.

Uploaded by

Amit sinha
Copyright
© © All Rights Reserved
Available Formats
Download as PPSX, PDF, TXT or read online on Scribd
You are on page 1/ 14

Arrays

Gagan Deep
Rozy Computech Services
3rd Gate, K.U., Kurukshetra-136119

M- 9416011599
Email [email protected]

Arrays
Array is a linear list of homogenous elements,

stored at successive memory locations in


consecutive order.
In C, array always starts from 0 and end with size-1
and be written as
rollnumb[0], rollnumb[1].rollnumb[49]
and be declared as
int rollnumb[50];
means declaration syntax is
datatype arrayname [size];
Types of array
One-Dimensional
Multi-Dimensional

One Dimensional Arrays


Array having only one subscript variable is called

one-dimensional array and also called as single


dimensional array or linear array.
Example of one dimensional array definition
int marks[5] = {50, 85, 60, 55, 90};
And represented as
marks[0] =50
marks[1] = 85
marks[2] = 60
marks[3] = 55
marks[4] = 90
A specific element in an array is accessed by an
index.

Accessing One Dimensional Arrays


Program 1 : Input and output of an
Array

#include <stdio.h>
void main()
{ int marks[5],i;
printf(Input of Array marks);
for (i=0; i<5; i++)
scanf(%d, &marks[i]);
printf(Output of Array marks);
for (i=0; i<5; i++)
printf(marks[%d] = %d , i,
marks[i]);

Input of Array marks


(if you entered )
50
85
60
55
90
(you will get)
Output of Array marks
marks[0] =50
marks[1] = 85
marks[2] = 60
marks[3] = 55
marks[4] = 90

Program 2 : Find the largest in an array.


#include<stdio.h>
void main() { int a[20], i, n, largest;
printf("\nEnter no of elements :");
scanf("%d", &n);
for (i = 0; i < n; i++) // Input Array
scanf("%d", &a[i]);
largest = a[0]; //Consider first element as largest
for (i = 0; i < n; i++) //Process
if (a[i] > largest)
largest = a[i];
printf("\nLargest Element : %d",
largest);}//output largest

Program 3 : Linear Search


#include<stdio.h>
void main() { int a[20], i, n, item, loc=-1;
printf("\nEnter no of elements :"); scanf("%d",
&n);
for (i = 0; i < n; i++)
// Input Array
scanf("%d", &a[i]);
printf("\nEnter the item to be search:"); scanf("%d",
&item);
for (i = 0; i < n; i++)
//Process
if (a[i] == item)
{ loc= i;
break; }
if (loc>=0)
//output largest
printf("\nItem Found: %d", loc+1);

Program 4 : Insertion of Item in an Array


#include<stdio.h>
void main() { int a[20], i, n, j, k, item, loc;
printf("Enter the size of an array: "); scanf("%d",&n);
for(i=0;i<n;i++) scanf("%d",&a[i]); //Input
Array
printf("Enter the item to be insert: ");
scanf("%d",&item);
printf(At what location: "); scanf("%d",&loc);
j=n-1; k=loc-1;
while(j>=k){ //process
a[j+1]=a[j];
j=j-1;
}
a[k]=item; n=n+1;
for(i=0;i<n;i++)

Program 5 : Bubble Sort


#include<stdio.h>
void main() { int a[20], i, n, p,c;
printf("Enter the size of an array: "); scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&a[i]); //Input Array
for (p=0; p<n-1;p++) //Bubble Sort Process Ascending
for(c=0; c< n-1-p; c++)
if a[c] > a[c+1]
{t=a[c]; a[c]=a[c+1]; a[c+1]=t;}//swapping
for(i=0;i<n;i++)
printf("%d",a[i]); //Output Array

Do Yourself
Understand Algorithms and do the programming of
Following
1. Deletion from an Array
2. Merging
3. Insertion Sort
4. Selection Sort
5. Merge Sort
6. Radix Sort
7. Second Largest Element in the List
8. Delete the item from the list
9. Prime Numbers using Sieves Algorithm

MultiDimensional Array
arrays are defined in much
the same manner as one dimensional arrays,
except that a separate square brackets
required in each subscript.
Thus, a two dimensional array will require
two pairs of square bracket,
aij -> a[i][j] -> a[rows][coulmn]
a three dimensional array will require three
pairs of square brackets, and so on
aijk -> a[i][j][k] -> a[page][rows]
[coulmn]
Multidimensional

MultiDimensional Array
In general terms, a multidimensional array

definition can be written as


Storage class data-type array[exp 1][exp 2]
[exp n]
Where storage-class refers to the storage
class of the array,
Where data-type is the data type
array is the array name
exp 1, exp 2 and exp n are positive valued
integer expression that indicate the number
of array elements associated with each

2 Dimensional Array
2 dimensional array can be expressed as

col1 col2
col3
Row 1 X[0][0] X[0][1]
Row 2 X[1][0] X[1][1]
Row 3

Row n

X[n-1][0]

X[n-1][1]

coln
X[0][n1]
X[1][n-1]

X[n-1][n1]

Addition of two matrices Programs 6


#include <stdio.h>
void main()
{ float a[3][3], b[3][3], c[3][3];
int i,j;
printf("Enter the elements of
1st matrix\n");
for(i=0;i<3;++i)
for(j=0;j<3;++j)
scanf("%f",&a[i][j]);

for(i=0;i<3;++i)
for(j=0;j<3;++j)
c[i][j]=a[i][j]+b[i][j];
printf("\nSum Of Matrix:");

for(i=0;i<3;++i)
{
for(j=0;j<3;++j)
printf("Enter the elements of
printf("%.1f\t",c[i][j]);
2nd matrix\n");
for(i=0;i<3;++i)
printf("\n"); }
for(j=0;j<3;++j)
}
scanf("%f",&b[i][j]);

THANKS!
If you have any queries you
can contact me at :
[email protected]

You might also like