-
Notifications
You must be signed in to change notification settings - Fork 353
/
Copy pathsum_positions.cpp
147 lines (110 loc) · 2.71 KB
/
sum_positions.cpp
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
/* Title : User need to enter the elements he wants to enter .
The possible pairs of positions will be found whose sum
will equal to the query number accepted .
ex : Elements : 2 3 4 5
Query element : 8
Output : Addition of elements present at position 2 and 4 = 8.
*/
#include<iostream>
#include<cstdlib>
#include<conio.h>
int fact (int n);
using namespace std;
int main()
{
int i , j ,n,th;
int opt , count=0;
do
{
cout<<"\n Enter the total number of elements : \n"<<endl; // Enter more than 1.
cin>>n;
if(n<=1)
{
cout<<"\n Invalid Input .\n Try again later ! "<<endl;
exit(0);
}
int fac=fact(n);
int a[n];
cout<<"\n Enter the "<<n<<" elements : \n"<<endl;
for(i=0;i<n;i++)
{
cin>>a[i];
}
do
{
cout<<"\n Enter the query you wish to choose : ";
cin>>th;
cout<<"\n";
count=0;
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
if(a[i]+a[j]==th)
{
cout<<" Found : "<<" Addition of elements at positions "<<i+1<<" and "<<j+1<<endl;
}
else
{ count++ ; }
}
}
if(count==fac)
{
cout<< "\n Oops ! \n Not found .\n"<<endl;
}
cout<<"\n Do you wish to check for another query with same data ? \n 1.Yes 2.Press any to do not\n"<<endl; // Press 1 to contiune only .
cin>>opt; // It will be done on the same data sets .
}while(opt==1);
cout<<"\n Do you wish to continue ? \n Press 1 to continue or any to stop . \n"<<endl; // Press 1 to continuw\e .
cin>>opt;
}while(opt==1); // You will need to enter the data of arrays and query again .
}
int fact(int n)
{
int i=n-1, sum=0;
while(i>=1)
{
sum=sum+i ;
i--;
}
return sum;
}
/*
Enter the total number of elements :
7
Enter the 7 elements :
106
-53
0
53
21
4
23
Enter the query you wish to choose : 53
Found : Addition of positions 1 and 2
Found : Addition of positions 3 and 4
Do you wish to check for another query with same data ?
1.Yes 2.Press any to do not
1
Enter the query you wish to choose : 4
Found : Addition of positions 3 and 6
Do you wish to check for another query with same data ?
1.Yes 2.Press any to do not
2
Do you wish to continue ?
1
Enter the total number of elements :
3
Enter the 3 elements :
12
22
0
Enter the query you wish to choose : 23
Oops !
Not found .
Do you wish to check for another query with same data ?
1.Yes 2.Press any to do not
2
Do you wish to continue ?
2
*/