blogger Indonesia

Follow judhyns blog

Arrays


ARRAYS

Introduction

An array is a data structure used to process multiple elements with the same data type when a number of such elements are known. You would use an array when, for example, you want to find out the average grades of a class based on the grades of 50 students in the class. Here you cannot define 50 variables and add their grades. This is not practical. Using an array, you can store grades of 50 students in one entity, say grades, and you can access each entity by using subscript as grades[1], grades[2]. Thus you have to define the array of grades of the float data type and a size of 50. An array is a composite data structure; that means it had to be constructed from basic data types such as array integers.

Program

#include  main() {     int a[5];  \\A     for(int i = 0;i<5;i++)     {        a[i]=i;\\B     }     printarr(a); } void printarr(int a[]) {     for(int i = 0;i<5;i++)     {         printf("value in array %d\n",a[i]);     } } 

Explanation

  1. Statement A defines an array of integers. The array is of the size 5—that means you can store 5 integers.

  2. Array elements are referred to using subscript; the lowest subscript is always 0 and the highest subscript is (size –1). If you refer to an array element by using an out-of-range subscript, you will get an error. You can refer to any element as a[0], a[1], a[2], etc.

  3. Generally, you can use a for loop for processing an array. For the array, consecutive memory locations are allocated and the size of each element is same.

  4. The array name, for example, a, is a pointer constant, and you can pass the array name to the function and manipulate array elements in the function. An array is always processed element by element.

  5. When defining the array, the size should be known.


Note

The array subscript has the highest precedence among all operators thus a[1] * a[2] gives the multiplication of array elements at position 1 and position 2.

Points to Remember

  1. An array is a composite data structure in which you can store multiple values. Array elements are accessed using subscript.

  2. The subscript operator has the highest precedence. Thus if you write a[2]++,it increments the value at location 2 in the array.

  3. The valid range of subscript is 0 to size 1.

No comments:

feed

PR Check

activesearchresults

judhyn's blog

 

http://www.judhyn.blogspot.com | |