216 Programming and Data Structures
Explanation Here, sorting of numbers is made through the exchange method. The element number
given by the outer fo r loop of an array num [ 10 ] is compared with all the other elements of the same
array If the first element is larger than the successive element, the larger value is assigned to variable
'tem p7 (temp=num[i] ;) andinplaceof larger value smaller value is replaced (num [i]=num[j];).
In place of smaller value the value of ' temp variable (which is larger) is replaced (num [ j ] = temp;).
Thus, the array elements in ascending order are obtained using the above program.
7.22 Write a program to evaluate the following series. The series contains sum of square of
numbers from 1 to 'n'. Store result of each term in an array. Calculate value of V using
array.
s= l 2+22+32+42.~n
# include <stdio.h>
# include cconio. h>
# include <math.h>
void mainO
{
sta tic int sqr[15];
int i,n ,s= 0 ;
c lr s c r();
printf ("Enter value ofn
scanf ("%d",&cn);
for (i=0p<n,H++)
sqr[i]=pozv(i+l,2);
for(i=0,i<n,'i++)
{
printf ("%dn",sqr[i]);
s=s+sqr[i];
I
printf ("Sunt of square: %d",s);
I
OUTPUT:
Enter value of n: 4
1
4
9
16
Sum of square: 30
Explanation The above program evaluates squares upto ' n 7 numbers. The value of ' n ' is entered
through the keyboard. Square of each number is stored in an array sqr [ 15 ] and their sum is calculated.
Final result is displayed.
7.7 TWO-DIMENSIONAL ARRAY
Two-dimensional array can be thought as a rectangular display of elements with rows and columns.
For example elements of int x [ 3 ] [ 3 ] are shown in the Table 7.4.
Arrays 217
Table 7.4 Array Elements in Matrix form
Coll Col2 Col3
Rowl x[0] [0] x[0] [1]
x[0] [2]
Row2 x[l] [0] x[l] [1]
x [l] [2]
Row3
x[2] [0] x[2] [1]
x[2] [2]
The arrangement of array elements shown in the above Table 7.4 is only for the sake of understanding.
Conceptually the elements are shown in matrix form. Physically array elements are stored in one
continous form in the memory.
The two-dimensional array is a collection of a number of one-dimensional arrays, which are placed
one after another. For example in the above table each row of a two dimensional array can be thought
of as a single-dimensional array.
7.23 Write a program to display two-dimensional array elements together with their
addresses.
# Include <stdio.h>
# include <conio.h>
void mainO
{
Int i,j;
int a[3][3]={{l,2,3},{4,5,6},{7,8,9}}>
clrs crO ;
printf (" Array Elements andaddresss. ");
printf (" Col-0 Col-1 Col-2 ");
printf (" ===== ===== ======«">;
printf (“roivO");
for (i=0,i<3;i++)
/
for ()=0,-j<3,]'++)
printf (" %d [%5d] ", a[i][j], &a[i][j] );
printf("nRow%d"ri+l);
printf("r ");
I
OUTPUT:
Array Elements and addresss.
Col-0 Col-1 Col-2
RowO 1 [4052] 2 [4054] 3 [4056]
Rowl 4 [4058] 5 [4060] 6 [4062]
Row2 7 [4064] 8 [4066] 9 [4068]
218 Programming and Data Structures
Explanation In the above program two-dimensional array is declared and initialized. Using two
nes t ed for loops elements of an array together with their addresses are displayed. It is shown at the
output that elements of two-dimensional array are displayed in a rectangular form. But, in memory
they are not stored in this particular format. They are stored in a continuous memory location as
shown below.
Table 7.5 Memory Map of Two-dimensional array elemets
Row,col A[0][0] A[0[[1]
A[0[[2]
A[1[[0]
A[l[[l]
A[l[[2]
A[2[[0] A[2][l]
A[2[[2]
Value 1 2 3 4 5
6
7
8 9
Address
4052 4054
4056 4058 4060 4062 4064
4066
4058
7.24 Write a program to display the elements of two-dimensional array.
# include <stdio.h>
# include <conio.h>
void mainO
{
in t i,j;
int a[3] [3]»{{1,2,3},{4,5,6},{7,8,9}};
clrscrO j
printf ("Elements ofAnArray. n");
for (i=0,-i<3,i++)
{
for(j=0,j<3,i++)
printf ("%5d",a[i][jl);
printf{“n");
I
I
OUTPUT:
Elements of An Array.
1 2 3
4 5 6
7 8 9
Explanation The outer for loop gives row number and the inner for loop prints the column number.
Using both the variables ' i # and ' j ' all array elements are printed.
7.25 Write a program to display the balance & codeno in two separate columns. Indicate the
number of codenos, which are having the balance less than 1000.
# include <stdio.h>
# include <conio.h>
main ()
{
in t bal[5][2],i,j=0;
clrscrO ;
printf (" Enter Code No & Balance:n");
Arrays 219
for (I±0,i<5,i++)
scanf ("%d%d",&bal[iJ[l]Mal(iH2]);
printf ("Your Entered Data:");
for (i=0,i<5,i++)
{
printf (" %d%d",bal[i][l],bal[i][2]);
if(bal[i][2]<1000)
;++/
I
printf (" Balance Less than 1000 are %d",j-l);
QUIFUT;
Enter Code No & Balance:
1 900
2 800
3 1200
4 550
5 600
Your Entered Data:
1 900
2 800
3 1200
4 550
5 600
Balance Less than 1000 are 4
Explanation The above program finds the number of balance deposits less than 1000. Through the
keyboard the codeno and balance deposits are entered. The first fo r loop is initialized from 0 to less
than 5 for inputting the codenos and their balance deposi ts. The second fo r loop is for displaying
the entered elements. The i f statement checks whether the balance deposit is less than 10 0 0 or not.
With this the codenos having deposits less than 1000 are sorted out. The counter gets increased when
deposits are less than 1000.
7.26 Write a program to initialize single & two-dimensional arrays. Accept three elements in
single dimensional array. Using the elements of this array compute addition, square
and cube. Display the results in two-dimensional array.
#Include <stdio.h>
#include onath.h>
mainO
{
in t i , j=0, a[3] [3] ,b[3];
220 Programming and Data Structures
clrscr () ;
printf("n Enter Three Numbers : ");
for (i<*0,i<=2,i++)
scanfC%d",&blil);
for (i=0,i<=2,'i++)
I
ali][j] = b[i]*2;
a[i][j+l]=pow(b[i],2);
a[i]lj+2]=pow(blilJ);
I
clrscri);
prmtf("Number Addtion Square Cube '');
fo r (0^<-2,i++)
I
printf(" %d",b[i]);
fo r (;=0,7<=2,7++)
printf("t%4dt ",a[i]tj]);
jmn1f('n");
)
getchO;}
Qwtptttj
Enter Three Numbers: 5 6 7
Number Addition Square Cube
5 10 25 125
6 12
36 216
7 14 49
343
Explanation The one-dimensional array is used for storing the elements entered through the keyboard.
The second for loop performs operations such as multiplication (double), square and cube. The
square and cube of the entered numbers are performed by standard library function pow () defined in
m at h . h . The results obtained are assigned to the corresponding elements of two-dimensional arrays.
The elements of two arrays are displayed.
7.27 Read the matrix of the order up to 10 x 10 elements and display the same in the matrix
form.
# include <stdlo.h>
# Include <conio.h>
void m&inO
..................Content has been hidden....................

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