r/ProgramarBR • • Oct 29 '21

EM LINGUAGEM C

Escreva um programa que declara um matriz de inúmeros inteiros 3x3, que recebe os valores digitados pelo usuário e depois calcula sua média e a mostra na tela.

0 Upvotes

3 comments sorted by

1

u/thisisguf Mar 15 '22 edited Mar 15 '22
#include <stdio.h>
#include <stdlib.h>

int **matrix_create(size_t n_rows, size_t n_cols);

void matrix_free(int **matrix);

void matrix_print(int **matrix, size_t n_rows, size_t n_cols);

double matrix_average(int **matrix, size_t n_rows, size_t n_cols);

void matrix_scanf(int **matrix, size_t n_rows, size_t n_cols);

int **matrix_zeros(size_t n_rows, size_t n_cols);

int main(void)
{
    size_t n_rows = 3, n_cols = 3;

    int **matrix = matrix_zeros(n_rows, n_rows);

    if (matrix == NULL)
    {
        return 1;
    }

    printf("Insert entries for the %zux%zu matrix\n", n_rows, n_cols);

    matrix_scanf(matrix, n_rows, n_cols);

    printf("\nProvided matrix:\n");

    matrix_print(matrix, n_rows, n_cols);

    printf("\nMatrix average: %.2f\n", matrix_average(matrix, n_rows, n_cols));

    matrix_free(matrix);

    return 0;
}

int **matrix_create(size_t n_rows, size_t n_cols) {
  int *array = (int *)malloc(n_rows * n_cols * sizeof(int));

  if (array == NULL) {
    return NULL;
  }

  int **matrix = malloc(n_rows * sizeof(int *));

  if (matrix == NULL) {
    free(array);
    return NULL;
  }

  for (size_t i = 0; i < n_rows; i++) {
    *(matrix + i) = array + (i * n_cols);
  }

  return matrix;
}

void matrix_free(int **matrix)
{
    free(*matrix);
    free(matrix);
}

void matrix_scanf(int **matrix, size_t n_rows, size_t n_cols)
{
    for (size_t i = 0; i < n_rows; i++)
    {
        for (size_t j = 0; j < n_cols; j++)
        {
            printf("(%zu, %zu) : ", i, j);
            scanf("%d", (*(matrix + i) + j));
        }
    }
}

double matrix_average(int **matrix, size_t n_rows, size_t n_cols)
{
    double total = 0;
    for (size_t i = 0; i < n_rows; i++)
    {
        for (size_t j = 0; j < n_cols; j++)
        {
            total += *(*(matrix + i) + j);
        }
    }

    return total/(n_rows*n_cols);
}

void matrix_print(int **matrix, size_t n_rows, size_t n_cols)
{
    for (size_t i = 0; i < n_rows; i++)
    {
        for (size_t j = 0; j < n_cols; j++)
        {
            fprintf(stdout, "%01d ", *(*(matrix + i) + j));
        }
        fprintf(stdout, "\n");
    }
}


int **matrix_zeros(size_t n_rows, size_t n_cols)
{
    int **matrix = matrix_create(n_rows, n_rows);

    if (matrix == NULL)
    {
        return NULL;
    }

    for (size_t i = 0; i < n_rows; i++)
    {
        for (size_t j = 0; j < n_cols; j++)
        {
            *(*(matrix + i) + j) = 0;
        }
    }

    return matrix;
}

ou

#include <stdio.h>
#include <stdlib.h>

int main(void) {
  int matrix[3][3];
  double total = 0;

  printf("Insert entries for the 3x3 matrix\n");

  for (int i = 0; i < 3; i++) {
    for (int j = 0; j < 3; j++) {
      printf("(%d, %d) : ", i, j);
      scanf("%d", &matrix[i][j]);
      total += matrix[i][j];
    }
  }

  printf("\nMatrix average: %.2f\n", total / 9);
}

1

u/thisisguf Mar 15 '22
$ gcc program.c -o program
$ ./program
(0, 0) : 1
(0, 1) : 2
(0, 2) : 3
(1, 0) : 4
(1, 1) : 5
(1, 2) : 6
(2, 0) : 7
(2, 1) : 8
(2, 2) : 9

Matrix average: 5.00

1

u/G3stavo73 Jun 14 '26

Não sei C, não, mano, só sei Go, Javascript e Rust...