Array
- An array is a variable that can store multiple values in single variable.
- Array is a collection of homogeneous data .
- homogeneous means similar type
- array of index is always start from 0.
syntax :
datatype arrayname[size];
int number[5];
int marks[5] = {10,20,30,40,50};
index = 0
Example 1️⃣:
#include <iostream>
using namespace std;
int main() {
int marks[5]={10, 20, 30, 40, 50};
cout<<marks[0]<<endl;
cout<<marks[1]<<endl;
cout<<marks[2]<<endl;
cout<<marks[3]<<endl;
cout<<marks[4]<<endl;
return 0;
}
Output :
10
20
30
40
50
Explanation :
In a Line no 5
we declare and initialize array that name is marks
and size of array is [5]
and store value in array is {10, 20, 30, 40, 50}
and datatype of array is int
. we use semicolon ;
for terminate array.
In a Line no 7
marks[0]
we print array using index and index is start from 0 marks
is array variable and index of 10
is 0 endl
use to end of line and start from new line.
In a Line no 8
marks[1]
index of 20
is 1
.
In a Line no 9
marks[2]
index of 30
is 2
.
In a Line no 10
marks[3]
index of 40
is 3
.
In a Line no 11
marks[4]
index of 50
is 4
.
Example 2️⃣:
#include <iostream>
using namespace std;
int main() {
int marks[5]={10, 20, 30, 40, 50, 60};
cout<<marks[0]<<endl;
cout<<marks[1]<<endl;
cout<<marks[2]<<endl;
cout<<marks[3]<<endl;
cout<<marks[4]<<endl;
return 0;
}
Output :
ERROR
g++ /tmp/zIOh6NZyDx.cpp
tmp/zIOh6NZyDx.cpp: In function 'int main()':
tmp/zIOh6NZyDx.cpp:6:37:error: too many initializers for 'int[5]'
6 | int marks[5] = {10,20,30,40,50,60};
Explanation :
In this example we see this error because of size of array is 5 and we store the 6 value in array variable.
In line no 6
int marks[5] = {10,20,30,40,50,60};
this error are occour
Example 3️⃣:
#include <iostream>
using namespace std;
int main() {
int marks[5]={10, 20, 30, 40, 50,};
cout<<marks[0]<<endl;
cout<<marks[1]<<endl;
cout<<marks[2]<<endl;
cout<<marks[3]<<endl;
cout<<marks[4]<<endl;
cout<<marks[5]<<endl;
return 0;
}
Output :
10
20
30
40
50
32766
Explanation :
In a Line no 5
we declare and initialize array that name is marks
and size of array is [5]
and store value in array is {10, 20, 30, 40, 50}
and datatype of array is int
. we use semicolon ;
for terminate array.
In line no 11
we print marks[5]
but we can not store value in array varible for index 5.
compiler print the number 32766
it is address of index 5.
Example 4️⃣:
#include <iostream>
using namespace std;
int main() {
int marks[6]={10, 20, 30, 40, 50, 60};
cout<<marks[0]<<endl;
cout<<marks[1]<<endl;
cout<<marks[2]<<endl;
cout<<marks[3]<<endl;
cout<<marks[4]<<endl;
cout<<marks[5]<<endl;
return 0;
}
Output :
10
20
30
40
50
60
Explanation :
In a Line no 5
we declare and initialize array that name is marks
and size of array is [6]
and store value in array is {10, 20, 30, 40, 50, 60}
and datatype of array is int
. we use semicolon ;
for terminate array.
In a Line no 7
marks[0]
we print array using index and index is start from 0 marks
is array variable and index of 10
is 0 endl
use to end of line and start from new line.
In a Line no 8
marks[1]
index of 20
is 1
.
In a Line no 9
marks[2]
index of 30
is 2
.
In a Line no 10
marks[3]
index of 40
is 3
.
In a Line no 11
marks[4]
index of 50
is 4
.
In a Line no 12
marks[5]
index of 60
is 5
.
Example 5️⃣:
#include <iostream>
using namespace std;
int main() {
int marks[]={10, 20, 30, 40, 50, 60, 70 ,80};
cout<<marks[0]<<endl;
cout<<marks[1]<<endl;
cout<<marks[2]<<endl;
cout<<marks[3]<<endl;
cout<<marks[4]<<endl;
return 0;
}
Output :
10
20
30
40
50
Explanation :
In a line no 5
we declare array that name is marks
datatype of array is int
we can not mention size of array, and store multiple value
{10, 20, 30, 40, 50, 60, 70 ,80};
And print array name and index marks[0]
endl
for end of line and start from new line.
Without optimization code :
Example 6️⃣:
#include <iostream>
using namespace std;
int main() {
int marks[3];
cout<<"Enter Number 0 :";
cin>>marks[0];
cout<<"Enter Number 0 :";
cin>>marks[1];
cout<<"Enter Number 0 :";
cin>>marks[2];
cout<<"Number 0 :"<<marks[0]<<endl;
cout<<"Number 1 :"<<marks[1]<<endl;
cout<<"Number 2 :"<<marks[2]<<endl;
return 0;
}
Output :
Enter number 0 : 10
Enter number 1 : 20
Enter number 2 : 30
Number 0 : 10
Number 0 : 20
Number 0 : 30
Explanation :
Declare an array variable that name is marks
store multiple value {5, 4, 1, 2, 7}
;
Enter Number 0 :
using cout
object.
store index of 0 number in using cin object.
same as line 10 and 13.
print Number 0 :
and marks[0]
using cout endl
is used to terminate line.
With optimization code :
Example 7️⃣:
#include <iostream>
using namespace std;
int main() {
int marks[3];
for(int i=0; i<3; i++)
{
cout<<"Enter Number "<<i<<": "<<endl;
cin>>marks[i];
}
for(int i=0; i<3; i++)
{
cout<<"Number "<<i<<": "<<marks[i]<<endl;
}
}
Output :
Enter number 0 : 10
Enter number 1 : 20
Enter number 2 : 30
Number 0 : 10
Number 0 : 20
Number 0 : 30
Explanation :
Declare an array variable that name is marks
store multiple value {5, 4, 1, 2, 7}
;
start a for loop from index 0 to the i<3
and increase value by one.
print Enter Number
using cout and i
variable endl
for terminate line.
store variable of marks[i]
using cin.
next start a for loop from index 0 to the i<3
and increase value by one.
print Number
and variable i
and marks[i]
and endl.
Example 8️⃣️ :
#include <iostream>
using namespace std;
int main() {
int marks[5];
for(int i=0; i<5; i++)
{
cout<<"Enter Number "<<i<<": "<<endl;
cin>>marks[i];
}
for(int i=0; i<5; i++)
{
cout<<"Number "<<i<<": "<<marks[i]<<endl;
}
}
Output :
Enter number 0 : 10
Enter number 1 : 20
Enter number 2 : 30
Enter number 3 : 40
Enter number 4 : 50
Number 0 : 10
Number 1 : 20
Number 2 : 30
Number 3 : 40
Number 4 : 50
Write a program to calculate sum of elements presents in array :
#include <iostream>
using namespace std;
int main()
{
int arr[5] = {5, 4, 1, 2, 7};
int sum = 0;
for(int i=0; i<5; i++)
{
sum = sum + arr[i];
}
cout<<"Sum :"<<sum;
}
Output :
Sum :19
Explanination :
initialize an array variable that name is arr
store multiple value {5, 4, 1, 2, 7}
;
initialize sum
variable set value of sum
variable is 0
start a for loop from index 0 to the i<5
and increase value by one.
In every iteration , perform sum = sum + arr[i]
.
After the termination of the loop, print the value of sum
.
Wap to print only even number from given array :
#include <iostream>
using namespace std;
int main()
{
int numbers[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
for(int i=0; i<10; i++)
{
if(numbers[i] % 2==0)
{
cout<<Number[i] <<" ,";
}
}
}
Output :
2, 4, 6, 8, 10,
Explanination :
In a line no 5
initialize an array variable that name is arr
store multiple value {5, 4, 1, 2, 7}
;
In a line no 7
start a for loop from index 0 to the i<5
and increase value by one.
In a line no 9
check condition if(numbers[i] % 2==0)
if condition is true print even naumber.
Wap to print largest number from given array :
#include <iostream>
using namespace std;
int main()
{
int numbers[5] = {10, 50, 50, 20, 30};
int largest = 0;
for(int i=0; i<5; i++)
{
if(number[i] > largest)
{
largest = number[i];
}
}
cout<<"Largest Elements is = "<<largest;
}
Output :
Largest Elements is = 80
Explanination :
In a line no 5
initialize an array variable that name is arr
store multiple value {5, 4, 1, 2, 7}
;
In a line no 7
initialize largest
variable set value of largest
variable is 0
In a line no 9
start a for loop from index 0 to the i<5
and increase value by one.
In a line no 11
check condition if(numbers[i] > largest)
if condition is true store value number[i]
in largest
variable.
In a line no 16
Print Largest Elements is =
and largest
variable using cout
.