Commit 097713b6 by trkall

kodune

parent 8337d1a8
...@@ -5,18 +5,19 @@ void sisestus(double*, double*); ...@@ -5,18 +5,19 @@ void sisestus(double*, double*);
void rekursiiv(double*, int *, double, double); void rekursiiv(double*, int *, double, double);
double absoluutv22rtus(double); double absoluutv22rtus(double);
double power(double, int); double power(double, int);
void push(double*, int*, float); void push(double*, int*, double);
void v2ljastus(double *, int n); void v2ljastus(double *, int n);
int main(void) int main(void)
{ {
int n = 0; int n = 0;
double x, e; double x, e;
double * A = malloc(sizeof(double)); double *A = (double*)malloc(sizeof(double));
sisestus(&x, &e); sisestus(&x, &e);
push(A, &n, x); push(A, &n, x);
rekursiiv(A, &n, x, e); rekursiiv(A, &n, x, e);
v2ljastus(A, n); v2ljastus(A, n);
free(A);
// programm lõpetas töö edukalt // programm lõpetas töö edukalt
return 0; return 0;
} }
...@@ -39,8 +40,8 @@ void sisestus(double *x, double *e) ...@@ -39,8 +40,8 @@ void sisestus(double *x, double *e)
void rekursiiv(double *A, int *n, double x, double e) void rekursiiv(double *A, int *n, double x, double e)
{ {
double vahe = *(A+(*n-1)) -*(A+(*n-2)); double vahe = *(A+((*n)-1)) -*(A+((*n)-2));
if((*n) + 1 <= 15 && absoluutv22rtus(vahe) <= e) if(((*n) + 1 <= 15) && (absoluutv22rtus(vahe) <= e))
{ {
double vv = power(x,((*n)+1)*2-1)/((double)(((*n)+1))*2-1); double vv = power(x,((*n)+1)*2-1)/((double)(((*n)+1))*2-1);
push(A, n, vv); push(A, n, vv);
...@@ -65,7 +66,7 @@ double power(double g, int h) ...@@ -65,7 +66,7 @@ double power(double g, int h)
else return 1; else return 1;
} }
void v2ljastus(double *stack, int n) void v2ljastus(double *A, int n)
{ {
char * outputFilename = "l6pptulemus.txt"; char * outputFilename = "l6pptulemus.txt";
FILE *output; FILE *output;
...@@ -74,21 +75,21 @@ void v2ljastus(double *stack, int n) ...@@ -74,21 +75,21 @@ void v2ljastus(double *stack, int n)
int i; int i;
for(i = 0; i < n; i++) for(i = 0; i < n; i++)
{ {
fprintf(output, "%d) %.15lf\n",i, *(stack + i)); fprintf(output, "%d) %.15lf\n",i, *(A + i));
} }
fclose(output); fclose(output);
} }
void push(double *stack, int *n, float v22rtus) void push(double *A, int *n, double v22rtus)
{ {
double * temp = realloc(stack, sizeof(double)*(*n+1)); double *temp = (double*)realloc(A, sizeof(double*)*(1 + (*n)));
if(temp == NULL) if(temp == NULL)
{ {
printf("Ei saa mälu eraldada!\n"); printf("Ei saa mälu eraldada!\n");
return; return;
} }
stack = temp; A = temp;
*n = *n + 1; *n = *n + 1;
*(stack+(*n)-1) = v22rtus; *(A+((*n)-1)) = v22rtus;
printf("%d)%.15lf\t%x\n", *n, *(stack +(*n)-1), stack +*n); ///printf("%d)%.15lf\t%x\n", *n, *(A +((*n)-1)), *(A +(*n))); ///.15lf
} }
No preview for this file type
No preview for this file type
Elementide arv: 15
0) 0.100000000000000
1) 0.000333333333333
2) 0.000002000000000
3) 0.000000014285714
4) 0.000000000111111
5) 0.000000000000909
6) 0.000000000000008
7) 0.000000000000000
8) 0.000000000000000
9) 0.000000000000000
10) 0.000000000000000
11) 0.000000000000000
12) 0.000000000000000
13) 0.000000000000000
14) 0.000000000000000
#include <stdio.h>
#include <stdlib.h>
void sisestus(double*, double*);
void rekursiiv(double*, int *, double, double);
double absoluutv22rtus(double);
double power(double, int);
void push(double*, int*, float);
void v2ljastus(double *, int n);
int main(void)
{
int n = 0;
double x, e;
double * A = malloc(sizeof(double)*15);
sisestus(&x, &e);
push(A, &n, x);
rekursiiv(A, &n, x, e);
v2ljastus(A, n);
// programm lõpetas töö edukalt
return 0;
}
void sisestus(double *x, double *e)
{
do
{
printf("Sisesta reaalarvuline X (|x| < 1)");
scanf("%lf", x);
}
while(!(-1 < *x && *x < 1));
do
{
printf("Sisesta reaalarvuline e (0 < e <1)");
scanf("%lf", e);
}
while(!(0 < *e && *e < 1));
}
void rekursiiv(double *A, int *n, double x, double e)
{
double vahe = *(A-*n-1) -*(A-*n-2);
if((*n) + 1 <= 15 && absoluutv22rtus(vahe) <= e)
{
double vv = power(x,((*n)+1)*2-1)/((double)(((*n)+1))*2-1);
push(A, n, vv);
rekursiiv(A, n, x, e);
}
}
double absoluutv22rtus(double v22rtus)
{
if(v22rtus < 0)
return -1.0f* v22rtus;
else
return v22rtus;
}
double power(double g, int h)
{
if(h > 0)
{
return g * power(g, h-1);
}
else return 1;
}
void v2ljastus(double *stack, int n)
{
char * outputFilename = "l6pptulemus.txt";
FILE *output;
output = fopen(outputFilename, "w");
fprintf(output, "Elementide arv: %d\n", n);
int i;
for(i = 0; i < n; i++)
{
fprintf(output, "%d) %.15lf\n",i, *(stack + i));
}
fclose(output);
}
void push(double *stack, int *n, float v22rtus)
{
double * temp = realloc(stack, sizeof(double)*(*n+1));
if(temp == NULL)
{
printf("Ei saa mälu eraldada!\n");
return;
}
stack = temp;
*n = *n + 1;
*(stack+(*n)-1) = v22rtus;
printf("%d)%.15lf\t%x\n", *n, *(stack +(*n)-1), stack +*n);
}
File added
File added
Elementide arv: 15
0) 0.100000001490116
1) 0.000333333329763
2) 0.000001999999995
3) 0.000000014285714
4) 0.000000000111111
5) 0.000000000000909
6) 0.000000000000008
7) 0.000000000000000
8) 0.000000000000000
9) 0.000000000000000
10) 0.000000000000000
11) 0.000000000000000
12) 0.000000000000000
13) 0.000000000000000
14) 0.000000000000000
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