Advertisement
Blue0066

Blatt4

Jan 21st, 2020
511
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.72 KB | None | 0 0
  1. // Main.cpp : Diese Datei enthält die Funktion "main". Hier beginnt und endet die Ausführung des Programms.
  2. //
  3.  
  4. #include <iostream>
  5. #include <string>
  6. #include <cmath>
  7. using namespace std;
  8.  
  9. /***********************************Aufgabe 2***************************/
  10. /*
  11. convert from lowercase to uppercase
  12. */
  13. string uppercaseOf(string s) {
  14.  
  15.     for (int i = 0; i < s.length(); i++)
  16.         if (s[i] >= 'a' && s[i] <= 'z')
  17.             s[i] = s[i] - 32;
  18.  
  19.     return s;
  20. }
  21. /**********************************************************************/
  22.  
  23. /********************************Aufgabe 3*****************************/
  24. bool isPalindrome(string s) {
  25.  
  26.     string text = uppercaseOf(s);
  27.     int left = 0;
  28.     int right = text.length() - 1;
  29.  
  30.     while (left < right) {
  31.  
  32.         if (text[left] != text[right]) {
  33.             return false;
  34.         }
  35.         else {
  36.             left++;
  37.             right--;
  38.         }
  39.  
  40.     }
  41.     return true;
  42.  
  43. }
  44. /**********************************************************************/
  45.  
  46. /****************************Aufgabe 5*********************************/
  47. //function
  48. double f(double x) {
  49.     return pow(x, 4.0) - 5* pow(x, 3.0) + 9 * x +3;
  50.     //return  pow(x, 3.0) - 7*pow(x,2.0) + 8*x-3;
  51. }
  52. //estimate the derivative
  53. double deriv(double a, double h) {
  54.  
  55.     return (f(a + h) - f(a)) / h;
  56. }
  57. /**********************************************************************/
  58.  
  59. /*****************************Aufgabe 7 *******************************/
  60.  
  61. double findZeros(double (*f)(double), double x) {
  62.     double newX = x;
  63.     int counter = 0;
  64.     do {
  65.         counter++;
  66.         x = newX;
  67.         cout << "The " << counter << " root is: " << x <<"\n";
  68.         newX = x - f(x) / deriv(x, 0.001);
  69.     } while (abs(f(x)) > 0.0001);
  70.  
  71.     return x;
  72. }
  73.  
  74.  
  75. /**********************************************************************/
  76.  
  77. /*****************************Aufgabe 9 *******************************/
  78.  
  79. /**********************************************************************/
  80.  
  81. /*****************************Aufgabe 10 ******************************/
  82. double calMean(double * arr, int length) {
  83.  
  84.     double sum = 0.0;
  85.  
  86.     for (int i = 0; i < length;i++) {
  87.         sum += arr[i];
  88.     }
  89.     return sum / length;
  90. }
  91.  
  92. // Standardabweichung
  93. double stdDev(double* arr, int length) {
  94.  
  95.     double mean = calMean(arr, length);
  96.     double stdDeviation = 0.0;
  97.  
  98.     for (int i = 0; i < length;i++) {
  99.         stdDeviation += pow(arr[i] - mean, 2.0);
  100.     }
  101.  
  102.     return sqrt(stdDeviation / length);
  103. }
  104. void  readValues(double* arr, int length) {
  105.     double n;
  106.  
  107.     for (int i = 0; i < length;i++) {
  108.         cout << "\n Enter a number to the array: " << endl;
  109.         cin >> n;
  110.         cout << "\n";
  111.         arr[i] = n;
  112.     }
  113.  
  114.  
  115. }
  116. int readLength() {
  117.     int length;
  118.     cout << "\n Enter the number of values in the array: " << endl;
  119.     cin >> length;
  120.     cout << "\n";
  121.  
  122.     return length;
  123. }
  124. void displayArray(double* arr, int length) {
  125.  
  126.     for (int i = 0; i < length; i++)
  127.         cout << arr[i] << "\n" << endl;
  128. }
  129.  
  130. void readAndDisplay() {
  131.     int numberOfValues = readLength();
  132.     double* arr = new double[numberOfValues];
  133.     readValues(arr, numberOfValues);
  134.     displayArray(arr, numberOfValues);
  135.  
  136.     cout << "The mean is: " << calMean(arr, numberOfValues)<< "\n\n";
  137.     cout << "The standardDeviation  is: " << stdDev(arr, numberOfValues) << "\n\n";
  138.  
  139.     delete[] arr;
  140.  
  141. }
  142. /*********************************************************************/
  143.  
  144.  
  145. int main()
  146. {
  147.    
  148.     //Test: Aaufgabe 2
  149.     string text = "karlsruhe";
  150.     cout << "The text in lowercase: " + text << " is in uppercase : " + uppercaseOf(text) + "\n\n";
  151.  
  152.     //Test: Aaufgabe 3
  153.     string s1 = "rentner";
  154.     string s2 = "aaba";
  155.     if (isPalindrome(s1))
  156.         cout << s1 + " is Palindrome.\n\n";
  157.     else
  158.         cout << s1 + " is not Palindrome.\n\n";
  159.  
  160.     //Test: Aaufgabe 7
  161.     cout << findZeros(f, 6) << "\n\n";
  162.  
  163.     //Test: Aaufgabe 9
  164.  
  165.     //Test: Aaufgabe 10
  166.     readAndDisplay();
  167. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement