about summary refs log tree commit diff
path: root/lang/cpptour/myvec.cc
blob: 1314730e109a0fd53c161519fccf5ded6816d9f0 (plain) (blame)
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
#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;
}