-
Notifications
You must be signed in to change notification settings - Fork 138
/
Copy patharraysFun.cpp
78 lines (55 loc) · 1.69 KB
/
arraysFun.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#include <iostream> // Basic IO functions
#include <cmath> // C-math functions such as sqrt, pow ...
#include <cassert> // Assertions
using namespace std;
// Function prototypes
//---------------------------------//
double scalarProduct (double [], double [], int);
double vectorSum (double [], int);
double vectorNorm (double [], int);
void printVectorVert (double xs[], int size);
// Main function
//-------------------------------
int main()
{
int size = 4 ;
double v1[] = {1.0, 2.0, 3.0, 4.0} ;
double v2[] = {3.0, 4.0, 5.0, 6.0} ;
cout << "\n\nVector v1 = " << endl ;
printVectorVert(v1, size);
cout << "\n\nVector v2 = " << endl ;
printVectorVert(v2, size);
cout << "\n\nScalar product is = " << scalarProduct(v1, v2, size) << endl;
cout << "Vector v1 sum is = " << vectorSum(v1, size) << endl;
cout << "Vector v2 Norm is = " << vectorNorm(v1, size) << endl;
assert(scalarProduct(v1, v2, size) == 50.0);
return 0 ; // Return 0 as status code.
}
// Functions Implementations
// -------------------------------- //
double scalarProduct(double xs[], double ys[], int size){
double sum = 0.0;
for (int i = 0; i <= size -1; i++){
sum = sum + xs[i] * ys[i];
}
return sum;
}
double vectorSum(double xs[], int size){
double sum = 0.0 ;
for (int i = 0; i <= size - 1; i++){
sum = sum + xs[i];
}
return sum;
}
double vectorNorm(double xs[], int size){
double sum = 0.0 ;
for (int i = 0; i <= size - 1; i++){
sum = sum + xs[i] * xs[i];
}
return sqrt(sum);
}
void printVectorVert(double xs[], int size){
for (int i = 0; i <= size -1; i++){
cout << "v[" << i << "] = " << xs[i] << endl ;
}
}