Skip to content

Instantly share code, notes, and snippets.

@AhmedMourad0
Last active May 30, 2019 02:38
Show Gist options
  • Save AhmedMourad0/d8f807ec4cc6c4284071a28185af8e5a to your computer and use it in GitHub Desktop.
Save AhmedMourad0/d8f807ec4cc6c4284071a28185af8e5a to your computer and use it in GitHub Desktop.
#include <iostream>
#include <cmath>
using namespace std;
int factorial(int n) {
int fact = 1;
for (int i = 1; i <= n; ++i)
fact *= i;
return fact;
}
float calculate(int n, float x) {
float result = 1;
for (int i = 1; i <= n; ++i) {
if (i % 2 != 0)
result += pow(x, i) / factorial(i);
}
return result;
}
void print(int n, float x) {
cout << "Sum of " << n << " terms at (x = " << x << ") = " << calculate(n, x) << endl;
}
int main() {
float x;
int n;
cout << "Enter x:";
cin >> x;
cout << "Enter n:";
cin >> n;
print(n, x);
return 0;
}
#include <iostream>
#include <string>
using namespace std;
int* multiply(int arr[], const int length, int num) {
for (int i = 0; i < length; ++i)
arr[i] *= num;
return arr;
}
string toString(int arr[], int length) {
string str = "[";
for (int i = 0; i < length; ++i) {
str += to_string(arr[i]);
if (i < length - 1)
str += ", ";
else
str += "]";
}
return str;
}
int main() {
const int length = 10;
int numbers[length] = {
5,
15,
70,
9,
88,
69,
74,
87,
62,
33
};
cout << toString(numbers, length) << endl << toString(multiply(numbers, length, 2), length) << endl;
return 0;
}
#include <iostream>
#include <string>
using namespace std;
int max(const int arr[], int length) {
if (length <= 0)
return -1;
int max = arr[0];
for (int i = 0; i < length; ++i)
if (arr[i] > max)
max = arr[i];
return max;
}
int min(const int arr[], int length) {
if (length <= 0)
return -1;
int min = arr[0];
for (int i = 0; i < length; ++i)
if (arr[i] < min)
min = arr[i];
return min;
}
string toString(int arr[], int length) {
string str = "[";
for (int i = 0; i < length; ++i) {
str += to_string(arr[i]);
if (i < length - 1)
str += ", ";
else
str += "]";
}
return str;
}
int main() {
const int length = 5;
int numbers[length];
cout << "Enter the " << length << " numbers:";
for (int i = 0; i < length; ++i)
cin >> numbers[i];
cout << "numbers: " << toString(numbers, length) << endl
<< "max: " << max(numbers, length) << endl
<< "min: " << min(numbers, length) << endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment