Sie sind auf Seite 1von 28

ARRAYS in C

Need of Arrays

What is the output of the following program?


main( )
{
int x ;
x=5;
x = 10 ;
printf ( "\nx = %d", x ) ;
}
Why so?
Because when a value 10 is assigned to x, the earlier value of x, i.e. 5, is lost.
Thus, ordinary variables (the ones which we have used so far) are capable of holding
only one value at a time. However, there are situations in which we would want to
store more than one value at a time in a single variable.

Example of Arrays
Suppose we wish to arrange the percentage marks
obtained by 100 students in ascending order. In such a
case we have two options to store these marks in
memory:
Construct 100 variables to store percentage marks obtained
by 100 different students, i.e. each variable containing one
students marks.
Construct one variable (called array or subscripted variable)
capable of storing or holding all the hundred values.

Definition

An Array is a collection of similar elements. These similar


elements could be all ints, or all floats, or all chars, etc. ex: p,
9, =.
Usually, the array of characters is called a string ex: Hello
All elements of an array must be of the same type. i.e. we cannot
have an array of 10 numbers, of which 5 are ints and 5 are floats.
EXAMPLE

Assume the following group of numbers, which represent percentage marks


obtained by five students.
Int per[5] = { 48, 88, 34, 23, 96 } ;

1-Dimensional Arrays

Array Declaration Syntax:

typename variablename[size];
typename is any Data type
variablename is any legal variable name
size is a number indicating the size of the array

For example
int a[10];

2002

2012

2020

A[0]

A[5]

A[9]

Declares an array of ints with subscripts ranging from


0 to 9, as array elements are numbered starting from 0.
There
10*sizeof(int)
bytes
memory
reserved
a[0]area[1]
a[2] a[3] a[4]
a[5]ofa[6]
a[7] a[8]
a[9] for
this array.
We can use a[0]=10; x=a[2]; a[3]=a[2]; etc.

Initializing Arrays

Initialization of arrays can be done by a comma separated list


while declaring it.
For example:
int array [4] = { 100, 200, 300, 400 };

This is equivalent to:

Manav Rachna College of Engg

int array [4];


array[0] = 100;
array[1] = 200;
array[2] = 300;
array[3] = 400;

You can also let the compiler figure out the array size for you:
int array[] = { 100, 200, 300, 400};

Arrays
An array is a collection of similar elements.
The first element in the array is numbered 0, so
the last element is 1 less than the size of the
array.
An array is also called as a subscripted variable.
Before using an array, its type and dimension
must be declared.
Elements of an array are stored in contiguous
memory locations.

A Simple Program Using Array


Write a program to find average marks obtained by a class of 30
students in a test.
main( )
{
int avg, sum = 0, i marks[30] ; /* array declaration */
for ( i = 0 ; i <= 29 ; i++ )
{ printf ( "\nEnter marks " ) ;
scanf ( "%d", &marks[i] ) ; /* store data in array */
}
for ( i = 0 ; i <= 29 ; i++ )
sum = sum + marks[i] ; /* read data from an array*/
avg = sum / 30 ;
printf ( "\nAverage marks = %d", avg ) ;

Program to count the no of positive and negative numbers


main( )
{
int a[50],n,count_neg=0,count_pos=0,i;
printf(Enter the size of the arrayn);
scanf(%d,&n); // Input size of Array
printf(Enter the elements of the arrayn);
for(i=0;i < n;i++)
scanf(%d,&a[i]); //Input array elements
for(I=0;i < n;i++)
{ if(a[i] < 0)
count_neg++;
else if (a[i] > 0)
count_pos++;
}
printf(There are %d negative nos in the arrayn,count_neg);
printf(There are %d positive nos in the arrayn,count_pos);
}

Multidimensional Arrays
Arrays in C can have virtually as many
dimensions as you want.
Definition is accomplished by adding additional
subscripts when it is defined.
For example:
int a [4] [3] ; //2- dimensional Array
defines a two dimensional array
a is an array with 4 elements, each of int [3];

In memory:

a[0][0] a[0][1] A[0][2] a[1][0] a[1][1] a[1][2] A[2][0] A[2][1]a[2][2] A[3][0] A[3][1]

a[3][2]

Initializing Multidimensional Arrays


The following initializes a[4][3]:
int a[4] [3] = { {1, 2, 3} , { 4, 5, 6} , {7, 8, 9} , {10, 11, 12} };

Also can be done by:

a[0][0] = 1;
a[0][1] = 2;
a[0][2] = 3;
a[1][0] = 4;
...
a[3][2] = 12;

Manav Rachna College of Engg

int a[4] [3] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 };


is equivalent to

Multidimensional arrays
How to interpret a declaration like:
int d[2][4];

This is an array with two elements:


Each element is an array of four int values

The elements are laid out sequentially in


memory, just like a one-dimensional array
Row-major order: the elements of the rightmost
subscript are stored contiguously
(int)

(int)

(int)

(int)

(int)

(int)

d[0][0]

d[0][1]

d[0][2]

d[0][3]

d[1][0]

d[1][1]

d[0]

(int)

d[1][2]

(int)

d[1][3]
d[1]

/* Example program to accept and display a matrix */


main()

{
int a[10][10], i,j;
printf(enter the element of
the matrix a of order 3*4\n);
for(i=0;i < 3;i++)
{ for(j=0;j < 4;j++)
scanf(%d,&a[i][j]);
printf( matrix a \n);
}

for(i=0;i < 3;i++)


{ for(j=0;j <4++)
printf(%d/t,a[i][j]);
}

Output:

enter the element of the


matrix a of order 3*4
1 2 3 4 5 6 7 8 9 10 11 12

matrix a

/* Example program to add two matrices & store the results in the 3rd matrix */
main()

{
int a[10][10],b[10][10],c[10]
[10],i,j,m,n,p,q;
printf(enter the order of the
matrixn);
scanf(%d%d,&p,&q); //Input
row n column
if(m==p && n==q)
{
printf(matrix can be
added\n);
printf(enter the elements
of the
matrix a);
for(i=0;i < m;i++)
for(j=0;j < n;j++)
scanf(%d,&a[i][j]);
/*enter
elements of matrix*/
printf(enter the elements of
the
matrix b);

for(i=0;i < p;i++)


for(j=0;j < q;j++)
scanf(%d,&b[i][j]);
//elements of
matrix b
printf(the sum of the matrix a
and b is);
for(i=0;i < m;i++)
for(j=0;j < n;j++)
c[i][j]=a[i][j]+b[i][j];
//matrix addition

An Example -- Sorting
for(x=0; x<n; x++)
{
for(y=0; y<n-x; y++)
{
if(array[y]>array[y+1])
{
temp = array[y+1];
array[y+1] = array[y];
array[y] = temp;
}
}
}
printf("\n\n Finally sorted array is
:"); for( i=0;i<=n-1;i++)
printf( %d ",a[i]);

main()
{
int a[100],n,I,x,y,temp;
printf("\n\n Enter integer valu
e for total no.s of elements to
be sorted:
scanf("%d",&n);
for( i=0;i<=n-1;i++)
{ printf("\n\n Enter integer value fo
r element no.%d : ", i+1);
scanf("%d",&a[i]);
}
}

EXAMPLE-SEARCHING AN ELEMENT IN AN ARRAY


main()
{
int a[10],i,n,target;

int found,i,pos;
printf("enter size" );
scanf("%d",&n);
for (i=0;i<n;i++)
{
printf("enter array element ");
scanf("%d",&a[i]);
}
printf("enter the target element ");
scanf("%d",&x);

found=0;
found*/

/*1-found

0-not

for (i=0;i<n;i++)
if (a[i] == x)
{
found=1;
pos=i;
break;
}
if (found)
printf("%d found at position
%d\n",x,pos);
else
printf("%d not found\n",x);
}

TIME TO THINK????
Array elements are stored in
(a) Random memory location
(b) Sequential memory locations
(c) Scattered memory locations
(d) Direct memory locations

Two dimensional array elements are stored


(a) system dependent
(b) in row major order
(c) complier dependent
(d) in column major order

Manav Rachna College of Engg

If the size of an integer array is n, the number of elements that can be stored in it is
(a) n-1
(b) n-2
(c) n+1
(d) N

Dynamic memory allocation in array results in


(a) allocation of memory at runtime
(b) allocation of memory at debugging time
(c) allocation of memory at compile time
(d) All

Under which of the following conditions, the size of the array need not be
specified?
(a) when initialization is a part of definition
(b) when the compiler is smart
(c) when it is a declaration
(d) when it is a formal parameter location of memory at file saving time

Manav Rachna College of Engg

One dimensional array is known as


(a) matrix
(b) vector
(c) set
(d) Table

Das könnte Ihnen auch gefallen