Advertisement
Guest User

Untitled

a guest
May 24th, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.54 KB | None | 0 0
  1. #include<iostream>
  2. #include<ctime>
  3. #include<string>
  4. using namespace std;
  5. void showVector(double *vectorExample, int n,string vectorName)
  6. {
  7.     cout << "Wektor" <<" "<< vectorName << endl;
  8.     for (int i = 0; i < n; i++)
  9.     {
  10.         cout << vectorExample[i] << endl;
  11.     }
  12.  
  13.  
  14. }
  15.  
  16. void setVectorValue(double*vectorExample, int n,string vectorName)
  17. {
  18.    
  19.     cout << "Wpisz wartosci wektora" <<" "<< vectorName << endl;
  20.     for (int i = 0; i < n; i++)
  21.     {
  22.         cin >> vectorExample[i];
  23.  
  24.  
  25.     }
  26.  
  27. }
  28.  
  29. double scalarByValue(double*VectorExampleA, double*VectorExampleB, int n) {
  30.  
  31.     double result=0;
  32.  
  33.     for (int i = 0; i < n; i++)
  34.     {
  35.         result += (VectorExampleA[i] * VectorExampleB[i]);
  36.  
  37.  
  38.  
  39.     }
  40.  
  41.  
  42.     return result;
  43.  
  44. }
  45.  
  46. double &scalarByReference(double*VectorExampleA, double*VectorExampleB, int n, double &resultByReference)
  47. {
  48.     for (int i = 0; i < n; i++)
  49.     {
  50.         resultByReference += (VectorExampleA[i] * VectorExampleB[i]);
  51.  
  52.  
  53.  
  54.     }
  55.  
  56.  
  57.     return resultByReference;
  58.  
  59.  
  60. }
  61.  
  62. double *scalarByPointer(double*VectorExampleA, double*VectorExampleB, int n, double *resultByPointer)
  63. {
  64.     //double*wsk = resultByPointer; //alternatywne rozwiazanie, mniej optymalne
  65.    
  66.     for (int i = 0; i < n; i++)
  67.     {
  68.         (*resultByPointer) += (VectorExampleA[i] * VectorExampleB[i]);
  69.         //(*wsk) += (VectorExampleA[i] * VectorExampleB[i]);
  70.  
  71.  
  72.  
  73.     }
  74.  
  75.  
  76.     return resultByPointer;
  77.  
  78. }
  79.  
  80. double mathExpression(double*VectorExampleA, double*VectorExampleB, int n)
  81.  
  82. {
  83.     double valA=0, valB=0;
  84.     for (int i = 0; i < n; i++)
  85.     {
  86.  
  87.         valA += pow(VectorExampleA[i], 2);
  88.         valB+= pow(VectorExampleB[i], 2);
  89.  
  90.  
  91.     }
  92.  
  93.     return ((valA + valB) / (scalarByValue(VectorExampleA, VectorExampleB, n)));
  94.  
  95.  
  96.  
  97. }
  98.  
  99.  
  100. int main() {
  101.     double *vectorA,*vectorB,resultByPointer=0,resultByReference=0;
  102.     int n;
  103.     cout << "Podaj liczbe elementow wektora\n";
  104.     cin >> n;
  105.     vectorA = new double[n];
  106.     vectorB = new double[n];
  107.     setVectorValue(vectorA,  n,"A");
  108.     cout << endl;
  109.     setVectorValue(vectorB, n,"B");
  110.     cout << endl;
  111.    
  112.     showVector(vectorA,n,"A");
  113.    
  114.     showVector(vectorB,n,"B");
  115.     cout << endl;
  116.    
  117.     cout << "Iloczyn skalarny(przez wartosc ) wynosi\n";
  118.     cout << scalarByValue(vectorA, vectorB, n)<<endl;
  119.  
  120.         cout << "Iloczyn skalarny(przez wskaznik) wynosi\n";
  121.    
  122.     cout << *scalarByPointer(vectorA, vectorB, n, &resultByPointer) << endl;
  123.  
  124.  
  125.         cout << "Iloczyn skalarny(przez referencje) wynosi\n";
  126.      
  127.         cout << scalarByReference(vectorA, vectorB, n, resultByReference) << endl;
  128.      
  129.         cout << "Wartosc wyrazenia\n";
  130.        
  131.         cout << mathExpression(vectorA, vectorB, n)<<endl;
  132.    
  133.    
  134.    
  135.    
  136.    
  137.     system("pause");
  138.     return 0;
  139. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement