Download presentation
Presentation is loading. Please wait.
Published byAbel Garrison Modified over 8 years ago
1
Arrays C provides the option to the user to combine similar data types into a single entity 2000 2002 2004 2006 2008 It followed contiguous memory allocation a[0]a[1]a[2]a[3]a[4]
2
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); }
3
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};
4
Int a[5]={1,2,3,4,5}; Int a[]={1,2,3,4,5}; \\dimensional is optional\\dimensional Float b[]={1.0,2.0,3.0,4.0,5.0};
5
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];
6
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;
7
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.
8
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};
9
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
10
G)Wastage of memory if array size is too large
11
Multi dimensional array Its called matrix Array having more than one subscript Int a[][]; One subscript value denotes “Row” another subscript value denotes “column”
12
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]
13
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 Hello‘\0’
14
#include int main () { char greeting[6] = {'H', 'e', 'l', 'l', 'o', '\0'}; printf("Greeting message: %s\n", greeting ); return 0; }
15
Output: Hello
16
The following example shows the use of string #include main() { char month[15]; printf (”Enter the string”); gets (month); printf (”The string entered is %s”, month); }
17
Reading Strings from the terminal: char address[15]; scanf(%s,address);
18
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)
19
strlen(string); Strcmp(string1,string2); Strcmpi(string1,string2); \\not case sens\\not strcpy(string1,string2); strlwr(string); strrev(string); strcat(string1,string2); strncmp(string1,string2,length); strnicmp(string1,stringn,length); strset(string,symbol);\\replace
20
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
21
String with single char “a” String with multiple char “abg” String with numbers “12345” String with blanks “india is my country”
22
For example: ‘a’ is not equal to “a”
23
Guess the output
25
2
26
No Output
27
3
29
4
31
5
33
6
35
7
37
8
38
Output: 1 2 3 4 5 6 0 0 0 0
39
9
40
Output 6 5 4 3 2
41
10
50
}
52
string
53 0
55
Output ccccccccccccccccccc
57
Output 0000000000
59
Output No output
73
24
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.