Вы находитесь на странице: 1из 10

Programming Fundamentals (SWE-102) SSUET/QR/114

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];

This is called a single-dimensional array. The arraySize must be an integer constant


greater than zero and type can be any valid C data type.

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 [ ].

Lab 09:Introduction to Array 1


Programming Fundamentals (SWE-102) SSUET/QR/114

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};

Following is an example to assign a single element of the array:

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.

Accessing Array Elements


An element is accessed by indexing the array name. This is done by placing the index
of the element within square brackets after the name of the array. For example:

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;

//initialize elements of array n to 0


for(i=0;i<10;i++)
{
n[i]=i+100; //set element at location i to i+100
}
//output each array element’s value

for(j=0;j<10;j++)
{
printf(“Element[%d]=%d\n”,j,n[j]);
}
}

Lab 09:Introduction to Array 2


Programming Fundamentals (SWE-102) SSUET/QR/114

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

Passing Array Elements to a Function


Array elements can be passed to a function by calling the function by value, or by
reference. In the call by value we pass values of array elements to the function, whereas
in the call by reference we pass addresses of array elements to the function. These two
callsare illustrated below:

Example (Call by value):


#include<stdio.h>

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

Lab 09:Introduction to Array 3


Programming Fundamentals (SWE-102) SSUET/QR/114

Example (Call by refrence):


#include<stdio.h>

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

Lab 09:Introduction to Array 4


Programming Fundamentals (SWE-102) SSUET/QR/114

EXERCISE

A. Point out the errors, if any, in the following C statements.

1. Code
int char mixed[100] ;
main( )
{
inta[10], i ;
for ( i = 1 ; i<= 10 ; i++ )
{
scanf( "%d", a[i] ) ;
printf( "%d", a[i] ) ;
}
}

Error:Array Declaration before main().


Wrong declaration of array ‘a’.
No ‘&’ in scanf().

2. Code
main( )
{
intsize ;
scanf( "%d", &size ) ;
intarr[size] ;
for ( i = 1 ; i<= size ; i++ )
{
scanf( "%d", arr[i] ) ;
printf( "%d", arr[i] ) ;
}
}

Error:No spaces in variable declarations.


No ‘&’ in scanf().

Lab 09:Introduction to Array 5


Programming Fundamentals (SWE-102) SSUET/QR/114

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] ) ;
}
}

Error:Wrong variable declarations.

B. What would be the output of the following programs:

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:

Lab 09:Introduction to Array 6


Programming Fundamentals (SWE-102) SSUET/QR/114

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 ) ;
}

Lab 09:Introduction to Array 7


Programming Fundamentals (SWE-102) SSUET/QR/114

f ( int *x, int y )


{
inti ;
for ( i = 0 ; i< 5 ; i++ )
*(x + i) += 2 ;
y += 2 ;
}

Output

No output

Lab 09:Introduction to Array 8


Programming Fundamentals (SWE-102) SSUET/QR/114

C. Write the following C programs:

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:

Lab 09:Introduction to Array 9


Programming Fundamentals (SWE-102) SSUET/QR/114

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:

Lab 09:Introduction to Array 10

Вам также может понравиться