Created
November 4, 2024 10:02
-
-
Save juanfal/4dd2930cc85febd39c338fd634e50678 to your computer and use it in GitHub Desktop.
basic array subprograms
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
// 00.basicArraySubprograms.cpp | |
// juanfc 2024-11-04 | |
// | |
#include <iostream> | |
#include <array> | |
using namespace std; | |
// consts | |
const int N=5; | |
// types | |
typedef array<int,N> TVec; | |
int main() | |
{ | |
void printArr(TVec a); | |
void initArr0(TVec& a); | |
void readArr(TVec& a); | |
TVec readArr(void); | |
int sumArr(TVec a); | |
bool isIn(TVec a, int x); | |
int indexOf(TVec a, int x); | |
int count(TVec a, int x); | |
bool anyReps(TVec a); | |
bool anyReps2(TVec a); | |
int findPosFirstMax(TVec a); | |
cout << "No initialised: " << endl; | |
TVec z; | |
printArr(z); | |
cout << endl; | |
cout << "Input the " << N << " elements of an array" << endl; | |
TVec v = {{1,4,-3,4,5}}; //readArr(); | |
cout << "Array read: " << endl; | |
printArr(v); | |
cout << endl; | |
cout << "sumArr: " << sumArr(v) << endl; | |
cout << endl; | |
cout << "isIn(v, 5): " << isIn(v, 5) << endl; | |
cout << "isIn(v, 10): " << isIn(v, 10) << endl; | |
cout << endl; | |
cout << "indexOf(v, 5): " << indexOf(v, 5) << endl; | |
cout << "indexOf(v, 10): " << indexOf(v, 10) << endl; | |
cout << endl; | |
cout << "count(v, 4): " << count(v, 4) << endl; | |
cout << "count(v, 10): " << count(v, 10) << endl; | |
cout << endl; | |
cout << "anyReps(v): " << anyReps(v) << endl; | |
cout << "anyReps2(v): " << anyReps2(v) << endl; | |
cout << endl; | |
cout << "findPosFirstMax(v): " << findPosFirstMax(v) << endl; | |
cout << endl; | |
return 0; | |
} | |
// subprogram bodies | |
int findPosFirstMax(TVec a) | |
{ | |
int p = 0; | |
for (int i = 0; i < a.size(); ++i) | |
if (a[i] > a[p]) | |
p = i; | |
return p; | |
} | |
bool isIn(TVec a, int x) | |
{ | |
int i = 0; | |
while (i < a.size() and a[i] != x) | |
++i; | |
return i < a.size(); | |
} | |
int indexOf(TVec a, int x) | |
{ | |
int i = 0; | |
while (i < a.size() and a[i] != x) | |
++i; | |
int r; | |
if (i < a.size()) | |
r = i; | |
else | |
r = -1; | |
return r; | |
} | |
int count(TVec a, int x) | |
{ | |
int cnt = 0; | |
for (int i = 0; i < a.size(); ++i) | |
if (a[i] == x) | |
++cnt; | |
return cnt; | |
} | |
bool anyReps(TVec a) | |
{ | |
bool thereIsARep = false; | |
int i = 0; | |
while ( i < N-1 and not thereIsARep) { | |
int j = i + 1; | |
while ( j < N and not thereIsARep) { | |
thereIsARep = a[i] == a[j]; | |
j++; | |
} | |
i++; | |
} | |
return thereIsARep; | |
} | |
bool anyReps2(TVec a) | |
{ | |
int i = 0; | |
int j; | |
do { | |
j = i + 1; | |
while ( j < N and a[i] != a[j]) | |
++j; | |
i++; | |
} while ( j >= N and i < N-1); | |
return j < N; | |
} | |
int sumArr(TVec a) | |
{ | |
int s = 0; | |
for (int i = 0; i < a.size(); ++i) | |
s += a[i]; | |
return s; | |
} | |
TVec readArr(void) | |
{ | |
TVec r; | |
for (int i = 0; i < r.size(); ++i) | |
cin >> r[i]; | |
return r; | |
} | |
void readArr(TVec& a) | |
{ | |
for (int i = 0; i < a.size(); ++i) | |
cin >> a[i]; | |
} | |
void printArr(TVec a) | |
{ | |
for (int i = 0; i < a.size(); ++i) | |
cout << a[i] << " "; | |
} | |
void initArr0(TVec& a) | |
{ | |
for (int i = 0; i < a.size(); ++i) | |
a[i] = 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment