Goood
Goood
index
a
&a
(0x1000)
2
int a[5] = {10,20,30,40,50}; &a[i] = &*(a+i);
= a+i;
Formulae : &a[i] = a+i;
a[i] = *(a+i);
= *(i+a);
= i[a];
An array name, itself is
representing as base address.
So, finally, a[i] = i[a];
3
int a[5] = {10,20,30,40,50}; &a[i] = &*(a+i);
= a+i;
Formulae : &a[i] = a+i;
a[i] = *(a+i);
= *(i+a);
= i[a];
An array name, itself is
representing as base address.
So, finally, a[i] = i[a];
4
#include<stdio.h>
int main()
{
int a[5] = {10,20,30,40,50};
printf("%d %d %d %d %d\n",a[0],a[1],a[2],a[3],a[4]);
printf("%d %d %d %d %d\n",0[a],1[a],2[a],3[a],4[a]);
printf("%p %p %p %p p\n",&a[0],&a[1],&a[2],&a[3],&a[4]);
printf("%p %p %p %p %p\n",a+0,a+1,a+2,a+3,a+4);
}
// () ---> paranthesis
//5 [] ---> subscript operator
#include<stdio.h>
int main()
{
int a[5] = {10,20,30,40,50};
int i;
printf("\narray elements printing\n");
for(i=0;i<5;i++)
printf("%d %d %d %d\n",a[i],i[a],*(a+i),*(i+a));
printf("\naddress printing\n");
for(i=0;i<5;i++)
printf("%p %p %p %p\n",&a[i],&i[a],a+i,i+a);
}
6
#include<stdio.h>
int main()
{
int a[5];
printf("sizeof a : %d\n",sizeof a);
printf("sizeof a[0] : %d\n",sizeof a[0]); //1st ele size
}
// sizeof(array) = no.of ele * sizeof(Data type)
7
#include<stdio.h>
int main() {
// int a[5] = {10,20,30,40,50}; //valid
int a[] = {10,20,30,40,50}; //valid, here array size is
//depends on no.of initializers
int i, ele = sizeof a/sizeof a[0];
printf("no.of elements : %d\n",ele);
for(i=0;i<ele;i++)
printf("%d %p\n",a[i],&a[i]);
} //int a[5]; //valid //int a[]; //error
8
Create an array for 5 integers.
int a[3] = {10,20,30,40,50} //warning from compiler.
10
10 10
20 10
30
Array size : 12
a+0 a+1 a+2
(0x1000) (0x1004) (0x1008)
9
#include<stdio.h>
int main()
{
int a[3] = {10,20,30,40,50};
int i;
for(i=0;i<5;i++)
printf("a[%d] = %d a+%d = %p\n",i,a[i],i,a+i);
printf("&i = %p\n",&i);
}
10
Create an array for 5 integers.
int a[5] = {10,20,30} index
a
10
10 10
20 10
30 100 100
&a
(0x1000)
11
#include<stdio.h>
int main()
{
int a[5] = {10,20,30};
int i;
for(i=0;i<5;i++)
printf("%d ",a[i]);
printf("\n");
}
12
Create an array for 5 integers.
int a[5] = {};
a
&a
(0x1000)
13
#include<stdio.h>
int main()
{
int a[5] = {};
int i;
for(i=0;i<5;i++)
printf("%d ",a[i]);
printf("\n");
}
14
Create an array for 5 integers.
int a[5];
a
10
G.V 10
G.V 10
G.V 10
G.V 10
G.V
&a
(0x1000)
15
#include<stdio.h>
int main()
{
int a[5] = {[1] = 11, [3] = 33};
int i;
for(i=0;i<5;i++)
printf("%d ",a[i]);
printf("\n");
}
16
Create an array for 5 integers.
int a[5] = {[1] = 11, [3] = 33};
a
100 10
11 100 10
33 100
&a
(0x1000)
17
#include<stdio.h>
int main()
{
int a[3],i;
//a = {10,20,30}; //error
a[0] = 10;
a[1] = 20;
a[2] = 30;
for(i=0;i<3;i++)
printf("%d ",a[i]);
18 printf("\n");
#include<stdio.h>
int main()
{
int i,a[3] = {10,20,30};
a[0]++; // a[0] = a[0]+1;
a++; // a = a+1; //error, array address can’t be incremented.
for(i=0;i<3;i++)
printf("a[%d] = %d\n",i,a[i]);
printf("a = %p\n",a);
}
19//Array base address is constant address.
a[0]++ ---> incrementing array element is possible.
a++ ----> incrementing array base address is not possible.
Assignments :
1. write a program to scan 5 integers into an array and display it.
2. write a program to print the data in reverse in an array.
3. Write a program to enter 10 elements into an array and find the
particular given element is present or not. If present display that
how many no.of times the element is present.
20
10 array elements
stop
i++ i i
a[0] a[1] a[2] a[3] a[4] a[5] a[6] a[7] a[8] a[9] a[10]
10 80 40 30 10 20 70 50 10 60
a+0 a+1 a+2 a+3 a+4 a+5 a+6 a+7 a+8 a+9 a+10
ele
ele = 10;
21
no.of times the element is present in array
int main()
{
int a[] = {10,20,22,30,10,40,50,22,10,88,34,50,20,40,30};
int i,c=0,ele,n = sizeof a/sizeof a[0];
for(i=0;i<n;i++)
printf("%d ",a[i]);
printf("\n");
printf("Enter the element\n");
scanf("%d",&ele);
for(i=0;i<n;i++)
22 {
write a program to scan 5 integers into an array and display it.
#include<stdio.h>
int main()
{
int a[5],i;
printf("Enter the array elements\n");
for(i=0;i<5;i++)
scanf("%d",&a[i]); //(or) scanf("%d",a+i);
for(i=0;i<5;i++)
printf("%d ",i[a]);
printf("\n");
23}
Reverse printing array elements
#include<stdio.h>
int main()
{
int a[5] = {10,20,30,40,50};
int i,ele = sizeof a/sizeof a[0];
//reverse print
for(i=ele-1;i>=0;i--)
printf("%d ",a[i]);
printf("\n");
}
24
WAP to check that array elements are palindrome
format or not.
#include<stdio.h>
int main()
{
//int a[] = {10,20,30,40,50,40,30,20,10};
int a[] = {10,20,30,40,50,55,40,30,20,10};
int i,j,n;
n = sizeof a/sizeof a[0];
for(i=0;i<n;i++)
printf("%d ",a[i]);
printf("\n");
25
for(i=0,j=n-1;i<j;i++,j--)
{
if(a[i] != a[j])
break;
}
if(i<j)
printf("not palindrome format...\n");
else
printf("palindrome format...\n");
26}
Assignment:
4. Write a program to reversing the array elements in
a given array.
For ex :
a[0] a[1] a[2] a[3] a[4] a[5] a[6] a[7] a[8] a[9]
10 20 30 40 50 60 70 80 90 100
27
Reversing the array elements
#include<stdio.h>
int main()
{
int a[] = {10,20,30,40,50,60,70,80,90,100};
int i,j,ele,temp;
ele = sizeof a/sizeof a[0];
printf("Before reversing, array elements are...\n");
for(i=0;i<ele;i++)
printf("%d ",a[i]);
printf("\n");
28
Reversing the array elements
for(i=0,j=ele-1;i<j;i++,j--)
{
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
a+0 a+1 a+2 a+3 a+4 a+5 a+6 a+7 a+8 a+9
30
no.of times each element is present
#include<stdio.h>
int main()
{
int a[] = {10,20,22,30,10,40,50,22,10,88,34,50,20,40,30};
int i,j,c,n,ele;
n = sizeof a/sizeof a[0];
for(i=0;i<n;i++)
printf("%d ",a[i]);
printf("\n");
31
no.of times each element is present.
for(i=0;i<n;i++)
{
for(j=0;j<i;j++) {
if(a[i]==a[j])
break;
}
if(j==i) //confirmation that a[i] doesn't exist till from 0 to i-1
position
{
for(j=i+1,c=1;j<n;j++)
if(a[i] == a[j])
32
c++;
Assignment:
6.Write a program to display only the repeated elements
From the given Array.
For ex :
a[0] a[1] a[2] a[3] a[4] a[5] a[6] a[7] a[8] a[9]
10 80 40 30 10 20 70 10 10 30
a+0 a+1 a+2 a+3 a+4 a+5 a+6 a+7 a+8 a+9
Output : 10,30
33
Logic to print only repeated array elements
for(i=0;i<n;i++)
{
for(j=0;j<i;j++) {
if(a[i]==a[j])
break;
}
if(j==i)
{
for(j=i+1,c=1;j<n;j++)
if(a[i] == a[j])
34 c++;
Assignment:
7.Write a program to display only the non-repeated
elements from the given Array.
For ex :
a[0] a[1] a[2] a[3] a[4] a[5] a[6] a[7] a[8] a[9]
10 80 40 30 10 20 70 10 10 30
a+0 a+1 a+2 a+3 a+4 a+5 a+6 a+7 a+8 a+9
Output : 80,40,20,70
35
Logic to print only non-repeated array elements
for(i=0;i<n;i++)
{
for(j=0;j<i;j++) {
if(a[i]==a[j])
break;
}
if(j==i)
{
for(j=i+1,c=1;j<n;j++)
if(a[i] == a[j])
36 c++;
Assignment:
8.Write a program to copy one array elements into
another array.
For ex : int a[5] = {10,80,40,30,10};
int b[5];
a[0] a[1] a[2] a[3] a[4] a[5] a[6] a[7] a[8] a[9]
17 82 41 39 10 25 74 12 8 4
a+0 a+1 a+2 a+3 a+4 a+5 a+6 a+7 a+8 a+9
no.of evens = 6
no.of odds = 4
39
Assignment:
10.Write a program to find the no.of prime numbers
from the given array elements and display it.
For ex :
a[0] a[1] a[2] a[3] a[4] a[5] a[6] a[7] a[8] a[9]
17 82 41 39 10 25 74 11 8 4
a+0 a+1 a+2 a+3 a+4 a+5 a+6 a+7 a+8 a+9
Output :
17 , 41, 11
no.of primes = 3
40
Assignment:
11.Write a program to delete the array element based
On array index (array position) value.
For ex : a[0] a[1] a[2] a[3] a[4] a[5] a[6] a[7] a[8] a[9]
17 82 41 39 10 25 74 11 8 4
Index = 4;
Output :
a[0] a[1] a[2] a[3] a[4] a[5] a[6] a[7] a[8] a[9]
17 82 41 39 25 74 11 8 4 0
41
deletion of an element at given index
#include<stdio.h>
int main()
{
int a[] = {10,20,30,40,50,60,70,80,90,100};
int i,n,pos;
n = sizeof a/sizeof a[0];
for(i=0;i<n;i++)
printf("%d ",a[i]);
printf("\n");
printf("Enter the position\n");
scanf("%d",&pos);
42
deletion of an element at given index
if( !((pos>=0)&&(pos<=n-1)) )
{
printf("Invalid index value\n");
return 0;
}
for(i=pos;i<n-1;i++)
a[i] = a[i+1];
a[n-1] = 0;
printf("After deletion of an element\n");
for(i=0;i<n;i++)
printf("%d ",a[i]);
43 printf("\n");
}
Assignment:
12.Write a program to insert an element into an array
based on array index.
For ex : a[0] a[1] a[2] a[3] a[4] a[5] a[6] a[7] a[8] a[9]
17 82 41 39 10 25 74 11 8 0
44
Insert an element at given index
#include<stdio.h>
int main()
{
int a[10] = {10,20,30,40,50,60,70,80,90};
int i,index,ele, n = sizeof a/sizeof a[0];
for(i=0;i<n;i++)
printf("%d ",a[i]);
printf("\n");
printf("Enter the index value\n");
scanf("%d",&index);
if( !((index>=0)&&(index<=n-1)) )
{
printf("Invalid index value\n");
return 0;
45
}
Insert element at given index
printf("Enter the element\n");
scanf("%d",&ele);
for(i=n-2;i>=index;i--)
a[i+1] = a[i];
a[index] = ele;
a[0] a[1] a[2] a[3] a[4] a[5] a[6] a[7] a[8] a[9]
45 34 78 38 89 12 56 67 24 4
a+0 a+1 a+2 a+3 a+4 a+5 a+6 a+7 a+8 a+9
47
Biggest element in a given array
int main()
{
int a[] = {10,5,26,1,83,17,26,72,34,34,19,83};
int i,big, n = sizeof a/sizeof a[0];
for(i=0;i<n;i++)
printf("%d ",a[i]);
printf("\n");
big = a[0];
for(i=1;i<n;i++) {
if(big < a[i])
big = a[i];
}
printf("big = %d\n",big);
48
}
2nd big element in an array
#include<stdio.h>
int main()
{
int a[] = {10,10,10,10,26,1,83,17,26,72,34,34,19,83};
//int a[] = {10,10,10,10,10,10,10,10};
int i,big1,big2, n = sizeof a/sizeof a[0];
for(i=0;i<n;i++)
printf("%d ",a[i]);
printf("\n");
49 if(a[0]<a[1])
2nd big element in an array
for(i=2;i<n;i++)
{
if(a[i] > big1)
{
big2 = big1;
big1 = a[i];
}
else if( (a[i] > big2)&&(a[i]!=big1) )
big2 = a[i];
50 }
Bubble sort
Bubble Sort is a simple algorithm which is used to sort a given
set of n elements provided in form of an array with n number of
elements. Bubble Sort compares all the element one by one and
sort them based on their values.
10
30 10
50 10
20 10
10 10
40
52
Bubble sort
In Bubble sort operation, if there are 'n' no.of elements are there,
Then there will be 'n-1' iterations will come. And for every sorting
Operation there will be the adjucent elements will be compared in
a proper way.
53
Bubble sort
swap swap swap
1st iteration :
------------------ 0 1 2 3 4
0-1,1-2,2-3,3-4 30 50 20 10 40
40 50
a[0] a[1] a[2] a[3] a[4]
After 1st iteration ----------> 30 20 10 40 50
54
Bubble sort
swap swap
2nd iteration :
------------------ 0 1 2 3 4
0-1,1-2,2-3 30 20 10 40 50
55
Bubble sort
swap swap
3rd iteration :
------------------ 0 1 2 3 4
0-1,1-2 20 10 30 40 50
56
Bubble sort
swap
4th iteration :
------------------- 0 1 2 3 4
0-1 10 20 30 40 50
57
Bubble sort logic
for(i=0;i<n-1;i++)
{
for(j=0;j<n-1-i;j++)
{
if(a[j]>a[j+1])
{
temp = a[j];
a[j] = a[j+1];
a[j+1] = temp;
}
58 }
Selection sort
59
Selection sort
10
30 10
50 10
20 10
10 10
40
61
Selection sort
In Selection sort operation, if there are 'n' no.of elements are there,
Then there will be 'n-1' iterations will come. And for every sorting
Operation there will be the adjacent elements will be compared in
a proper way.
62
Selection sort
20 30
10 20
a[0] a[1] a[2] a[3] a[4]
After 1st iteration ---> 10 50 30 20 40
63
Selection sort
2nd iteration :
------------------
1-2,1-3,1-4 0 1 2 3 4
10 50 30 20 40
2nd smallest number will be found
and placed in 2nd position. 30 50
swap 20 30
swap
64
Selection sort
2nd iteration :
------------------
1-2,1-3,1-4 0 1 2 3 4
10 50 30 20 40
2nd smallest number will be found
and placed in 2nd position. 30 50
swap 20 30
swap
65
Selection sort
3rd iteration :
------------------ 0 1 2 3 4
2-3,2-4 10 20 50 30 40
30 50
3rd smallest number will be found
and placed in 3rd position.
66
Selection sort
swap
4th iteration :
------------------- 0 1 2 3 4
3-4 10 20 30 50 40
40 50
4th smallest number will be found
and placed in 4th position.
67
Selection sort logic
for(i=0;i<n-1;i++)
{
for(j=i+1;j<n;j++)
{
if(a[i]>a[j])
{
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
68
}