-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy patharmy.cpp
More file actions
41 lines (34 loc) · 729 Bytes
/
army.cpp
File metadata and controls
41 lines (34 loc) · 729 Bytes
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
/*The aim of the program is to find the years taken for an army officer to increment his rank to the next level. */
#include<bits/stdc++.h>
using namespace std;
int main()
{
//Declaring the rank
int n;
cin>>n;
//Declaring an array to know the years taken to increase the rank from i to i+1
int d[n];
//Accepting the array members
for(int i=0;i<n-1;i++)
{
cin>>d[i];
}
//Declaring the position i to the required rank
int a,b;
cin>>a>>b;
int s;
int sum=0;
s=b-a;
//If the difference between the ranks to achieved is 1 then a-1 th element is printed.
if(s==1)
{
cout<<d[a-1];
return 0;
}
//Else the sum is printed
else
for(int i=a-1;i<b-1;i++)
sum+=d[i];
cout<<sum;
return 0;
}