Array Programs
Array Programs
1
One dimensional array
2
Question 1
Write a program which adds and prints the sum of the
following numbers: 16, 2, 77, 40, 12071
3
Program 1:
# include < iostream>
using namespace std;
int main()
{
int arr[5] = {16, 2, 77, 40, 12071};
int n, sum = 0;
for(n = 0; n<5; n++)
{
sum += arr[n];
}
cout<<“Sum=”<<sum;
return 0;
} 4
Output
Sum=12206
5
Question 2
Write a program which adds first element of the first
array with first element of the second array, Second
element of the first array with second element of the
second array and so on.
Given
Elements of first Array: 1,2,3,4
Elements of second Array: 4,3,2,1
6
Program 2:
# include < iostream.h>
int main()
{
int A[4]={1,2,3,4};
int B[4]={4,3,2,1};
int sum;
for (int i=0; i<4; i++)
{
sum=A[i] + B[i];
cout<<sum<<",";
}
return 0;
}
7
Output
5,5,5,5,
8
Question 3
9
Program 3:
# include < iostream>
using namespace std;
int main()
{
int numbers[] = {8, 25, 36, 44, 52, 60, 75, 89};
int find;
int i, m=8;
cout<<"Enter a number to search:";
cin>> find;
for(i=0; (i<m) && (numbers[i] != find); i++)
{
continue;
} 10
if(i != m&&i==0)
cout<<find<<"is the"<<i+1<<"st element in the list"<<endl;
else if(i != m&&i==1)
cout<<find<<"is the"<<i+1<<"nd element in the list"<<endl;
else if(i != m&&i==2)
cout<<find<<"is the"<<i+1<<"rd element in the list"<<endl;
else if(i != m&&i>=3)
cout<<find<<"is the"<<i+1<<"th element in the list"<<endl;
else// it means if the previous ifs’ are false or if (i == m)
cout<<find<<" is not in the list"<<endl;
return 0;
}
11
Output
Enter a number to search: 36
36 is the 3rd element in the list
12
Question 4
Write a program which prints the minimum element in the
array that contains the following elements
8, 25, 36, 44, 2, 60, 75, 89
13
Program 4:
# include < iostream>
using namespace std;
int main()
{
int numbers[]={8, 25, 36, 44, 2, 60, 75, 89};
int minimum =numbers[0];
int a=8;
for(int i=1; i<a; i++)
{
if(numbers[i]<minimum)
minimum=numbers[i] ;
}
cout<<"The lowest member value of the array
is"<<minimum<<"."<<endl;
return 0;
}
14
Two dimensional array
15
Question 1
Write a program which adds all elements of the array
Given
Elements:
1,2,
3,4
16
Program 1:
# include <iostream.h>
int main()
{
int A[2][2]={{1,2},{3,4}};
int sum=0;
for (int i=0; i<2; i++)
{
for (int j=0; j<2; j++)
{
sum+=A[i][j];
}
}
cout<<sum;
return 0;
} 17
Output
10
18
Question 2
Write a program which adds first element of the first array
with first element of the second array, Second element of the
first array with second element of the second array and so on.
Given
Elements of first Array:
1,2,
3,4
sum=A[i][j] + B[i][j];
cout<<sum<<",";
}
cout<<endl;
}
return 0;
}
20
Output
5,5,
5,5,
21
Question 3
Write a program which displays the highest temperature in the
following temperature values that are stored in one array
26 34 22 17
24 32 19 13
28 38 25 20
22
Program 3:
# include < iostream>
using namespace std;
int main()
{
const int rows=3, columns =4;
int temp[rows][columns] = {{26, 34, 22, 17}, {24, 32, 19, 13}, {28, 38, 25,
20}};
int highest=0;
for(int i=0; i<rows; i++)
{
for(int j=0; j<columns; j++)
if(temp[i][j] > highest)
highest=temp [i][j];
}
cout<<"The highest temperature is = :"<<highest;
return 0;
} 23
Out put
The highest temperature is 38
24
Three dimensional array
25
Question 1
Write a program which adds all the elements in the array
(hint: the array has two sheets).
Given
Elements of the first sheet:
1,2,
3,4
#include<iostream.h>
int main()
{
int bill [2][2][2] = {{{1, 2},{3, 4}}, {{1, 2},{3, 4}}};
int n, sum = 0;
for(n = 0; n<2; n++)
for(int J = 0; J<2; J++)
for(int I = 0; I<2; I++)
{
sum += bill[n][J][I];
}
cout<<"Sum="<<sum;
}
Output
Sum=20
28
Question 2
Write a program which adds first element of the first sheet of the first array with first
element of the first sheet of the second array, second element of the first sheet of the
first array with second element of the first sheet of the second array and so on.
Given
First array
29
Program 2
#include<iostream.h>
int main()
{
int arr1[2][2][2] = {{{1, 2},{3, 4}},{{1, 2},{3, 4}}};
int arr2[2][2][2] = {{{1, 2},{3, 4}},{{1, 2},{3, 4}}};
int sum;
for(int n = 0; n<2; n++){
for(int J = 0; J<2; J++){
for(int I = 0; I<2; I++)
{
sum = arr1[n][J][I] +arr2[n][J][I];
cout<<sum<<“, ”;
}
cout<<endl;
}
cout<<endl<<endl;
}
return 0;
}
Output
2,4,
6,8,
2,4,
6,8,
31