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 ("%d\n",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.\n\n");
printf (" Col-0 Col-1 Col-2\n");
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\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 ("\nEnter Code No & Balance:\n");

Get Programming and Data Structures now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.