0% found this document useful (0 votes)
5 views68 pages

Goood

The document provides an overview of arrays in programming, specifically focusing on their structure, syntax, and operations in C. It includes examples of creating arrays, accessing elements, and various operations such as printing, reversing, and counting occurrences of elements. Additionally, it discusses the rules regarding array initialization and memory addresses.

Uploaded by

arunprince538
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views68 pages

Goood

The document provides an overview of arrays in programming, specifically focusing on their structure, syntax, and operations in C. It includes examples of creating arrays, accessing elements, and various operations such as printing, reversing, and counting occurrences of elements. Additionally, it discusses the rules regarding array initialization and memory addresses.

Uploaded by

arunprince538
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 68

Arrays

It is a collection of similar data items which are


Stored in a contiguios memory locations.
Create an array for 5 integers.

index
a

a[0] a[1] a[2] a[3] a[4]


Syntax:
Datatype arr[size]; 10
10 10
20 10
30 10
40 10
50

a+0 a+1 a+2 a+3 a+4


(0x1000) (0x1004) (0x1008) (0x1012) (0x1016)

&a[0] &a[1] &a[2] &a[3] &a[4]

&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.

a[0] a[1] a[2]

10
10 10
20 10
30
Array size : 12
a+0 a+1 a+2
(0x1000) (0x1004) (0x1008)

&a[0] &a[1] &a[2]


&a
(0x1000)

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

a[0] a[1] a[2] a[3] a[4]

10
10 10
20 10
30 100 100

a+0 a+1 a+2 a+3 a+4


(0x1000) (0x1004) (0x1008) (0x1012) (0x1016)

&a[0] &a[1] &a[2] &a[3] &a[4]

&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[0] a[1] a[2] a[3] a[4]

100 100 100 100 100

a+0 a+1 a+2 a+3 a+4


(0x1000) (0x1004) (0x1008) (0x1012) (0x1016)

&a[0] &a[1] &a[2] &a[3] &a[4]

&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

a[0] a[1] a[2] a[3] a[4]

10
G.V 10
G.V 10
G.V 10
G.V 10
G.V

a+0 a+1 a+2 a+3 a+4


(0x1000) (0x1004) (0x1008) (0x1012) (0x1016)

&a[0] &a[1] &a[2] &a[3] &a[4]

&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

a[0] a[1] a[2] a[3] a[4]

100 10
11 100 10
33 100

a+0 a+1 a+2 a+3 a+4


(0x1000) (0x1004) (0x1008) (0x1012) (0x1016)

&a[0] &a[1] &a[2] &a[3] &a[4]

&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

After reversing the array elements:


a[0] a[1] a[2] a[3] a[4] a[5] a[6] a[7] a[8] a[9]
100 90 80 70 60 50 40 30 20 10

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;
}

printf("After reversing , array elements are...\n");


for(i=0;i<ele;i++)
29printf("%d ",a[i]);
Assignment:
5. Write a program that how many number of times
each array element is present 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 80 40 30 10 20 40 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 ---> 4 times


80 ---> 1 time
40 ---> 2 time
30 ---> 2 time
20 ---> 1 time

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]


10 80 40 30 10

b[0] b[1] b[2] b[3] b[4]


10 80 40 30 10
37
Copy array elements into another array
int main()
{
int a[5] = {10,20,30,40,50};
int i,n,b[5];
n = sizeof a/sizeof a[0];
//b = a; //error, because addr = addr is invalid
for(i=0;i<n;i++)
b[i] = a[i];
for(i=0;i<n;i++)
printf("%d ",b[i]);
38 printf("\n");
Assignment:
9.Write a program to find the no.of even and odd numbers
From the given array elements.
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 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

Index = 4, element = 72;


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 72 10 25 74 11 8

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;

printf("After insertion of an element at given index\n");


for(i=0;i<n;i++)
printf("%d ",a[i]);
printf("\n");
} 46
find the biggest element from the array list

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.

If the given array has to be sorted in ascending


