Unit 3 Arrays - C Programming PDF
Document Details
Uploaded by Deleted User
Tags
Summary
This document is a set of lecture notes covering arrays in C programming. It explains the concepts of array notation, representation, manipulation, and multi-dimensional arrays. The document also delves into character arrays and strings, with illustrative examples.
Full Transcript
Unit-3 Arrays: Array notation and representation, manipulating array elements, using multi dimensional arrays, Character Arrays and Strings, Some Examples of Arrays Basic Concept Many applications require multiple data items that have common characteristics. In mathematics, we oft...
Unit-3 Arrays: Array notation and representation, manipulating array elements, using multi dimensional arrays, Character Arrays and Strings, Some Examples of Arrays Basic Concept Many applications require multiple data items that have common characteristics. In mathematics, we often express such groups of data items in indexed form x1, x2, x3, …, xn Why are arrays essential for some applications? Reading some numbers from the keyboard and then Sort them in ascending order. To find the mode and standard deviation etc. How such a set of data can be stored in computer and subsequently process them? Basic Concept 1-D array 3-D array 2-D array Introduction to arrays Arrays are example of structured data. In C language, arrays are referred to as structured data types. Array is a data structure which can represent a collection of data items which have the same data type (float/int/char) An array lets you declare and work with a collection of values of the same type. An array is defined as finite ordered collection of homogenous data, stored in contiguous memory locations. Types of Arrays in C In c programming language, arrays are classified into two types. They are as follows... Single Dimensional Array / One Dimensional Array Multi Dimensional Array Introduction to arrays The number of subscripts determines the dimensionality of the array. For example, x [i] refers to an element in the one-dimensional array x. Similarly, y[ i ][ j ] refers to an element in the two-dimensional array y. Pointers and arrays are closely related because all arrays make use of pointers internally. Single Dimensional Array In c programming language, single dimensional arrays are used to store list of values of same datatype. In other words, single dimensional arrays are used to store a row of values. In single dimensional array, data is stored in linear form. Single dimensional arrays are also called as one- dimensional arrays, Linear Arrays or simply 1-D Arrays. DEFINING AN ARRAY Arrays are defined in much the same manner as ordinary variables, except that each array name must be accompanied by a size specification (i.e., the number of elements). For a one-dimensional array, the size is specified by a positive integer expression, enclosed in square brackets. The expression is usually written as a positive integer constant. In general terms, a one-dimensional array definition may be expressed as storage-class data-type arrayname[ expression] ; DEFINING AN ARRAY We use the following general syntax to create an array... datatype arrayName [ size ] ; int rollNumbers ; Or datatype arrayName [ size ] = {value1, value2,...} ; int marks = { 89, 90, 76, 78, 98, 86 } ; Or datatype arrayName [ ] = {value1, value2,...} ; int marks [] = { 89, 90, 76, 78, 98, 86 } ; char studentName [] = "btechsmartclass" ; In the above example declaration, size of the array 'marks' is 6 and the size of the array 'studentName' is 16. This is because in case of character array, compiler stores one extra character called \0 (NULL) at the end. Introduction to arrays For example: if you want to store marks of 100 students, you can create an array for it. float marks; The individual array elements are distinguished from one another by the value that is assigned to a subscript (sometimes called an index). If the array contains n elements, the subscript will be an integer quantity whose values range from 0 to n-1. The size and type of arrays cannot be changed after its declaration. Each array element (i.e., each individual data item) is referred to by specifying the array name followed by one or more subscripts, with each subscript enclosed in square brackets. Introduction to arrays Homogenous data types All the data items constituting the group share the same name. int x; Individual elements are accessed by specifying the index. x x x x x is a 10-element one dimensional array DEFINING AN ARRAY Example: int x[lOO]; char text; static char message; static float n; The following kind of usage is illegal: int n; int marks[n]; How an array is stored in memory? Starting from a given memory location, the successive array elements are allocated space in consecutive memory locations. a x x+k x+2k Here, x is the starting address of the array in memory The array index starts at zero. That is, the location of a is at x Let k is the number of bytes allocated per array element Element a[i] is allocated at the memory location having address x + i*k How an array is stored in memory? Length of Array The first element in the array is numbered 0, so the last element is 1 less than the size of the array. Length = Upper Limit – Lower Limit + 1 or Length = UL – LL +1 Example: int marks UL=7 and LL=0 Length = 7 – 0 +1 = 8 It is clear that marks will store maximum or 8 elements only. 16 A Warning In C, while accessing array elements, array bounds are not checked. Example: int marks; : : marks = 75; The above assignment would not report any error; however, execution will fail. Rather, it may result in unpredictable program results. Initialization of Arrays General form type array_name[size] = { list of values }; Examples int marks = {72, 83, 65, 80, 76}; char name = {‘A’, ‘m’, ‘i’, ‘t’}; Some special cases If the number of values in the list is less than the number of elements, then the remaining elements are automatically set to zero. float total = {24.2, -12.5, 35.1}; total=24.2, total=-12.5, total=35.1, total=0, total=0 Initialization of Arrays If the size declares is less than the elements in the initialization, then the excess elements will be ignored float total = {2.2, -1.5, 3.5, 0.1, 0.2, 0.4}; The last element namely 0.4 will be ignored! The size may be omitted. In such cases the compiler automatically allocates enough space for all initialized elements. int flag[] = {1, 1, 1, 0}; char name[] = {‘A’, ‘m’, ‘i’, ‘t’}; The array cannot shrink or expand once the size is defined. Example #include int main (void){ int values; Output: int index; values = 197 values = 197; values = 32671 values = -101 values = -100; values = 547 values = 350; values = 0 values = values + values; values = 350 values = values / 10; values = -870469504 values = 21914 --values; values = 881711520 for ( index = 0; index < 10; ++index ) values = 35 printf ("values[%i] = %i\n", index, values[index]); return 0; } Example 1: Find the minimum of a Set of 10 Numbers #include int main() Array { declaration int a, i, min; for (i=0; i