MY CODE

mail@pastecode.io avatar
unknown
plain_text
2 years ago
2.8 kB
2
Indexable
Never
#include <stdio.h>

int main()
{
int arr1[3][3], i, j;
    int max_element = 0;
    int loop = 0;
    printf("\tLEKOPANE KENALEMANG 22001265 CODE :)\t \n");
    while (loop == 0) {

        /* Stored values into the array 1*/
        printf("Input elements in the matrix :\n");

        for (i = 0; i < 3; i++)
        {
            for (j = 0; j < 3; j++)
            {
                printf("element - [%d],[%d] : ", i, j);
                if (scanf_s("%d", &arr1[i][j]) != 1)
                {
                    printf("Invalid I.d entered\n");
                }
            }
        }
        // displayed matrix 3
        printf("\nThe matrix is : \n");

        for (i = 0; i < 3; i++)
        {
            printf("\n");
            for (j = 0; j < 3; j++)
            {
                printf("%d\t", arr1[i][j]);
            };
        }
        printf("\n\n");
        // odd elements in matrix 4
        printf("The odd elements in matrix: ");
        for(i=0;i<3;i++)
        {
          for(j=0;j<3;j++) {
              if(arr1[i][j] % 2 != 0) {
                printf("%d, ",arr1[i][j]);
              }
          }
        }
        printf("\n\n");
        // ascending order matrix 5
        int tmp,l;
        for (int i = 0; i < 3; i++) {
            for (int j = 0; j < 3; j++) {
                tmp = arr1[i][j];
                l = j + 1;
                for (int k = i; k < 3; k++) {
                    while (l < 3) {
                        if (tmp > arr1[k][l]) {
                            tmp = arr1[k][l];
                            arr1[k][l] = arr1[i][j];
                            arr1[i][j] = tmp;
                        }
                        l++;
                    }
                    l = 0;
                }
            }
        }
        printf("The ascending order of  matrix: \n");
        for (i = 0; i < 3; i++) {
            for (j = 0; j < 3; j++) {
                printf("%d ", arr1[i][j]);
            }
            printf("\n");
        }

        // largest element in matrix 6
        printf("\n\n");
        printf("The largets element in matrix is: ");
        for (i = 0; i < 3; i++) {
            for (j = 0; j < 3; j++) {
                if(arr1[i][j] > max_element) {
                    max_element = arr1[i][j];
                }
            }
        }
  printf("%d\n\n", max_element);

        // square all elements in matrix 7
        printf("Square all element in matrix: ");
        for (i = 0; i < 3; i++) {
            for (j = 0; j < 3; j++) {
                printf("%d ", arr1[i][j]*arr1[i][j]);
            }
            printf("\n");
        }
        printf("\n\n");
    }
return(0);
}