Commit ca1b1ab6 by aostap

Add new file

parents
Showing with 81 additions and 0 deletions
//Programmeerimine 2, kodutöö 2 , variant 21
#include <stdio.h>
#include <math.h>
int sisestus1 (char []); // e
int sisestus2 (char []);// x
double calculate_A(int , double );
void valjastus (FILE *, int , double []);
int main() {
double A[15];
double X = sisestus1 ("Sisesta reaalarvuline X : \n");
double e = sisestus2 ("Sisesta reaalarvuline E : \n");
int L = 1;
A[0] = X;
while (L < 15 && fabs(A[L-1] - A[L-2]) > e) {
A[L] = calculate_A(L+1, X);
L++;
}
printf("A elementide arv: %d\n", L);
FILE *fp; //faili avamine ja kontroll kas on see olemas
fp = fopen("F.txt", "w");
if (fp == NULL){
printf("File 'F1.txt' not found");
return 1;}
valjastus(fp,L, A);
fclose(fp); // faili kinni panemine
return 0;
}
int sisestus1 (char text[]) // E sisestus
{
float nr;
do {
printf("%s", text);
scanf ("%f", &nr);
}
while(nr<0 || nr>1); // 0<e<1
return nr;
}
int sisestus2 (char text[]) //X-i sisestus
{
float arv;
do {
printf("%s", text);
scanf ("%f", &arv);
}
while(fabs(arv) > 0 ); //|x|<1
return arv;
}
double calculate_A(int L, double X) {
if (L == 1) {
return X;
}
double sum = 0;
for (int i = 1; i <= L-1; i++) {
sum += ((2*i-1) * calculate_A(i, X));
}
return -1.0 / L * pow(X, 2*L-3) * sum;
}
void valjastus (FILE *fp, int L, double A[]){
fprintf(fp, "A elemintide arv %d\n", L);
for (int i = 0; i < L; i++) {
fprintf( fp,"A[%d] \t%lf\n", i+1, A[i]);
}}
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