-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStandard array questions
109 lines (78 loc) · 1.81 KB
/
Standard array questions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
PROBLEM STATEMENT 1:
Given an array of elements,find the sum.
CODE:
#include<bits/stdc++.h> // header file which includes all the standard libraries
using namespace std;
int main()
{
int a[5] = {1,2,4,6,0};
int sum = 0;
for(int i = 0;i < 5;i++)
{
cout << sum + a[i];
}
cout << " Sum of the elements of Array : " << sum << endl;
return 0;
}
PROBLEM STATEMENT 2:
Given an array of elements,find the maximum element.
CODE:
#include<bits/stdc++.h> // header file which includes all the standard libraries
using namespace std;
int main()
{
int a[5] = {1,2,4,6,0};
int max = a[0]; // Initiallization
for(int i=0;i<5;i++)
{
if(a[i] > max)
{
max = a[i];
}
}
cout << "The maximum element in the array << max << endl;
return 0;
}
PROBLEM STATEMENT 3:
Given an array of elements,find the count of even elements.
CODE:
#include<bits/stdc++.h> // header file which includes all the standard libraries
using namespace std;
int main()
{
int a[7] = { 1,2,4,5,7,8,10};
int count = 0;
for(int i = 0;i<7;i++)
{
// Checking if the element is even or not
if(a[i] % 2 == 0)
{
count = count + 1;
}
}
cout << "The count of even numbers is " << count << endl;
return 0;
}
PROBLEM STATEMENT 1:
Given an array of elements,find the pair of elements with sum as 5.
CODE:
#include<bits/stdc++.h> // header file which includes all the standard libraries
using namespace std;
int main()
{
int a[5] = { 1,4,2,3,6};
int value = 5;
int count = 0;
for(int i = 0;i<5;i++)
{
for(int j = i + 1;j<5;j++)
{
if(a[i] + a[j] == 5)
{
count = count + 1;
}
}
}
cout << "The sum of the elements with sum as : " << value << " is " << count ;
return 0;
}