0% found this document useful (0 votes)
9 views

Arrays

The document discusses one-dimensional arrays in C. It defines an array as a fixed size collection of elements of the same data type. Each element is identified by an index from 0 to size-1. Arrays allow storing, processing, and manipulating a set of values more efficiently than individual variables. The key points are: 1) Arrays are declared with a type, name, and size. 2) Elements are accessed using indexes in brackets after the name. 3) Arrays can be initialized with a list of values at declaration. 4) Loops are commonly used to input/output all elements of an array.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

Arrays

The document discusses one-dimensional arrays in C. It defines an array as a fixed size collection of elements of the same data type. Each element is identified by an index from 0 to size-1. Arrays allow storing, processing, and manipulating a set of values more efficiently than individual variables. The key points are: 1) Arrays are declared with a type, name, and size. 2) Elements are accessed using indexes in brackets after the name. 3) Arrays can be initialized with a list of values at declaration. 4) Loops are commonly used to input/output all elements of an array.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

62

ONE-DIMENSIONAL ARRAY

INTRODUCTION TO ARRAY
A variable of fundamental data types, int, float, char, double, can store only one value at a time. Thus if it is required to store a set
of values, then we have to use a set of variable. But the storing, processing, and manipulation of a set of variables is not efficient. C
supports a derived data type, called Array, that can be used to handle large volume of data and facilitates efficient storing, processing,
and manipulation of data item.

DATA TYPES

Fundamental Derived Data User-Defined


Types Types Data Types

- Integer - Pointers - Structures


- Floating - Functions - Union
- Character - Arrays - Enumeration
- Double
- Void

Array is a fixed size collection of data items of the same data type. The individual data item of an array is known as element of an
array.Each element of an array share the same name but they are identified by a uniqueindex/subscript. Hence an array is also known as
Indexed/subscript variable.

An array can be of the following types:


1. One-Dimensional Array
2. Two-Dimensional Array
3. Multi-Dimensional Array.

ONE-DIMENSIONAL ARRAY :
In a 1D-Array, each element of an array is identified by a single subscript. The elements of the array are logically organized in a
single row of data items.

Declaration of 1D-Array :
Syntax:- datatypearrayname[size];
 Datatype of an array specifies the datatype of each element of an array.
 Size of an array specifies the maximumnumber of elements of an array. The size should always be an integer constant.

Example: intSalary[20]; /* Declares an array salary of 20 elements each of type int */


floatnum[30]; /* Declare an array num of 30 elements each of type float */.

When an array is declared, the computer reserves memory location for all the elements of an array at the time of compilation. Hence
the size should be integer constant, but not a variable.

Example : When an array num is declared of size 10, as: int x[10];
The computer reserves 10 memory locations at the time of compilation as follows:
63

1050 1052 1054 1056 1058 1060 1062 1064 1066 1068

x[0] x[1] x[2] x[3] x[4] x[5] x[6] x[7] x[8] x[9]

Thus each element of an array have the same name but they differ in their index/subscript. The index of the elements of the array
starts from 0 and index of last element of the array is size-1. The index/subscript of first element x[0]=0. The index/subscript of
second element x[1]=1.The index/subscript of the first element of an array is called lower bound. The index/subscript of the last
element of an array is called Upper bound.

Example : In C, Lower bound of an array=0.


Upper bound of an array=size-1.

In C, there is no check on bounds of an array. If for an array num[5] having valid elements num[0], num[1], num[2], num[3] and
num[4], we tries to access the elements beyond these subscript like num[–2], num[8], the compiler will generate no error message. Thus
the programmer should take care about the bound checking of an array.

ACCESSING ELEMENTS OF 1D-ARRAY :


Once an array is declared, we can access the individual element of an array, by means of its subscript/index. All the elements of an
array can be processed through a for loop with the index of the elements as the loop counter.
for(i=0; i<n; i++)
{
/* x[i] is the current element of the array to be proceesed */
}
 Inputting The Elements of an Array :
Once the Array is declared, it is required to store the values of the elements of the array from the keyboard.
Let us Input the values of the array with 10 elements declared as:
int x[10];
printf(“ Enter The Elements of the Array “);
for(i=0; i<10; i++)
scanf(“%d”, &x[i]);

But if the size of the array is not mentioned in the program, then storing data into array requires:
1. Inputting the Number of Elements of The Array
2. Inputting the Value of Each Element of the Array

int x[30]; /* Array x is declared with maximum size 30 */


printf(“Enter the Number of Elements of the Array “);
scanf(“%d”,&n);
printf(“ Enter The Elements of the Array “);
for(i=0; i<n; i++)
scanf(“%d”, &x[i]);
64
 Printing the Array element :
The Elements of the Array can be displayed on the output screen as follows:
printf(“\nThe Elements of the Array are “);
for(i=0; i<n; i++)
printf("%d \t",x[i]);

Sample Program: A Program to Input and Display The elements of an Array


