Please help me to make this c++ code work. Only needs help with one part.?

This is a discussion on Please help me to make this c++ code work. Only needs help with one part.? within the C++ Programming forums, part of the General Programming Boards category; The only problem is this part: Code: " num[i] ^= num[i+1]; num[i+1] ^= num[i]; num[i] ^= num[i+1]; " #include <iostream> ...

  1. #1
    Registered User
    Join Date
    Mar 2010
    Posts
    7

    Please help me to make this c++ code work. Only needs help with one part.?

    The only problem is this part:
    Code:
    " num[i] ^= num[i+1];
    num[i+1] ^= num[i];
    num[i] ^= num[i+1]; "
    
    #include <iostream>
    #include <cmath>
    
    using namespace std;
    
    double integrate(double lower, double upper);
    double f(double x);
    
    int main()
    {
    int i, j;
    int ranges[][2] = {{0,1},{1,2},{2,3},{3,4},{4,5},{5,6},
    {6,7},{7,8},{8,9},{9,10}};
    double num[10];
    for(i=0; i<10; ++i)
    num[0] = integrate(ranges[i][0],ranges[i][1]);
    
    i = 0;
    while ((i+1) < 10) {
    if (num[i] > num[i+1]) {
    num[i] ^= num[i+1]; // from here to
    num[i+1] ^= num[i];
    num[i] ^= num[i+1]; here, it shows an error //
    if (i > 0) i = i -1;
    } else {
    i += 1;
    }
    }
    
    for(i=0; i<10; ++i)
    cout << num[i] << endl;
    return 0;
    }
    double integrate(double lower, double upper)
    {
    double n = 0.0;
    double dx = (upper-lower)/2000;
    for (double i = lower; i < upper-dx; i += dx)
    {
    n += sin(i)*dx;
    
    }
    return n;
    }
    
    double f(double x)
    {
    return sin(x);
    }
    I tried to sort this code from smallest to largest.
    Last edited by Salem; 04-14-2010 at 12:46 PM. Reason: Let's do the time warp again

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    So one day you should learn how to swap two items. Hint: ^ is not involved.

  3. #3
    In the Land of Diddly-Doo g4j31a5's Avatar
    Join Date
    Jul 2006
    Posts
    476
    huh? why did you do the XOR? and if you are thinking to do a swap between 2 variables, that isn't the way to do it. complementing tabstop's comment: hint: temporary variable.
    ERROR: Brain not found. Please insert a new brain!

    “Do nothing which is of no use.” - Miyamoto Musashi.

  4. #4
    Registered User
    Join Date
    Mar 2010
    Location
    Norway
    Posts
    25
    Code:
    int a;
    int b[4];
    b[1] = 1; b[2] = 5; b[3] = 3
    
    for(int i = 1; i < 4; i++)
    {
         for(int ii = 1; ii < 4; ii++)
         {
           if(b[ii] < b[i])
            {
             a = b[ii];
            b[ii] = b[i];
            b[i] = a;
            }
         }
    }
    Swaps, B[1] contains the largest number after the swap is done, B[3] the smallest.

  5. #5
    Registered User
    Join Date
    Mar 2010
    Posts
    7
    ManyTimes, your code is wrong.

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    But surely you can identify the "swap" portion of the code?

  7. #7
    Registered User
    Join Date
    Mar 2010
    Posts
    7
    swap portion? I have never seen "ii"

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    But surely you can identify which portion of his code does the swapping? Bonus super hint: variable names do not actually matter.

  9. #9
    Registered User NeonBlack's Avatar
    Join Date
    Nov 2007
    Posts
    435
    Quote Originally Posted by gulfx01 View Post
    The only problem is this part:
    Code:
    " num[i] ^= num[i+1];
    num[i+1] ^= num[i];
    num[i] ^= num[i+1]; "
    
    #include <iostream>
    #include <cmath>
    
    using namespace std;
    
    double integrate(double lower, double upper);
    double f(double x);
    
    int main()
    {
    int i, j;
    int ranges[][2] = {{0,1},{1,2},{2,3},{3,4},{4,5},{5,6},
    {6,7},{7,8},{8,9},{9,10}};
    double num[10];
    for(i=0; i<10; ++i)
    num[0] = integrate(ranges[i][0],ranges[i][1]);
    
    i = 0;
    while ((i+1) < 10) {
    if (num[i] > num[i+1]) {
    num[i] ^= num[i+1]; // from here to
    num[i+1] ^= num[i];
    num[i] ^= num[i+1]; here, it shows an error //
    if (i > 0) i = i -1;
    } else {
    i += 1;
    }
    }
    
    for(i=0; i<10; ++i)
    cout << num[i] << endl;
    return 0;
    }
    double integrate(double lower, double upper)
    {
    double n = 0.0;
    double dx = (upper-lower)/2000;
    for (double i = lower; i < upper-dx; i += dx)
    {
    n += sin(i)*dx;
    
    }
    return n;
    }
    
    double f(double x)
    {
    return sin(x);
    }
    I tried to sort this code from smallest to largest.
    http://en.wikipedia.org/wiki/Swap_(computer_science)
    I copied it from the last program in which I passed a parameter, which would have been pre-1989 I guess. - esbo

  10. #10
    Registered User
    Join Date
    Sep 2009
    Posts
    68
    Gulfx01. The idea of swap that everyone is throwing around is quite simple.

    Say I have two variables. A and B. I want A to hold B's value and B hold A's value.

    So if you want to put them in order...

    In this case I put them in an array.

    Code:
    int sort[2];
    
    sort[0] = 5;
    sort[1] = 3;
    
    If (sort[1] < sort[0])
    {
         int temp; // Holds one of the values temporarily while the two are swapped
    
         temp = sort[0]; // So now the temp variable stores the value of 5
         sort[0] = sort[1]; // Now 3 is stored in the first position
         sort[0] = temp; // Now 5 is stored in the second position
    }
    obviously you can make this into a function

    Code:
    void swap(int& a, int& b)
    {
         // The same idea as above.
    
        int temp;
        temp = a;
        a = b;
        b = temp;
    }
    Last edited by bleuz; 04-13-2010 at 09:43 PM.

  11. #11
    Registered User
    Join Date
    Jan 2007
    Posts
    330
    The x^=y^=x^=y trick is common among beginning programmers.
    They dont realize it's non portable, can only be used on integers and is slower than the temporary variable swap. They just think its cool that you can use xor to swap two things

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to Make This code more efficient
    By Soulzityr in forum C Programming
    Replies: 9
    Last Post: 04-12-2010, 01:29 AM
  2. Adding extra lines of code doesn't work
    By Yoshi1981 in forum C++ Programming
    Replies: 6
    Last Post: 03-17-2010, 09:12 PM
  3. Help with part of code.
    By ajdspud in forum C++ Programming
    Replies: 4
    Last Post: 02-23-2006, 04:21 PM
  4. Obfuscated Code Contest
    By Stack Overflow in forum Contests Board
    Replies: 51
    Last Post: 01-21-2005, 03:17 PM
  5. How do I make and edit box scroll down with code?
    By Unregistered in forum Windows Programming
    Replies: 0
    Last Post: 11-26-2001, 01:33 PM

Tags for this Thread


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