LECTURE 19
Md. Minhaz Akram
EEE 1109
Array
In C++, an array is a collection of elements of the same data type stored in contiguous memory locations. Arrays
allow you to store multiple values in a single variable, making it easier to work with collections of data.
Basic Syntax:
dataType arrayName[arraySize];
dataType: The type of data the array will store (e.g., int, float, char, etc.).
arrayName: The name of the array.
arraySize: The number of elements the array will hold (must be a constant value).
Example:
#include <iostream>
using namespace std;
int main() {
int numbers[5] = {1, 2, 3, 4, 5}; // Declare and initialize an array of 5 integers
// Access and print array elements
for (int i = 0; i < 5; i++) {
cout << "Element at index " << i << " is: " << numbers[i] << endl;
}
return 0;
}
Key Points:
1. Array Indexing: Array indices in C++ start from 0. So, the first element is array[0], the second is
array[1], and so on.
2. Fixed Size: Once an array's size is defined, it cannot be changed. You cannot add or remove elements after
creation.
3. Initializing Arrays: Arrays can be initialized when they are declared, or elements can be assigned
individually later.
Write C++ code to take several values in array then find maximum, minimum, average
#include <iostream>
#include <limits> // For numeric limits
using namespace std;
int main() {
int n; // Number of elements in the array
// Ask the user to input the number of elements
cout << "Enter the number of elements in the array: ";
cin >> n;
// Create an array of size 'n'
int arr[n];
Page 1 of 2
// Take input values for the array
cout << "Enter " << n << " integers: " << endl;
for (int i = 0; i < n; i++) {
cin >> arr[i];
}
// Initialize variables for max, min, and sum
int maxVal = arr[0];
int minVal = arr[0];
int sum = 0;
// Traverse the array to find max, min, and sum
for (int i = 1; i < n; i++) {
if (arr[i] > maxVal) {
maxVal = arr[i];
}
if (arr[i] < minVal) {
minVal = arr[i];
}
sum += arr[i];
}
// Calculate the average
double average = double(sum) / n;
// Display the results
cout << "Maximum value: " << maxVal << endl;
cout << "Minimum value: " << minVal << endl;
cout << "Average value: " << average << endl;
return 0;
}
Explanation of the Code:
1. User Input:
o The program first asks the user to input the number of elements (n).
o Then, it takes n integer values to fill the array.
2. Finding Maximum and Minimum:
o maxVal is initialized to the initial member of the array arr[0]; and minVal to the initial member
of the array arr[0];
o The program loops through the array and updates maxVal and minVal based on comparisons.
3. Calculating the Sum:
o As the program loops through the array, it adds up all the elements to calculate the sum.
4. Calculating the Average:
o The average is simply the sum divided by the number of elements. We cast the sum to double to
ensure accurate division (since integer division truncates decimals).
Example Output:
Enter the number of elements in the array: 5
Enter 5 integers:
12 4 19 8 7
Maximum value: 19
Minimum value: 4
Average value: 10
Page 2 of 2