#include<stdio.h>
#include<conio.h>
void main()
{
int num[40];
int i,n;
clrscr();
printf(“Enter the Number of Elements of Array “);
scanf(“%d”,&n);
printf(“Enter the Elements of Array “);
for(i=0;i<n;i++)
scanf(“%d”,&num[i]);
printf(“The Elements of Array are “);
for(i=0;i<n;i++)
printf(“%d\t”,num[i]);
getch();
}

Sample Program: A Program to Compute the Sum of a Set of 10 Elements


#include<stdio.h>
#include<conio.h>
void main()
{
int num[10];
int i,sum=0;
clrscr();
printf(“Enter the Elements of Array “);
for(i=0;i<10;i++)
scanf(“%d”,&num[i]);
for(i=0;i<10;i++)
sum = sum + num[i];
printf(“The Elements of Array are “);
for(i=0;i<n;i++)
printf(“%d\t”,num[i]);
printf(“\nThe Sum of Elements = %d “,sum);
getch();
}

INITIALIZATION OF 1D-ARRAY :
At the time of declaration of the array, we can assign an initial value to the elements of an array.
Syntax : datatype arrayname[size]={list of values};

Example : int x[10]={15,20,30,18,12,-78, 90,12,54,32};


1050 1052 1054 1056 1058 1060 1062 1064 1066 1068
15 20 30 18 12 -78 90 12 54 32
x[0] x[1] x[2] x[3] x[4] x[5] x[6] x[7] x[8] x[9]

If we are initializing an array, then it is optional to specify the size of an array. The compiler allocate memory space for all the
initialized elements.
Example : int x[ ]={ 10,20,30,40} /* Here size of the array become the no. of initial values. */
65
If the no. of initial values is less than the size of the array, then remaining elements are by default initialized to 0.

Example : int x[10]={10,20};

1050 1052 1054 1056 1058 1060 1062 1064 1066 1068
10 20 0 0 0 0 0 0 0 0
x[0] x[1] x[2] x[3] x[4] x[5] x[6] x[7] x[8] x[9]

Important Tips:
 i=3; In this case, element num[3]=80 and then i will become 4.
num[i++]=80;

 i=3 In this case i will become 4 and then num[4]=80.


num[++i]=80;

 i=3;
num[i]++; /* In this case, the element num[3] will increment by 1. */

 i=0;
int num[4]={ 10,20,30,40};
p=num[i++] + num[++i] + num[i++]; /* p = num[1] + num[1] + num[1] = 20 + 20 +20 =60 */
printf(“%d\n”,p);
for(i=0; i<4; i++)
printf(“%d”, num[i]);
Output : 60 10 20 30 40

ARRAYS AND POINTER :

The base address of the array is the memory address of the first element (0 th index) of the array. All the elements of an array are
stored in contiguous memory locations. The compiler defines the name of the array as a constant pointer to the first element of the array .
Example : int x[5];
In the above example, x is the constant pointer to the x[0] element of array, i.e., x= &x[0].

To declare a pointer to an array, we can make pointer point to the x[0] elements by the following assignment:
int *ptr;
ptr = x;

(ptr + i) gives the address of x[i] element. In other words (ptr + i) = &x[i]
*(ptr + i) gives the value of x[i] element. In other words *(ptr + i) = x[i]

Sample Program: A Program to Compute the Sum of the Elements of Array Through Pointer
#include<stdio.h>
#include<conio.h>
void main()
{
int x[15];
int *ptr = x;
66
int i,n,sum=0;
/* Input The Elements of The Array */
printf("Enter the No. of Elements of Array ");
scanf("%d",&n);
printf(“\nEnter the Elements of the Array “);
for( i=0; i<n; i++)
{
scanf(“%d”, ptr);
ptr++;
}
ptr=x;
/* Compute the sum of the set of numbers */
for(i=0;i<n;i++)
{
sum=sum + *ptr;
ptr++;
}
printf("The sum of the elements = %d",sum);
}

ARRAY & FUNCTION


It is possible to pass the values of all the elements of an array to a function. To pass an entire array to a called function, it is required to
pass the following arguments to called function:
1. The Name of the Array ( Base Address of the Array)
2. The Size of the Array

The called function is defined with the array as formal argument as follow:
ReturnType FunctionName(int arrayname[], int size)
{
/* Body of the Function that can access the Input Array */
}

Sample Program: A Program to compute the sum of a set of numbers using function.
#include<stdio.h>
#include<conio.h>
/* Function Prototype */
void sum(int[],int);

/* Definition of a function with the array as argument */


void sum(int x[ ], int n)
{
int s=0,i;
for(i=0;i<n;i++)
s=s+x[i];
printf("The result = %d",s);
}

void main()
{
int x[20];
/* Accept the Array */
printf("Enter the No. of Elements of the Array ");
scanf("%d",&n);
printf(“Enter the Elements of the Array “);
for(i=0;i<n;i++)
scanf("%d",&x[i]);
/* Call the function sum() */
sum(x,n);
getch();
}
67

You might also like