---
title: "String; replace diagonal by minimum"
subject: "Computer Programming"
chapter: 7
key: "semester_1/computer_programming:string-min-matrix-diagonal"
source: https://www.bctengineer.com/q/semester_1/computer_programming/string-min-matrix-diagonal
site: BCT Engineering
---

# String; replace diagonal by minimum

Computer Programming · Chapter 7: Arrays and Strings

- Asked in: 2072 Chaitra, 2+8 marks
- Answer checked by Claude Opus 5.5 (high effort) on 2026-10-14
- Page: https://www.bctengineer.com/q/semester_1/computer_programming/string-min-matrix-diagonal
- Whole chapter: https://www.bctengineer.com/md/semester_1/computer_programming/chapter-7

## Question

What is a string? Write a program to read a 3*3 square matrix, find minimum integer value of a matrix, replace the diagonal elements by the minimum element and display it using pointer.

## Short answer

A string is a character array terminated by '\0'. The program reads 9 integers into a 3x3 matrix using a pointer, finds the minimum value via pointer arithmetic, then sets all diagonal elements (positions where i=j) to that minimum, and prints the modified matrix.

## Answer

#### String
A string is a sequence of characters stored in a character array and ended by the null character `'\0'`. It is declared as `char name[20];` and can be initialised as `char name[] = "Nepal";` (6 bytes including `'\0'`). Strings are read with `scanf("%s")`, `gets()` or `fgets()`, printed with `%s`, and handled with `<string.h>` functions such as `strlen`, `strcpy`, `strcmp` and `strcat`.

#### Program
A pointer `p` is set to the first element of the matrix. A 2D array is stored row by row, so element a[i][j] is `*(p + i*3 + j)`. The program finds the minimum with the pointer, puts it on the diagonal positions (i = j), and prints the matrix using the pointer.

```c
#include <stdio.h>

int main(void)
{
    int a[3][3], *p = &a[0][0], min;

    printf("Enter 9 integers: ");
    for (int i = 0; i < 9; i++)
        scanf("%d", p + i);              /* p + i is the address of element i */

    min = *p;
    for (int i = 1; i < 9; i++)
        if (*(p + i) < min)
            min = *(p + i);

    for (int i = 0; i < 3; i++)
        *(p + i * 3 + i) = min;          /* a[i][i] = min */

    printf("Minimum = %d\nMatrix after replacing the diagonal:\n", min);
    for (int i = 0; i < 3; i++) {
        for (int j = 0; j < 3; j++)
            printf("%4d", *(p + i * 3 + j));
        printf("\n");
    }
    return 0;
}
```

**Sample run** (matrix 7 2 9 / 4 8 6 / 5 3 1)

Input: `7 2 9 4 8 6 5 3 1`

```text
Enter 9 integers: Minimum = 1
Matrix after replacing the diagonal:
   1   2   9
   4   1   6
   5   3   1
```

## Check yourself

The answer's key points as multiple-choice questions, with the correct option and why.

1. What is a string in C?
   A. A character array ended by the null character '\0'
   B. An int array
   C. A group ended by a newline
   D. A char with fixed length 1
   Answer: A. The null character marks the end. (Key point: String definition)

2. With p = &a[0][0], which expression is a[i][j]?
   A. *(p + i + j)
   B. *(p + j*3 + i) always
   C. *(p + i*3 + j)
   D. *p + i*3 + j
   Answer: C. A 2D array is stored row by row with 3 columns. (Key point: Pointer access)

3. Which elements are replaced by the minimum?
   A. a[i][i], at offsets i*3 + i
   B. a[i][2-i]
   C. The first row
   D. Every element
   Answer: A. The diagonal has i = j. (Key point: Diagonal)

4. For 7 2 9 / 4 8 6 / 5 3 1, what are the new diagonal elements?
   A. 2, 2, 2
   B. 7, 8, 1
   C. 1, 1, 1
   D. 9, 9, 9
   Answer: C. The minimum is 1. (Key point: Sample minimum)

## Related in Computer Programming

- [Reverse array and display via pointer](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Apointer-reverse-array) — Computer Programming, chapter 9
- [Copy string converting lower to upper using pointer](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Apointer-array-elements-copy-string-upper) — Computer Programming, chapter 9, asked 3×
- [Second largest element using pointer](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Apointer-second-largest) — Computer Programming, chapter 9
- [Accessing array elements using pointer](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Apointer-access-array-elements) — Computer Programming, chapter 9
- [Sum and average between M and N using pointer](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Apointer-sum-average-range) — Computer Programming, chapter 9
- [Reverse a string using functions](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Astring-reverse-with-count-function) — Computer Programming, chapter 7, asked 2×

## Related in other subjects

- [8086 ALP: count vowels and display string](https://www.bctengineer.com/question.md?key=semester_4%2Fmicroprocessor%3Aalp-8086-count-vowels-display) — Microprocessor, chapter 3
- [Derive CST element stiffness matrix](https://www.bctengineer.com/question.md?key=semester_8%2Ffinite_element_method%3Acst-stiffness-matrix-derivation) — Finite Element Method, chapter 6
- [Transition elements; non-typical 3d element](https://www.bctengineer.com/question.md?key=semester_2%2Fengineering_chemistry%3Atm-not-typical) — Engineering Chemistry, chapter 5
- [Bar stiffness matrix and 2D truss element](https://www.bctengineer.com/question.md?key=semester_8%2Fcomputational_techniques_in_civil_engineering%3Abar-stiffness-truss-rotation) — Computational Techniques in Civil Engineering, chapter 4
