One-Dimensional Arrays or 1D


One-Dimensional Arrays or 1D

Definition:- 

A list of values of the same data type can be stored under one variable name using only one subscript  and such a variable is known as single subscripted value or one-dimensional array.

The subscript of the array can be integer constant, integer variable or integer expression that yields integer result.

Declaration


Like any other variable, arrays must be declared before they are used.  The general form of array declaration is

Syntax:          
datatype variable-name[size];

The datatype specifies the type of element that will be contained in the array, such as int, float or char.
The variable-name can any valid C variable name and it specifies the name of the array.
The size indicates the maximum number of elements that can be stored inside the array.
The subscript must start with 0 and its range is from 0 to size-1.

In Declaration, the size must be either integer constant or symbolic constant.

Eg:-
int num[5];

size of array num is 10 bytes because num has 5 integers and each int has 2 bytes so total of 5*2=10 bytes are allocated.

Declares an array num which can store a maximum of 5 integer numbers and the computer reserves five continuous storage locations as shown below.
 Memory allocation locations depends on the particular size of the datatype.

                                                                                   

Sample program to print the memory allocated for arrays of integer type.

#include<stdio.h>
#include<conio.h>
void main()
{
 int number[5],i;
 clrscr();
 for(i=0;i<5;i++)
 {
  printf("\n number[%d] stored at %u",i,&number[i]);
 }
 getch();
}
Output

 number[0] stored at 65516
 number[1] stored at 65518
 number[2] stored at 65520
 number[3] stored at 65522
 number[4] stored at 65524

* Here number[0] is stored at 65516 and number[1] is stored at 65518 because int occupies 2 bytes of memory, number[1] is stored at 65518 rather than 
stored at 65517.

Initialization

After an array is declared, its elements must be initialized otherwise garbage values will be stored.
An Array can be initialized in two ways.

  1. Compile time initialization
  2. Run time initialization

Compile time initialization

Initialize the elements when the array is declared itself.

Syntax:
datatype variable-name[size]={list of values};

The values in the list are separated by commas.



Eg:-

1)         int a[4]={4,0,-10,42};

Here the size of the array and the list of values are equal.
           
This would cause the array number to store the values as shown below.

  


2)         int a[4]={0};

Here all array elements will be assigned to zero.

3)         int a[4]={4,-10};

Here the size of the array is greater than the list of values. Then only that many values will be initialized and remaining 
elements will be set to zero automatically.

 

3)     char name[]={‘r’,’a’,’j’,’u’};

 Here we are not declaring the size of array name and it has 4 values so the size of array name is 4 i.e system automatically converts into name[4]

4)        int a[4]={4,0,7,3,1,8,9};

Here the size of the array is less than the list of values. Now an error will occur saying “Too many initializers”.

Sample program to print array elements which are allocated at compile time.

#include<stdio.h>
#include<conio.h>
void main()
{
 int a[4]={4,0,-10,42},i;
clrscr();
 for(i=0;i<4;i++)
 {
  printf("\n a[%d]=%d",i,a[i]);
 }
  getch();
}
Output
 a[0]=4
 a[1]=0
 a[2]=-10
 a[3]=42

 Run time initialization

An array can be initialized using scanf() function through keyboard.

Sample program to print array elements which are allocated at Run time.

#include<stdio.h>#include<conio.h>void main(){ int a[4],i; clrscr(); printf("\n Enter array elements: "); for(i=0;i<4;i++)  scanf("%d",&a[i]); printf(" Array elements: "); for(i=0;i<4;i++)  printf("\n a[%d]=%d",i,a[i]); getch();}

Output

 

 Enter array elements: 42

-24  0  63

 Array elements:

 a[0]=42

 a[1]=-24

 a[2]=0

 a[3]=63


Accessing Array elements


       Once an array is declared, the individual elements of the array can be referred by the use of subscript along with array name.

Subscript specifies the element position in the array. Subscript starts at 0. i.e. first number is started at  position 0, second number is started at position 1 etc.

Syntax   arrayname[i];

             Here variable i refers to the ith element of the array.
Ex:    int  abc[5]= {1,4,5,6,7}
       abc[2]  refers value 5.

Entering data into an array

            int num [10];
            for (i=0; i<10; i++)
               scanf (“%d”, & num [i]);

Reading data from an array


            sum=0;
            for (i=0; i<10; i++)
                        sum = sum + a [i];


Sample program which shows the array elements usage just like ordinary variables.

#include<stdio.h>
#include<conio.h>
void main()
{
 int a[4]={1,2,3,4},i;
 a[0]=a[0]*6;
 a[2]=a[3]/a[1];
 a[1]=a[1]+4;
 for(i=0;i<4;i++)
  printf("\t a[%d]=%d",i,a[i]);
}

Output

 a[0]=6             a[1]=6              a[2]=2              a[3]=4

Ex:- Write a program to declare 10 marks and assign values and find out average of 10 marks.

#include<stdio.h>
#include<conio.h>
void main()
{
 int m[10],sum=0;
float avg;
printf(“Enter 10 Marks: ); 
for(i=0;i<10;i++)
{
scanf(“%d”,&m[i]);
sum=sum+m[i];
}
avg=(float)sum/10;
printf(“Avg=%f”,avg);

}

No comments:

Post a Comment