From 9d53d8857eaa1c9405894a88ca75bc4657e42f35 Mon Sep 17 00:00:00 2001 From: msglm Date: Sat, 14 Jan 2023 05:31:48 -0600 Subject: Inital Commit --- C++/ArrayExamples/ArrayExamples.cpp | 73 +++++++++++++++++++++++++++++++++++++ 1 file changed, 73 insertions(+) create mode 100644 C++/ArrayExamples/ArrayExamples.cpp (limited to 'C++/ArrayExamples') 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 +#include +#include +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 . + */ + -- cgit v1.2.3