Could someone help me with a simple problem

Back to topics list To post a new topic, please log in or register
avatar
2
dgodfrey 2015.05.11 18:07
 

I have a program in which a local variable, temp1, is assigned a value based on the highest value of an array.  It does this without any problems.

 

The comment block looks like this when I run it with the last line commented out.

 

480 is the value of temp1 at that point.  In the very next line of code, though, where I am trying to normalize the rawvalue variables,  temp1 is set to zero somehow and if I attempt to use it in that next line by removing the slashes, the program crashes with a zero divide error, due to temp1 being set to zero.  What am I doing wrong here? 

avatar
145
aakcaagac 2015.05.11 18:26 #
 

Three things that runs through my mind:

1) The for loop continues from 0 to 27 (because of "<"). You check whether rawvalue[j] is bigger than temp1. You assign rawvalue[j] to temp1 in case of if being valid. This process continues for j = 0, j = 1, j = 2, ... j = 27. Now what happens if the if statement doesn't get triggered at all. If rawvalue[j] == -1 (for example) 0 < -1 == false. What about 0 < 0 == false (in case rawvalue[j] == 0) ? Please note that 0 <= 0 == true and 0 < 0 == false (so the if statement is not being triggered).

2) Do you want to abort once the if statement is valid ? Maybe you need to add a break; so the entire for loop is being exited ? This is just a guess, since it's not clear to me whether you explicitly want to for loop all 28 elements.

3) You should clearly check for rawvalue[0] == 0 and avoid any division be zero that way.

avatar
53
amir_avatar 2015.05.11 18:31 #
 
obviously if what you say is true than the missing line where you normalize the rawvalue variables is at fault. Can you add that piece of code?

edit: btw, your temp1 is not having the highest value of the array as you say, but the highest absolute value of the array. In that case 480 can be because the lowest of the array was -480.
avatar
2
dgodfrey 2015.05.11 20:34 #
 
amir_avatar:
obviously if what you say is true than the missing line where you normalize the rawvalue variables is at fault. Can you add that piece of code?

edit: btw, your temp1 is not having the highest value of the array as you say, but the highest absolute value of the array. In that case 480 can be because the lowest of the array was -480.

Thanks for the answers.  The error trapping for the zero value worked.  Although the current value of temp1 is not zero, obvioiusly during the initialization at some point it is, and thats where the zero divide was happening.

BTW, I was trying to get the greatest absolute value to normalize the array into percentages of the maximum deviation from zero.  That's still not working, lol.  I'll get there. 



avatar
12998
WHRoeder 2015.05.11 20:52 #
 
dgodfrey:  I'll get there. 
Next time don't post an image
Don't paste code
Play video
Please edit your post.
For large amounts of code, attach it.

avatar
145
aakcaagac 2015.05.11 20:57 #
 
dgodfrey:

Although the current value of temp1 is not zero, obvioiusly during the initialization at some point it is, and thats where the zero divide was happening.

Actually it can. Think about this:

temp1=0;
for (int j = 0; j < 28; j++) {
        if (temp1 < normvalue[j])       <--- if this is always false (for 28 times), then temp1 stays 0 no matter what follows
}

Now assume this:

normvalue[0] = -12;

normvalue[1] = -32;

...

normvalue[26] = -33;

normvalue[27] = 0;

What happens is this, let's assume j == 27;

if (temp1 < normvalue[27]) temp1 = normvalue[27];

The if statement is false and thus normvalue[27] returns 0;

temp1 will be 0.

0 / 0 * 1000 == division by zero

avatar
53
amir_avatar 2015.05.11 21:10 #
 
aakcaagac:

Actually it can. Think about this:

Now assume this:

normvalue[0] = -12;

normvalue[1] = -32;

...

normvalue[26] = -33;

normvalue[27] = 0;

What happens is this, let's assume j == 27;

if (temp1 < normvalue[27]) temp1 = normvalue[27];

The if statement is false and thus normvalue[27] returns 0;

temp1 will be 0.

0 / 0 * 1000 == division by zero

It can not be false for 28 times unless all the array is filled with only zeroes.
Obviously your example is not the case, you wrote:

normvalue[1] = -32; 

.

.

.

So obviously temp1 which starts from zero will be smaller then any of those normvalue[*] absoulutes that you gave and thus, the condition will be true some times (at least once if the largest is first in the array). 

avatar
145
aakcaagac 2015.05.11 21:28 #
 

Yes I remembered (while I taking a shower) that you've been using MathAbs(); which of course turn negative values into positive. So yes only 28 times 0 will keep temp1 to stay at 0. Make sure your normvalues are not sending 28 times 0's or have temp1 checked once the for loop is left.

temp1 = 0;
for (int j = 0; j < 28; j++) {
  ;
}

if (temp1 == 0) return -1;

textvalue[0] = rawvalue[0] / temp1 * 1000;


avatar
Moderator
3418
GumRai 2015.05.12 02:52 #
 

I just don't see that it is possible to get a zero divide error from that code if the variables are declared properly.

if temp1 and raw1[] are declared as  integers and txtvalue[] is a double with a value other than zero then  

your commented code would set  txtvalue[]  to zero due to casting.

If you then have ????/txtvalue[]  somewhere later in your code, you will get the zero divide error

avatar
21
CristianoKonrad 2015.05.12 21:21 #
 
.
Back to topics list  

To add comments, please log in or register