Commit cf6031aa by trkall

eksam

parent 8c96e8fb
#include <stdio.h>
#include <stdlib.h>
/**
annab m2lu muutujale newContents, suurusega stackElementT suuruse korrutatud muutujaga maxSize
kui mlu andmine ei t66ta, kirjutatakse faili, mille nimi koodis on stderr, hunniku x'e ning pannakse fail kinni s6numiga 1
**/
double *sisselugemine(double*, int*, char*);
double *arvutus(double*, double*, int);
void v2ljastus(double*, int, char*);
int main(void)
{
double *A = (double*)malloc(sizeof(double));
double *B = (double*)malloc(sizeof(double));
int m;
char* FileName = "eksamtxt.txt";
char* FileName2 = "l6pp.txt";
A = sisselugemine(A, &m, FileName);
B = arvutus(A, B, m);
v2ljastus(B, m, FileName2);
free(A);
free(B);
return 0;
}
double *sisselugemine(double *A, int *m, char* FileName)
{
FILE *input;
int i = 0;
double buf;
double* temp;
input = fopen(FileName, "r");
while(fscanf(input,"%lf",&buf)!=EOF)
{
temp = (double*)realloc(A, sizeof(double)*(i+1));
if( temp == NULL)
{
printf("Memory Allocation Failed");
fclose(input);
free(A);
exit(1);
}
A = temp;
*(A+i) = buf ;
i++;
}
*m = i;
fclose(input);
return A;
}
double *arvutus(double *A, double *B, int m)
{
int i, n;
double temp = 0;
for(i=0;i<m;i++)
{
temp += *(A+i);
}
*(B+0) = temp;
for(n=1;n<m-1;n++)
{
B = (double*)realloc(B, sizeof(double)*(n+1));
*(B + n) = *(B +(n-1)) - *(A+n);
}
return B;
}
void v2ljastus(double* B, int m, char* FileName)
{
FILE *output;
int i;
output = fopen(FileName, "w");
for(i = 0;i < m-1;i++)
{
fprintf(output,"%d)\t%0.2lf\n", i+1, *(B+i));
}
fclose(output);
}
4.5
-23
128
55
0.77
13
-7.5
0.85
1) 171.62
2) 194.62
3) 66.62
4) 11.62
5) 10.85
6) -2.15
7) 5.35
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment