Commit 8337d1a8 by trkall

suur kodune t66

parent 18f308e8
#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));
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
#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));
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 *A, 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, *(A + i));
}
fclose(output);
}
void push(double *A, int *n, float v22rtus)
{
double * temp = realloc(A, sizeof(double)*(*n+1));
if(temp == NULL)
{
printf("Ei saa mälu eraldada!\n");
return;
}
A = temp;
*n = *n + 1;
*(A+(*n)-1) = v22rtus;
printf("%d)%.15lf\t%x\n", *n, *(A +(*n)-1), A +*n);
}
Elementide arv: 15
0) 0.899999976158142
1) 0.243000000715256
2) 0.118097998201847
3) 0.068328127264977
4) 0.043046720325947
5) 0.028528235852718
6) 0.019552813842893
7) 0.013726075179875
8) 0.009810106828809
9) 0.007109745871276
10) 0.005210428033024
11) 0.003853451460600
12) 0.002871592063457
13) 0.002153693931177
14) 0.001624182332307
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
int L = 15; void sisestus(double*, double*);
int MAX = 1; void rekursiiv(double*, int, double, double);
int MIN = 0; double absoluutv22rtus(double);
double power(double, int);
/* int push(double*, int, double);
1. klaviatuurilt sisestatakse reaalarvulised X (|X|<1) ja e (0<e<1); void v2ljastus(double *, int n);
2. rekursiivse funktsiooni abil moodustatakse reaalarvuline massiiv A
elementidega
A1 = X,
A2 = X^3/3,
A3 = X^5/5,
. . .
kuni massiivi A elementide arv L kas vastab tingimusele |A[L] – A[L–1]| <=e või (kui see tingimus ei ole rahuldatud) L = 15;
3. faili F väljastatakse massiivi A elementide arv L ning elemendid
koos indeksitega.
*/
double aste(double, int);
double reku(double);
double absolutevodka(double);
int main(void) int main(void)
{ {
double x; int n = 0;
double e; double x, e;
double* A = (double*)malloc(sizeof(double*)); double * A = (double*)malloc(sizeof(double));
printf("Sisestage x:"); sisestus(&x, &e);
scanf("%lf", &x); n = push(A, n, x);
printf("\nSisestage e:"); rekursiiv(A, n, x, e);
scanf("%lf", &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));
}
double aste(double x, int y) void rekursiiv(double *A, int n, double x, double e)
{ {
int i; double vahe = 0;
for (i = 1;i < y; i++) if(n>1)
{ {
x*=x; vahe = *(A+(n-1)) -*(A+(n-2));
} }
return x; if((n + 1) <= 15 && absoluutv22rtus(vahe) <= e)
{
double vv = power(x,(n+1)*2-1)/((double)((n+1))*2-1);
n = push(A, n, vv);
rekursiiv(A, n, x, e);
}
} }
double absolutevodka(double x) double absoluutv22rtus(double v22rtus)
{ {
if( x < 0) if(v22rtus < 0)
{ return -1.0f* v22rtus;
x *= -1; else
} return v22rtus;
return x; }
double power(double g, int h)
{
if(h > 0)
{
return g * power(g, h-1);
}
else return 1;
}
void v2ljastus(double *A, 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, *(A + i));
}
fclose(output);
}
int push(double *A, int n, double v22rtus)
{
A = (double*)realloc(A, sizeof(double*)*(1 + n));
n++;
*(A+(n-1)) = v22rtus;
printf("%d)%.15lf\t%x\n", n, *(A +(n-1)), *(A + n));
return n;
} }
No preview for this file type
Elementide arv: 1
0) 0.900000000000000
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