C++ Arrays
Arrays are used to store multiple values in a single variable, instead of declaring
separate variables for each value.
To declare an array, define the variable type, specify the name of the array followed
by square brackets and specify the number of elements it should store:
string cars[4];
declared a variable that holds an array of four strings.
To insert values to use an array literal - place the values in a comma-separated list,
inside curly braces:
In C++, array index starts from 0.
store only fixed set of elements in C++ array.
A collection of related data items stored in adjacent memory places is referred to as an
array in the C/C++ programming language or any other programming language for
that matter.
Elements of an array can be accessed arbitrarily using its indices.
They can be used to store a collection of any type of primitive data type, including
int, float, double, char, etc.
An array in C/C++ can also store derived data types like structures, pointers, and
other data types, which is an addition.
The array representation in a picture is provided below.
Advantages of C++ Array
Code Optimization (less code)
Random Access
Easy to traverse data
Easy to manipulate data
Easy to sort data etc.
Disadvantages of C++ Array
Fixed size
C++ Array Types
There are 2 types of arrays in C++ programming:
1. Single Dimensional Array
2. Multidimensional Array
C++ Single Dimensional Array
Let's see a simple example of C++ array, where we are going to create, initialize and traverse
array.
1. #include <iostream>
2. using namespace std;
3. int main()
4. {
5. int arr[5]={10, 0, 20, 0, 30}; //creating and initializing array
6. //traversing array
7. for (int i = 0; i < 5; i++)
8. {
9. cout<<arr[i]<<"\n";
10. }
11. }
Output:
10
0
20
0
30
C++ Array Example: Traversal using foreach loop
We can also traverse the array elements using foreach loop. It returns array element one by
one.
1. #include <iostream>
2. using namespace std;
3. int main()
4. {
5. int arr[5]={10, 0, 20, 0, 30}; //creating and initializing array
6. //traversing array
7. for (int i: arr)
8. {
9. cout<<i<<"\n";
10. }
11. }
Output:
10
20
30
40
50
Why do we need arrays?
With a limited number of objects, we can use regular variables (v1, v2, v3,..), but when we
need to hold many instances, managing them with normal variables becomes challenging. To
represent numerous instances in one variable, we use an array.
C++ array with empty members
The maximum number of elements that can be stored in an array in C++ is n. What will
happen, though, if we store fewer than n elements?
For example,
1. // store only 3 elements in the array
2. int x[6] = {19, 10, 8};
Important things to remember while using arrays in C++
1. The array's indexes begin at 0. Meaning that the first item saved at index 0 is x[0].
2. The final element of an array with size n is kept at index (n-1). This example's final
element is x[5].
3. An array's elements have sequential addresses. Consider the scenario where
x[0beginning ]'s address is 2120.
4. The address of the subsequent element, x[1], will then be 2124, followed by x[2],
2128, and so forth.
5. Each element in this case has a four-fold increase in size. This is due to the fact that
int has a 4 byte capacity.
What is two-dimensional array?
Each element in this kind of array is described by two indexes, the first of which denotes a
row and the second of which denotes a column.
As you can see, the components are arranged in a two-dimensional array using rows and
columns; there are I number of rows and j number of columns.
What is a multi-dimensional array?
A two-dimensional array is the most basic type of multidimensional array; it also qualifies as
a multidimensional array. There are no restrictions on the array's dimensions
How to insert it in array?
1. int mark[5] = {19, 10, 8, 17, 9}
2. // change 4th element to 9
3. mark[3] = 9;
4. // take input from the user
5. // store the value at third position
6. cin >> mark[2];
7. // take input from the user
8. // insert at ith position
9. cin >> mark[i-1];
10.
11. // print first element of the array
12. cout << mark[0];
13. // print ith element of the array
14. cout >> mark[i-1];
How to display the sum and average of array elements?
1. #include <iostream>
2. using namespace std;
3. int main() {
4. // initialize an array without specifying the size
5. double numbers[] = {7, 5, 6, 12, 35, 27};
6. double sum = 0;
7. double count = 0;
8. double average;
9. cout << "The numbers are: ";
10. // print array elements
11. // use of range-based for loop
12. for (const double &n : numbers) {
13. cout << n << " ";
14. // calculate the sum
15. sum += n;
16. // count the no. of array elements
17. ++count;
18. }
19. // print the sum
20. cout << "\nTheir Sum = " << sum << endl;
21. // find the average
22. average = sum / count;
23. cout << "Their Average = " << average << endl;
24.
25. return 0;
26. }
Output:
The numbers are: 7 5 6 12 35 27
Their Sum = 92
Their Average = 15.3333
C++ if-else
In C++ programming, if statement is used to test the condition. There are various types of if
statements in C++.
if statement
if-else statement
nested if statement
if-else-if ladder
C++ IF Statement
The C++ if statement tests the condition. It is executed if condition is true.
1. if(condition){
2. //code to be executed
3. }
C++ If Example
1. #include <iostream>
2. using namespace std;
3.
4. int main () {
5. int num = 10;
6. if (num % 2 == 0)
7. {
8. cout<<"It is even number";
9. }
10. return 0;
11. }
Output:/p>
It is even number
C++ IF-else Statement
The C++ if-else statement also tests the condition. It executes if block if condition is true
otherwise else block is executed.
Current Time 0:00
/
Duration 18:10
1. if(condition){
2. //code if condition is true
3. }else{
4. //code if condition is false
5. }
C++ If-else Example
1. #include <iostream>
2. using namespace std;
3. int main () {
4. int num = 11;
5. if (num % 2 == 0)
6. {
7. cout<<"It is even number";
8. }
9. else
10. {
11. cout<<"It is odd number";
12. }
13. return 0;
14. }
Output:
It is odd number
C++ If-else Example: with input from user
1. #include <iostream>
2. using namespace std;
3. int main () {
4. int num;
5. cout<<"Enter a Number: ";
6. cin>>num;
7. if (num % 2 == 0)
8. {
9. cout<<"It is even number"<<endl;
10. }
11. else
12. {
13. cout<<"It is odd number"<<endl;
14. }
15. return 0;
16. }
Output:
Enter a number:11
It is odd number
Output:
Enter a number:12
It is even number
C++ IF-else-if ladder Statement
The C++ if-else-if ladder statement executes one condition from multiple statements.
1. if(condition1){
2. //code to be executed if condition1 is true
3. }else if(condition2){
4. //code to be executed if condition2 is true
5. }
6. else if(condition3){
7. //code to be executed if condition3 is true
8. }
9. ...
10. else{
11. //code to be executed if all the conditions are false
12. }
C++ If else-if Example
1. #include <iostream>
2. using namespace std;
3. int main () {
4. int num;
5. cout<<"Enter a number to check grade:";
6. cin>>num;
7. if (num <0 || num >100)
8. {
9. cout<<"wrong number";
10. }
11. else if(num >= 0 && num < 50){
12. cout<<"Fail";
13. }
14. else if (num >= 50 && num < 60)
15. {
16. cout<<"D Grade";
17. }
18. else if (num >= 60 && num < 70)
19. {
20. cout<<"C Grade";
21. }
22. else if (num >= 70 && num < 80)
23. {
24. cout<<"B Grade";
25. }
26. else if (num >= 80 && num < 90)
27. {
28. cout<<"A Grade";
29. }
30. else if (num >= 90 && num <= 100)
31. {
32. cout<<"A+ Grade";
33. }
34. }
Output:
Enter a number to check grade:66
C Grade
Output:
Enter a number to check grade:-2
wrong number