switching value of variable?

This is a discussion on switching value of variable? within the C++ Programming forums, part of the General Programming Boards category; Code: cout << "Input numbers are: " << x << " , " << y << endl; if ( x ...

  1. #16
    Registered User
    Join Date
    Jul 2011
    Posts
    10
    Code:
     cout << "Input numbers are: " << x << " , " << y << endl;
    
        if ( x < y) 
    
        
           { // exchange values of x and y 
    
    		 temp=x;
    		 x = y;
    		 y=temp;
    	
    	}
    	cout<<"x="<<x<<",y="<<y<<endl;
    
        /* At this point we will always have x >= y */
    
    	
        remainder=(x % y);
    	
        while (remainder!=0);
             {         
                x=y;
    			y=x % y;
    			remainder=x%y;
             }
    
        // display the result
         cout << endl ;
         cout << "The GCD is: " << y << endl ;

  2. #17
    Registered User
    Join Date
    Jul 2011
    Posts
    10
    That's with the changes. It terminates before GCD can be displayed.

  3. #18
    Registered User
    Join Date
    Jun 2011
    Posts
    2,234
    Code:
        while (remainder!=0);
        {
            x=y;             // x = y
    	y=x % y;         // y = x % y = x % x = 0
    	remainder=x%y;   // remainder = x % y = x % 0 = crash
        }

  4. #19
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    That's not how the Euclidean algorithm goes. It's more like:
    Code:
    x <- y
    y <- remainder
    remainder <- x%y
    Note the subtle difference.

  5. #20
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,866
    Take a look at your while-loop:
    Code:
     remainder=(x % y);
    	
        while (remainder!=0);{         
                x=y;
                y=x % y;
                remainder=x%y;
    }
    Now take a look at what the algorithm looks like:
    Code:
    while b ≠ 0
           t := b
           b := a mod b
           a := t
    Can you see the difference?
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  6. #21
    Registered User
    Join Date
    Jul 2011
    Posts
    10
    Well it did crash. As in nothing happend. I see the differences there in the formula. I declared remainder as an int at the start. However I was trying to intialize it and have the value declared as x%y however I don't think that worked.

  7. #22
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,449
    while (remainder!=0);
    In case it is there in your real code, it shouldn't be. Don't put a semicolon at the end of a loop or if statement (unless you want it to do nothing; that is, no body).
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Switching to RGB from GRB
    By HQSneaker in forum Game Programming
    Replies: 20
    Last Post: 09-28-2004, 07:48 AM
  2. Switching hardrives
    By incognito in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 03-01-2003, 08:42 PM
  3. About variable type switching..
    By Dragonlord in forum C Programming
    Replies: 6
    Last Post: 08-31-2002, 05:22 PM
  4. Switching arrays
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 02-18-2002, 01:55 PM
  5. switching
    By iain in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 08-11-2001, 08:29 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21