splitting a certain value three ways - if/else

Hi!

Im trying to input three values PreComp, InlComp and PosComp into if else statements, then depending on the values of these three numbers use the if/else statements to allocate certain values to each of my variables Gain1 to Gain9.

The maximum value i can assign to any of the gains is 25.


Heres an example of how i need it to work....

If PreComp is 65 (and the max value of any a Gain is 25) then:
Gain 1 = 25
Gain 2 = 25
Gain 3 = 15

..i.e. totalling 65, with no one gain greater than 25.


If PreComp was 45 on the next run, then the Gains would be as follows:

Gain 1 = 25
Gain 2 = 20
Gain 3 = 0


Heres my 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
  double PreComp = atof(argv[++argi]);
	double InlComp = atof(argv[++argi]);
	double PosComp = atof(argv[++argi]);

	double Gain1 = 0;
	double Gain2 = 0;
	double Gain3 = 0;
	double Gain4 = 0;
	double Gain5 = 0;
	double Gain6 = 0;
	double Gain7 = 0;
	double Gain8 = 0;
	double Gain9 = 0;


    double Gain_PreComp = 0.65*PreComp;

    if (Gain_PreComp > 50){
    Gain1 = 25, Gain2 = 25, Gain3 = Gain_PreComp - 50;
    else {if (25 < Gain_PreComp <= 50)
    Gain1 = 25, Gain2 = Gain_PreComp - 25, Gain3 = 0;
    else {if (Gain_PreComp <= 25)
    Gain1 = Gain_PreComp, Gain2 = 0, Gain3 = 0;
    }}}


    double Gain_InlComp = 0.65*InlComp;

    if (Gain_InlComp > 50){
    Gain4 = 25, Gain5 = 25, Gain6 = Gain_InlComp - 50;
    else {if (25 < Gain_InlComp <= 50)
    Gain4 = 25, Gain5 = Gain_InlComp - 25, Gain6 = 0;
    else {if
    (Gain_InlComp <= 25)
    Gain4 = Gain_InlComp;
    Gain5 = 0;
    Gain6 = 0;
    }}}


    double Gain_PosComp = 0.65*PosComp;

    if (Gain_PosComp > 50){
    Gain7 = 25, Gain8 = 25, Gain9 = Gain_PosComp - 50;
    else {if (25 < Gain_PosComp <= 50)
    Gain7 = 25, Gain8 = Gain_PosComp - 25, Gain9 = 0;
    else {if (Gain_PosComp <= 25)
    Gain7 = Gain_PosComp, Gain8 = 0, Gain9 = 0;
    }}}


Last edited on
To get the values of Gain1, Gain2 and Gain3 take the value of PreComp divide by 25 to see how many time 25 goes into PreComp and then take the mod (%) of PreComp by 25.
Your example 65/25 = 2.6, take the integer of 2 and then 65%25 = 15. So, two 25s and one 15.
1
2
3
4
5
6
7
8
9
10
11
double GainValues[9];
int i(0);
for (i=0;i<9;i++){
    GainValues[i] = 0.0;
}
for(i = 0; i<int(PreComp/25); i++){
      GainValues[i] = 25;
}
if ( PreComp%25 ) {
      GainValue[i] = PreComp%25;
}


Edit: Fixing my mistype
Last edited on
cheers.

but that would be 25+25+6=56 ..not 65....do you mean 25+25+(0.6*25)=25+25+(15)=65.

also the PreComp, InlComp and PosComp are separate numbers which must be divided up independently, i.e....

PreComp gets divided up into Gain 1, Gain 2 and Gain3.
InlComp gets divided up into Gain 4, Gain 5 and Gain6.
PosComp gets divided up into Gain 7, Gain 8 and Gain9.


...using your method would i be right in doing this then:


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

double GainValues1[3];
int i(0);
for (i=0;i<3;i++)
{
GainValues1[i] = 0.0;
}

for(i = 0; i<int(PreComp/25); i++)
{
 GainValues1[i] = 25;
}
if ( PreComp%25 )
{
 GainValues1[i] = PreComp%25;
}


