about summary refs log tree commit diff
path: root/cpptour/myvec.cc
diff options
context:
space:
mode:
authorNguyễn Gia Phong <vn.mcsinyx@gmail.com>2019-07-21 03:21:00 +0700
committerNguyễn Gia Phong <vn.mcsinyx@gmail.com>2019-07-21 03:21:00 +0700
commit029688e143109344989b1529259e391822abb0aa (patch)
tree556f96273a5caed7df2e70a6ba253bcc7627bd33 /cpptour/myvec.cc
parentbe6678fbca007e73d69c9a9c5cddb8241a987149 (diff)
downloadcp-029688e143109344989b1529259e391822abb0aa.tar.gz
[cpptour] Learn the basis
Diffstat (limited to 'cpptour/myvec.cc')
-rw-r--r--cpptour/myvec.cc36
1 files changed, 36 insertions, 0 deletions
diff --git a/cpptour/myvec.cc b/cpptour/myvec.cc
new file mode 100644
index 0000000..1314730
--- /dev/null
+++ b/cpptour/myvec.cc
@@ -0,0 +1,36 @@
+#include <iostream>
+
+using namespace std;
+
+struct Vector
+{
+  int sz;       // number of elements
+  double* elem; // pointer to elements
+};
+
+void
+vector_init (Vector& v, int s)
+{
+  v.elem = new double[s];
+  v.sz = s;
+}
+
+double
+read_and_sum (int s)
+{
+  Vector v;
+  vector_init (v, s);
+  for (int i = 0; i != s; ++i)
+    cin >> v.elem[i];
+
+  double sum = 0;
+  for (int i = 0; i != s; ++i)
+    sum += v.elem[i];
+  return sum;
+}
+
+int
+main ()
+{
+  cout << read_and_sum (5) << endl;
+}