Академический Документы
Профессиональный Документы
Культура Документы
LAB # 09
INTRODUCTION TO ARRAY
OBJECTIVE
To study arrays in C language.
THEORY
An array is a collection of data items, all of the same type, accessed using a common
name.An array is used to store a collection of data, but it is often more useful to think
of an array as a collection of variables of the same type.Instead of declaring individual
variables, such as number0, number1, ..., and number99, you declare one array variable
such as numbers and use numbers[0], numbers[1], and ..., numbers[99] to represent
individual variables. A specific element in an array is accessed by an index.
All arrays consist of contiguous memory locations. The lowest address corresponds to
the first element and the highest address to the last element.
Declaring Arrays
To declare an array in C, a programmer specifies the type of the elements and the
number of elements required by an array as follows.
type arrayName[arraySize];
Initializing Arrays
An array can either be initialized one by one or using a single statement as follows:
double balance[5]={1000.0,2.0,3.4,7.0,50.0};
The number of values between braces { } cannot be larger than the number of elements
that can be declared for the array between square brackets [ ].
If size of an array is not defined , an array just big enough to hold the initialization is
created. Therefore, the statement will look as follows:
double balance[]={1000.0,2.0,3.4,7.0,50.0};
balance[4]=50.0;
The above statement assigns the 5th element in the array with a value of 50.0. All arrays
have 0 as the index of their first element which is also called the base index and the last
index of an array will be total size of the array minus 1. Shown below is the pictorial
representation of the array.
double salary=balance[9];
The above statement will take the 10th element from the array and assign the value to
salary variable. The following example Shows how to use all the three above
mentioned concepts viz. declaration, assignment, and accessing arrays
Example:
#include<stdio.h>
main()
{
intn[10]; // n is an array of 10 integers
inti,j;
for(j=0;j<10;j++)
{
printf(“Element[%d]=%d\n”,j,n[j]);
}
}
Output:
Element[0]=100
Element[1]=101
Element[2]=102
Element[3]=103
Element[4]=104
Element[5]=105
Element[6]=106
Element[7]=107
Element[8]=108
Element[9]=109
main( )
{
inti ;
intmarks[ ] = { 55, 65, 75, 56, 78, 78, 90 } ;
for ( i = 0 ; i<= 6 ; i++ )
display ( marks[i] ) ;
}
display ( int m )
{
printf( "%d ", m ) ;
}
Output:
55 65 75 56 78 78 90
main( )
{
inti ;
intmarks[ ] = { 55, 65, 75, 56, 78, 78, 90 };
for ( i = 0 ; i<= 6 ; i++ )
disp( &marks[i] ) ;
}
disp( int *n )
{
printf( "%d ", *n ) ;
}
Output:
55 65 75 56 78 78 90
EXERCISE
1. Code
int char mixed[100] ;
main( )
{
inta[10], i ;
for ( i = 1 ; i<= 10 ; i++ )
{
scanf( "%d", a[i] ) ;
printf( "%d", a[i] ) ;
}
}
2. Code
main( )
{
intsize ;
scanf( "%d", &size ) ;
intarr[size] ;
for ( i = 1 ; i<= size ; i++ )
{
scanf( "%d", arr[i] ) ;
printf( "%d", arr[i] ) ;
}
}
3. Code
main( )
{
inti, a = 2, b = 3 ;
intarr[ 2 + 3 ] ;
for ( i = 0 ; i<a+b ; i++ )
{
scanf( "%d", &arr[i] ) ;
printf( "\n%d", arr[i] ) ;
}
}
1. Code:
main( )
{
intnum[26], temp ;
num[0] = 100 ;
num[25] = 200 ;
temp = num[25] ;
num[25] = num[0] ;
num[0] = temp ;
printf( "\n%d %d", num[0], num[25] ) ;
}
Output:
2. Code
main( )
{
intarray[26], i ;
for ( i = 0 ; i<= 25 ; i++ )
{
array[i] = 'A' + i ;
printf( "\n%d %c", array[i], array[i] ) ;
}
}
Output
3. Code
main( )
{
inta[5], i, b = 16 ;
for ( i = 0 ; i< 5 ; i++ )
a[i] = 2 * i ;
f ( a, b ) ;
for ( i = 0 ; i< 5 ; i++ )
printf( "\n%d", a[i] ) ;
printf( "\n%d", b ) ;
}
Output
No output
1. Fifteen numbers are entered from the keyboard into an array. The number to be
searched is entered through thekeyboard by the user. Write a program to find if the
number tobe searched is present in the array.
Source Code:
#include<stdio.h>
main()
{
int arr[15],i,a;
for(i=0;i<15;i++)
scanf("%d",&arr[i]);
printf("Enter the number you want to search in the array ");
scanf("%d",&a);
for(i=0;i<15;i++)
{
if(a==arr[i])
{
printf("\nThe number is present in the array ");
break;
}
}
}
Output:
2. Fifteen numbers are entered from the keyboard into an array. Write a program to
find the largest number in the array and its index number.
Source Code:
#include<stdio.h>
main()
{
aaadadadadddadadaa
int large[15],i,index_no;
scanf("%d",&large[0]);
for(i=1;i<=14;i++)
{
scanf("%d",&large[i]);
if(large[0]<large[i])
{
large[0]=large[i];
index_no=i;
}
}
printf("\nThe largest number is %d and index number is %d ",large[0],index_no);
}
Output: