Unit-4 Arrays and Strings PDF
Document Details
Uploaded by Deleted User
Tags
Summary
This document provides an introduction to C arrays, focusing on one-dimensional arrays. It details declaration, initialization, and basic concepts of array manipulation in C programming. The document uses examples to illustrate how arrays work. It also explains how to read and write data to/from arrays.
Full Transcript
B.Sc. IT Semester - I CTUC101 Introduction to Programming Unit-4 Arrays and Strings Arrays: We have used fundamental data types int, char, float, double to store variety of data. But such type of variables can store only one va...
B.Sc. IT Semester - I CTUC101 Introduction to Programming Unit-4 Arrays and Strings Arrays: We have used fundamental data types int, char, float, double to store variety of data. But such type of variables can store only one value at a given time. Therefore, they can be used to handle limited amount of data. To process large volume of data in terms of reading, processing and printing we need a powerful data type. C supports a derived data type known as array that can be used for such applications. An array is a fixed-size sequential collection of elements of the same data type. It is grouping of same- like data. An array can be used to represent a list of numbers or list of names. Examples: List of employees in an organization List of products Test score of class students List of customers and their contact numbers and so on. An array is a sequential collection of related data items that share a common name. We can refer to individual values by writing a number called index or subscript in brackets after the array name. One-Dimensional Arrays: A list of items can be given one variable name using only one subscript and such a variable is called a single-subscript variable or one-dimensional array. If we want to represent a set of five numbers, say (35, 20, 40, 57, 19) by an array variable number, then we may declare the variable number as: int number ; and the computer reserves five storage location as: 1 The values to the array elements can be assigned as follows: number = 35; number = 40; number = 20; number = 57; number = 19; this would cause the array number to store the values as follows: Declaration of One-Dimensional Arrays: Like any other variable, arrays must be declared before they are used so that the compiler can allocate space for them in the memory. The syntax form of array declaration is type variable-name[size]; The type specifies the type of element that will be contained in the array, such as int, float or char. The size indicates maximum number of elements that can be stored inside the array. For example, float height ; declares the height to be an array containing 50 real elements. Any subscript 0 to 49 are valid. int group ; declares the group as an array to contain a maximum of 10 integer constants. Remember, any reference outside the declared limits would not necessarily cause an error. Rather, it might result in unpredictable program results. C language treats character string simply as arrays of characters. The size in character string represents the maximum number of characters that the string can hold. char name ; declare the name as character array (string) variable that can hold a maximum of 10 characters. Suppose, we read the following string constant into the string variable name. 2 When the compiler sees the character string, it terminates it with an additional null character. Thus, the element name holds the null character ‘\0’. When declaring character arrays, we must allow one extra element space for the null terminator. Initialization of One-Dimensional Arrays: After an array is declared, its elements must be initialized. Otherwise, they will contain garbage. An array can be initialized at the following two stages: (1) At compile time (2) At run time (1) Compile Time Initialization: We can initialize an array when we declare them. The general form of such arrays is: type array-name[size] = { list of values }; The values in the list are separated by commas. int number = { 0, 0, 0 }; will declare the variable number as an array of size 3 and will assign zero to each element. If the number of values in the list is less than the number of elements, then only that many elements will be initialized. The remaining elements will be set to 0 automatically. float total = { 0.0, 3.5, -2}; will initialize the first three elements to 0.0, 3.5 and -2 and the remaining two elements to zero. And if the array type is char then NULL will be set to the remaining elements. char city = {‘B’}; will initialize first element to B and remaining four to NULL. The size may be omitted. 3 In such cases, compiler allocates enough space for all initialized elements. int counter[ ] = { 1,1,1,1 }; will declare the counter array to contain four elements with initial values 1. Character arrays may be initialized in a similar way char name[ ] = { ‘C’, ‘M’, ‘P’, ‘I’, ‘C’, ‘A’, ‘\0’}; declares the name to be an array of seven characters, initialized with string CMPICA ending with the null character. We can also assign the string directly as char name[ ] = “CMPICA”; If we have more initializers than the declared size, then it will produce unpredicted result in output. int number = { 10, 20, 30, 40, 50 }; (2) Run Time Initialization: An array can be explicitly initialized at run time. This approach is usually applied for initializing large arrays. …… int n; for ( i=0; i