Presentation is loading. Please wait.

# Arrays and Strings.

## Presentation on theme: "Arrays and Strings."— Presentation transcript:

Arrays and Strings

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.

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.

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

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); }

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

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

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

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) The type of data it can hold (int,char, float etc). The number of values it can hold i.e the maximum number of elements it can hold A name.

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

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]

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.

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

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.

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.

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);

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;

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;

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: Printf(“%d”,a[i]);

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 Then the compiler accesses the value stored in a[5] at memory location starting with byte (5*2) .

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.

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."

Similar presentations

Ads by Google