198 Programming and Data Structures
b) In the statement a [2 ] =a [3] or vice versa, value of a [2 ] is assigned to a [3], where
both the elements are of the same array.
c) The array elements are stored in continous memory locations. The amount of storage
required for holding elements of the array depends on its type and size. The total size in bytes
for a single dimensional array is computed as shown below.
Total bytes = sizeof (data type) X size of array
7.5 ONE-DIMENSIONAL ARRAY
The elements of an integer array a [ 5 ] are stored in continuous memory locations. It is assumed that
the starting memory location is 2 0 0 0. Each integer element requires 2 bytes. Hence subsequent element
appears after gap of 2 locations. Table 7.1 shows the locations of elements of an integer array.
Table 7.1 Integer Data Type & Their Memory Locations
Element A[0] A[l] A [2]
A [3]
A [4]
Address 2000
2002
2004
2006
2008
Similarly, the elements of an arrays of any data type are stored in continuous memory location. The
only difference is that number of locations are different for different data types.
An example is illustrated below based on this point.
7.1 Write a program to print bytes reserved for various types of data & space required for
storing them in memory using arrays.
# Include <stdio.h>
# include <conio.h>
mainQ
{
int i [10] ;
char c[10];
long 1 [10];
clrscrO ;
printf ("The type 'int' requires %d Bytes ",sizeof( in t));
printf ("nThe type 'char'requires %d Bytes",sizeof(char));
printf ("nTlte type 'long' requires %dBytes"fsizeof(long));
printf (" %d memory locations are reserved for ten 'int' elements"rsizeof(i));
printf ("n %d memory locations are reserved for ten 'char' elements", sizeofc));
printf (" %d memory locations are reserved for ten 'long' elements",sizeof(l));
I
OUTPUT:
The type 'int' requires 2 Bytes
The type 'char7 requires 1 Bytes
The type 'long' requires 4 Bytes
20 memory locations are reserved for ten 'int' elements
10 memory locations are reserved for ten 'char' elements
40 memory locations are reserved for ten 'long' elements
Arrays 199
Explanation The sizeof () function provides the size of data type in bytes. In the above example
in t, char, & long type of data variables are supplied to this function which gives the results 2 ,1 &
4 bytes respectively Required number of memory locations for in t , char and long will be 2 ,1 and 4.
Memory locations required for the arrays = argument of an array X sizeo f [data type]. In the
above example array in t i [10] requires 2 0 memory locations since each element requires 2 memory
locations. Memory requirement for various data types will be as given in the Table 7.2.
Table 7.2 Data Type & Their Required Bytes
Data Type
Memory Requirement
Character
lbyte
Integer
2 bytes
Float
4 bytes
Long
4 bytes
Double
8 bytes
Character arrays are called strings. There is a slight difference between an integer array and a
character array In character array NULL ( ' ') character is automatically added at the end, where as
in integer or other types of arrays no character is placed at the end.
The NULL character acts as an end of the character array By using this NULL character compiler
detects the end of the character array. When a compiler reads the NULL character '0', there is end of
character array.
Tip Detailed information about strings (character array) is given in another chapter "Working with
s trings../' The explanation about strings is given in brief in this chapter.
Given below is an example of string.
7.2 Write a program to display character array with their address.
# include<stdio.h>
# include <conio.h>
void main ()
{
char name [10] ={'A', 'R', 'R', 'A'
int i=0;
clrscrO;
printf ("n Character Memory Location ");
while (name[i]!=r0')
I
printf(" [%c]t [%u]",name[i],&name[i]);
i++;
I
I
OUTPUT:
Character Memory Location
[A] 4054
[R] 4055
[R] 4056
[A] 4057
[Y] 4058
200 Programming and Data Structures
Explanation The elements of an array are stored in contiguous memory locations. In the above
example elements of one-dimensional array 'A '/ R '/R '/A '/ Y ' are stored from location 4054 to
4058.
One-dimensional character array elements will be stored in memory as per Table 7.3.
Table 7.3 Character Array Elements & Their Locations
Array Element No
0 1
2
3 4
Elements
A R R A
Y
Memory Addresses
4054
4055 4056 4057
4058
Tips In case a NULL 1 0 ' is initialized in the above example after ' Y' the result displayed will be
'ARRAY'.
7.3 Write a program to add even and odd numbers from 1 to 10. Store them and display their
results in two separate arrays.
# include <stdio.h>
# include <conio.h>
void main ()
{
int sumo=0, sume=0, i=0,odd[5],even[5] ,a=-l,-l;
clrscrO ;
for (i=l;i<=10;i++)
{
if (i%2==0)
even[++a]=i;
else
odd[++b]=i;
}
printf (" Even Odd");
for (i=0,'i<5,'i++)
{
pfrintf(" %d t%d",even[i],odd[i]);
sume=sume+even[i];
sumo=sumo+odd[i];
I
prin tf("n f=============s======== n ");
printf ("Addition: %d %14d”,sume,sumo);
}
QUT3PUT;
Even Odd
2 1
4 3
6 5
8 7
10 9
Addition: 30 25
Arrays 201
Explanation The fo r loop executes 10 times. In the fo r loop the value of loop variable ' i ' is tested
for ' even ' and 'odd' conditions. If the value of ' i ' is even then it is assigned to array even []
otherwise odd [ ] . Thus, for ten times this task is performed. Finally the second fo r loop displays both
the even [] and odd [] arrays. In the same array sum of even and odd elements are calculated and
displayed.
7.4 Write a Program to input five numbers through the keyboard. Compute & display addition
of even numbers and product of odd numbers.
# include <stdio.h>
void main ()
{
int a=0,msl,i,num[5];
clrscrO;
for (i=0fl<5,'i++)
I
fmntf(" EnterNumberl%d]:",i+l);
scanf r%d",&omm[i]);
I
printf ( n== ==~ = ~ = ==~ = ================");
for (i=0;i<5,~i++)
I
if(num[i]%2==0)
I
printf (“ Even Number: %d",num[i]);
a=a+num[i];
I
else
I
printf (n Odd Number: %d",num[i]);
m=m*num[i];
}
}
printf ("n==-=======-==============~======'');
printf (n Addition of Even Numbers: %d",a);
printf (" Product of Odd Numbers :%d",m);
printf ("n=~~-========================='');
}
OUTPUT:
Enter Number[l]: 1
Enter Number[2]: 2
Enter Number[3]: 3
Enter Number[4]: 4
Enter Number[5]: 5
Odd Number : 1
Even Number: 2
Odd Number :3
Even Number: 4
202 Programming and Data Structures
Odd Number : 5
Addition of Even Numbers: 6
Product of Odd Numbers: 15
Explanation In the above example five integers are entered through the keyboard. To detect even
and odd numbers mod (%) operation is carried out and remainder of each number is obtained. If
remainder is 0 then the number is even and added to variable ' a'. If remainder is non - zero then this
number is multiplied to'm'. Variables % a ' and'm' are initialized with 0 and 1 respectively. Both the
variables are printed through printf () function which gives addition of even numbers and the
product of odd numbers respectively.
7.5 Write a program to print stfing in the reverse order.
For example HELLO would give result as OLLEH.
#include <stdio.h>
#include <canio.h>
mainO
{
static char s [15] ;/*Static Storage class initializes variables */
int i;
clrscrO;
puts ("Enter a String: ");
gets (s);
puts("Reverse Siring:");
for 0=15,i>=0,i—)
printf (“%c"Mil);
}
OUTPUT:
Enter a String:
HELLO
Reverse Siring :
OLLEH
Explanation As we have discussed earlier an array stores its elements in a continuous form and
counting of array elements starts from * 0 '. By applying reverse loop elements are printed from last to
first.
7.6 Write & display a program to detect the occurrence of a character in a given string.
#include <stdio.h>
#include <conio.h>
mainO
{
static char s [15];
int i,c«0;
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.227.10.45