Possible addition of integers

Jun 25, 2013 at 10:23am
Task:
Suppose the default number is 10. You are supposed to write a program that outputs the total number of possible addition pairs of integers. Input two integers a and b. The range of both integers will be 1 to a and 1 to b. For example, a=6 and b=8, then there are 5 possibilities.
2+8=10
3+7=10
4+6=10
5+5=10
6+4=10
cannot be 7+3=10 because repeated
cannot be 8+2=10 because repeated
cannot be 9+1=10 because it overflow the range

EXAMPLE 1:
input: 6
8
output: 5 //5 possibilities

EXAMPLE 2:
input: 12
4
output: 4 //4 possibilities

EXAMPLE 3:
input: 1
9
output: 1 //1 possibility


After line 14, the output should be an integer >=0. However, my output is always 0. May some skilled people please help me with the code? Thank you.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include<cstdio>
#include<iostream>
using namespace std;
int main(){
int fdice,sdice,de=10,fsum,ssum,sum=0;
scanf("%d\n%d",&fdice,&sdice);
ssum=sdice;
fsum=de-ssum;
for(int i=0;fsum==fdice;i++){
fsum++;
ssum--;
sum++;
}
printf("%d\n",sum);
system("pause");
return 0;
}
Jun 25, 2013 at 10:33am
your for loop sustenance condition (fsum==fdice) is wrong. It means that the loop will be terminated as soon as fsum != fdice which will be true in most cases, especially since you are modifying fsum within your loop.
Jun 25, 2013 at 10:38am
So I should put fsum<=fdice?
Jun 25, 2013 at 1:21pm
well one possible "inefficient" solution is to loop 'i' from 1 to 10 and for each i you check whether the following conditions hold true:
i <= a
i <= b
10 - i <= a
10 - i <= b
and increment a counter accordingly.
Topic archived. No new replies allowed.