Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- program.c
- #include "functions.h"
- /* Zadani su vam fileovi functions.c i functions.h
- * Ovaj file ne morate mijenjati.
- * Prema pozivima funkcija u ovom fileu trebate implementirati funkcije
- * u functions.c fileu, cije su definicije u functions.h.
- * Komentari iznad funkcija vam govore sto se ocekuje od tih funkcija.
- * */
- int main(void)
- {
- setvbuf(stdout, NULL, _IONBF, 0);
- Mat *concatmatrix, *B, *C;
- char filename[100];
- fgets(filename, 100, stdin);
- //ubij \n iz fgets-a
- filename[strlen(filename)-1] = '\0';
- B = newMatrixFromFile(filename);
- printMatrix(B);
- fgets(filename, 100, stdin);
- //ubij \n iz fgets-a
- filename[strlen(filename)-1] = '\0';
- C = newMatrixFromFile(filename);
- printMatrix(C);
- concatmatrix = concatenateMatrix(B, C);
- printMatrix(concatmatrix);
- return 0;
- }
- functions.c
- #include "functions.h"
- /* novina u odnosu na dosadasnje primjere je parametar init_value koji
- * cijelu matricu postavlja na tu predanu vrijednost, tj. inicjilizira
- * cijelu matricu na tu vrijednost */
- Mat *allocateMatrix(int rows, int cols, double init_value) {
- Mat *M = (Mat*) malloc(sizeof(Mat));
- M->rows = rows;
- M->cols = cols;
- int i,j;
- M->data = (double**) malloc(sizeof(double*) * rows);
- for (i = 0; i < rows; i++) {
- M->data[i] = (double*) malloc(sizeof(double)*cols);
- }
- for (i = 0; i < rows; i++) {
- for (j = 0; j < cols; j++) {
- M->data[i][j] = init_value;
- }
- }
- return M;
- }
- void printMatrix(Mat *M) {
- int rows, cols;
- rows = M->rows;
- cols = M->cols;
- int i,j;
- for (i = 0; i < rows; i++) {
- for (j = 0; j < cols; j++) {
- printf("%.2f ", M->data[i][j]);
- }
- printf("\n");
- }
- }
- /* funkcija prima filename i ucitava maticu iz filea
- * treba alocirati novu matricu koristeći allocateMatrix funkciju
- * nije bitno koju cete vrijednost odabrati za init_value, ionako sve
- * vrijednosti morate ucitati iz filea.
- * Podaci u matrici su tipa double pa napominjemo da koristite za format
- * "%lf" unutar fscanf-a, a ne "%f"
- * */
- Mat* newMatrixFromFile(char *filename) {
- FILE *p=fopen(filename,"r");
- int rows,cols;
- fscanf(p,"%d",&rows);
- fscanf(p,"%d",&cols);
- Mat* m=allocateMatrix(rows,cols,0);
- int i,j;
- for(i=0;i<rows;i++)
- {
- for(j=0;j<cols;j++)
- fscanf(p,"%lf",&m->data[i][j]);
- }
- return m;
- }
- /* funkcija vraca matricu koja predstavlja spoj dvije matrice vertikalno.
- * Redove druge matrice dodaje ispod redova prve.
- * Obratite pozornost da matrice mogu biti razlicitih velicina.
- * U tom slucaju gledaju se vece dimenzije. Na indeksima na kojima manja matrica
- * nema vrijednost, pretpostavite vrijednost 0.0. Primjer:
- * matrica m1:
- * 1.0 2.0 3.0
- * 4.0 5.0 6.0
- * matrica m2:
- * 1.0 2.0
- * 4.0 5.0
- * 8.0 10.0
- * U ovom slucaju spojena matrica bila bi:
- * 1.0 2.0 3.0
- * 4.0 5.0 6.0
- * 1.0 2.0 0.0
- * 4.0 5.0 0.0
- * 8.0 10.0 0.0
- * */
- Mat* concatenateMatrix(Mat *m1, Mat *m2) {
- int rows,cols;
- if(m1->rows>m2->rows)
- rows=m1->rows;
- else
- rows=m2->rows;
- if(m1->cols>m2->cols)
- cols=m1->cols;
- else
- cols=m2->cols;
- }
- functions.h
- #ifndef FUNCTIONS_H
- #define FUNCTIONS_H
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- typedef struct matrix {
- int rows;
- int cols;
- double **data;
- } Mat;
- Mat* allocateMatrix(int rows, int cols, double init_value);
- Mat* newMatrixFromFile(char *filename);
- Mat* concatenateMatrix(Mat *m1, Mat *m2);
- void printMatrix(Mat *M);
- #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement