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_1.cpp
More file actions
75 lines (66 loc) · 1.8 KB
/
Sumatoria_1.cpp
File metadata and controls
75 lines (66 loc) · 1.8 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
70
71
72
73
74
75
/*
@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: x^i / i!
*/
#include<iostream>
#include <ctime>
#include <math.h>
#include <iomanip>
#include <cstdlib>
using namespace std;
long double sumatoria(double x , size_t v);
int Aleatorio(int x, int y);
unsigned long long factorial(size_t n);
int main() {
double x=0;
size_t opc=0 , valor=0;
cout << "1. Aleatorio" << endl
<< "2. Determinista" << endl;
while( opc < 1 || opc > 2 ) {
cin>>opc;
}
if( opc == 1){
valor = Aleatorio(-3, 17);
}else{
cin>>valor;
}
cin>>x;
cout<<setprecision(16)<<sumatoria(x,valor);
return 0;
}
int Aleatorio(int x, int y){
/*La definicion de la funcion aleatorio que genera de forma aleatoria la cota
superior de la sumatoria, que debe estar entre 3 y 17 inclusive.*/
srand(time(NULL));
int aleatorio=0;
aleatorio = rand() % (y - x + 1) + x;
return aleatorio;
}
long double sumatoria(double x , size_t v){
/*Escriba la definicion de la funcion sumatoria cuyo objetivo es realizarla la sumatoria pedida.*/
long double sum=0;
if(x==0){
return 1;
}else{
for(int i=0; i<=v; i++){
sum += (pow(x,i))/factorial(i);
}
return sum;
}
}
unsigned long long factorial(size_t n){
/*Escriba la definicion de la funcion factorial que calcula de forma recursiva el factorial de su parametro*/
if(n==0||n==1){
return 1;
}else{
if (n<0){
return 0;
}else{
return n*factorial(n-1);
}
}
}