Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream>
- #include<ctime>
- #include<string>
- using namespace std;
- void showVector(double *vectorExample, int n,string vectorName)
- {
- cout << "Wektor" <<" "<< vectorName << endl;
- for (int i = 0; i < n; i++)
- {
- cout << vectorExample[i] << endl;
- }
- }
- void setVectorValue(double*vectorExample, int n,string vectorName)
- {
- cout << "Wpisz wartosci wektora" <<" "<< vectorName << endl;
- for (int i = 0; i < n; i++)
- {
- cin >> vectorExample[i];
- }
- }
- double scalarByValue(double*VectorExampleA, double*VectorExampleB, int n) {
- double result=0;
- for (int i = 0; i < n; i++)
- {
- result += (VectorExampleA[i] * VectorExampleB[i]);
- }
- return result;
- }
- double &scalarByReference(double*VectorExampleA, double*VectorExampleB, int n, double &resultByReference)
- {
- for (int i = 0; i < n; i++)
- {
- resultByReference += (VectorExampleA[i] * VectorExampleB[i]);
- }
- return resultByReference;
- }
- double *scalarByPointer(double*VectorExampleA, double*VectorExampleB, int n, double *resultByPointer)
- {
- //double*wsk = resultByPointer; //alternatywne rozwiazanie, mniej optymalne
- for (int i = 0; i < n; i++)
- {
- (*resultByPointer) += (VectorExampleA[i] * VectorExampleB[i]);
- //(*wsk) += (VectorExampleA[i] * VectorExampleB[i]);
- }
- return resultByPointer;
- }
- double mathExpression(double*VectorExampleA, double*VectorExampleB, int n)
- {
- double valA=0, valB=0;
- for (int i = 0; i < n; i++)
- {
- valA += pow(VectorExampleA[i], 2);
- valB+= pow(VectorExampleB[i], 2);
- }
- return ((valA + valB) / (scalarByValue(VectorExampleA, VectorExampleB, n)));
- }
- int main() {
- double *vectorA,*vectorB,resultByPointer=0,resultByReference=0;
- int n;
- cout << "Podaj liczbe elementow wektora\n";
- cin >> n;
- vectorA = new double[n];
- vectorB = new double[n];
- setVectorValue(vectorA, n,"A");
- cout << endl;
- setVectorValue(vectorB, n,"B");
- cout << endl;
- showVector(vectorA,n,"A");
- showVector(vectorB,n,"B");
- cout << endl;
- cout << "Iloczyn skalarny(przez wartosc ) wynosi\n";
- cout << scalarByValue(vectorA, vectorB, n)<<endl;
- cout << "Iloczyn skalarny(przez wskaznik) wynosi\n";
- cout << *scalarByPointer(vectorA, vectorB, n, &resultByPointer) << endl;
- cout << "Iloczyn skalarny(przez referencje) wynosi\n";
- cout << scalarByReference(vectorA, vectorB, n, resultByReference) << endl;
- cout << "Wartosc wyrazenia\n";
- cout << mathExpression(vectorA, vectorB, n)<<endl;
- system("pause");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement