You are on page 1of 21

SUHAIL T A

www.edutalks.org
Arrays
 An array is a collection of identical data
elements or homogeneous type of data
elements

 Stored in consecutive memory locns under


a common heading or a variable name

 Individual values are called 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

Data type arrayname[size of array];

Eg. int age[5];

Size of array

Name of array

Data type 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

 Indicates the particular element of array which we want to access

 Numbering of elements starts from zero


 Eg. #include <stdio.h>
main)
{
int i,sum,m;
int s[100];
printf(“how many numbers”);
scanf(“%d”,&m);

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

 Characters assigned one after another in memory

 To mark the end of the string, c uses the null character

 Are enclosed within double quotes

 Eg. “my age is ten” is a string


 Eg. #include<stdio.h>
main()
{
char month[15];
printf(“enter the string”);
gets(month);
printf(“The string entered is %s”,month);
}

www.edutalks.org
Strings
 Arrays of strings

 An array of string is 2d array


 Eg. #include<stdio.h>
const int NUM=5;
const int LENGTH=9;
main()
{
int j;
char names[NUM][LENGTH]={“Tejaswi”, “Prasad”, “Prakash”,
“Prasanth”, “Anand”};
for (j=0;j<=NUM;j++)
printf(“%s\n”, names[j]));
}

www.edutalks.org
Strings
 Functions in strings

 To use string fns, the header file “string.h” must be


included in the pgm
i.e #include<string.h>
 Eg. #include<stdio.h>
const int NUM=5;
const int LENGTH=9;
main()
{
int j;
char names[NUM][LENGTH]={“Tejaswi”, “Prasad”, “Prakash”, “Prasanth”,
“Anand”};
for (j=0;j<=NUM;j++)
printf(“%s\n”, names[j]));
}

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

 Accepts two strings as parameters and returns an integer, whose


value is

 Less than 0, if the first string is less than second

 Equal to 0 if both are equal

 Greater than 0, if the first string is greater than second

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

 Length of the string is the no of characters present in it, excluding


the terminating null character

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

suhailta@gmail.com

www.edutalks.org

You might also like