473,320 Members | 1,920 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

What's wrong with my if loop??

16
Hi...

So I have problem with my if condition..I don't know what's wrong but it keeps resulting the wrong answer....

So here's the part of my code I have problem with:

Expand|Select|Wrap|Line Numbers
  1.  
  2.  for (i=0; i<size2; i++){          
  3.       for (k = 0; k < point3[i]; k++){       
  4.            for (a=0;a<b[i][k];a++){
  5.               if (90<sudut [i][k][a]<=180){
  6.                 teta[i][k][a]=180-sudut[i][k][a]; 
  7.               }  
  8.               else{          
  9.                 if (180<=sudut [i][k][a]<270){
  10.                    teta[i][k][a]=sudut[i][k][a]-180;
  11.                 }
  12.                 else {
  13.                      if (270<=sudut [i][k][a]<360){
  14.                         teta[i][k][a]=360-sudut[i][k][a]; 
  15.                      }
  16.                      else {
  17.                           if (0<=sudut[i][k][a]<90){
  18.                              teta[i][k][a]=sudut[i][k][a];
  19.                           }
  20.                      }
  21.                 }
  22.               }        
  23.  
  24.               printf ("[%d][%d][%d]=%f\t%f\n",i,k,a,sudut[i][k][a],teta[i][k][a]);
  25.  
  26.            }
  27.       }
  28.     } 
  29.  
and some of the result is:( I edited it for make it easier to see)

sudut [0][0][0] = 53.157051
teta [0][0][0] = 126.842949

sudut [0][0][1] = 206.000000
teta[0][0][1] = -26.000000

sudut[0][0][4] =341.555695
teta[0][0][4] = -161.555695

sudut [0][1][0] =127.272743
teta [0][1][0] = 52.727257

All of the result is wrong, I just show some because it is so many. I'm sorry...


P.S: if I change the order of the conditions, only the first condition will be result the right answers.

Could you tell me, where I did wrong?
Thank you for your help....
May 6 '09 #1
2 2199
Banfa
9,065 Expert Mod 8TB
You can not write a conditional of the form a <= b < c. It doesn't have the meaning you think it is evaluated as (a <= b) < c. Since the output of (a <= b) is 0 or 1 if c > 1 (always in your case) the condition is always true.

Look at this test program

Expand|Select|Wrap|Line Numbers
  1. #include<stdio.h>
  2.  
  3. int main()
  4. {
  5.     int i;
  6.  
  7.     for (i=0; i<10; i++)
  8.     {
  9.         printf ("%d: %d %d\n",i, (4<i<=8), (4<i && i<=8));
  10.     }
  11.  
  12.     return 0;
  13. }
  14.  
Also your code would lay out better if your made use of else if rather than else { if ... }

i.e. this

Expand|Select|Wrap|Line Numbers
  1. if (...){
  2.     ...
  3. }  
  4. else if (...){
  5.     ...
  6. }
  7. else if (...){
  8.     ...
  9. }
  10. else if (...){
  11.      ...
  12. }        
  13.  
instead of this

Expand|Select|Wrap|Line Numbers
  1. if (...){
  2.     ...
  3. }  
  4. else{          
  5.     if (...){
  6.         ...
  7.     }
  8.     else {
  9.          if (...){
  10.             ...
  11.          }
  12.          else {
  13.               if (...){
  14.                  ...
  15.               }
  16.          }
  17.     }
  18. }        
  19.  
P.S. if is not a loop
May 6 '09 #2
mingke
16
Thank you for correcting my mistake and my codes...

It's working fine now...
May 6 '09 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

226
by: Stephen C. Waterbury | last post by:
This seems like it ought to work, according to the description of reduce(), but it doesn't. Is this a bug, or am I missing something? Python 2.3.2 (#1, Oct 20 2003, 01:04:35) on linux2 Type...
51
by: WindAndWaves | last post by:
Can anyone tell me what is wrong with the goto command. I noticed it is one of those NEVER USE. I can understand that it may lead to confusing code, but I often use it like this: is this...
6
by: Niklaus | last post by:
Hi, Can someone point out what is wrong with this code ? How can i make it better optimize it. When run it gives me seg fault in linux. But windows it works fine(runs for a long time). Do we...
5
by: ken | last post by:
Hi, I have two questions the first is: in the example below how can I call an event from within a statement, such as replace Stop1 with cmdStop1 which is a button on my form? My second question...
669
by: Xah Lee | last post by:
in March, i posted a essay “What is Expressiveness in a Computer Language”, archived at: http://xahlee.org/perl-python/what_is_expresiveness.html I was informed then that there is a academic...
30
by: Bill Reid | last post by:
#define MAX_VALUES 64 typedef struct { unsigned value_1; double value_2; double value_3; double value_4; } VALUES; typedef struct {
16
by: chutsu | last post by:
Ok Here is a problem, I got a imaginary database program that I need to code, to add a patient I have function inser_patient. but when I try to input the details it doesn't quite work the way I...
14
by: Mohamed Mansour | last post by:
Hey there, this will be somewhat a long post, but any response is appreciated! I have done many PInvoke in the past from C++ to C#, but I did PInvoke within C# not C++/CLI. Can someone explain...
10
by: DavidSeck.com | last post by:
Hi, I am working with the Facebook API right now, an I have kind of a problem, but I don't know what I am doing wrong. So I have a few arrays, f.ex.: User albums: array(2) {
16
by: raylopez99 | last post by:
I am running out of printing paper trying to debug this...it has to be trivial, but I cannot figure it out--can you? Why am I not printing text, but just the initial string "howdy"? On the...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, youll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: Shllpp 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.