Question

Write a user defined function named upper_half() which takes a square two dimensional array, with size as argument and prints the upper half of the array.

e.g.,
2 3 1 5 0                              2 3 1 5 0
7 1 5 3 1                                 1 5 3 1
2 5 7 8 1     The output will be  1 7 8
0 1 5 0 1                                      0 1
3 4 9 1 5                                         5

Source Code

#include <stdio.h>

void input(int matrix[][10], int);
void display(int matrix[][10], int);
void upper_half(int matrix[][10], int);

int main()
{
    int s;
    int array[10][10];

    printf("Enter size of square matrix :");
    scanf("%d", &s);

    input(array, s);
    printf("\n");
    display(array, s);
    printf("\n");
    upper_half(array, s);

    return 0;
}

void input(int matrix[][10], int n)
{
    int i, j;
    for (i = 0; i < n; i++)
    {
        for (j = 0; j < n; j++)
        {
            printf("Enter data in [%d][%d] : ", i, j);
            scanf("%d", &matrix[i][j]);
        }
    }
}

void display(int matrix[][10], int n)
{
    int i, j;
    for (i = 0; i < n; i++)
    {
        for (j = 0; j < n; j++)
        {
            printf("%d\t", matrix[i][j]);
        }
        printf("\n");
    }
}

void upper_half(int matrix[][10], int n)
{
    int i, j;
    for (i = 0; i < n; i++)
    {
        for (j = 0; j < n; j++)
        {
            if (i <= j)
            {
                printf("%d\t", matrix[i][j]);
            }
            else
            {
                printf(" \t");
            }
        }
        printf("\n");
    }
}

Output

Enter size of square matrix :4
Enter data in [0][0] : 10
Enter data in [0][1] : 11
Enter data in [0][2] : 12
Enter data in [0][3] : 13
Enter data in [1][0] : 14
Enter data in [1][1] : 15
Enter data in [1][2] : 16
Enter data in [1][3] : 17
Enter data in [2][0] : 18
Enter data in [2][1] : 19
Enter data in [2][2] : 20
Enter data in [2][3] : 21
Enter data in [3][0] : 22
Enter data in [3][1] : 23
Enter data in [3][2] : 24
Enter data in [3][3] : 25

10      11      12      13
14      15      16      17
18      19      20      21
22      23      24      25

10      11      12      13
          15      16      17
                    20      21
                              25