Problem Sheet 5

This problem sheet was created by Sir Narasimhan T, Assistant Professor, IT Department LBS College of Engineering, Kasaragod

The softcopy in .pdf is available here. There is  a bit more in the file.

1) Calculate the mean, variance and standard deviation of a set of numbers.

2) A m × n matrix M is said to have a saddle point if there is an entry M[i][j] such that it is the smallest value in row i and the largest value in column j. For example, consider the matrix:
20   30   40
56   78   45
1   2   3
Here 45 is the saddle point because it is the smallest in row1 but largest in column2. Given a matrix, find the saddle point if it exists.

3) Determine the norm of a matrix. Norm is defined as the square root of the sum of the squares of matrix elements. A sample input and output is shown below:
Enter the size of matrix
2  3
Enter 6 elements
1  2  3  4  5  6
The matrix is
1  2  3
4  5  6
The norm is 9.539392

4) A magic square of order n is a square matrix of integers from 1 to n^2 arranged randomly in a square grid fashion. The peculiarity of magic square is that the numbers in each row, and in each column, and the numbers in the forward and backward diagonals, all add to the same constant M where M =
n(n^2+1)/2.  For example, the matrix
2  7  6
9  5  1
4  3  8
is a magic square since each row, each column and the two diagonals add to (3×10)/2 =15. Write a program to input a square matrix from the user and that checks whether it is a magic square or not. The order of the matrix should also be input.

5) Convert decimal number to binary.

6) Interchange two rows in a matrix. Sample input and output is shown below
Enter number of rows
3
Enter number of columns
4
Enter the elements
4 8 12 16 1 2 3 4 3 9 12 18
The input matrix is
4 8 12 16
1 2 3 4
3 9 12 18
Which two rows you want to interchange?
0 2
The matrix after exchanging row 0 and row 2 is
3 9 12 18
1 2 3 4
4 8 12 16

7) The following is known as Pascal’s triangle.
1
1  1
1  2  1
1  3  3  1
1  4  6  4  1
1  5  10  10  5  1

Any element p[i][j] (except the 1’s at the left and right ends) in this triangle p is given by
p[i][j] = p[i-1][j-1] + p[i-1][j]
Input the number of rows and print the Pascal triangle.

8)Interchange two columns in a matrix. Sample input and output is shown below
Enter number of rows
4
Enter number of columns
3
Enter the elements
4 8 12 16 1 2 3 4 3 9 12 18
The input matrix is
4 8 12
16 1 2
3 4 3
9 12 18
Which two columns you want to interchange?
1 2
The matrix after exchanging column 1 and column 2 is
4 12 8
16 2 1
3 3 4
9 18 12

9) Input a string and print all its prefixes and suffixes. A sample input and output is given below:
Enter a string
cprogram
The prefixes of cprogram are
cprogram
cprogra
cprogr
cprog
cpro
cpr
cp
c
The suffixes of cprogram are
cprogram
program
rogram
ogram
gram
ram
am
m

10) A square matrix is called lower triangular if all the entries above the main diagonal are zero. Conversely a square matrix is called upper triangular if all the entries below the main diagonal are zero. A matrix that is both upper and lower triangular is a diagonal matrix. (A diagonal matrix will have non zero entries only on the main diagonal). Find whether an input matrix falls under any of these categories.

11) Write a program that takes nouns and that forms their plurals on the basis of these rules:
• If the noun ends in “y”,remove the y and add “ies”
• If the noun ends in “s”, “ch” or “sh”, add “es”.
• In all other cases just add “s”.
Print each noun and its plural. Sample input and output is listed here:
Enter how many nouns
5
Enter the nouns
chair
dairy
dish
church
circus
The nouns and their plurals are
chair – chairs
dairy – dairies
dish – dishes
church – churches
circus – circuses

12) Perform addition, subtraction and transpose operations on matrices. The user will enter ’a’ for addition,’s’ for subtraction and ’t’ for transpose. Only if the user enters ’a’ or ’s’, the second matrix needs to be input.

13) The alphabetic distance between 2 letters x and y is defined by assigning ’A’=1, ’B’=2, … ’Z’=26. Then the distance is y − x if y ≥ x, and (y + 26) − x if y < x. For instance, the alphabetic distance between ’B’ and ’D’ is 4 − 2 = 2, while the distance between ’D’ and
’B’ is (2 + 26) − 4 = 24. Given two words of equal length, the distance between them is the sum of alphabetic distances between the corresponding characters. Your aim is to input two words and print the distance between them.
Sample input and output – 1
Enter the words
AAAA ABCD
Distance between AAAA and ABCD is 6
Sample input and output – 2
Enter the words
DEADLY ULTIMO
Distance between DEADLY and ULTIMO is 65
[Note above that 6 = 0 + 1 + 2 + 3 and 65 = 17 + 7 + 19 + 5 + 1 + 16]

14) Write a program to form a Latin square. A Latin square is represented by an m × m two dimensional array filled with m different characters such that no element gets repeated either in the row or in the column. For example, if m = 4; the chosen characters are A, B, C, D and the first row is A B C D , then second row cannot have A as the first element as every column should have unique elements. Input the order m and the first row of the matrix and print the Latin square. A sample input and output is shown below:
Enter the order
3
Enter the first row
C A B
The Latin square is
C A B
A B C
B C A

15) Caesar cipher is a way of creating a code word (secret word) from a given word by substituting each character in the word with another character which is obtained by adding m (a constant which is called key) to the original character. For example if the original character is ’A’ and the value of m is 3, then the substituted character will be ’D’, which is A + 3. Assume that
the input word contains only upper case letters (ASCII 65-90). The following procedure can be used to obtain the codeword:-
• Take each character in the word and add m to it.
• If the number obtained is greater than 90, subtract 90 from the number. Let the result
be r. Now add r-1 to 65. In this way, the substituted character will always lie between A
and Z.
A sample input and output is shown below:
Enter the word
QWERTY
Enter the key
4
The codeword is
UAIVXC

16) Super Mario is studying how to use a mysterious Power Square. The Power Square is an n × n matrix with integer values between 0 and n^2−1. A number y is a neighbour of another number x in the Power Square if y is directly above or below x, or directly to the left or right of x.
Note that some values in the Power Square need not have all the four neighbours. Mario wants you to help him write a program that when given a number x in the Power Square, displays the Power Square and all the neighbours of x. Two sets of sample input and output are shown below:

SET 1
Value of n please?
3
9 Values please?
8 7 6 5 4 3 2 1 0
Neighbours of whom?
7
Power Square is
8 7 6
5 4 3
2 1 0
The neighbours of 7 are
8 6 4

SET 2
Value of n please?
4
16 Values please?
1 5 9 12 0 2 8 10 6 11 15 14 4 7 3 13
Neighbours of whom?
2
Power Square is
1 5 9 12
0 2 8 10
6 11 15 14
4 7 3 13
The neighbours of 2 are
0 5 8 11

Leave a comment