Chapter Lists for "Let us C Solution"
 S.N Chapter S.N Chapter 1. 5 Arrays 2. 6. Puppetting On Strings 3. 7. Structures 4. 8. Input/Output in C

CHAPTER: The Array solutions:
-------------------------------------------------------------------
[D] Answer the following:

(a) Twenty-five numbers are entered from the keyboard into an array. The number to be searched is entered through the keyboard by the user. Write a program to find if the number to be searched is present in the array and if it is present, display the number of times it appears in the array.

(b) Twenty-five numbers are entered from the keyboard into an array. Write a program to find out how many of them are positive, how many are negative, how many are even and how many odd.

(c) Implement the Selection Sort, Bubble Sort and Insertion sort algorithms on a set of 25 numbers. (Refer Figure 8.11 for the logic of the algorithms) ? Selection sort
? Bubble Sort
? Insertion Sort
figure see in book

(d) Implement the following procedure to generate prime numbers from 1 to 100 into a program. This procedure is called sieve of Eratosthenes.
step 1
Fill an array num[100] with numbers from 1 to 100
step 2
Starting with the second entry in the array, set all its multiples to zero.
step 3
Proceed to the next non-zero element and set all its multiples to zero.
step 4
Repeat step 3 till you have set up the multiples of all the non-zero elements to zero
step 5
At the conclusion of step 4, all the non-zero entries left in the array would be prime numbers, so print out these numbers.

-----------------------------------------------
[I] Attempt the following:

(a) Write a program to copy the contents of one array into another in the reverse order.

(b) If an array arr contains n elements, then write a program to check if arr[0] = arr[n-1], arr[1] = arr[n-2] and so on.

(c) Find the smallest number in an array using pointers.

(d) Write a program which performs the following tasks:
? initialize an integer array of 10 elements in main( )
? pass the entire array to a function modify( )
? in modify( ) multiply each element of array by 3
? return the control to main( ) and print the new array elements in main( )

(e) The screen is divided into 25 rows and 80 columns. The characters that are displayed on the screen are stored in a special memory called VDU memory (not to be confused with ordinary memory). Each character displayed on the screen occupies two bytes in VDU memory. The first of these bytes contains the ASCII value of the character being displayed, whereas, the second byte contains the colour in which the character is displayed. For example, the ASCII value of the character present on zeroth row and zeroth column on the screen is stored at location number 0xB8000000. Therefore the colour of this character would be present at location number 0xB8000000 + 1. Similarly ASCII value of character in row 0, col 1 will be at location 0xB8000000 + 2, and its colour at 0xB8000000 + 3. With this knowledge write a program which when executed would keep converting every capital letter on the screen to small case letter and every small case letter to capital letter. The procedure should stop the moment the user hits a key from the keyboard. This is an activity of a rampant Virus called Dancing Dolls. (For monochrome adapter, use 0xB0000000 instead of 0xB8000000).

More than one dimension [L] Attempt the following:

(a) How will you initialize a three-dimensional array threed[3][2][3]? How will you refer the first and last element in this array?

(b) Write a program to pick up the largest number from any 5 row by 5 column matrix.

(c) Write a program to obtain transpose of a 4 x 4 matrix. The transpose of a matrix is obtained by exchanging the elements of each row with the elements of the corresponding column.

(d) Very often in fairs we come across a puzzle that contains 15 numbered square pieces mounted on a frame. These pieces can be moved horizontally or vertically. A possible arrangement of these pieces is shown below:

Figure 8.12 As you can see there is a blank at bottom right corner. Implement the following procedure through a program: Draw the boxes as shown above. Display the numbers in the above order. Allow the user to hit any of the arrow keys (up, down, left, or right). If user hits say, right arrow key then the piece with a number 5 should move to the right and blank should replace the original position of 5. Similarly, if down arrow key is hit, then 13 should move down and blank should replace the original position of 13. If left arrow key or up arrow key is hit then no action should be taken. The user would continue hitting the arrow keys till the numbers aren't arranged in ascending order. Keep track of the number of moves in which the user manages to arrange the numbers in ascending order. The user who manages it in minimum number of moves is the one who wins. How do we tackle the arrow keys? We cannot receive them using scanf( ) function. Arrow keys are special keys which are identified by their 'scan codes'. Use the following function in your program. It would return the scan code of the arrow key being hit. Don't worry about how this function is written. We are going to deal with it later. The scan codes for the arrow keys are: up arrow key - 72 down arrow key - 80 left arrow key - 75 right arrow key - 77

```/* Returns scan code of the key that has been hit */
#include "dos.h" getkey( )
{
union REGS i, o ;
while ( !kbhit( ) )
i.h.ah = 0 ; int86 ( 22, &i, &o ) ;
return ( o.h.ah ) ;
}
```

(e) Those readers who are from an Engineering/Science background may try writing programs for following problems.
(1) Write a program to add two 6 x 6 matrices.
(2) Write a program to multiply any two 3 x 3 matrices.
(3) Write a program to sort all the elements of a 4 x 4 matrix.
(4) Write a program to obtain the determinant value of a 5 x 5 matrix.

from L.(f-i) match the following. Solution not available

(j) Write a program that interchanges the odd and even components of an array.

(k) Write a program to find if a square matrix is symmetric.

(l) Write a function to find the norm of a matrix. The norm is defined as the square root of the sum of squares of all elements in the matrix.

(m) Given an array p[5], write a function to shift it circularly left by two positions. Thus, if p[0] = 15, p[1]= 30, p[2] = 28, p[3]= 19 and p[4] = 61 then after the shift p[0] = 28, p[1] = 19, p[2] = 61, p[3] = 15 and p[4] = 30. Call this function for a (4 x 5 ) matrix and get its rows left shifted.

(n) A 6 x 6 matrix is entered through the keyboard and stored in a 2-dimensional array mat[7][7]. Write a program to obtain the Determinant values of this matrix.

(o) For the following set of sample data, compute the standard deviation and the mean. -6, -12, 8, 13, 11, 6, 7, 2, -6, -9, -10, 11, 10, 9, 2 The formula for standard deviation is

where xi is the data item and x is the mean.

(p) The area of a triangle can be computed by the sine law when 2 sides of the triangle and the angle between them are known. Area = (1 / 2 ) ab sin ( angle ) Given the following 6 triangular pieces of land, write a program to find their area and determine which is largest,

```Plot No.    a       b       angle
1           137.4   80.9    0.78
2           155.2   92.62   0.89
3           149.3   97.93   1.35
4           160.0   100.25  9.00
5           155.6   68.95   1.25
6           149.7   120.0   1.75```

(q) For the following set of n data points (x, y), compute the correlation coefficient r,

```x           y
34.22       102.43
39.87       100.93
41.85       97.43
43.23       97.81
40.06       98.32
53.29       98.32
53.29       100.07
54.14       97.08
49.12       91.59
40.71       94.85
55.15       94.65```

(r) For the following set of point given by (x, y) fit a straight line given by y = a + bx
where,

```x       y
3.0     1.5
4.5     2.0
5.5     3.5
6.5     5.0
7.5     6.0
8.5     7.5
8.0     9.0
9.0     10.5
9.5     12.0
10.0    14.0```

(s) The X and Y coordinates of 10 different points are entered through the keyboard. Write a program to find the distance of last point from the first point (sum of distance between consecutive points).

Comment with facebook