code problem

hello, my code takes as input the number of fractions then the fractions which are either 1/4,1/2,3/4
and then it counts how many 1s there are for example:
1/4 + 3/4 = 1
1/2 + 1/2 = 1
1/4 + 1/4 + 1/4 + 1/4 = 1

but the line 40 is not executing...
heres the code:

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
# include <iostream>
# include <cmath>
# include <string>
using namespace std;
int fractions[3]={0};
int main()
{
	int n;
	cin>>n;
	for (int i=0;i<n;i++)
	{
		int a;
		char b;
		int c;
		cin>>a>>b>>c;
		if (a==3)
			fractions[2]++;
		else if (c==2)
			fractions[1]++;
		else
			fractions[0]++;
	
	}
	long long ans=0;
		for (int i=0;i<3;i++)
		cout<<fractions[i]<<" ";
	cout<<endl;
	ans+=fractions[0]/4;
	fractions[0]%=4;
		for (int i=0;i<3;i++)
		cout<<fractions[i]<<" ";
	cout<<endl;
	ans+=fractions[1]/2;
	fractions[1]=fractions[1]%2;
		for (int i=0;i<3;i++)
		cout<<fractions[i]<<" ";
	cout<<endl;
	ans+=min(fractions[0],fractions[2]);
		fractions[2]-=min(fractions[0],fractions[2]);
	fractions[0]-=min(fractions[0],fractions[2]);//not working

		for (int i=0;i<3;i++)
		cout<<fractions[i]<<" ";
	cout<<endl;
	cout<<ans<<endl;
	for (int i=0;i<3;i++)
		cout<<fractions[i]<<" ";
	cout<<endl;
return 0;
}
What do you mean saying "not working"?
i tried the input
7
1/4
1/4
1/4
1/4
1/4
1/2
3/4


first its taking 4 1/4 to make 1
then it should take 1 3/4 and 1 1/4 but its not subtracting in line 40
I recommend stepping through it with a debugger. That way, you'll be able to see what's happening to the values of your variables with each line of code.
yes, but the error is obvious
In line 39 the value to be substracted is the same in line 40
It is substracted in line 39 but no in line 40
Before executing line 39, what are the values of:

fractions[0]
fractions[2]

?

After executing line 39, what are their values?
Last edited on
im sorry for disturbing you guys,

the problem is solved, its a silly mistake by me (as usual), first min() function returns 1 then im substracting it from fractions[2] thats why min() is returning 0 in line 40



sorry again
Glad you managed to solve it!
Topic archived. No new replies allowed.