0% found this document useful (0 votes)
38 views2 pages

C Lab: 2D Array Challenges

This lab assignment requires students to work with 2D arrays in C through three progressively challenging questions. Students must create programs that handle matrix creation, row averages, and saddle point detection based on input from text files. The assignment emphasizes constraints on array sizes and prohibits the use of pointers.

Uploaded by

omsharma8834
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)
38 views2 pages

C Lab: 2D Array Challenges

This lab assignment requires students to work with 2D arrays in C through three progressively challenging questions. Students must create programs that handle matrix creation, row averages, and saddle point detection based on input from text files. The assignment emphasizes constraints on array sizes and prohibits the use of pointers.

Uploaded by

omsharma8834
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

Lab: 2D Array

Lab: 2D Array
This lab assignment focuses on 2D arrays in C. You are required to solve three
questions of increasing difficulty to test your understanding of 2D array decla-
ration, initialization, traversal, and manipulation.

General Instructions
• Write your programs in C.
• Input will be provided via a text file redirected through the command line
using <.
• Test your programs using the command: ./program < [Link], where
X is the input file number.
• Ensure your program handles the given constraints: array sizes will satisfy
1 ≤ M, N ≤ 10.
• Do not use pointers, as they have not been covered in class.
• Test with multiple inputs, including edge cases (e.g., 1 × 1 matrices).
• Submit your source code files named qX.c, where X is the question number
(1 to 3).

Question 1
Write a C program to read an integer N (1 ≤ N ≤ 10) from a file and create an
N ×N matrix where each element is the sum of its row and column indices. Print
the matrix.
Input Format:
• First line: An integer N .
Sample Input ([Link]):
1 3

Command: ./q1 < [Link]


Sample Output:
1 0 1 2
2 1 2 3
3 2 3 4

Hint: For each cell (i,j) (1-based), compute the sum i + j. Use nested loops
to generate and print the matrix.

Question 2
Write a C program to read a 2D array of size M × N (1 ≤ M, N ≤ 10) from a file
and compute the average of elements in each row (rounded down to the nearest

1
Lab: 2D Array

integer). Print the average of each row on a new line.


Input Format:
• First line: Two integers M and N , the number of rows and columns.
• Next M lines: N integers each, representing the elements of the 2D array.
Sample Input ([Link]):
1 3 4
2 1 2 3 4
3 5 6 7 8
4 9 10 11 12

Command: ./q2 < [Link]


Sample Output:
1 2
2 6
3 10

Hint: For each row, compute the sum of elements, divide by the number of
columns, and use integer division to round down.

Question 3
Write a C program to read a square matrix of size N × N (1 ≤ N ≤ 10) from a
file and find the saddle point, if it exists. A saddle point is an element that is the
minimum in its row and the maximum in its column. Print the row and column
indices (0-based) of the saddle point, or -1 -1 if none exists.
Input Format:
• First line: An integer N , the size of the square matrix.
• Next N lines: N integers each, representing the elements of the matrix.
Sample Input ([Link]):
1 3
2 1 2 3
3 4 5 6
4 7 8 9

Command: ./q3 < [Link]


Sample Output:
1 2 0

Hint: For each element, check if it is the minimum in its row and the maximum
in its column. Use nested loops to iterate through all elements and track the
indices of any saddle point found.

You might also like