Unit - 3@array

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 14

UNIT – 3

Array & string

1
Array
• Array is defined as the collection of similar type of data items
stored at contiguous memory locations. that are referenced by a
common name.

• For example, if we want to store the marks of a student in 5


subjects, then we don't need to define different variables for the
marks in the different subject.

• Instead of that, we can define an array which can store the marks
in each subject at the contiguous memory locations.
2
Advantage of Array

 Code Optimization: Less code to the access the data.

 Ease of traversing: By using the for loop, we can


retrieve the elements of an array easily.

 Random Access: We can access any element randomly


using the array.

3
Disadvantage of C Array

Fixed Size:
Whatever size, we define at the time of
declaration of the array, we can't exceed the limit.

4
Types
One-Dimensional array(1-D)

Two-Dimensional array(2-D)

5
How to declare an array

• Syntax:
datatype array_name[size];

Example: int x[3];


x
X[0]
X[1]
X[2]
Array initialization

• Syntax:
data_type array_name[size]={values};

Example: int x[3]={5,3,7};


x
5 X[0]
3 X[1]
7 X[2]

7
Example

 Program to set values of array and display it

 Program to get values of array from users and display it

8
#include<stdio.h>
void main()
{
int a[5]={10,20,30,40,50},i;
clrscr();
for(i=0;i<5;i++)
{
printf("\nThe value in a[%d] is %d",i,a[i]);
}
getch();
} OUTPUT

The value in a[0] is 10


The value in a[1] is 20
The value in a[2] is 30
The value in a[3] is 40
The value in a[4] is 50
9
#include<stdio.h> OUTPUT
#include<conio.h> Enter five values :
void main() 10
{ 20
int a[5],i; 30
clrscr(); 40
printf("Enter five values : \n"); 50
for(i=0;i<5;i++) The value in a[0] is 10
{ The value in a[1] is 20
scanf("%d",&a[i]); The value in a[2] is 30
} The value in a[3] is 40
for(i=0;i<5;i++) The value in a[4] is 50
{
printf("\nThe value in a[%d] is %d",i,a[i]);
}
getch();
}
10
//program to find sum of elements in an array
#include<stdio.h>
#include<conio.h>
void main()
{
int a[5],i,sum=0;
clrscr();
printf("Enter five values : \n");
OUTPUT
for(i=0;i<5;i++)
Enter five values :
{
1
scanf("%d",&a[i]);
2
} 3
for(i=0;i<5;i++) 4
{ 5
sum=sum+a[i]; The sum of elements in array is : 15
}
printf(“The sum of elements in array is : %d”,sum);
getch();
}
11
//program to sort number in ascending order
#include<stdio.h>
#include<conio.h> }
void main() }
{ printf(“Ascending order : \n”);
int a[5],i,j,t; for(i=0;i<5;i++)
{
clrscr(); printf(“%d\t”,a*i+);
printf("Enter five values : \n"); }
for(i=0;i<5;i++) getch();
{ }
scanf("%d",&a[i]);
} OUTPUT
for(i=0;i<5;i++) Enter five values :
{
for(j=i+1;j<5;j++) 40
{ 10
if(a[i]>a[j]) 30
{ 50
t=a[i]; 20
a[i]=a[j];
Ascending order :
a[j]=t;
} 10 20 30 40 50
12
//program to find maximum no in an array
#include<stdio.h>
#include<conio.h>
void main() printf(“Maximum no is %d“,max);
{ getch();
}
int a[5],i,max;
clrscr(); OUTPUT
printf("Enter five values : \n"); Enter five values :
for(i=0;i<5;i++) 10
{ 20
scanf("%d",&a[i]); 30
}
40
max=a[0];
for(i=1;i<5;i++) 50
{ Maximum no is 50
if(max<a[i])
{
max=a[i];
}
}
13
//program to search element in array(Linear Search)
#include<stdio.h>
#include<conio.h> else
{
void main() printf(“Value not found”);
{ }
int a[5]={10,20,30,40,50}; }
int key; getch();
}
clrscr();
printf("Enter search value:\n");
scanf("%d",&key);
for(i=0;i<5;i++) OUTPUT
{ Enter search value : 40
if(key==a[i]) Value found
{
printf(“Value found”);
break;
}

14

You might also like