1-D array

This is a discussion on 1-D array within the C++ Programming forums, part of the General Programming Boards category; Write a C-Program that swaps the elements of 1-D array (10 elements) : Example: If the given array is: 5 ...

  1. #1
    Registered User
    Join Date
    Apr 2006
    Posts
    16

    1-D array

    Write a C-Program that swaps the elements of 1-D array (10 elements) :
    Example:
    If the given array is:
    5 8 9 2 3 1 11 17 43 6
    The new array will be:
    6 43 17 11 1 3 2 9 8 5
    Your program should consist of the following functions:
    1. Function READ to read the elements of the array
    2. Function Display to print the elements of the new array
    3. Function SWAP to swap the elements of the array
    4. main function to call the previous functions
    Note: in swap function, do not use an intermediate array for swapping, i.e. do not
    declare a new array and fill it with the elements of the original array starting from the
    last element. The swap operation should be done on one array

    The size of array must be N (Entered by user)



    my question is how can i swap the elements of the array????

  2. #2
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,074
    Use a temp variable.
    Code:
    int arry[5] = { 1, 2, 3, 4, 5 };
    {
       int temp = arry[0];
       arry[0] = arry[1];
       arry[1] = temp;.
    }
    etc, etc..
    Sent from my iPadŽ

  3. #3
    Registered User
    Join Date
    Oct 2005
    Posts
    271
    Or maybe:
    Code:
    for(int i = 0; i < N; i++)
       i_new_array[i] = i_old_array[N-i]

  4. #4
    Registered User
    Join Date
    Apr 2006
    Posts
    16
    SlyMaelstrom

    i didnot get it
    can u explain more

  5. #5
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,074
    What more do you want me to say, OP? You use a temp variable. Meaning to say, the value you're about to overwrite, you store somewhere else so you still have it available to swap with.

    If you have two big TVs in two boxes and they have to swap boxes, what do you do? You take one out and you put it somewhere else temporarily until you move the other one into it's box. Then to take it out of the temp space and put it in the other box.
    Sent from my iPadŽ

  6. #6
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,074
    Quote Originally Posted by cunnus88
    Or maybe:
    Code:
    for(int i = 0; i < N; i++)
       i_new_array[i] = i_old_array[N-i]
    Think about this better.

    [0] = 5 - [1] = 3 - [2] = 7 - [3] = 9
    N = 3

    i_new array[0] = i_new_array[3];

    [0] = 9 - [1] = 3 - [2] = 7 - [3] = 9

    i_new array[1] = i_new_array[2];

    [0] = 9 - [1] = 7 - [2] = 7 - [3] = 9

    i_new array[2] = i_new_array[1];

    [0] = 9 - [1] = 7 - [2] = 7 - [3] = 9

    i_new array[3] = i_new_array[0];

    [0] = 9 - [1] = 7 - [2] = 7 - [3] = 9

    See? You gotta have that temp variable.
    Sent from my iPadŽ

  7. #7
    Registered User
    Join Date
    Apr 2006
    Posts
    16
    Think about this better.

    [0] = 5 - [1] = 3 - [2] = 7 - [3] = 9
    N = 3

    i_new array[0] = i_new_array[3];

    [0] = 9 - [1] = 3 - [2] = 7 - [3] = 9

    i_new array[1] = i_new_array[2];

    [0] = 9 - [1] = 7 - [2] = 7 - [3] = 9

    i_new array[2] = i_new_array[1];

    [0] = 9 - [1] = 7 - [2] = 7 - [3] = 9

    i_new array[3] = i_new_array[0];

    [0] = 9 - [1] = 7 - [2] = 7 - [3] = 9

    See? You gotta have that temp variable.

    i didnt undestand`anything

  8. #8
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,074
    Mmm, sorry if that confused you, it was directed more toward cunnus. Though, it's a good idea to do a run down of the example I gave you and see the results if you're still confused.
    Sent from my iPadŽ

  9. #9
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,744
    PHP Code:
    // Loop through the array all neat...
    for (int i(0), j(1); ArrayLength && ArrayLength-i; ++i, ++j)
    {
    // Now, we created the temp variable for a very good reason. It stores the current place
    // in the array. 
       
    int temp = array[i];
    // Then we let the current place and the second place swap.
       
    array[i] = array[j];
    // Then we set the second place to the value in the first place which is kept inside of temp.
       
    array[j] = temp;

    Last edited by whiteflags; 05-11-2006 at 09:29 PM.

  10. #10
    Registered User
    Join Date
    Apr 2006
    Posts
    16
    in question he says (The swap operation should be done on one array)

    if i used




    Code:
    for(int i = 0; i < N; i++)
       i_new_array[i] = i_old_array[N-i]
    will it be correct



    sorry guys i am a begginer

  11. #11
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,744
    No, as Sly explained. Also, that example uses two arrays, so you're professor won't accept it anyway.

  12. #12
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,074
    In regards to citizen's code:

    It's generally a good idea to check to make sure your code works before posting it. You can't update both variables at the same time in a single for loop for a swap function to work. You need two nested for loops. Also, we try not to give away complete code to new users that post assignments.
    Sent from my iPadŽ

  13. #13
    The larch
    Join Date
    May 2006
    Posts
    3,573
    You'll need to swap something, but what? In this particular example you'll need to swap:
    0 <-> 9, 1 <-> 8, 2 <-> 7, 3 <-> 6, 4 <-> 5.
    Can you see a pattern here? Can you do it in a loop? What would be the end condition of the loop? Can you write the swap function so that it would work on arrays of any size?

  14. #14
    Registered User
    Join Date
    Nov 2005
    Posts
    545
    Quote Originally Posted by SlyMaelstrom
    In regards to citizen's code:

    It's generally a good idea to check to make sure your code works before posting it. You can't update both variables at the same time in a single for loop for a swap function to work. You need two nested for loops. Also, we try not to give away complete code to new users that post assignments.
    It wasn't complete anyway as he has to put the numbers backwards rather than just swap the numbers next to each other.

  15. #15
    Registered User
    Join Date
    Apr 2006
    Posts
    16
    i solved it but the problem is that the first element of new array is always 0
    Code:
    #include<stdio.h>
    main( )
    {
    int	i,n,j ;
    float	arr[5], arr_new[5]   ;
    int temp;
    
    
    for (i=0 ; i<5 ; ++i) 
          {
    printf ("Enter the %dth element of array :", i) ;
    scanf ( "%f", &arr[i] ) ;
    
           }
    
    for (i=0 ; i<5 ; ++i) 
          {
    printf ("the value of element #%d is %f \n", i,arr[i]) ;
    
           }
    for( i = 0, j = 5; i<5; i++,j--)
    {
    temp = arr[i];
    arr[i] = arr_new[j];
    arr_new[j] = temp;
    }
    
    
    
     for (i=0 ; i<5 ; ++i) 
          {
    printf ("the new array value of element #%d is %f \n", i,arr_new[i]) ;
    
           }
    }

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

Similar Threads

  1. Replies: 16
    Last Post: 05-29-2009, 07:25 PM
  2. Replies: 6
    Last Post: 11-09-2006, 02:28 AM
  3. [question]Analyzing data in a two-dimensional array
    By burbose in forum C Programming
    Replies: 2
    Last Post: 06-13-2005, 07:31 AM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  5. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM

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