Untitled

mail@pastecode.io avatar
unknown
plain_text
3 years ago
2.1 kB
5
Indexable
Never
#include <stdio.h>
#include <stdlib.h>

void printMatrix(int *matrix, int row, int column);
int largestValue(int *matrix, int row, int column, int *largest_row, int *largest_column);
int *maxValue(int *matrix, int row, int column);

int main(void)
{
    int stu_id = 70357003;

    int r, c;
    printf("Enter the size of row for a matrix: ");
    scanf("%d", &r);
    printf("Enter the size of column for a matrix: ");
    scanf("%d", &c);

    int mat[r][c];

    for(int i = 0; i < r; i++){
        for(int j = 0; j < c; j++){
            printf("Enter value for Matrix[%d][%d]: ", i+1, j+1);
            scanf("%d", &mat[i][j]);
        }
    }

    printMatrix(mat, r, c);
    int *max;
    max = maxValue(mat, r, c);
    int max_row = (max - (int *)mat)/r + 1;
    int max_column = (max - (int *) mat)%r + 1;
    printf("The largest value in the matrix is %d.\n", *max);
    printf("The index of the largest value is Matrix[%d][%d].\n", max_row, max_column);

//    int largest_row, largest_column;
//    int max = largestValue(mat, r, c, &largest_row, &largest_column);
//    printf("The largest value in this matrix is: %d\n", max);
//    printf("The index of this value is Matrix[%d][%d]\n", largest_row, largest_column);

    printf("\n\nStudent ID: %d\n\n", stu_id);
    return 0;
}

int *maxValue(int *matrix, int row, int column){
    int max = 0, count = 0;
    int add_max;
    for(int i = 0; i < row; i++){
        for(int j = 0; j < column; j++){
            if(*(matrix + i*row+j) > max){
                max = *(matrix + i*row+j);
                add_max = (matrix + i*row+j);
            }
//            count++;
        }
    }

    return add_max;
}

void printMatrix(int *matrix, int row, int column){
    printf("Matrix in 2D format:\n");
    int count = 0;
    for(int i = 0; i < row; i++){
        for(int j = 0; j < column; j++){
            printf("%d ", *(matrix + count));
            if(j == column - 1){
                printf("\n");
            }
            count++;
        }
    }
}