PWC Unit-3 (Arrays and Strings).pdf
Document Details
Uploaded by UnrealManticore
Tags
Full Transcript
Basics of Logic Development (IT3009) Unit - 3: Arrays and Strings Chhotubhai Gopalbhai Patel Institute of Technology, Bardoli Subject Basics of Logic Development (IT3009)...
Basics of Logic Development (IT3009) Unit - 3: Arrays and Strings Chhotubhai Gopalbhai Patel Institute of Technology, Bardoli Subject Basics of Logic Development (IT3009) Unit – 3 Arrays and Strings Prepared by – Mr. Viral H. Panchal 1 Basics of Logic Development (IT3009) Unit - 3: Arrays and Strings CONTENTS 1. Basics of Array 2. One-dimensional Arrays 2.1. Declaration of One-dimensional Arrays 2.2. Initialization of One-dimensional Arrays 2.3. Solved Programs (One-dimensional Arrays) 3. Two-dimensional Arrays 3.1. Declaration of Two-dimensional Arrays 3.2. Initialization of Two-dimensional Arrays 3.3. Solved Programs (Two-dimensional Arrays) 4. Multidimensional Arrays 5. Basics of String 6. Declaration and Initializing String Variables 7. Reading Strings from Terminal 7.1. Using scanf() Function 7.2. Using getchar() and gets() Functions 8. Writing Strings to Screen 8.1. Using printf() Function 8.2. Using putchar() and puts() Functions 9. Arithmetic Operations on Characters 10. String Handling Functions (Built-in String Functions) 10.1. strcat() Function 10.2. strcmp() Function 10.3. strcpy() Function 10.4. strlen() Function 10.5. strrev() Function 10.6. Other String Functions 11. Solved Programs (Strings) Prepared by – Mr. Viral H. Panchal 2 Basics of Logic Development (IT3009) Unit - 3: Arrays and Strings 1. Basics of Array An array is a collection of elements of similar data types referred by the same variable name. An array is a linear and homogeneous data structure. Linear data structure stores its individual data elements in a sequential order in the memory. Homogeneous means all individual data elements are of the same data type. The individual elements of an array are referred by their index or sub-script value. In C the index begins at zero and is always written inside square brackets. For example, if marks of 10 students are stored in an array named mark, then mark refers to the marks of the first student, mark refers to the marks of the second student and mark refers to the marks of the tenth student, that is, mark[n-1] refers to the marks of nth student. Advantages of Array: Unlike variable an array can store number of values under single name. The array elements are stored contiguously. Array index helps to access any random values from the array and perform operations on it. An array is a flexible data structure, means we can perform addition or removal of elements at any position. Disadvantages of Array: Only elements of same data types can be stored in an array. We cannot store elements of multiple data types in a single array. If array size is big, that much amount of memory gets reserved. Big size of arrays and multi-dimensional arrays are complicated to access and manipulate. If there is need to store more elements than array size, then it is not possible and shortage of memory occurs. If less elements are stored than array size then it leads to memory wastage. Prepared by – Mr. Viral H. Panchal 3 Basics of Logic Development (IT3009) Unit - 3: Arrays and Strings Types of Arrays: Arrays are categorized according to the dimensions used to define it. Here the dimension indicates the number of rows and columns used to set size of an array. There are following types of arrays: One-dimensional arrays Two-dimensional arrays Multidimensional arrays 2. One-dimensional Arrays An array with single subscript is called as one-dimensional array. 2.1. Declaration of One-dimensional Arrays The syntax of declaring one-dimensional array is: data_type array_name[size]; where, data_type = the type of data stored in the array array_name = name of the array size = maximum number of elements that an array can hold For example, float height; declares the height to be an array containing 20 real elements. Any subscripts 0 to 19 are valid. Similarly, int marks; declares the marks as an array to contain a maximum of 10 integer constants. The C language treats character strings simply as arrays of characters. The size in a character string represents the maximum number of characters that the string can hold. For example, char name; declares the name as a character array (string) variable that can hold a maximum number of 10 characters. Prepared by – Mr. Viral H. Panchal 4 Basics of Logic Development (IT3009) Unit - 3: Arrays and Strings Suppose we read the following string constant into the string variable name. “WELL DONE” Each character of the string is treated as an element of an array name and is stored in the memory as follows: ‘W’ ‘E’ ‘L’ ‘L’ ‘’ ‘D’ ‘O’ ‘N’ ‘E’ ‘\0’ When the compiler sees a 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 an extra element space for the null terminator. 2.2. Initialization of One-dimensional Arrays After an array is declared, its elements must be initialized. Otherwise, they will contain “garbage”. You can initialize the array elements one by one. Syntax: array_name[index] = value; For example, marks = 35; marks = 70;... marks = 86; You can also initialize the complete array directly. Syntax: data_type array_name[size] = {list of values}; For example, int marks = {35,70,40,55,26,43,56,82,78,86}; This array is stored in the memory as follows: 35 70 40 55 26 43 56 82 78 86 marks marks marks marks marks marks marks marks marks marks Prepared by – Mr. Viral H. Panchal 5 Basics of Logic Development (IT3009) Unit - 3: Arrays and Strings The declaration and initialization of character array: Example: char a = {‘L’,’E’’A’,’R’,’N’,’ ‘,’C’}; This array is stored in the memory as follows: ‘L’ ‘E’ ‘A’ ‘R’ ‘N’ ‘’ ‘C’ ‘\0’ a a a a a a a a The declaration and initialization of float array: Example: float price = {1.25,0.75,3.5,10.2}; This array is stored in the memory as follows: 1.25 0.75 3.5 10.2 price price price price The array size may be omitted during declaration. Example: int marks[] = {45,66,84}; is equivalent to int marks = {45,66,84}; In such cases, the subscript is assumed to equal to the number of elements in the array (3 in this case). The elements which are not explicitly initialized are automatically set to zero. Example: int x = {2,5}; implies x = 2; x = 5; x = 0; x = 0; x = 0; Prepared by – Mr. Viral H. Panchal 6 Basics of Logic Development (IT3009) Unit - 3: Arrays and Strings 2.3. Solved Programs (One-dimensional Arrays) Write a program to find out the Maximum and Minimum number from given 10 numbers. #include int main() { int a,i,max,min; for(i=0; i