im stuck with my homework

Nov 18, 2011 at 7:42am
Hey :(
I've been thinking for a while on one program for my homework and i just can't come up with a solution, maybe im just dumb xD Can anyone atleast point me in the right direction atleast where to start?

A dude puts a bugs into one jar, and b bugs in another one (a is two times bigger than b (a = 2*b)) they breed, and after T days there are N bugs in total.
Now you get only the N, and must calculate how much bugs there are and how many days passed since the dude put the bugs in the jars.
In one day the a bugs multiply by 2, and the b bugs multiply by 3.

So basically all we know is that (a*2 + b*3) * y = n, and that a = 2*b.
Please help, i only need to understand how to solve it and i can code in everything by myself.
Nov 18, 2011 at 8:08am
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
using namespace std;

void doQueryUser(void);

int main(void) {
    doQueryUser();

    return 0;
}

void doQueryUser(void) {
    cout << "Please type in ..." << endl;
    // ...
}


Or what do you want exactly?
Nov 18, 2011 at 8:26am
I need the solving of it, i mean the function to get the a, b and T from only the n.
like if N = 17 then t = 2, a = 2 and b = 1 because:
2 * 2 * 2 = 8; 1 * 3 * 3 = 9 and 2 + 8 = 17
Nov 18, 2011 at 3:30pm
Using int or double?
Nov 18, 2011 at 3:41pm
Nested while or for loops. First one goes through all possible versions of a & b until a + b > T. Second one performs the transition of days i.e counts T until a + b > T. Have comparison if (a+b == T) then output T, a & b. Might be multiple soloutions. Also may be no solutions so output for that. Only really considered this for int and double/floating point might get pretty complicated...
Nov 18, 2011 at 6:01pm
To me the problem is not just coding of the equation but the mathematics of your problem.

You are trying to find three variables a (bugs in 1st jar) ,b (bugs in 2nd jar) and T (number of days)

where as you only have two equations.

a=2*b and (a*2 + b*3) * T = n

As per my knowing of maths, number of equations must be equal to number of variables to be calculated
Last edited on Nov 18, 2011 at 6:02pm
Nov 18, 2011 at 6:28pm
Just substitute a for 2*b to get onen equation.
Last edited on Nov 18, 2011 at 6:28pm
Nov 18, 2011 at 6:33pm
that will leave you with

7*b*T = n ;

resulting either

b = n/(7*T)

or


T = n/(7*b)

now you still cant sole it by having only "n".
Nov 19, 2011 at 5:06pm
your right. The beauty about using the comp tho is that you can quickly simulate the different instances of a and thus b and the number of days. You then can check to see whether they equate to n. Also due to the relationship between A and B (a being double) and that they are either doubled or tripled each day there shouldn't be too many simulations to be performed for different values of b and thus a. The above formula is also incorrect because T will have a different effect on a and b (a * 2) (b * 2).
Nov 26, 2011 at 5:17pm
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
#include <iostream>

using namespace std;

int main(){
    
    int a=0;
    int b=0;
    int t=0;
    int n=1;
    bool found=false;
    
    cout << "Please input the N : ";
    cin >> n;
        
    for(b=1;b<100;b++){
                 
                 for(t=1;t<100;t++){
                      
                      if(7*b*t==n){
                                   
                                   found=true;
                                   break;
                                   
                                   }              
                                  
                                  if(found){
                                            
                                            break;
                                            
                                            }  
                                    
                                    }
                                  
                 }

    cout << "For then given N = " << n << endl;
    cout << "Bugs in Jar A were = " << 2*b << endl;    
    cout << "Bugs in Jar B were = " << b << endl;
    cout << "Time spent was = " << t << " Days" << endl;
    
     for(t=1;t<100;t++){
                 
                 for(b=1;b<100;b++){
                      
                      if(7*b*t==n){
                                   
                                   found=true;
                                   break;
                                   
                                   }              
                                  
                                  if(found){
                                            
                                            break;
                                            
                                            }  
                                    
                                    }
                                  
                 }

    cout << "\n OR \n\n";
    cout << "Bugs in Jar A were = " << 2*b << endl;    
    cout << "Bugs in Jar B were = " << b << endl;
    cout << "Time spent was = " << t << " Days" << endl;
    
 return 0;   
 
}


The code does the job to some extent but gives only two possible answers, although they were correct as per given conditions but still not the one I had in mind.

@award982
Seems you don't need it any more but I was curious to solve it.
Nov 26, 2011 at 7:41pm
I think you guys have made a horrible and HUGE mistake. The expression a=2*b is true only at the first day. Once the bugs start multiplying, this isn't true! Try this out, consider 1 bug in b, hence 2 bugs in a, initially. At the end of the second day, bugs in a are 4(they doubled), and bugs in b are 3(they tripled), so total is 7. According to your formula,
7*b*T should be 7,
which is not true!!!
Topic archived. No new replies allowed.