Skip to content

Instantly share code, notes, and snippets.

@juanfal
Last active November 23, 2023 08:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save juanfal/9a4dcd2f17558ffce53b4793fe3ccba7 to your computer and use it in GitHub Desktop.
Save juanfal/9a4dcd2f17558ffce53b4793fe3ccba7 to your computer and use it in GitHub Desktop.
Open arrays
// t12e06.OpenArray.cpp
// juanfc 2023-11-23
//
#include <array>
#include <iostream>
using namespace std;
const int N = 100;
typedef array<int, N> TVector;
struct TOpenArr {
int noelems;
TVector ar;
};
void printOArr(TOpenArr a);
TOpenArr sumOArr(TOpenArr a, TOpenArr b);
int main()
{
TOpenArr a = {3, {{1, 2, 3}}};
TOpenArr b = {3, {{3, 2, 1}}};
printOArr(sumOArr(a, b));
}
TOpenArr sumOArr(TOpenArr a, TOpenArr b)
{
TOpenArr r = {a.noelems};
for (int i = 0; i < a.noelems; ++i)
r.ar[i] = a.ar[i] + b.ar[i];
return r;
}
void printOArr(TOpenArr a)
{
for (int i = 0; i < a.noelems; ++i)
cout << a.ar[i] << " ";
cout << endl;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment