#### Example Programs

Program 

The result of a quiz competition is to be prepared as follows:

The quiz has five questions with four multiple choices (A, B, C, D), with each question carrying 1 mark for the correct answer. Design a program to accept the number of participants N such that N must be greater than 3 and less than 11. Create a double dimensional array of size (Nx5) to store the answers of each participant row-wise.

Calculate the marks for each participant by matching the correct answer stored in a single dimensional array of size 5. Display the scores for each participant and also the participant(s) having the highest score.

Example: If the value of N = 4, then the array would be: Test your program with the sample data and some random data:

Example 1

INPUT : N = 5

Participant 1 D A B C C

Participant 2 A A D C B

Participant 3 B A C D B

Participant 4 D A D C B

Participant 5 B C A D D

Key: B C D A A

OUTPUT : Scores :

Participant 1 D A B C C

Participant 1 = 0

Participant 2 = 1

Participant 3 = 1

Participant 4 = 1

Participant 5 = 2

Highest score: Participant 5

Example 2

INPUT : N = 4

Participant 1 A C C B D

Participant 2 B C A A C

Participant 3 B C B A A

Participant 4 C C D D B

Key: A C D B B

OUTPUT : Scores :

Participant 1 = 3

Participant 2 = 1

Participant 3 = 1

Participant 4 = 3

Highest score:

Participant 1

Participant 4

Example 3

INPUT : N = 12

OUTPUT : INPUT SIZE OUT OF RANGE.

Program 

Write a program to declare a square matrix M [ ] [ ] of order ‘N’ where ‘N’ must be greater than 3 and less than 10. Allow the user to accept three different characters from the keyboard and fill the array according to the instruction given below:

(i) Fill the four corners of the square matrix by character 1.

(ii) Fill the boundary elements of the matrix (except the four corners) by character 2.

(iii) Fill the non-boundary elements of the matrix by character 3.

Test your program with the following data and some random data:

Example 1:

INPUT: N = 4

FIRST CHARACTER: @

SECOND CHARACTER: ?

THIRD CHARACTER: #

OUTPUT:

@ ? ? @

? # # ?

? # # ?

@ ? ? @

Example 2:

INPUT: N = 5

FIRST CHARACTER: A

SECOND CHARACTER: C

THIRD CHARACTER: X

OUTPUT:

A C C C A

C X X X C

C X X X C

C X X X C

A C C C A

Example 3:

INPUT: N = 12

OUTPUT: SIZE OUT OF RANGE

Program 

Write a  program  to  declare  a  square  matrix  A[  ][  ]  of  order  MxM  where  'M'  is  the  number  of  rows  and  the  number  of  columns,  such  that  M  must  be  greater  than  2  and    less  than 10.  Accept  the  value  of  M  as  user  input.  Display  an  appropriate  message  for    an  invalid  input.  Allow  the  user  to  input  integers   into   this   matrix.   Perform   the   following tasks:

(a) Display the original matrix.

(b) Rotate the matrix 90° clockwise as shown below:

Original matrix

1 2 3

4 5 6

7 8 9

Rotated matrix

7 4 1

8 5 2

9 6 3

(c) Find the sum of the elements of the four comers of the matrix.

Test your program with the sample data and some random data: Example 1

INPUT : M = 3

3 4 9

2 5 8

1 6 7

OUTPUT :

ORIGINAL MATRIX

3 4 9

2 5 8

1 6 7

MATRIX AFTER ROTATION

1 2 3

6 5 4

7 8 9

Sum of the corner elements = 20

Example 2

INPUT : M = 4

1 2 4 9

2 5 8 3

1 6 7 4

3 7 6 5

OUTPUT :

ORIGINAL MATRIX

1 2 4 9

2 5 8 3

1 6 7 4

3 7 6 5

MATRIX AFTER ROTATION
3 1 2 1
7 6 5 2
6 7 8 4
5 4 3 9
Sum of the corner elements = 18

Program 

A wondrous square is an n by n grid which fulfils the following conditions:

It contains integers from 1 to n2, where each integer appears only once.

The sum of integers in any row or column must add up to 0.5 x n x (n2 + 1).

For example, the following grid is a wondrous square where the sum of each row or column is 65 when n=5.

 17 24 1 8 15 23 5 7 14 16 4 6 13 20 22 10 12 19 21 3 11 18 25 2 9

Write a program to read n (2 <= n <= 10) and the values stored in these n by n cells and output if the grid represents a wondrous square.

Also output all the prime numbers in the grid along with their row index and column index as shown in the output. A natural number is said to be prime if it has exactly two divisors. For example, 2, 3, 5, 7, 11 The first element of the given grid i.e. 17 is stored at row index 0 and column index 0 and the next element in the row i.e. 24 is stored at row index 0 and column index 1.

Test your program for the following data and some random data:

Input:

n = 4

 16 15 1 2 6 4 10 14 9 8 12 5 3 7 11 13

Output:

Yes, it represents a wondrous square

 Prime Row Index Column Index 2 0 3 3 3 0 5 2 3 7 3 1 11 3 2 13 3 3 15 0 1

Input:

n=3

 1 2 4 3 7 5 8 9 6