Presentation is loading. Please wait.

Presentation is loading. Please wait.

Arrays and Strings. Speciality Conventional variables also called scalar variables can only store only one value. The stored value is atomic type.(It.

Similar presentations


Presentation on theme: "Arrays and Strings. Speciality Conventional variables also called scalar variables can only store only one value. The stored value is atomic type.(It."— Presentation transcript:

1 Arrays and Strings

2 Speciality Conventional variables also called scalar variables can only store only one value. The stored value is atomic type.(It can not be further sub-devided into legitimate data types. Ex : integer, floating point, double precision data.

3 Specialty Atomic data types can be represented as char ch; int n; float price; Arrays are Aggregate type of data. Also known as structured data type or as Data structure. The values can be decomposed and operations are available to retrieve and update individual values. Hence these are further called as Derived data types.

4 Why Array? Can be used to perform certain operations like To print the numbers in the reverse order. Ex: Suppose problem is to print three numbers in reverse order. The program can be written as

5 Why Array? void main() { int n1,n2,n3; printf(“\n enter the values”); scanf(“%d %d %d”,&n1,n2,n3); printf(“\nThe numbers in the reverse oreder are %d,%d,%d”,n3,n2,n1); }

6 Why Array? 1.But problem would be severe if number of inputs are more. 2.Also it is required that the number of inputs must match with that the user expects.

7 Why Array? To find out the average of numbers and to find out the number which is larger than the average. This problem requires 1.To read all the numbers and to calculate the average. 2.To read again to compare the entered numbers against the calculated average.(difficult as it involves entering values twice and accuratly with no mistakes).

8 Definition A data structure consisting of an ordered set of data values of homogeneous(same) type. The individual data elements are Ordered Fixed in size homogeneous

9 One-dimensional Array Have a single subscript or index whose value refers to the individual array element ranging from 0 to (n-1), where n is the total number of elements in the array. Declaration (Requirements) 1.The type of data it can hold (int,char, float etc). 2.The number of values it can hold i.e the maximum number of elements it can hold 3.A name.

10 One-dimensional Array Syntax : Data_type array_name [size]; Ex : int a[100]; An array element can be referenced as [ ] In the above example the array index starts at 0, and the elements are referred as a[0], a[1],a[2], … ………., a[99].

11 One-dimensional Array Each array element is called an indexed variable or subscripted variable. Internally the index is used as an offset from the array’s starting position. a[0] a[1] a[2] a[3] a[4]

12 One-dimensional Array It is mandatory to give the size of the array. We can not write int a[]; Here the compiler will generate error. Variable size declaration is also not allowed. Ex : int n; double x[n], y[n]; This kind of declaration is illegal in c and will generate error.

13 One-dimensional Array However declaration is allowed in terms of symbolic constant. Ex : #define n 20 void main() { double x[n], y[n]; }

14 One-dimensional Array Since the number of array elements can also be given by an expression int x[n+1]; is also allowed. But there must be no unknowns Note : C never checks the array index neither during compilation nor during runtime. wrong indexed elements are easily accepted but gives unpredictable results.

15 One-dimensional Array Initialization : Initializing the array elements int arr[ ] ={2,5,4,7,9}; Here the compiler will deduce the size of the array from the initialization element. This is known as Automating sizing.

16 Accessing Array Elements Single operations involving entire arrays are not permitted. Ex : if a[] and b[] are arrays of int type then we can not write a=0; or b=a; Opeartions like assignment, comparision can be carried out on element-by-element basis using loops. Ex: a[3]=10; a[3]=2*(a[0]-4);

17 Accessing Array Elements Using integer expressions the array elements can be accessed sequentially by the counter in a for loop. Ex : for (i=0;i<5;i++) total=total + a[i]; Here i has been used both as a counter and as an subscript. For adding the array elements the same procedure can also be used. Ex : for(i=0;<5;i++) a[i]=0;

18 Other allowed operations If int a[10];.Then the allowed operations on this array are To increament the i’th element. a[i]++; To add n to the i’th element. A[i]+=n; To copy the contents of i’th element to the k’th element. A[k]=a[i]; To copy the contents from array a[] to array b[]. for(i=0;i<10;i++) b[i]=a[i]; To exchange the values in a[i] and a[k]. Using third variable temp. int temp; temp=a[i]; a[i]=a[k]; a[k]=temp;

19 Storing and Printing Storing values in an array : Reading the input into an array can be done like int a[10],I; For(i=0;i<10;i++) Scanf(“%d”,&a[i]); Printing an array: For(i=0;i<10;i++) Printf(“%d”,a[i]);

20 Internal representation of Arrays in c An array is implemented as a single block of memory. The element 0 occupies the first slot in the block. Thus the subscripts are closely related to actual memory addresses and to the notation of pointers. For an array of length ‘len’ of int type the compiler will allocate len*sizeof(int) bytes. Thus for an array int a[10] it allocates 20 bytes(10*sizeof(int)) suppose say starting from 64789. Then the compiler accesses the value stored in a[5] at memory location starting with byte 64789+(5*2).

21 References to elements outside the array element If an element outside the boundary is accessed the following events may happen. Produce some sort of error. Program may crash. Produce incorrect output. Even produce correct output. It all depends on what information is being stored in the memory locations surrounding the block of memory reserved for the array.

22 Dynamic Memory allocation A program is allocated a small area of memory called the stack. This memory is used by the variables in the program. Disadvantages A variable looses it’s scope when the program goes to the next closing curly bracket it is destroyed and the allocated memory is freed up. So it is needed to be careful when getting functions to fill in arrays. The size of memory allocated from the stack must be fixed at compile time. It is not possible to declare a variable for the size as the compiler does not know how big the array will be. Ex : int size; scanf(“%d”,&size); int a[size]; This code will not work.


Download ppt "Arrays and Strings. Speciality Conventional variables also called scalar variables can only store only one value. The stored value is atomic type.(It."

Similar presentations


Ads by Google