Skip to content

Instantly share code, notes, and snippets.

@juanfal
Last active November 16, 2023 11:42
Show Gist options
  • Save juanfal/cc28c4728f7179e5e9c90214d09860b0 to your computer and use it in GitHub Desktop.
Save juanfal/cc28c4728f7179e5e9c90214d09860b0 to your computer and use it in GitHub Desktop.
invert array
// t9e09.invertArr.cpp
// juanfc 2023-11-14
// https://gist.github.com/juanfal/cc28c4728f7179e5e9c90214d09860b0
#include <iostream>
#include <array>
using namespace std;
// consts
const int N=5;
// types
typedef array<float,N> TVec;
// prototypes
void writeArr(TVec a);
void invertArr(TVec& a);
int main()
{
TVec a = {{1, 2, 3, 4, 5}};
writeArr(a); cout << endl;
invertArr(a);
cout << "after invert:" << endl;
writeArr(a); cout << endl;
return 0;
}
void swap(int& a, int& b);
void invertArr(TVec& a)
{
for (int i = 0; i < N/2; ++i) {
swap(a[i], a[N-i-1]);
}
}
void writeArr(TVec a)
{
for (int i = 0; i < N; ++i)
cout << a[i] << " ";
}
void swap(int& a, int& b)
{
int t = a;
a = b; b = t;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment