Skip to content

Instantly share code, notes, and snippets.

@juanfal
Last active November 16, 2023 11:43
Show Gist options
  • Save juanfal/0de9ce7af220a1d3fb59a3bb983b7d5d to your computer and use it in GitHub Desktop.
Save juanfal/0de9ce7af220a1d3fb59a3bb983b7d5d to your computer and use it in GitHub Desktop.
find 12
// t9e10.find12.cpp
// juanfc 2023-11-14
// https://gist.github.com/juanfal/0de9ce7af220a1d3fb59a3bb983b7d5d
#include <iostream>
#include <array>
using namespace std;
// consts
const int N=5;
// types
typedef array<float,N> TVec;
// prototypes
void writeArr(TVec a);
int find12(TVec a);
int main()
{
TVec a = {{3, 1, 2, 3, 4}};
writeArr(a); cout << endl;
cout << "12 is at: "
<< find12(a) << endl;
a = (TVec){{3, 2, 2, 2, 1}};
writeArr(a); cout << endl;
cout << "12 is at: "
<< find12(a) << endl;
return 0;
}
void swap(int& a, int& b);
int find12(TVec a)
{
int i = 1;
while (i < N and not (a[i-1] == 1 and a[i]==2))
++i;
return (i==N)? -1: 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