order, then bubble sort will start by comparing the first element of
the array with the second element, if the first element is greater
than the second element, it will swap both the elements, and then
move on to compare the second and the third element, and so
on.

If we have total n elements, then we need to repeat this


process for n-1 times.
51
Bubble sort

Ex : let's take array elemets 30,50,20,10,40


Input :
a[0] a[1] a[2] a[3] a[4]

10
30 10
50 10
20 10
10 10
40

a+0 a+1 a+2 a+3 a+4


(0x1000) (0x1004) (0x1008) (0x1012) (0x1016)

52
Bubble sort

a[0] a[1] a[2] a[3] a[4]


30 50 20 10 40

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.

Here, there are 5 elements are there. So there will be 4 iterations.


For each iteration there are proper elements comparision is done.

For 1st iteration : 0-1,1-2,2-3,3-4


For 2nd iteration : 0-1,1-2,2-3 Positions based
For 3rd iteration : 0-1,1-2 elements comparison.
For 4th iteration : 0-1

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

1st biggest number will be found 20 50


and placed in last position.
10 50

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

2nd biggest number will be found 20 30


and placed in 2nd last position.
10 30

a[0] a[1] a[2] a[3] a[4]


After 2nd iteration ----------> 20 10 30 40 50

55
Bubble sort
swap swap
3rd iteration :
------------------ 0 1 2 3 4
0-1,1-2 20 10 30 40 50

3rd biggest number will be found 10 20


and placed in 3rd last position.

a[0] a[1] a[2] a[3] a[4]


After 3rd iteration ----------> 10 20 30 40 50

56
Bubble sort
swap
4th iteration :
------------------- 0 1 2 3 4
0-1 10 20 30 40 50

4th biggest number will be found


and placed in 4th last position.

a[0] a[1] a[2] a[3] a[4]


After 4th iteration ----------> 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

Selection sort is conceptually the most simplest sorting


algorithm. This algorithm will first find the smallest element in
the array and swap it with the element in the first position, then it
will find the second smallest element and swap it with the
element in the second position, and it will keep on doing this until
the entire array is sorted.

It is called selection sort because it repeatedly selects


the next-smallest element and swaps it into the right place.

59
Selection sort

Following are the steps involved in selection sort ( for


sorting a given array in ascending order )

1) Starting from the first element, we search the


smallest element in the array, and replace it with the element in
the first position.
2) We then move on to the second position, and
look for smallest element present in the sub array, starting from
index 1, till the last index.
3) We replace the element at the second position in
the original array, or we can say at the first position in the sub
60 array, with the second smallest element. This is repeated,
Selection sort

Ex : let's take array elemets 30,50,20,10,40


Input :
a[0] a[1] a[2] a[3] a[4]

10
30 10
50 10
20 10
10 10
40

a+0 a+1 a+2 a+3 a+4


(0x1000) (0x1004) (0x1008) (0x1012) (0x1016)

61
Selection sort

a[0] a[1] a[2] a[3] a[4]


30 50 20 10 40

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.

Here, there are 5 elements are there. So there will be 4 iterations.


For each iteration there are proper elements comparison is done.

For 1st iteration : 0-1,0-2,0-3,0-4


For 2nd iteration : 1-2,1-3,1-4 Positions based
For 3rd iteration : 2-3,2-4 elements comparison
For 4th iteration : 3-4

62
Selection sort

1st iteration : swap


------------------
swap
0-1,0-2,0-3,0-4

1st smallest number will be found


and placed in 1st position. 0 1 2 3 4
30 50 20 10 40

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

a[0] a[1] a[2] a[3] a[4]


After 2nd iteration ----------> 10 20 50 30 40

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

a[0] a[1] a[2] a[3] a[4]


After 2nd iteration ----------> 10 20 50 30 40

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.

a[0] a[1] a[2] a[3] a[4]


After 3rd iteration ----------> 10 20 30 50 40

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.

a[0] a[1] a[2] a[3] a[4]


After 4th iteration ----------> 10 20 30 40 50

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
}

You might also like