1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
|
#include <iostream>
using namespace std; // <--- Best not to use.
constexpr int MAXSIZE{ 10 };
void userInput(int myArray[MAXSIZE]); // <--- Size is not requited for a 1D array or the 1st diminsion of a 2D.
void printOddNumbers(const int myArray[MAXSIZE]); // <--- Size is requited for the 2nd diminsion of a 2D array. OK if you leave.
void printEvenNumbers(const int myArray[MAXSIZE]);
int main()
{
// <--- Switch comments for testing.
// int myArray[MAXSIZE]{}; // <--- ALWAYS initialize your variables.
int myArray[MAXSIZE]{ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
//int myArray[MAXSIZE]{ 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 };
//userInput(myArray); // <--- Skipped for testing. We already know it works.
printOddNumbers(myArray);
printEvenNumbers(myArray);
return 0;
}
void userInput(int myArray[MAXSIZE])
{
int index = 0;
cout << "\nEnter 10 numbers:\n";
for (int row = 0; row < MAXSIZE; ++row)
{
cout << ++index << ". Enter a number: ";
cin >> myArray[row];
}
cout << "\n";
}
void printOddNumbers(const int myArray[MAXSIZE])
{
int oddSum{};
int end{ !(myArray[MAXSIZE - 1] % 2) ? MAXSIZE - 2 : MAXSIZE - 1 };
cout << "The odd numbers you entered were: ";
for (int row = 0; row < MAXSIZE; row++)
{
if (myArray[row] % 2)
{
cout << myArray[row] << (row < end ? ", " : "");
oddSum += myArray[row];
}
}
cout << "\n";
//for (int row = 0; row < MAXSIZE; row++)
//{
// if (myArray[row] % 2)
// {
// oddSum += myArray[row];
// }
//}
cout << "Their sum is: " << oddSum << "\n";
}
void printEvenNumbers(const int myArray[MAXSIZE])
{
int evenSum{};
int end{ myArray[MAXSIZE - 1] % 2 ? MAXSIZE - 2 : MAXSIZE - 1 };
cout << "\nThe even numbers you entered were: ";
for (int row = 0; row < MAXSIZE; row++)
{
if (!(myArray[row] % 2))
{
cout << myArray[row] << (row < end ? ", " : "");
evenSum += myArray[row];
}
}
cout << "\n";
//for (int row = 0; row < MAXSIZE; ++row)
//{
// if (!(myArray[row] % 2))
// {
// evenSum += myArray[row];
// }
//}
cout << "Their sum is: " << evenSum << "\n";
}
|