summaryrefslogtreecommitdiffstats
path: root/C++/ArrayExamples/ArrayExamples.cpp
blob: 119a709f4dc17601de911d17b2368c4dc22c7bdf (plain) (blame)
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
// 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/>.
	 */