Unit 2 Array
Unit 2 Array
• The for loop is much the same, but now the body of the loop causes
each student’s marks to be added to a running total stored in a
variable called sum.
• When all the marks have been added up, the result is divided by 30,
the number of students, to get the average.
Processing an array
• Single operations which involves entire arrays are not permitted in c.
• If a and b are similar array (i.e., same data type, same dimensionality and
same size), assignment operations, comparisons operations etc. may be
carried out on an element-by-element basis.
• This process done by loop→ each pass in loop equal to process one array
element.
• Thus number of passes equals to number of array elements to be
processed.
Program to calculate the average of n numbers, then compute the
main( ) deviation of each number about average
{
int n, count;
float avg, d, sum=0;
float list[100];
printf("enter n value:");
scanf("%d", &n);
for(count=0; count<n; count++)
{
printf("i=%d", count++)'
scanf("%f", &list[count]);
sum+= list[count];
}
avg = sum/n;
printf("\nthe average is %.2f, avg");
for(count=0; count<n; count++)
{
d = list[count]-avg;
printf("i=%d x=%.2f d=%.2f", count ++, list[count],d);
}
}
Passing arrays to functions
• An entire array can be passed to a function as an
argument
• To pass an array to a function, the array name must
appear by itself.
• When declaring a one dimensional array as a formal
argument, the array name is written with a pair of empty
square brackets.
• When writing the function prototype that include array
arguments, an empty pair of square backets must follow
the name of each array arguments, thus indicating that
the argument is an array.
Example
float avg(int a, float x[]);
main()
{
float avg (int a, float x[])
int n;
{
float avg;
………
float list[100];
}
.....
avg = avg(n,list);
........
}
Searching
int arr[5] = {3,4,5,6,7};
1010 1014 1018 1022 1026
3 4 5 6 7
arr[0] arr[1] arr[2] arr[3] arr[4]
Search Key 5
Searching
int arr[5] = {3,4,5,6,7};
1010 1014 1018 1022 1026
3 4 5 6 7
arr[0] arr[1] arr[2] arr[3] arr[4]
Linear Search
int arr[5] = {3,4,5,6,7};
arr[0] = 5?
3 4 5 6 7
arr[0] arr[1] arr[2] arr[3] arr[4]
Search Key 5
Linear Search
int arr[5] = {3,4,5,6,7};
arr[1] = 5?
3 4 5 6 7
arr[0] arr[1] arr[2] arr[3] arr[4]
Search Key 5
Linear Search
int arr[5] = {3,4,5,6,7};
arr[2] = 5?
3 4 5 6 7
arr[0] arr[1] arr[2] arr[3] arr[4]
Search Key 5
Linear Search
int arr[5] = {3,4,5,6,7}; Variable i – subscript
i = i+1 i = i+1
3 4 5 6 7
arr[0] arr[1] arr[2] arr[3] arr[4]
Linear Search
int arr[5] = {3,4,5,6,7}; Search key = 8
i = i+1 i = i+1 i = i+1 i = i+1
3 4 5 6 7
arr[0] arr[1] arr[2] arr[3] arr[4]
#include<stdio.h> Linear Search
int main()
{ for(i=0;i<=9;i++)
int a[10],i,flag=0,searchKey; {
printf("Enter 10 integers: if(a[i]==searchKey)
\n"); printf("\n search key (%d) found at %d
for(i=0;i<=9;i++) position",searchKey,i+1); flag=1;
{ break;
scanf("%d", &a[i]); }
} if((i= =10)&&(flag= =0))
printf("The array elements are: \n"); printf("\n search key (%d) is not found",
for(i=0;i<=9;i++) searchKey);
{ }
printf("%d\t", a[i]);
}
printf("\n Enter the element to be
searched: ");
scanf("%d",&searchKey);
Finding Maximum
int arr[5] = {3,4,5,6,7};
1010 1014 1018 1022 1026
3 4 5 6 7
arr[0] arr[1] arr[2] arr[3] arr[4]
Maximum =
?
Finding Maximum
int arr[5] = {3,4,5,6,7};
1010 1014 1018 1022 1026
3 4 5 6 7
arr[0] arr[1] arr[2] arr[3] arr[4]
Finding Maximum
int arr[5] = {3,4,5,6,7}; Assume, maximum= first element in array
maximum=a[0]
arr[1] > maximum?
3 4 5 6 7
arr[0] arr[1] arr[2] arr[3] arr[4]
3 4 5 6 7
arr[0] arr[1] arr[2] arr[3] arr[4]
3 4 5 6 7
arr[0] arr[1] arr[2] arr[3] arr[4]
3 4 5 6 7
arr[0] arr[1] arr[2] arr[3] arr[4]
3 4 5 6 7
arr[1] arr[3] arr[4]
Finding Maximum
int arr[5] = {3,4,8,6,7};
1010 1014 1018 1022 1026
3 4 8 6 7
arr[0] arr[1] arr[2] arr[3] arr[4]
Finding Maximum
int arr[5] = {3,4,8,6,7}; Assume, maximum= first element in array
maximum=a[0]
arr[1] > maximum?
3 4 8 6 7
arr[0] arr[1] arr[2] arr[3] arr[4]
3 4 8 6 7
arr[0] arr[1] arr[2] arr[3] arr[4]
3 4 8 6 7
arr[0] arr[1] arr[2] arr[3] arr[4]
Then, maximum= 8
Finding Maximum
int arr[5] = {3,4,8,6,7}; maximum=8
3 4 8 6 7
arr[0] arr[1] arr[2] arr[3] arr[4]
Then, maximum= 8
Finding Maximum
int arr[5] = {3,4,8,6,7}; max = a[0]
Max=a[1] Max=a[2]
i = i+1 i = i+1 i = i+1
i=1 i=2 i=3 i=4
arr[1] > max? arr[2] > max? arr[3] > max? arr[4] > max?
3 4 8 6 7
arr[1] arr[2] arr[3] arr[4]
Ref: Programming with C, Byron S. Gottfried.
Finding Maximum
#include<stdio.h> max=a[0];
int main() for(i=0;i<=9;i++)
{ {
int a[10],i,max; if(a[i]>max)
printf("Enter 10 integers: \n"); {max=a[i];}
for(i=0;i<=9;i++) }
{ printf(“Maximum value=%d", max);
scanf("%d", &a[i]); }
}
Sorting
int a[5] = {99, 15, 76, 55, 34};
99 15 76 55 34
a[0] a[1] a[2] a[3] a[4]
Sorted array 15 34 55 76 99
a[0] a[1] a[2] a[3] a[4]
Bubble sort 99 > 55
Pass-1
99 > 15
15 76 99 55 34
a[0] a[1] a[2] a[3] a[4]
99 15 76 55 34
99 > 34
a[0] a[1] a[2] a[3] a[4]
99 > 76
15 76 55 99 34
a[0] a[1] a[2] a[3] a[4]
15 99 76 55 34
a[0] a[1] a[2] a[3] a[4]
15 76 55 34 99
a[0] a[1] a[2] a[3] a[4]
Bubble sort
Pass-2 76 > 34
15 < 76 15 55 76 34 99
a[0] a[1] a[2]
15 76 55 34 99 a[3] a[4]
76 > 55 15 55 34 76 99
a[0] a[1] a[2] a[3] a[4]
15 76 55 34 99
a[0] a[1] a[2] a[3] a[4]
15 55 34 76 99
a[0] a[1] a[2] a[3] a[4]
Bubble sort
Pass-3
15 < 55
15 55 34 76 99
a[0] a[1] a[2] a[3] a[4]
55 > 34
15 55 34 76 99
a[0] a[1] a[2] a[3] a[4]
15 34 55 76 99
a[0] a[1] a[2] a[3] a[4]
Bubble sort
Pass-3
i – variable for subscript
15 < 55
15 34 55 76 99
a[0] a[1] a[2] a[3] a[4]
Bubble sort program
for(i=0;i<n;i++)
{
for(j=0;j<n-1;j++)
#include<stdio.h> {
main() if(a[j]>a[j+1])
{ {
int a[]={99,15,76,55,34},i,j,temp; temp=a[j];
int n=5; a[j]=a[j+1];
printf("\n unsorted array \t"); a[j+1]=temp;
for(i=0;i<n;i++) }
}
{
}
printf("%d \t",a[i]); printf("\n Sorted array\t\t");
} for(i=0;i<n;i++)
{
printf("%d \t",a[i]);
}
}
Selection Sort
int a[5] = {99, 15, 76, 55, 34};
99 15 76 55 34
a[0] a[1] a[2] a[3] a[4]
Sorted array 15 34 55 76 99
a[0] a[1] a[2] a[3] a[4]
Removal of duplicates from an ordered
array
#include<stdio.h>
int main()
{
int a[20], i, j, k, n;
printf("Enter n value:");
scanf("%d", &n);
printf("Enter the elements:");
for (i=0; i<n;i++)
{
scanf("%d", &a[i]);
}
for (i=0; i<n;i++)
{
for (j=i+1; j<n;j++)
{
if(a[i]==a[j]) //check for duplicates
{
for (k=j; k<n-1;k++) //delete current position
{
a[k]=a[k+1];
}
n--; //decrease size of an array after removing duplicate element
j--;// if the position of element changes, don’t increase the element j
}
}
}
printf("After removal of duplicates:");
for (i=0; i<n;i++)
{
prinf("%d", a[i]);
}
return 0;
}
Partition an array
#include<stdio.h>
main()
{
int a[10], a1[10], a2[10], i, pos, k1=0, k2=0, n;
printf("Enter n value:");
scanf("%d", &n);
printf("Enter the elements:");
for (i=0; i<n;i++)
{
scanf("%d", &a[i]);
}
printf(“enter the position to split the array into two:”);
scanf(“%d,”&pos);
for (i=0; i<n;i++)
{
if(i<pos)
a1[k1++]=a[i];
else
a2[k2++]=a[i];
}
printf(“Elements of first array:”);
for (i=0; i<k1;i++)
printf(“%d/n, a1[i]”);
printf(“Elements of second array:”);
for (i=0; i<k2;i++)
printf(“%d/n, a2[i]”);
To insert an element into an array
#include<stdio.h>
main()
{
int a[20], i, pos, n, value;
printf("Enter number of elements:");
scanf("%d", &n);
for (i=0; i<n;i++)
{
scanf("%d", &a[i]);
}
printf(“enter the position of a new element:”);
scanf(“%d,”&pos);
printf(“enter the value to insert:”);
scanf(“%d,”&value);
for (i=n-1; i>=pos-1;i--) //shift elements forward
{
a[i+1]=a[i];
}
a[pos-1]=value; //insert value at position
printf(“Resultant array is:”);
for (i=0; i<=n;i++)
printf(“%d/n, a[i]”);
}
To delete an element into an array
#include<stdio.h>
main()
{
int a[20], i, pos, n;
printf("Enter number of elements:");
scanf("%d", &n);
for (i=0; i<n;i++)
{
scanf("%d", &a[i]);
}
printf(“enter the location of an element to be deleted:”);
scanf(“%d,”&pos);
if(pos>=n+1) //check whether the deletion is possible or not
printf(“Deletion is not possible”);
else
{
for (i=pos-1; i>=n-1;i++)
// use for loop to delete the element and update the index
{
a[i]=a[i+1];
}
printf(“Resultant array is:”);
for (i=0; i<n-1;i++)
printf(“%d/n, a[i]”);
}
Multi-Dimensional Arrays
Multi-dimensional array
• Requires a pair of square bracket for each subscript
• 2-d array requires two pairs of square brackets
• 3-d array requires three pairs of square brackets
• General syntax
data type arrayname[integer constant1]……………[integer constantn]
• An m x n, 2-d array can be thought of as a table of values having m
rows and n columns
• A 3-d array is a set of tables
2-d array
2-d array example
int values[3][4] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12};
• values[0][0] = 1 • values[1][0] = 5 • values[2][0] = 9
• values[0][1] = 2 • values[1][1] = 6 • values[2][1] = 10
• values[0][2] = 3 • values[1][2] = 7 • values[2][2] = 11
• values[0][3] = 4 • values[1][3] = 8 • values[2][3] = 12
• int values[3][4] = {
• Points to remember {1,2,3,4},
• Row index starts with 0 { 5,6,7,8} ,
• Column index starts with 0 {9,10,11,12}
};
2-d array example
• values[1][0] = 4
int values[3][4] = {
• values[1][1] = 5
{1,2,3},
• values[1][2] = 6
{4,5,6},
• values[1][3] = 0
{7,8,9}
}; • values[2][0] = 7
• values[0][0] = 1
• values[2][1] = 8
• values[0][1] = 2
• values[2][2] = 9
• values[0][2] = 3
• values[2][3] = 0
• values[0][3] = 0
2-d array example
int values[3][4] = {1,2,3,4,5,6, 7,8,9};