summaryrefslogtreecommitdiffstats
path: root/C++/ArrayExamples/ArrayExamples.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'C++/ArrayExamples/ArrayExamples.cpp')
-rw-r--r--C++/ArrayExamples/ArrayExamples.cpp73
1 files changed, 73 insertions, 0 deletions
diff --git a/C++/ArrayExamples/ArrayExamples.cpp b/C++/ArrayExamples/ArrayExamples.cpp
new file mode 100644
index 0000000..119a709
--- /dev/null
+++ b/C++/ArrayExamples/ArrayExamples.cpp
@@ -0,0 +1,73 @@
+// Name: msglm
+// Date:
+// Program Name:
+// Description:
+
+
+#include <iostream>
+#include <string>
+#include <iomanip>
+using namespace std;
+
+// Named constants
+
+const int ARRAY_SIZE = 5;
+
+int main() {
+ //Variable declaration
+ int num[ARRAY_SIZE]; //Declaring array size
+ int maxIndex;
+ int minIndex;
+ int sum = 0;
+
+ //Program title and description for the user
+
+ cout << "Array Example : This program will sho whow to declare, init, and raverse through \n an array. and find largest, smallest, sum, and average value.\n";
+
+ //get ints from user to initialize
+ for (int i = 0 ; i < ARRAY_SIZE; i++) {
+ cout << "Enter Value " << i+1 << ": " << endl;
+ cin >> num[i];
+ }
+ cout << endl;
+
+ //Check input by printing to the console
+ for (int i = 0 ; i < ARRAY_SIZE; i++) {
+ cout << "num[" << i << "]= " << num[i] << endl;
+ }
+
+ //Find largest value
+ maxIndex = 0;
+ for (int i = 0; i < ARRAY_SIZE; i++) {
+ if(num[maxIndex] < num[i]) {
+ maxIndex = i;
+ }
+ }
+ cout << "Largest Value: " << num[maxIndex] << endl;
+
+ //Find smallest value
+ minIndex = 0;
+ for (int i = 0; i < ARRAY_SIZE; i++) {
+ if(num[minIndex] > num[i]) {
+ minIndex = i;
+ }
+ }
+ cout << "Largest Value: " << num[minIndex] << endl;
+
+ float average;
+ //Find sum and average
+ for (int i = 0; i < ARRAY_SIZE; i++) {
+ sum = sum + num[i];
+ }
+ average = sum/ARRAY_SIZE;
+
+ cout << "Sum: " << sum << endl;
+ cout << "Average: " << average << endl;
+}
+
+ /*This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
+ * You should have received a copy of the GNU General Public License along with this program. If not, see <https://www.gnu.org/licenses/>.
+ */
+