C Arrays
C Arrays
www.edutalks.org
Arrays
An array is a collection of identical data
elements or homogeneous type of data
elements
www.edutalks.org
Arrays
Eg. #include<stdio.h>
main()
{
int age[5];
int i,n;
float sum=0,avg;
printf(“enter the no of persons”);
scanf(“%d”,&n);
if (n<=0 || n>5)
printf(”invalid no of persons entered”);
else
{
for (i=0;i<n;i++)
{
printf(“Age”)
www.edutalks.org
Arrays
scanf(“%d”,&age[i]);
sum=sum+age[i];
}
avg=sum/n;
printf(“ages are”);
for (i=0;i<n;i++)
printf(“%d”,age[i]);
printf(“average=%f”,avg);
}
}
www.edutalks.org
Arrays
Declaration
Must be accompanied by a size of specification,ie the no
of elements
Syntax
Size of array
Name of array
www.edutalks.org
Arrays
Dimensioning the array
Declaring the name and the type of an array and setting the no of
elements in the array
Three types
1. One dimensional
2. Two dimensional
3. Multi dimensional
www.edutalks.org
Arrays
1. One dimensional array
A list of items can be given one variable name using only
one subscript
Eg. int age[5];
2. Two dimensional array
Two subscripts, one denotes the row, and the other
column
Eg. int a[10][10];
a is declared as matrix having 10 rows and 10 columns.
The first element is a[0][0] and the last element is
a[9][9]
www.edutalks.org
Arrays
Accessing array elements
Specify array name, followed by square braces enclosing an
integer- array index
www.edutalks.org
Arrays
printf(“enter the numbers”);
for (i=0;i<=m;i++)
{
scanf(“%d”,&s[i]);
}
sum=0;
for (i=0;i<=m;i++)
{
sum=sum+s[i];
}
printf(“the sum of the numbers is %d”,sum);
}
www.edutalks.org
Arrays
Searching of array element
Used to find the location where element is available or not
Two types
1. Linear Searching
Access each element of an array one by one sequentially and see
whether it is the desired element or not
Eg. #include<stdio.h>
main()
{
int a[100],n,i,item,loc=-1;
printf(“enter the no of elements”);
scanf(“%d”,&n);
printf(“enter the nos:”);
for (i=0;i<=n;i++)
www.edutalks.org
{
Arrays
scanf(“%d”,&a[i]);
}
printf(“enter the no to be searched:”);
scanf(“%d”,&item);
for (i=0;i<=n;i++)
{
if (item==a[i])
{
loc=i;
break;
}
}
if (loc>=0)
printf(“%d is found in position %d”,item,loc+1);
else
printf(“item does not exist”);
}
www.edutalks.org
Arrays
2. Binary Searching
Extremely efficient algorithm
Searches the given item in minimum possible comparisons
First sort the array elements
Logic is
1. First find the middle element of the array
2. Compare the mid element with an item
3. There are 3 cases
i. If it is a desired element then search is successful
ii. If it is less than desired item then search only the first half
of the array
iii. If it is greater than the desired element, then search in the
second half of the array
Repeat the steps until an element is found or exhausted in the
search area
www.edutalks.org
Arrays
Eg. #include<stdio.h>
main()
{
int a[100],n,i,item,beg,end,mid;
printf(“enter the no of elements”);
scanf(“%d”,&n);
printf(“enter the no.s in ascending order”);
for (i=0;i<=n-1;i++)
{
scanf(“%d”,&a[i]);
}
printf(“enter the no to be searched”);
scanf(“%d”,&item);
beg=0;
end=n-1;
mid=(beg+end)/2;
while (beg<=end && item!=a[mid])
www.edutalks.org
Arrays
{
if (item>a[mid])
beg=mid+1;
else
end=mid-1;
mid=(beg+end)/2;
}
If (a[mid]==item)
printf(“%d is found in position %d”,item,mid+1);
Else
Printf(“item does not exist”);
}
www.edutalks.org
Strings
Arrays of characters
www.edutalks.org
Strings
Arrays of strings
www.edutalks.org
Strings
Functions in strings
www.edutalks.org
Strings
Functions in strings
1. strcat()
Concatenates two strings
Appends one string at the end of another
Accepts two strings as parameters and stores the contents of the
second string at the end of the first
2. strcmp()
Compares two strings
www.edutalks.org
Strings
3. strcpy()
Copies one string to another
Accepts two strings as parameters and copies the second
string by character into the first one up to and including the
null character of the second string
4. strlen()
This fn returns an integer which denotes the length of the string
passed
www.edutalks.org
Strings
5. strrev()
Process of exchanging the characters in the string
The first character exchanged with last one, second
character exchanged with second last one and so on, till we
reach the mid position
6. strlwr()
Convert all the characters in a string from upper case to
lowercase
7. strupr()
Lowercase to uppercase
www.edutalks.org
Keep in touch through
www.edutalks.org