double GainValues2[3];
int j(0);
for (j=0;j<3;j++)
{
 GainValues2[j] = 0.0;
}

for(j = 0; j<int(InlComp/25); j++)
{
 GainValues2[j] = 25;
}
if ( InlComp%25 )
{
 GainValues2[i] = InlComp%25;
}


double GainValues3[3];
int k(0);
for (k=0;k<3;k++)
{
 GainValues3[k] = 0.0;
}

for(k = 0; k<int(PosComp/25); j++)
{
 GainValues3[k] = 25;
}
if ( PosComp%25 )
{
 GainValues3[i] = PosComp%25;
}

Last edited on
Sorry, mistype. 65%25 is 15 which would be 25+25+15 = 65.
Last edited on
Yes. Change to
1
2
3
4
// Note the change from i to j
GainValues2[j] = InlComp%25;
// And the change from i to k
GainValues3[k] = PosComp%25;

You could also make a function out of this.
Last edited on
oops my error missed those :) thanks.

how would i make a function? im semi-new to c++, can you explain a little more?

I just noticed that your numbers PreComp could have a fractional part given that they are doubles.

So I made some changes.
Notice how I am not using the % anymore. It wouldn't work with a double/float.

You could remove some of the hardcoded numbers i.e. 75 and make it more flexible.
Also, see how the values don't get set if the number is greater than 75, an error message prints out.

I hope this helps.

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
#include <iostream>

using std::cout;
using std::endl;

bool SetGainValues(double  *gainVals, int len, double Comp){
   bool err(false);
   int i(0),blocks(int(Comp/25));
   for (i=0;i<len;i++){
       gainVals[i] = 0.0;
   }
   if ( blocks*25 <= 75 ) { 
      for(i = 0; i<blocks; i++){
            gainVals[i] = 25;
      }
      if ( Comp-(blocks*25) ) {
            gainVals[i] = Comp-(blocks*25);
      }
   }
   else {
      err = true;
   }
   return err;
}

int main() 
{
    double PreComp(55.5),InlComp(33), PosComp(100);
    double GainValues1[3],
           GainValues2[3],
           GainValues3[3]; 

    // Be sure to check the values of Comp such 
    // that they are not above 75!
    if ( SetGainValues(GainValues1,3,PreComp) ){
       cout << "Error trying to set GainValues1!" << endl;
    }
    if ( SetGainValues(GainValues2,3,InlComp) ) {
       cout << "Error trying to set GainValues2!" << endl;
    }
    if ( SetGainValues(GainValues3,3,PosComp) ) {
       cout << "Error trying to set GainValues3!" << endl;
    }

    int i(0);
    cout << PreComp << endl;
    for (i=0; i<3; i++ ){
       cout << "GainValues1[" << i << "] = " << GainValues1[i] << endl; 
    }
    cout << InlComp << endl;
    for (int i(0); i<3; i++ ){
       cout << "GainValues2[" << i << "] = " << GainValues2[i] << endl; 
    }
    cout << PosComp << endl;
    for (int i(0); i<3; i++ ){
       cout << "GainValues3[" << i << "] = " << GainValues3[i] << endl; 
    }
}

Output:

./a.out
Error trying to set GainValues3!
55.5
GainValues1[0] = 25
GainValues1[1] = 25
GainValues1[2] = 5.5
33
GainValues2[0] = 25
GainValues2[1] = 8
GainValues2[2] = 0
100
GainValues3[0] = 0
GainValues3[1] = 0
GainValues3[2] = 0

thanks histrungalot, thats just what i was looking for.

one question left, i notice in the three 'for' statements at the bottom of main, that the first 'for' has i=0, however the second and third are i(0)...why is this? is there a reason (or is it just 'alternative notation') ?
Last edited on
There is a difference but in this case it is just a cut and paste error.
The ones with the int i(0) are scoped to in the for loop only. I should have just done the i = 0. But both work.
thanks for this histrungalot!
Topic archived. No new replies allowed.