Advertisement
Guest User

Untitled

a guest
Sep 17th, 2019
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.49 KB | None | 0 0
  1. program.c
  2.  
  3. #include "functions.h"
  4.  
  5. /* Zadani su vam fileovi functions.c i functions.h
  6.  * Ovaj file ne morate mijenjati.
  7.  * Prema pozivima funkcija u ovom fileu trebate implementirati funkcije
  8.  * u functions.c fileu, cije su definicije u functions.h.
  9.  * Komentari iznad funkcija vam govore sto se ocekuje od tih funkcija.
  10.  * */
  11.  
  12. int main(void)
  13. {
  14. setvbuf(stdout, NULL, _IONBF, 0);
  15.   Mat *concatmatrix, *B, *C;
  16.   char filename[100];
  17.  
  18.   fgets(filename, 100, stdin);
  19.   //ubij \n iz fgets-a
  20.   filename[strlen(filename)-1] = '\0';
  21.   B = newMatrixFromFile(filename);
  22.   printMatrix(B);
  23.  
  24.   fgets(filename, 100, stdin);
  25.   //ubij \n iz fgets-a
  26.   filename[strlen(filename)-1] = '\0';
  27.   C = newMatrixFromFile(filename);
  28.   printMatrix(C);
  29.  
  30.   concatmatrix = concatenateMatrix(B, C);
  31.      
  32.   printMatrix(concatmatrix);
  33.  
  34.   return 0;
  35. }
  36. functions.c
  37. #include "functions.h"
  38.  
  39. /* novina u odnosu na dosadasnje primjere je parametar init_value koji
  40.  * cijelu matricu postavlja na tu predanu vrijednost, tj. inicjilizira
  41.  * cijelu matricu na tu vrijednost */
  42. Mat *allocateMatrix(int rows, int cols, double init_value) {
  43.   Mat *M = (Mat*) malloc(sizeof(Mat));
  44.   M->rows = rows;
  45.   M->cols = cols;
  46.   int i,j;
  47.   M->data = (double**) malloc(sizeof(double*) * rows);
  48.   for (i = 0; i < rows; i++) {
  49.     M->data[i] = (double*) malloc(sizeof(double)*cols);
  50.   }
  51.   for (i = 0; i < rows; i++) {
  52.     for (j = 0; j < cols; j++) {
  53.       M->data[i][j] = init_value;
  54.     }
  55.   }
  56.   return M;
  57. }
  58.  
  59. void printMatrix(Mat *M) {
  60.   int rows, cols;
  61.   rows = M->rows;
  62.   cols = M->cols;
  63.   int i,j;
  64.   for (i = 0; i < rows; i++) {
  65.     for (j = 0; j < cols; j++) {
  66.       printf("%.2f   ", M->data[i][j]);
  67.     }
  68.     printf("\n");
  69.   }
  70. }
  71.  
  72. /* funkcija prima filename i ucitava maticu iz filea
  73.  * treba alocirati novu matricu koristeći allocateMatrix funkciju
  74.  * nije bitno koju cete vrijednost odabrati za init_value, ionako sve
  75.  * vrijednosti morate ucitati iz filea.
  76.  * Podaci u matrici su tipa double pa napominjemo da koristite za format
  77.  * "%lf" unutar fscanf-a, a ne "%f"
  78.  * */
  79. Mat* newMatrixFromFile(char *filename) {
  80. FILE *p=fopen(filename,"r");
  81. int rows,cols;
  82. fscanf(p,"%d",&rows);
  83. fscanf(p,"%d",&cols);
  84. Mat* m=allocateMatrix(rows,cols,0);
  85. int i,j;
  86. for(i=0;i<rows;i++)
  87. {
  88.     for(j=0;j<cols;j++)
  89.     fscanf(p,"%lf",&m->data[i][j]);
  90. }
  91. return m;
  92. }
  93.  
  94. /* funkcija vraca matricu koja predstavlja spoj dvije matrice vertikalno.
  95.  * Redove druge matrice dodaje ispod redova prve.
  96.  * Obratite pozornost da matrice mogu biti razlicitih velicina.
  97.  * U tom slucaju gledaju se vece dimenzije. Na indeksima na kojima manja matrica
  98.  * nema vrijednost, pretpostavite vrijednost 0.0. Primjer:
  99.  * matrica m1:
  100.  * 1.0  2.0  3.0
  101.  * 4.0  5.0  6.0
  102.  * matrica m2:
  103.  * 1.0  2.0
  104.  * 4.0  5.0
  105.  * 8.0 10.0
  106.  * U ovom slucaju spojena matrica bila bi:
  107.  *  1.0    2.0    3.0
  108.  *  4.0    5.0    6.0
  109.  *  1.0    2.0    0.0
  110.  *  4.0    5.0    0.0
  111.  *  8.0   10.0    0.0
  112.  * */
  113. Mat* concatenateMatrix(Mat *m1, Mat *m2) {
  114. int rows,cols;
  115. if(m1->rows>m2->rows)
  116. rows=m1->rows;
  117. else
  118. rows=m2->rows;
  119. if(m1->cols>m2->cols)
  120. cols=m1->cols;
  121. else
  122. cols=m2->cols;
  123.  
  124. }
  125. functions.h
  126. #ifndef FUNCTIONS_H
  127. #define FUNCTIONS_H
  128.  
  129. #include <stdio.h>
  130. #include <stdlib.h>
  131. #include <string.h>
  132.  
  133. typedef struct matrix {
  134.   int rows;
  135.   int cols;
  136.   double **data;
  137. } Mat;
  138.  
  139. Mat* allocateMatrix(int rows, int cols, double init_value);
  140.  
  141.  
  142. Mat* newMatrixFromFile(char *filename);
  143.  
  144. Mat* concatenateMatrix(Mat *m1, Mat *m2);
  145.  
  146. void printMatrix(Mat *M);
  147.  
  148.  
  149. #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement