Array and String |
|
Array Declaration |
|
Arrays are defined in the same manner as ordinary variables, except that each arrayname must be accompanied by the size specification. |
|
The general form of array declaration is: |
|
data_type array_name[size]; |
|
data-type specifies the type of array, size is a positive integer number or symbolic constant that indicates the maximum number of elements that can be stored in the array. |
|
Example: |
float height[50]; |
|
This declaration declares an array named height containing 50 elements of type float. |
|
|
|
|
Array and String |
|
Array Initialization |
|
The Element of array are initialized in the same way as the ordinary variables. |
|
Example: |
|
int num[6]={2,4,5,35,12,12,67}; |
|
An example showing the index and its corresponding data (prices) > |
|
 |
|
An example program of entering data into Array: |
|
 |
|
Out put of the program |
|
 |
|
In this example, using the for loop, the process of asking and receiving the marks is accomplished. |
|
When the count has the value 0, the scanf() statement will cause the cvalue to be stored atnum[0]. |
|
The process continues until count has the value greater than 5. |
|
Reading data from Array |
|
In the above program we enter the data into an Array. Now to read value from this array , we will again use for Loop. |
|
The below program segment explains the retrieval of the values from the array: |
|
 |
|
|
|
|
Array and String |
|
Two Dimensional Array |
|
Two dimensional array (2-D array) is also called Matrix |
|
General form of 2-D array is: |
|
data_type array_name[row_size][column_size]; |
|
Example: |
int marks [4][2] |
|
Different ways of Initialization of a 2-Dimensional Array: |
|
int table [2][3]={0,0,0,1,1,1}; |
initialization done by row. |
|
int table[2][3]={{0,0,0},{1,1,1}}; |
surrounding the elements of each row by braces. |
|
int table[2][3]={{0,0,0,}, |
initialization as matrix. |
|
An example program that stores roll number and marks obtained by a student side by side in Matrix: |
|
 |
|
Explanation |
|
There are two parts of the program. In the first part a for loop will read the values of rollno. and marks, whereas in the second part another loop will print these values. |
|
|
|
|
Array and String |
|
Multi Dimensional Array |
|
Arrays of three or more dimension is called Multi-Dimensional Array. |
|
General form Multi-Dimensional Array: |
|
data_type array_name[s1][s2][s3]......[sn]; |
|
Example: |
int survey[3][5][12] |
|
Here survey is a 3-dimensionalarray declared to contain 180 integer_type elements. |
(3x5x12=180) |
|
Initialization of 4-Dimensional Array: |
|
static int arr [3] [4] [2]={{{2,4}, {7,3}, (3,4}, {5,1}, }, {{3,4}, {3,4}, {3,2}, {4,5}}, {{2,3}, {2,7}, {2,3}, {4,3}}} |
|
In this example, the outer array has three element , each of which is a two dimensional array of four rows, each other of which is a one dimensional array of two elements. |
|
An example program to sort an integer array: |
|
 |
|
Out put of the program |
|
 |
|
|
|
|
Array and String |
|
String |
|
Strings in C are represented by arrays of characters. The end of the string is marked with a special character, the null character, which is simply the character with the value 0. |
|
Because C has no built-in facilities for manipulating entire arrays (copying them, comparing them, etc.), it also has very few built-in facilities for manipulating strings. |
|
In fact, C's only truly built-in string-handling is that it allows us to use string constants(also called string literals) in our code. |
|
Whenever we write a string, enclosed in double quotes, C automatically creates an array of characters for us, containing that string, terminated by the \0 character. |
|
For example, we can declare and define an array of characters, and initialize it with a string constant: |
|
char string[] = "Hello, world!"; |
|
Two ways to initilize string > |
|
 |
|
In this case, we can leave out the dimension of the array, since the compiler can compute it for us based on the size of the initializer. |
|
This is the only case where the compiler sizes a string array for us, however; in other cases, it will be necessary that we decide how big the arrays we use to hold strings. |
|
An example program showing the character data type array: |
|
 |
|
Out put of the program |
|
 |
|
In the above example, a character based array named word is declared, and each element of array is assigned a character. |
|
The last element is filled with a zero value, to signify the end of the character string (in C, there is no string type, so character based arrays are used to hold strings). |
|
A printf statement is then used to print out all elements of the array. |
|
|
|
|
|
|
|
|
|
|
No comments:
Post a Comment