232 Programming and Data Structures
Oil
1 0 10 0
10 0 0 110 0
1
0 0 1 110 1
10 1 0 1111
10 1
1
1110
11 0 0 10 10
110
1 10 11
111
0 10 0 1
1
11 1 10 0 0
Explanation In this program the binary bits corresponding to the Hex are obtained. The first two for
loops accomplish this. Exclusive OR operation with previous and successive bits obtains the gray
code. The MSB bits should not be changed.
7.8 THREE-OR MULTI-DIMENSIONAL ARRAYS
The C program allows array of two or multi dimensions. The compiler determines the restriction on it.
The syntax of multi-dimensional array as follows.
Arrayname [SI] [S2] [S3] . .. [Sn]
Where, Si is the size of the ith dimensions.
Three-dimensional array can be initialized as follows.
int mat [3] [3 [3] ={ {
{1 ,2 ,3 }
{4 ,5 ,6 }
{7 ,8 ,9 }
};
{
{1 -4 ,7 }
{2 ,5 ,8 }
{3 ,6 ,9 }
}'•
{
{1 ,4 ,4 }
{2 ,4 ,7 }
{6 ,6 ,3 }
};
}
A three dimensional array can be thought of as an array of arrays. The outer array contains three
elements. The inner array size is two dimensional with size [ 3 ] [ 3 ].
7.37 Write a program to explain the working of three-dimensional array.
# include <Stdio.h>
# include cconio. h>
void main()
{
int array_3d[3] [3] [3];
Arrays 233
in t a ,b ,c;
c lr s c r O ;
fo r (a=0;a<3;a++)
fo r (b=0;b<3;b++)
fo r (c=0;c<3;c++)
array_3d[a][b][c]=a+b+c;
for (a=0;a<3;a++)
I
printf (" n ");
fo r (b=0;b<3;b++)
I
fo r (c=0;c<3;c++)
printf ("%3d",array_3d[a][b][c]);
printf ( n ");
I
I
)
OUTPUT:
012
123
234
123
234
345
234
345
456
Explanation The three-dimensional array array_3d is initialized. The first there fo r loops are used
for adding the values of a, b & c. Here, initially a & b are zero a n d 'c ' varies from 0 to 2. Hence, the
addition of a, b & c will be 0 1 2. This will be printed in the first row. The second output row in
whicha =0, b = la n d c varies from 0 to 2. Thus, the output of second row will be 1 2 3.1nthisway
the values of a, b & c are changed and the total 27 iterations are carried out.
7.38 Write a program to explain the working of four- dimensional array.
# include <stdio.h>
# include <conio.h>
void main ()
{
int array_4d[2][2 ][2 ][2 ];
int a,b,c,d;
clrscrO ;
for (a=0;a<2;a++)
for (b=0;b<2;b++
for (c=0;c<2;c++)
234 Programming and Data Structures
for (d=0;d<2rf++)
array_4d[a]to][c][d]=a+b+c+d;
for (a=0;a<u;a++)
printf (' n ");
fo r (b=0#><2&++)
{
fo r (c= 0;e < 2lx + + .)
I
for (d*0pl<2rf++)
printf (%3d"srray_4d[a][b][c][d]);
printf (^ n ");
) I
I
I
Q u m m
01
12
12
23
12
23
23
34
Ex j.. a nation Here in the above-cited program instead of three-dimension, four-dimensional array is
used. The operations performed are same as explained in the previous example.
7.39 Write a program to read the quantity & rate of certain items using multidimensional
array. Calculate total cost by multiplying quantity & rate and offer 2% discount on it &
display the net amount.
# include <stdio.h>
# include <conio.h>
mainO
{
long m[2] [2] £23 [2];
in t a,b,c,d ;
jlr s c r ();
printf ("Enter Quantity & Raten");
for (a=0;a<2;a++)
for (b=0;b<2;b++)
for (c=0;c<2;c++)
for (d=0;d<l;d++)
I
i f (a ~ 0 )
Arrays 235
I
printf ("a =% d b=%d c~%d d=% dn”,a,b,c,d);
scanf ("%ld%ld",& an[a][b][c][d],& cm [a][b][c][d+l]);
I
else
I
m [a ]M c ][d ]^ m [a -l ]M c ]ld ]* m [a -l ]M c ][d ^ V ;
m [a ][b ][c][d + l]= m [a -l][b][c ]ld ]*m [a -l][b ][c ][d +l]*2 ll0 0 ;
)
f
printf (//f=====================================n");
printf ("tQ uantity Rate Amount Discount (@ 2 % )Net Am oun tn");
printf ('//f============================i============= t//);
for (a=0;a<l;a++)
for (b=0;b<2;b++)
fo r (c=0;c<2;c++)
fo r (d=0;d<l;d++)
I
printf ("n % 1 0ld % 1 0 ld ",m [a ][b][c ][dLm [a ][b ][c][d+l]);
printf ("%8ld.00 %6ld.00 %12ld.00”, m [a + l][b ][c ][d ],m [a + l][b ][c ][d + l],
m [a + l][b ][c ][d ]-tn [a + l][b ][c ][d + l]);
I
printf (//«^=============== =====================w");
getchO;
I
OUTPUT:
Enter Quantity & Rate
A-0 b=0 c=0 d=0
25 50
A=0.b=0 c=l d=0
30 60
A=0 b=l c=0 d=0
35 70
A=0 b=l c=l d=0
40 75
Quantity Rate Amount Discount (@2%) Net Amount
25 50 1250.00 25.00 1225.00
30 60 1800.00 36.00 1764.00
35 70 2450.00 49.00 2401.00
40 75 3000.00 60.00 2940.00
Explanation In the above example the four-dimension arraym [2] [2] [2] [2] is declared. The first
four fo r loops are used to read the quantity and rate. The values of variables 'a ' and 'd # forfourtimes
remains zero where as values of 'b ' and xc # changes to 1, (0 ,0 ), 2 (0 ,1 ), 3 (l,0 )a n d 4 (1,1).
This happens while execution of fo r loops. The i f statement checks as to whether the value of a = 0 or
not. As long as its value is zero reading operation is performed. When it is greater than zero rate and
quantity are multiplied for obtaining the amount. Also discount is calculated. Amount and discount
are stored in the array m [2] [2] [2] [2] .Net amount is printed after subtracting discount from gross
amount.
..................Content has been hidden....................

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