Skip to content

Instantly share code, notes, and snippets.

@juanfal
Last active November 16, 2023 11:39
Show Gist options
  • Save juanfal/32fdcebd7eac6c07726f33eb1e5ede0e to your computer and use it in GitHub Desktop.
Save juanfal/32fdcebd7eac6c07726f33eb1e5ede0e to your computer and use it in GitHub Desktop.
min in an array
// t9e06.minVec.cpp
// juanfc 2023-11-14
//
#include <iostream>
#include <array>
using namespace std;
// consts
const int N=3;
// types
typedef array<float,N> TVec;
// prototypes
void writeArr(TVec a);
int minVec(TVec a);
int main()
{
TVec a = {{1, -22, 0}};
writeArr(a); cout << endl;
cout << "Min value in it: "
<< minVec(a) // 1
<< endl;
a = (TVec){{0, 1, 0}};
writeArr(a); cout << endl;
cout << "Min value in it: "
<< minVec(a) // 0
<< endl;
return 0;
}
int minVec(TVec a)
{
int min = a[0];
for (int i = 1; i < N; ++i)
if (a[i] < 0) min = a[i];
return min;
}
void writeArr(TVec a)
{
for (int i = 0; i < N; ++i)
cout << a[i] << " ";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment