Thread: Selection statements are changing the values of my variables!!

  1. #1
    Registered User
    Join Date
    Jan 2006
    Posts
    7

    Unhappy Selection statements are changing the values of my variables!!

    Hi everyone,

    Something mysterious is going on with my program and I'm just wondering if any of you had any idea what is happening.

    I have a loop like so:

    Code:
    	for(i=0;i<=apples;i=i+10){
    		pears=sqrt(pow((i-oranges[myfunction(i,oranges,apples)]),2));
    		printf("%f ",pears);
    		
    		/*if(pears>30.0){
    			selection=5.0;
    		}
    		if(pears<=30.0){
    			selection=10.0;
    		}*/
    		
    	}
    When this is executed, I correctly get the pears values that are expected (a list of 100 numbers that run from 0 to 250 to 0 to 250 to 0 again).

    However, when I uncomment the selection statements in order to actually do something useful with this list of numbers, the pears values all change! From a careful inspection, it seems the correct pears values are having the value of the 'selection' variable taken away from them. How are a couple of selection statements changing the value of a variable!?

    Is it a programming fault or a compiler problem? I am using the DigitalMars C Compiler if that is of any use.

    Thanks in advance,

    T

  2. #2
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Unless selection is a global that's accessed in myfunction, there is nothing inside the comment that can have any effect. Care to post the whole program?
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  3. #3
    Registered User
    Join Date
    Jan 2006
    Posts
    7
    Code:
    #include <stdio.h>
    #include <math.h>
    
    int myfunction(int,float[],int);
    
    int main(void){
    	
    	float oranges[3];
    	float selection;
    	int apples=1000;
    	int i;
    	float pears;
    	float select;
    	
    	for(i=0;i<=apples;i=i+10){
    		pears=sqrt(pow((i-oranges[myfunction(i,oranges,apples)]),2));
    		printf("%f ",pears);
    		
    		/*if(pears>30.0){
    			selection=5.0;
    		}
    		if(pears<=30.0){
    			selection=10.0;
    		}*/
    		
    	}
    	
    	while(0==0){}
    }
    
    int myfunction(int position,float oranges[],int apples){
    	int returnvar;
    	float pears=apples+1;
    	int i;
    	
    	for(i=1;i<=3;i++){
    		if(sqrt((oranges[i]-position)*(oranges[i]-position))<pears){
    			pears=sqrt((oranges[i]-position)*(oranges[i]-position));
    			returnvar=i;
    		}
    	}
    	
    	return returnvar;
    }
    For starters, I don't like the way I'm squaring the value then square rooting it to make any negatives positive (i.e |Modulus| in the world of Maths). Is there a function in C that will allow me to do this?

    Thanks for you help,

    T
    Last edited by ThorntonReed; 01-06-2006 at 05:19 AM.

  4. #4
    Registered User
    Join Date
    Jan 2006
    Posts
    7
    If it helps, the purpose of myfunction is to take an input variable 'position' and return the element in the oranges array that this 'position' is closest to. I.e position=260, returnvar=2; position=0, returnvar=1; position=740, returnvar=2; position=800, returnvar=3 etc.

    Cheers

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > for(i=1;i<=3;i++)
    All arrays start at 0, not 1
    Your code has already gone wrong when it tried to use oranges[3]

    for ( i = 0 ; i < 3 ; i++ )
    Would be the correct loop here.

  6. #6
    Registered User
    Join Date
    Jan 2006
    Posts
    7
    You, sir, are a genius. Either that or I'm a class-A idiot...or a bit of both. Thanks very much for pointing that out, that's made it work now!!

    T

  7. #7
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Salem's a genius, but that's nothing to do with it. You just made a typical newbie error. Catches very many newcomers. Nothing to worry about, as long as you remember from now on.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  8. #8
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Code:
    while(0==0){}
    There are better ways to keep the console window open after your program has finished -- see the FAQ.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > There are better ways to keep the console window open after your program has finished
    For example
    Code:
    while(1==1){}

  10. #10
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Or just plain
    Code:
    while(1);
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  11. #11
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Code:
    for( ; ; ) ;
    It's almost ASCII art.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  12. #12
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  13. #13
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    With
    Code:
    #define rof
    You can write
    Code:
    ;for( ; ; )rof;

  14. #14
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Well, if you bring the preprocessor into it you can do almost anything:

    Code:
    #define INFINITETINIFNI for(;;);
    INFINITETINIFNI
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  15. #15
    Registered User
    Join Date
    Nov 2005
    Posts
    95

    Wired

    The "oranges[3]" has never been initalized. And contains garbage all throw the program.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem on - Selection on which item to purchase
    By karipap in forum C Programming
    Replies: 5
    Last Post: 04-07-2009, 06:19 AM
  2. Replies: 2
    Last Post: 03-05-2009, 10:25 AM
  3. Effecient Selection Statements?
    By 031003b in forum C++ Programming
    Replies: 1
    Last Post: 10-25-2007, 06:08 AM
  4. selection problem
    By Ken JS in forum C Programming
    Replies: 3
    Last Post: 08-04-2007, 09:47 PM
  5. assign values to charechter variables
    By simhap in forum C++ Programming
    Replies: 5
    Last Post: 10-07-2001, 07:55 PM