Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Main.cpp : Diese Datei enthält die Funktion "main". Hier beginnt und endet die Ausführung des Programms.
- //
- #include <iostream>
- #include <string>
- #include <cmath>
- using namespace std;
- /***********************************Aufgabe 2***************************/
- /*
- convert from lowercase to uppercase
- */
- string uppercaseOf(string s) {
- for (int i = 0; i < s.length(); i++)
- if (s[i] >= 'a' && s[i] <= 'z')
- s[i] = s[i] - 32;
- return s;
- }
- /**********************************************************************/
- /********************************Aufgabe 3*****************************/
- bool isPalindrome(string s) {
- string text = uppercaseOf(s);
- int left = 0;
- int right = text.length() - 1;
- while (left < right) {
- if (text[left] != text[right]) {
- return false;
- }
- else {
- left++;
- right--;
- }
- }
- return true;
- }
- /**********************************************************************/
- /****************************Aufgabe 5*********************************/
- //function
- double f(double x) {
- return pow(x, 4.0) - 5* pow(x, 3.0) + 9 * x +3;
- //return pow(x, 3.0) - 7*pow(x,2.0) + 8*x-3;
- }
- //estimate the derivative
- double deriv(double a, double h) {
- return (f(a + h) - f(a)) / h;
- }
- /**********************************************************************/
- /*****************************Aufgabe 7 *******************************/
- double findZeros(double (*f)(double), double x) {
- double newX = x;
- int counter = 0;
- do {
- counter++;
- x = newX;
- cout << "The " << counter << " root is: " << x <<"\n";
- newX = x - f(x) / deriv(x, 0.001);
- } while (abs(f(x)) > 0.0001);
- return x;
- }
- /**********************************************************************/
- /*****************************Aufgabe 9 *******************************/
- /**********************************************************************/
- /*****************************Aufgabe 10 ******************************/
- double calMean(double * arr, int length) {
- double sum = 0.0;
- for (int i = 0; i < length;i++) {
- sum += arr[i];
- }
- return sum / length;
- }
- // Standardabweichung
- double stdDev(double* arr, int length) {
- double mean = calMean(arr, length);
- double stdDeviation = 0.0;
- for (int i = 0; i < length;i++) {
- stdDeviation += pow(arr[i] - mean, 2.0);
- }
- return sqrt(stdDeviation / length);
- }
- void readValues(double* arr, int length) {
- double n;
- for (int i = 0; i < length;i++) {
- cout << "\n Enter a number to the array: " << endl;
- cin >> n;
- cout << "\n";
- arr[i] = n;
- }
- }
- int readLength() {
- int length;
- cout << "\n Enter the number of values in the array: " << endl;
- cin >> length;
- cout << "\n";
- return length;
- }
- void displayArray(double* arr, int length) {
- for (int i = 0; i < length; i++)
- cout << arr[i] << "\n" << endl;
- }
- void readAndDisplay() {
- int numberOfValues = readLength();
- double* arr = new double[numberOfValues];
- readValues(arr, numberOfValues);
- displayArray(arr, numberOfValues);
- cout << "The mean is: " << calMean(arr, numberOfValues)<< "\n\n";
- cout << "The standardDeviation is: " << stdDev(arr, numberOfValues) << "\n\n";
- delete[] arr;
- }
- /*********************************************************************/
- int main()
- {
- //Test: Aaufgabe 2
- string text = "karlsruhe";
- cout << "The text in lowercase: " + text << " is in uppercase : " + uppercaseOf(text) + "\n\n";
- //Test: Aaufgabe 3
- string s1 = "rentner";
- string s2 = "aaba";
- if (isPalindrome(s1))
- cout << s1 + " is Palindrome.\n\n";
- else
- cout << s1 + " is not Palindrome.\n\n";
- //Test: Aaufgabe 7
- cout << findZeros(f, 6) << "\n\n";
- //Test: Aaufgabe 9
- //Test: Aaufgabe 10
- readAndDisplay();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement