Arrays - Chettinad College of Engineering and Technology

Download Report

Transcript Arrays - Chettinad College of Engineering and Technology

Arrays
• C provides the option to the user to combine
similar data types into a single entity
• 2000 2002 2004 2006 2008
a[0]
a[1]
a[2]
a[3]
a[4]
• It followed contiguous memory allocation
Main ()
{
float avg,sum=0;
Int I,a[5];
Printf(“Enter five numbers”);
for(i=0;i<5;i++)
Scanf(“%d”,&a[i]);
for(i=0;i<5;i++)
Sum=sum+a[i];
avg=sum/5;
Printf(“The sum of given number is %f”,sum);
Printf(“Average of given number is %f”,avg);
}
• Like normal variable array can also initialized
during declaration
example
• Int[5]={1,2,3,4,5};
• Int[]={1,2,3,4,5};
• Float[]={1.0,2.0,3.0,4.0,5.0};
• Int a[5]={1,2,3,4,5};
• Int a[]={1,2,3,4,5}; \\dimensional is optional
• Float b[]={1.0,2.0,3.0,4.0,5.0};
Array declaration and Initilization
•
•
•
•
•
Datatype var_name[size];
The above term is syntax for declaring array
Eg: int b[6];
Float c[6];
Int d[5][5];
• initialize array in C either one by one or using a
single statement
• double balance[5] = {1000.0, 2.0, 3.4, 17.0, 50.0};
• If you omit the size of the array, an array just big
enough to hold the initialization is created.
• double balance[] = {1000.0, 2.0, 3.4, 17.0, 50.0};
• To assign a single element in array
• A[4]=45;
Limitation of array
a)Static data :
• Array is static data structure
• Memory is allocated during compile time
• Once memory is allocated during compile time
, it cannot be changed during Run time.
b)Can hold the data from the same data type
• It cannot hold data from a different data type
in a common name
• Int a[5]={1,2,3,4.5,6};
c)Inserting element is more difficult in array
d)Deleting is not a easy task because the data is
stored in contiguous memory allocation
e)Bounds checking:
 It doesnt show error for out of bounds value,
instead it will shows the garbage value
f)Shortage of memory: if we don’t know the
memory size exactly.
 Memory is allocated during the compile time
itself
• G)Wastage of memory
if array size is too large
Multi dimensional array
• Its called matrix
• Array having more than one subscript
Int a[][];
One subscript value denotes “Row”
another subscript value denotes “column”
A[0][0]
A[0][1]
A[0][2]
A[1][0]
A[1][1]
A[1][2]
A[2][0]
A[2][1]
A[2][3]
Strings
• It is a one-dimensional array of characters
which is terminated by a null character '\0'.
• char greeting[6] = {'H', 'e', 'l', 'l', 'o', '\0'};
• the memory presentation string
H
e
l
l
o
‘\0’
#include <stdio.h>
int main ()
{
char greeting[6] = {'H', 'e', 'l', 'l', 'o', '\0'};
printf("Greeting message: %s\n", greeting );
return 0;
}
• Output:
Hello
The following example shows the use
of string
#include < stdio.h >
main()
{
char month[15];
printf (”Enter the string”);
gets (month);
printf (”The string entered is %s”, month);
}
Reading Strings from the terminal:
• char address[15];
scanf(%s,address);
String operations
1. Length (number of characters in the string).
2. Concatentation (adding two are more strings)
3. Comparing two strings.
4. Substring (Extract substring from a given
string)
5. Copy(copies one string over another)
•
•
•
•
•
•
•
•
•
•
strlen(string);
Strcmp(string1,string2);
Strcmpi(string1,string2); \\not case sens
strcpy(string1,string2);
strlwr(string);
strrev(string);
strcat(string1,string2);
strncmp(string1,string2,length);
strnicmp(string1,stringn,length);
strset(string,symbol);\\replace
String constants
•
•
•
•
It is written in pair of double quotas
String is declared as character array
String is not a data type
Single character string doesn’t have the
equivalent int value
String with single char
• “a”
String with multiple char
• “abg”
String with numbers
• “12345”
String with blanks
• “india is my country”
• For example:
• ‘a’ is not equal to “a”
Guess the output
2
3
4
5
6
7
8
9
10
}
string