array
array
locations. Arrays are used to store multiple values in a single variable, instead of declaring
separate variables for each value.
dataType arrayName[arraySize];
Where:
#include <iostream>
using namespace std;
int main() {
// Declare an array of 5 integers
int numbers[5] = {10, 20, 30, 40, 50};
return 0;
}
Explanation:
1. Declaration: int numbers[5] = {10, 20, 30, 40, 50}; declares an integer array
named numbers with 5 elements, initialized with values.
2. Accessing Elements: numbers[0] refers to the first element of the array (which is 10).
Similarly, numbers[1] refers to the second element (which is 20).
3. Modifying Elements: You can change any element by assigning a new value.
numbers[2] = 100; changes the value of the third element to 100.
Example 2: Looping Through an Array
You can use loops to iterate through arrays, which is especially useful when working with larger
arrays.
#include <iostream>
using namespace std;
int main() {
// Declare and initialize an array
int numbers[5] = {5, 10, 15, 20, 25};
return 0;
}
Explanation:
For Loop: The loop for(int i = 0; i < 5; i++) runs from i = 0 to i = 4 (because
array indexing starts from 0), and in each iteration, numbers[i] gives the element at
index i.
The output will be: 5 10 15 20 25
1. Arrays have a fixed size; once you declare an array with a size, you can't change the size
later.
2. Array elements are accessed using their index.
3. You can initialize arrays at the time of declaration.
4. Arrays can store elements of any data type, like int, float, char, etc.
In C++, a character array can be used to store strings (though it's more common to use
std::string).
#include <iostream>
using namespace std;
int main() {
// Declare a character array
char name[6] = {'H', 'e', 'l', 'l', 'o', '\0'}; // Null-terminated string
return 0;
}
Explanation:
The array char name[6] = {'H', 'e', 'l', 'l', 'o', '\0'} holds a string
"Hello".
The \0 (null character) marks the end of the string in character arrays.
Key Takeaways:
Arrays are useful for storing a collection of elements of the same type.
They have a fixed size, and the elements can be accessed and modified using indices.
Loops are often used to process arrays efficiently.
What is an Array?
An array is a collection of variables that are stored in a single name. Instead of declaring multiple
variables to store a list of values, you can use an array. All the elements in an array are of the
same data type, like int, float, char, etc.
Example:
Imagine you have 5 students and you want to store their marks. Instead of declaring 5 different
variables, you can create an array.
Without Arrays:
With Arrays:
In this array:
marks[0] = 85
marks[1] = 90
marks[2] = 78
marks[3] = 88
marks[4] = 92
Array Declaration:
To declare an array, you need to specify the type, the name, and the size.
Syntax:
data_type array_name[size];
Example:
Example:
You can use loops like for or while to go through all elements in an array.
Example:
#include<iostream>
using namespace std;
int main() {
int marks[5] = {85, 90, 78, 88, 92};
return 0;
}
Output:
Marks of student 1 are: 85
Marks of student 2 are: 90
Marks of student 3 are: 78
Marks of student 4 are: 88
Marks of student 5 are: 92
Key Points:
1. Array Size: The size of an array is fixed when you declare it.
2. Indexes: Array indexes start at 0.
3. Access: You can access any element using its index.
#include<iostream>
int z;
z=x+y;
int z;
z=x+y;
}
int main()
int a=5;
int b=10;
float c=3.5;
sum(a,b);
sum(a,c);