Single dimensional arrays - Syntax and Examples with explanation | Coding classpoint

Single dimensional arrays and functions





Array subscript always start from zero which is known as lower bound and upper value is known as upper bound and the last subscript value is one less than the size of array. Subscript can be an expression i.e. integer value. It can be any integer, integer constant, integer variable, integer expression or return value from functional call that yield integer value. 

So if i & j are not variable then the valid subscript are:

                  ar [i*7],ar[i*i],ar[i++],ar[3];

The array elements are standing in continuous memory locations and the amount of storage required for hold the element depend in its size & type.

Total size in byte for 1D array is:

Total bytes=size of (data type) * size of array

Example : if an array declared is: 
                 int [20]; 
                 Total byte= 2 * 20 =40 byte.

ACCESSING OF ARRAY ELEMENT:

/*Write a program to input values into an array and display them*/

#include 
int main() 
{
 int arr[5],i; 
for(i=0;i<5;i++) 
printf(“enter a value for arr[%d] \n”,i); 
scanf(“%d”,&arr[i]); 
} 
printf(“the array elements are: \n”); 
for (i=0;i<5;i++) 
printf(“%d\t”,arr[i]); 
return 0; 

OUTPUT: 

Enter a value for arr[0] = 12 
Enter a value for arr[1] =45 
Enter a value for arr[3] =98 
Enter a value for arr[4] =21 
Enter a value for arr[2] =59 

The array elements are 12 45 59 98 21 

Example: From the above example value stored in an array are and occupy its memory addresses 2000, 2002, 2004, 2006, 2008 respectively.

                     a[0]=12, a[1]=45, a[2]=59, a[3]=98, a[4]=21 


/* Write a program to add 10 array elements */

#include 
 void main() 
{
 int i ; 
int arr [10]; 
int sum=o; 
for (i=0; i<=9; i++) 
printf (“enter the %d element \n”, i+1);
scanf (“%d”, &arr[i]); 
for (i=0; i<=9; i++) 
{ sum = sum + a[i]; 
printf (“the sum of 10 array elements is %d”, sum); 

OUTPUT: 
Enter a value for arr[0] =5 
Enter a value for arr[1] =10
Enter a value for arr[2] =15
Enter a value for arr[3] =20
Enter a value for arr[4] =25
Enter a value for arr[5] =30 
Enter a value for arr[6] =35 
Enter a value for arr[7] =40 
Enter a value for arr[8] =45 
Enter a value for arr[9] =50 
Sum = 275

while initializing a single dimensional array, it is optional to specify the size of array. If the size is omitted during initialization then the compiler assumes the size of array equal to the number of initializers.

For example:

         int marks[]={99,78,50,45,67,89};

If during the initialization of the number the initializers is less then size of array, then all the remaining elements of array are assigned value zero .

For example: 

       int marks[5]={99,78};

Here the size of the array is 5 while there are only two initializers so After this initialization, the value of the rest elements are automatically occupied by zeros such as 

               Marks[0]=99 , Marks[1]=78 , Marks[2]=0, Marks[3]=0, Marks[4]=0 

Again if we initialize an array like 

int array[100]={0}; 

Then the all the element of the array will be initialized to zero. If the number of initializers is more than the size given in brackets then the compiler will show an error. 

For example: 

int arr[5]={1,2,3,4,5,6,7,8};//error 

we cannot copy all the elements of an array to another array by simply assigning it to the other array like, by initializing or declaring as

int a[5] ={1,2,3,4,5}; 
int b[5]; 
b=a;//not valid

(note:-here we will have to copy all the elements of array one by one, using for loop.)

Post a Comment

0 Comments
* Please Don't Spam Here. All the Comments are Reviewed by Admin.