[go: up one dir, main page]

0% found this document useful (0 votes)
40 views8 pages

Practical Questions (2D Array) (Only Program)

The document outlines six programming tasks involving matrix operations and user input validation. Each task specifies requirements for matrix dimensions, input values, and expected outputs, including sorting, calculating decimal equivalents, and displaying results. Examples are provided for each task to illustrate the expected input and output formats.

Uploaded by

Priyanshu Verma
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
40 views8 pages

Practical Questions (2D Array) (Only Program)

The document outlines six programming tasks involving matrix operations and user input validation. Each task specifies requirements for matrix dimensions, input values, and expected outputs, including sorting, calculating decimal equivalents, and displaying results. Examples are provided for each task to illustrate the expected input and output formats.

Uploaded by

Priyanshu Verma
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Question 1

Write a program to declare a matrix A[][] of order (M x N) where 'M' is the number of rows and 'N' is the
number of columns such that the value of 'M' must be greater than 0 and less than 10 and the value of
'N' must be greater than 2 and less than 6. Allow the user to input digits (0 - 7) only at each location, such
that each row represents an octal number.
Example:
2 3 1 (decimal equivalent of 1st row = 153 i.e. 2x82 + 3x81 + 1x80)

4 0 5 (decimal equivalent of 2nd row = 261 i.e. 4x82 + 0x81 + 5x80)

1 5 6 (decimal equivalent of 3rd row = 110 i.e. 1x82 + 5x81 + 6x80)


Perform the following tasks on the matrix:
1. Display the original matrix.
2. Calculate the decimal equivalent for each row and display as per the format given below.
Test your program for the following data and some random data:
Example 1 :
INPUT :
M=1
N=3
ENTER ELEMENTS FOR ROW 1: 1 4 4
OUTPUT:
FILLED MATRIX DECIMAL EQUIVALENT

1 4 4 100

Example 2 :
INPUT :
M=3
N=4
ENTER ELEMENTS FOR ROW 1 : 1 1 3 7
ENTER ELEMENTS FOR ROW 2 : 2 1 0 6
ENTER ELEMENTS FOR ROW 3 : 0 2 4 5
OUTPUT:
FILLED MATRIX DECIMAL EQUIVALENT

1 1 3 7 607

2 1 0 6 1094

0 2 4 5 165

Example 3 :
INPUT :
M=3
N=3
ENTER ELEMENTS FOR ROW 1 : 2 4 8
OUTPUT :
INVALID INPUT
Example 4 :
INPUT :
M=4
N=6
OUTPUT :
OUT OF RANGE
Question 2
Write a program to declare a single-dimensional array a[] and a square matrix b[][] of size N, where N > 2
and N < 10. Allow the user to input positive integers into the single dimensional array.
Perform the following tasks on the matrix:
1. Sort the elements of the single-dimensional array in ascending order using any standard sorting
technique and display the sorted elements.
2. Fill the square matrix b[][] in the following format:
If the array a[] = {5, 2, 8, 1} then, after sorting a[] = {1, 2, 5, 8}
Then, the matrix b[][] would fill as below:
12581251121211251111222155128125
3. Display the filled matrix in the above format.
Test your program for the following data and some random data:
Example 1
INPUT:
N=3
ENTER ELEMENTS OF SINGLE DIMENSIONAL ARRAY: 3 1 7
OUTPUT:
SORTED ARRAY: 1 3 7
FILLED MATRIX
137131113111331713
Example 2
INPUT:
N = 13
OUTPUT:
MATRIX SIZE OUT OF RANGE
Example 3
INPUT:
N=5
ENTER ELEMENTS OF SINGLE DIMENSIONAL ARRAY: 10 2 5 23 6
OUTPUT:
SORTED ARRAY: 2 5 6 10 23
FILLED MATRIX
2561023256102256252525622561022222555526662510102562325610
Question 3
Write a program to declare a matrix a[][] of order (m × n) where 'm' is the number of rows and 'n' is the
number of columns such that the values of both 'm' and 'n' must be greater than 2 and less than 10.
Allow the user to input integers into this matrix. Perform the following tasks on the matrix:
1. Display the original matrix.
2. Sort each row of the matrix in ascending order using any standard sorting technique.
3. Display the changed matrix after sorting each row.
Test your program for the following data and some random data:
Example 1
INPUT:
M=4
N=3
ENTER ELEMENTS OF MATRIX:
11−23516790431811593−216013748
OUTPUT:
ORIGINAL MATRIX
11−23516790431811593−216013748
MATRIX AFTER SORTING ROWS
−23115716049138−25013743111698
Example 2
INPUT:
M=3
N=3
ENTER ELEMENTS OF MATRIX
2251973612913622795361319126
OUTPUT:
ORIGINAL MATRIX
2251973612913622795361319126
MATRIX AFTER SORTING ROWS
5192271236691357619129223613
Example 3
INPUT:
M = 11
N=5
OUTPUT:
MATRIX SIZE OUT OF RANGE.
Question 4
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:
Q1 Q2 Q3 Q4 Q5

Participant 1 A B B C A

Participant 2 D A D C B

Participant 3 A A B A C

Participant 4 D C C A B

Key to the question: D C C B A


Note: Array entries are line fed (i.e. one entry per line)
Test your program for the following 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 = 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.
Question 5
Write a program to declare a square matrix A[][] of order (M × M) where 'M' must be greater than 3 and
less than 10. Allow the user to input positive integers into this matrix. Perform the following tasks on the
matrix:
1. Sort the non-boundary elements in ascending order using any standard sorting technique and
rearrange them in the matrix.
2. Calculate the sum of both the diagonals.
3. Display the original matrix, rearranged matrix and only the diagonal elements of the rearranged
matrix with their sum.
Test your program for the following data and some random data:
Example 1
INPUT:
M=4
9 2 1 5
8 13 8 4
15 6 3 11
7 12 23 8
OUTPUT:
ORIGINAL MATRIX
9 2 1 5
8 13 8 4
15 6 3 11
7 12 23 8
REARRANGED MATRIX
9 2 1 5
8 3 6 4
15 8 13 11
7 12 23 8
DIAGONAL ELEMENTS
9 5
3 6
8 13
7 8
SUM OF THE DIAGONAL ELEMENTS = 59
Example 2
INPUT:
M=5
7 4 1 9 5
8 2 6 10 19
13 1 3 5 1
10 0 5 12 16
1 8 17 6 8
OUTPUT:
ORIGINAL MATRIX
7 4 1 9 5
8 2 6 10 19
13 1 3 5 1
10 0 5 12 16
1 8 17 6 8
REARRANGED MATRIX
7 4 1 9 5
8 0 1 2 19
13 3 5 5 1
10 6 10 12 16
1 8 17 6 8
DIAGONAL ELEMENTS
7 5
0 2
5
6 12
1 8
SUM OF THE DIAGONAL ELEMENTS = 46
Example 3
INPUT:
M=3
OUTPUT:
THE MATRIX SIZE IS OUT OF RANGE.
Question 6

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 CC A
C X XX C
C X XX C
C X XX C
A C CC A
Advertisement
Example 3: INPUT: N = 15
OUTPUT: SIZE OUT OF RANGE

You might also like