CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Jul 2010
    Posts
    75

    Qestions About The Array

    hello

    how can i swap between the array

    with to elements or couple of numbers for example i got

    a { 5 , 2 , 3 , 7 }

    b { 2 , 7 , 1 , 9 }

    how can i swap between 2 and 7
    7 & 3
    1 & 2
    3 & 7

    what should i write i mean the code ?

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,430

    Re: Qestions About The Array

    To swap elements i and j within the same int array a;
    Code:
    int temp = a[i];
    a[i] = a[j];
    a[j] = temp;
    Victor Nijegorodov

  3. #3
    Join Date
    Jul 2010
    Posts
    75

    Re: Qestions About The Array

    can u explain to me the code cuz i didnt understand it

  4. #4
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,430

    Re: Qestions About The Array

    No, sorry!
    You seem to first must learn the basics of mathematics, logic and algorithms
    and only after that - go on with basics of programming.
    Victor Nijegorodov

  5. #5
    Join Date
    Jul 2009
    Posts
    46

    Re: Qestions About The Array

    Quote Originally Posted by jacksparrow View Post
    can u explain to me the code cuz i didnt understand it
    int temp = a[i];
    a[i] = a[j];
    a[j] = temp;

    He has created an integer variable called "temp" and assigned the value of a[i] to it. Then his has assigned the value of a[j] to a[i]. Finally, he assigned the value of a[i] (which was assigned to temp earlier) and assigned it to a[j].

    a[i] goes into 'temp', a[j] goes into a[i], and temp goes into a[j]. Simple enough.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured