This repository was archived by the owner on May 18, 2024. It is now read-only.
forked from its-anaehm/CPP
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSumatoria_2.cpp
More file actions
69 lines (60 loc) · 1.41 KB
/
Sumatoria_2.cpp
File metadata and controls
69 lines (60 loc) · 1.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
/*
@author its_anaehm
@date 07-2020
@version 0.1
*/
/*
Programa que calcula la sumatoria desde un i=x ingresado por el usuario, hasta un n generado de forma aleatoria o introducido por el usuario (Esto dependiendo de su elección en el menú de opciones). El n-ésimo término está definido por: (-1^i)(x^(2-*i)) / (2*i)!
*/
#include<iostream>
#include <math.h>
#include <iomanip>
#include <cstdlib>
#include <ctime>
using namespace std;
size_t Aleatorio(void);
long double sumatoria(double , size_t);
unsigned long long factorial(size_t);
int main() {
double x;
size_t opc , valor;
cout<< "1. Aleatorio" <<endl;
cout<< "2. Determinista" <<endl;
while(opc < 1|| opc > 2) {
cin>>opc;
}
if( opc == 1){
valor=Aleatorio();
}else{
cin>>valor;
}
cin>>x;
cout<<setprecision(16)<<sumatoria(x,valor);
return 0;
}
size_t Aleatorio(void) {
srand(time(0));
size_t V= 5+rand()%(15);
}
long double sumatoria(double x , size_t v) {
long double sum=0;
if(x==0){
return 1;
}else{
for(int i=0; i<=v; i++){
sum=sum+(pow(-1,i))*(pow(x,2*i))/factorial(2*i);
}
return sum;
}
}
unsigned long long factorial(size_t n){
if(n==0||n==1){
return 1;
}else{
if (n<0){
return 0;
}else{
return n*factorial(n-1);
}
}
}