---
title: "Pass array; transpose 3×3 matrix"
subject: "Computer Programming"
chapter: 7
key: "semester_1/computer_programming:pass-array-transpose-3x3"
source: https://www.bctengineer.com/q/semester_1/computer_programming/pass-array-transpose-3x3
site: BCT Engineering
---

# Pass array; transpose 3×3 matrix

Computer Programming · Chapter 7: Arrays and Strings

- Asked in: 2069 Chaitra, 2+6 marks
- Video: [C_57 Two Dimensional Array program 2 | Program to print Transpose of Matrix](https://www.youtube.com/watch?v=EmqelMa54Y4) — Jenny's Lectures CS IT
- Answer checked by Claude Opus 5.5 (high effort) on 2026-10-14
- Page: https://www.bctengineer.com/q/semester_1/computer_programming/pass-array-transpose-3x3
- Whole chapter: https://www.bctengineer.com/md/semester_1/computer_programming/chapter-7

## Question

Explain how array can be passed to a function. Write a program to transpose a 3*3 matrix. Two matrix are input from the main () function and pass to a user defined function with argument as array. The result is displayed from the main function.

## Short answer

The array name gives the address. The transpose function copies element a[i][j] to t[j][i]. In main, read two 3×3 matrices, call transpose on each, and display the transposed matrices.

## Answer

#### Passing an array to a function
Only the array name is written in the function call, for example `transpose(a, t);`. The name gives the address of the first element, so the called function works on the original array (call by reference) and no copy is made. The parameter is declared as an array (`int a[3][3]`, or `int a[][3]`); for 2D arrays the column size is compulsory. Changes made to the array inside the function are visible in the caller.

#### Program
Both matrices are read in `main()` and passed to the function `transpose(a, t)`, which stores a[i][j] in t[j][i]. The transposed matrices are then displayed from `main()`.

```c
#include <stdio.h>

void transpose(int a[3][3], int t[3][3])
{
    for (int i = 0; i < 3; i++)
        for (int j = 0; j < 3; j++)
            t[j][i] = a[i][j];
}

int main(void)
{
    int a[3][3], b[3][3], ta[3][3], tb[3][3];
    printf("Enter first matrix (9 values): ");
    for (int i = 0; i < 3; i++)
        for (int j = 0; j < 3; j++)
            scanf("%d", &a[i][j]);
    printf("Enter second matrix (9 values): ");
    for (int i = 0; i < 3; i++)
        for (int j = 0; j < 3; j++)
            scanf("%d", &b[i][j]);

    transpose(a, ta);
    transpose(b, tb);

    printf("Transpose of first matrix:\n");
    for (int i = 0; i < 3; i++) {
        for (int j = 0; j < 3; j++)
            printf("%4d", ta[i][j]);
        printf("\n");
    }
    printf("Transpose of second matrix:\n");
    for (int i = 0; i < 3; i++) {
        for (int j = 0; j < 3; j++)
            printf("%4d", tb[i][j]);
        printf("\n");
    }
    return 0;
}
```

**Sample run**

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

```text
Enter first matrix (9 values): Enter second matrix (9 values): Transpose of first matrix:
   1   4   7
   2   5   8
   3   6   9
Transpose of second matrix:
   9   6   3
   8   5   2
   7   4   1
```

## Check yourself

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

1. Why does the called function work on the original array?
   A. A copy is made and returned
   B. Arrays are global automatically
   C. The array name gives the address of the first element
   D. The function uses pointers to a temporary
   Answer: C. No copy is made; the address is passed. (Key point: Call by address)

2. Which statement performs the transpose?
   A. t[j][i] = a[i][j]
   B. t[i][j] = a[j][i] only on the diagonal
   C. t[i][j] = a[i][j]
   D. t[i][i] = a[j][j]
   Answer: A. Rows become columns, so element (i,j) goes to (j,i). (Key point: Transpose rule)

3. In a 2D array parameter, which size is compulsory?
   A. The row size
   B. Neither
   C. The column size
   D. Both only when initialised
   Answer: C. The column size is needed to locate a[i][j]. (Key point: Column size)

## Related in Computer Programming

- [Pass student array; display passed students](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Apass-whole-array-student-info) — Computer Programming, chapter 7
- [Switch versus user defined function](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Aswitch-vs-user-function) — Computer Programming, chapter 5
- [Significance of arrays; multiply 3×3 matrices](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Aarray-significance-multiply-matrices) — Computer Programming, chapter 7
- [Sort array using a function](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Aarray-sort-function) — Computer Programming, chapter 7
- [Transpose m×n matrix in FORTRAN](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Afortran-matrix-transpose) — Computer Programming, chapter 11
- [Pass array; largest and smallest](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Apass-array-largest-smallest) — Computer Programming, chapter 7

## Related in other subjects

- [Protected specifier; student/test/result program](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Aprotected-three-class-program) — Object Oriented Programming, chapter 6
- [Transpose of matrix using OOP](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Amatrix-transpose-oop) — Object Oriented Programming, chapter 1
- [Default argument and overloading program](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Adefault-arg-overloading-program) — Object Oriented Programming, chapter 3
- [Exception with argument and specification program](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Aexception-program-arg-specification) — Object Oriented Programming, chapter 10
