Untitled

mail@pastecode.io avatar
unknown
c_cpp
2 years ago
1.2 kB
3
Indexable
Never
#include <stdio.h>
#include <stdlib.h>

double** make_matrix(int m, int n) {
  double** a;
  int k;
  a = calloc(m, sizeof(double*));
  for (k = 0; k < m; k += 1)
    a[k] = calloc(n, sizeof(double));
  return a;
}

void print_lin_program(int m, int n, double * b, double * c, double ** a) {
  int i,j;
  printf("max z = %2.3lfx%d ", c[0], 0);
  for (i = 1; i < n; i +=1) {
    printf("+%2.3lfx%d ", c[i], i);
  }
  printf("\n");
  for (i = 0; i < m; i +=1) {
    for (j = 0; j < n; j+=1) {
    printf("+ %2.3lfx%d ", a[i][j], j);
    }
    printf(" \u2264 %lf \n", b[i]);
  }
}

int main() {
  int m, n, i, j;
  double * b, * c;

  // & berättar för compiler att m och n ska modifieras (läsas som int?)
  scanf("%d %d", &m, &n);

  double ** a = make_matrix(m,n);
  c = calloc(n, sizeof(int));
  b = calloc(m, sizeof(int));

  for(i = 0; i < n; i+=1) {
    scanf("%lf", &c[i]);
  }
  for (i = 0; i < n; i+=1) {
    for (j = 0; j < m; j+=1) {
      scanf("%lf", &a[i][j]);
    }
    printf("\n");
  }

  for (i = 0; i < m; i+=1) {
    scanf("%lf", &b[i]);
  }
  print_lin_program(m,n,b,c,a);
  // måste loopa igenom varje rad
  free(a);
  free(b);
  free(c);
  return 0;

}