Update array/array_pari.cpp

This commit is contained in:
Vichingo455 2024-11-21 07:16:38 +00:00
parent 4f527a39d9
commit 981de52b3a
1 changed files with 41 additions and 41 deletions

View File

@ -1,42 +1,42 @@
/* /*
AUTHOR: Manuel Vichi AUTHOR: Vichingo455
The program asks in input 10 values, stores them in an array and then determines if all stored values are peer numbers or not. The program asks in input 10 values, stores them in an array and then determines if all stored values are peer numbers or not.
*/ */
#include <iostream> #include <iostream>
#include <array> #include <array>
#include <vector> #include <vector>
#include <cstdlib> #include <cstdlib>
#define ARRAY_SIZE 10 //The size of the integers' array #define ARRAY_SIZE 10 //The size of the integers' array
using namespace std; using namespace std;
//Check if a number is peer //Check if a number is peer
bool isPeer(int number) { bool isPeer(int number) {
return number % 2 == 0; return number % 2 == 0;
} }
//Print if all numbers on an array are peers or not //Print if all numbers on an array are peers or not
void printIfPeersOnPeerPositions(int array[]) { void printIfPeersOnPeerPositions(int array[]) {
int array_size = sizeof(array) / sizeof(array[0]); int array_size = sizeof(array) / sizeof(array[0]);
int number_peers = 0; int number_peers = 0;
for (int i = 0; i < array_size; i++) { for (int i = 0; i < array_size; i++) {
if (isPeer(array[i])) { if (isPeer(array[i])) {
number_peers++; number_peers++;
} }
i++; i++;
} }
if (number_peers == array_size) if (number_peers == array_size)
cout << "All numbers you gave me are peers." << endl; cout << "All numbers you gave me are peers." << endl;
else else
cout << "Not all numbers you gave me are peers." << endl; cout << "Not all numbers you gave me are peers." << endl;
} }
//Main function //Main function
int main() { int main() {
int integers[ARRAY_SIZE]; int integers[ARRAY_SIZE];
for (int i = 0; i < ARRAY_SIZE; i++) { for (int i = 0; i < ARRAY_SIZE; i++) {
cout << "Type a number and press enter (Remaining to type: " << ARRAY_SIZE - i - 1 << "). "; cout << "Type a number and press enter (Remaining to type: " << ARRAY_SIZE - i - 1 << "). ";
cin >> integers[i]; cin >> integers[i];
cout << endl; cout << endl;
} }
printIfPeersOnPeerPositions(integers); printIfPeersOnPeerPositions(integers);
return 0; return 0;
} }