Skip to content

Commit 1cfc699

Browse files
authored
Merge pull request #759 from Mayuri-cell/Mayuri-cell-patch-8
The position pairs to get the sum query.
2 parents 2b8a894 + 5ac038a commit 1cfc699

File tree

1 file changed

+147
-0
lines changed

1 file changed

+147
-0
lines changed

Arrays/sum_positions.cpp

Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
/* Title : User need to enter the elements he wants to enter .
2+
The possible pairs of positions will be found whose sum
3+
will equal to the query number accepted .
4+
5+
ex : Elements : 2 3 4 5
6+
Query element : 8
7+
Output : Addition of elements present at position 2 and 4 = 8.
8+
9+
*/
10+
#include<iostream>
11+
#include<cstdlib>
12+
#include<conio.h>
13+
int fact (int n);
14+
using namespace std;
15+
16+
int main()
17+
{
18+
int i , j ,n,th;
19+
int opt , count=0;
20+
21+
22+
do
23+
{
24+
cout<<"\n Enter the total number of elements : \n"<<endl; // Enter more than 1.
25+
cin>>n;
26+
if(n<=1)
27+
{
28+
cout<<"\n Invalid Input .\n Try again later ! "<<endl;
29+
exit(0);
30+
}
31+
int fac=fact(n);
32+
int a[n];
33+
cout<<"\n Enter the "<<n<<" elements : \n"<<endl;
34+
for(i=0;i<n;i++)
35+
{
36+
cin>>a[i];
37+
}
38+
39+
do
40+
{
41+
cout<<"\n Enter the query you wish to choose : ";
42+
cin>>th;
43+
44+
cout<<"\n";
45+
count=0;
46+
for(i=0;i<n;i++)
47+
{
48+
for(j=i+1;j<n;j++)
49+
{
50+
if(a[i]+a[j]==th)
51+
{
52+
cout<<" Found : "<<" Addition of elements at positions "<<i+1<<" and "<<j+1<<endl;
53+
}
54+
55+
else
56+
{ count++ ; }
57+
}
58+
}
59+
60+
if(count==fac)
61+
{
62+
cout<< "\n Oops ! \n Not found .\n"<<endl;
63+
}
64+
65+
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 .
66+
cin>>opt; // It will be done on the same data sets .
67+
}while(opt==1);
68+
69+
cout<<"\n Do you wish to continue ? \n Press 1 to continue or any to stop . \n"<<endl; // Press 1 to continuw\e .
70+
cin>>opt;
71+
}while(opt==1); // You will need to enter the data of arrays and query again .
72+
}
73+
74+
int fact(int n)
75+
{
76+
int i=n-1, sum=0;
77+
while(i>=1)
78+
{
79+
sum=sum+i ;
80+
i--;
81+
}
82+
return sum;
83+
}
84+
85+
/*
86+
Enter the total number of elements :
87+
88+
7
89+
90+
Enter the 7 elements :
91+
92+
106
93+
-53
94+
0
95+
53
96+
21
97+
4
98+
23
99+
100+
Enter the query you wish to choose : 53
101+
102+
Found : Addition of positions 1 and 2
103+
Found : Addition of positions 3 and 4
104+
105+
Do you wish to check for another query with same data ?
106+
1.Yes 2.Press any to do not
107+
108+
1
109+
110+
Enter the query you wish to choose : 4
111+
112+
Found : Addition of positions 3 and 6
113+
114+
Do you wish to check for another query with same data ?
115+
1.Yes 2.Press any to do not
116+
117+
2
118+
119+
Do you wish to continue ?
120+
121+
1
122+
123+
124+
Enter the total number of elements :
125+
3
126+
127+
Enter the 3 elements :
128+
12
129+
22
130+
0
131+
132+
Enter the query you wish to choose : 23
133+
134+
135+
Oops !
136+
Not found .
137+
138+
139+
Do you wish to check for another query with same data ?
140+
1.Yes 2.Press any to do not
141+
142+
2
143+
144+
Do you wish to continue ?
145+
146+
2
147+
*/

0 commit comments

Comments
 (0)