Commit 77f567c9 by trkall

stack overflow

parent 097713b6
......@@ -82,7 +82,7 @@ void v2ljastus(double *A, int n)
void push(double *A, int *n, double v22rtus)
{
double *temp = (double*)realloc(A, sizeof(double*)*(1 + (*n)));
double *temp = (double*)realloc(A, (1 + (*n))*sizeof(double));
if(temp == NULL)
{
printf("Ei saa mälu eraldada!\n");
......
No preview for this file type
No preview for this file type
Elementide arv: 15
0) 0.000000000000000
1) 0.000000000000000
2) 0.000000000000000
3) 0.000000000000000
4) 0.000000000000000
5) 0.000000000000000
6) 0.000000000000000
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));
sisestus(&x, &e);
push(A, &n, x);
rekursiiv(A, &n, x, e);
v2ljastus(A, n);
// programm lpetas 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 = malloc(sizeof(double)*(*n+1));
if(temp == NULL)
{
printf("Ei saa mlu eraldada!\n");
return;
}
stack = temp;
*n = *n + 1;
*(stack+(*n)-1) = v22rtus;
printf("%d)%.15lf\t%x\n", *n, *(stack +(*n)-1), stack +*n);
}
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