Last active
November 16, 2023 11:42
-
-
Save juanfal/cc28c4728f7179e5e9c90214d09860b0 to your computer and use it in GitHub Desktop.
invert array
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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