Thread: Have problems with copying my array!

  1. #1
    Registered User
    Join Date
    Sep 2008
    Posts
    54

    Question Have problems with copying my array!

    Guys i have this program i created. I have made a random number generator to the array, and then wanted to use to sorting functions, the functions seem to work properly. However, i wanted to make copies of the original array and work with the copies and functions so that the original doesnt get modified. After that, test the number of swaps each sorting function used. I tried copying the array in a copy variable, but its confusing me. Can anyone help me out. Here are the codes, im new to C too by the way.Thank U!

    ~Love Ava~

    Code:
    #define SIZE 100
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    void bubbleSort( int * const array, const int size );
    void swap( int *element1Ptr, int *element2Ptr );
    void selectionSort( int array[], int length );
    
    /* function prototypes */
    
    
    int main( void )
    {
       int array[ SIZE ]; /* declare the array of ints to be sorted (test with bublesort)*/
       int copy[SIZE];/*copy of original array to test with selectionsort*/
       strcpy(array ,copy );
       int i; /* int used in for loop */
        
        
       srand( time( NULL ) ); /* seed the rand function */
       
       for ( i = 0; i < SIZE; i++ )
          array[ i ] = rand() % 90 + 10; /* give each element a value */
           
       printf( "Unsorted array:\n" );
        
       for ( i = 0; i < SIZE; i++ ) /* print the array */
          printf( "%d  ", array[ i ] );
            
       printf( "\n\n" );
       bubbleSort( array, SIZE );
       printf( "Sorted array:\n" );
        
       for ( i = 0; i < SIZE; i++ ){ /* print the array */
          printf( "%d  ", array[ i ] );
          }
           
       system ("pause");
           
       return 0;
    } /* end function main */
    
    void bubbleSort( int * const array,int size )
    {
       void swap( int *element1Ptr, int *element2Ptr ); /* prototype */
       int pass; /* pass counter */
       int j;    /* comparison counter */
       
       /* loop to control passes */
       for ( pass = 0; pass < size - 1; pass++ ) {
    
          /* loop to control comparisons during each pass */
          for ( j = 0; j < size - 1; j++ ) {
    
             /* swap adjacent elements if they are out of order */
             if ( array[ j ] > array[ j + 1 ] ) {
                swap( &array[ j ], &array[ j + 1 ] );
             } /* end if */
    
          } /* end inner for */
    
       } /* end outer for */
    
    } /* end function bubbleSort */
    
    void swap( int *element1Ptr, int *element2Ptr )
    {
       int hold = *element1Ptr;
       *element1Ptr = *element2Ptr;
       *element2Ptr = hold;
    } /* end function swap */
    
    
    /* function that selection sorts the array */
    void selectionSort( int array[], int length )
    {
       int smallest; /* index of smallest element */
       int i, j; /* ints used in for loops */
        
       /* loop over length - 1 elements */
       for ( i = 0; i < length - 1; i++ ) {
          smallest = i; /* first index of remaining array */
            
          /* loop to find index of smallest element */
          for ( j = i + 1; j < length; j++ )
             if ( array[ j ] < array[ smallest ] )       
                    
            swap( &array[j],&array[smallest] ); /* swap smallest element */
          
       } /* end for */
    } /* end function selectionSort */

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    You should not use strcpy() on anything that isn't STRINGS. If you want to use a library function to copy generic data, then you should use memcpy().

    Edit: And of course, with code like this:
    Code:
       int array[ SIZE ]; /* declare the array of ints to be sorted (test with bublesort)*/
       int copy[SIZE];/*copy of original array to test with selectionsort*/
       strcpy(array ,copy );
    even if we replace strcpy with something that copies the array properly, it will just copy garbage, since array and copy contain nothing useful until you have filled them in with something meaningful. So you are just copying random gunk, really [and you're just lucky that the program doesn't crash at that point - obviously there is a zero somewhere in the data before you've done too much damage by using strcpy to copy something that isn't a string].

    --
    Mats
    Last edited by matsp; 09-24-2008 at 11:40 AM.
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Sep 2008
    Posts
    54
    Quote Originally Posted by matsp View Post
    You should not use strcpy() on anything that isn't STRINGS. If you want to use a library function to copy generic data, then you should use memcpy().

    --
    Mats
    Yes sorry love, i just finished reading that part. So memcpy would solve the problem?

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by AvaGodess View Post
    Yes sorry love, i just finished reading that part. So memcpy would solve the problem?
    Please read my edit as well.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Registered User
    Join Date
    Sep 2008
    Posts
    54
    Quote Originally Posted by matsp View Post
    Please read my edit as well.

    --
    Mats
    I know at that point it has basically nothing. But arent I feeding it some data in this part
    Code:
    for ( i = 0; i < SIZE; i++ )
          array[ i ] = rand() % 90 + 10; /* give each element a value */
           
       printf( "Unsorted array:\n" );
        
       for ( i = 0; i < SIZE; i++ ) /* print the array */
          printf( "%d  ", array[ i ] );
    I'm just asking for some help thats all, how could this little program be settled. Without doing what you say. Basically i wanted to make copies of the elements given to the array, and work with them in the function.

  6. #6
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    Quote Originally Posted by AvaGodess View Post
    Yes sorry love, i just finished reading that part. So memcpy would solve the problem?
    Think simple.
    Code:
    copy[i] = array[ i ] = rand() &#37; 90 + 10;
    da da!

    Is your nickname inspired from Sin City (curious) ?

    EDIT: Yes, memcpy() would work. But, in any case, you can always just use a loop and copy each individual element of the array.
    Last edited by C_ntua; 09-24-2008 at 11:53 AM.

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by AvaGodess View Post
    I know at that point it has basically nothing. But arent I feeding it some data in this part
    Code:
    for ( i = 0; i < SIZE; i++ )
          array[ i ] = rand() % 90 + 10; /* give each element a value */
           
       printf( "Unsorted array:\n" );
        
       for ( i = 0; i < SIZE; i++ ) /* print the array */
          printf( "%d  ", array[ i ] );
    I'm just asking for some help thats all, how could this little program be settled. Without doing what you say. Basically i wanted to make copies of the elements given to the array, and work with them in the function.
    Yes, but that is AFTER you called strcpy(). My point was that if we assume that you replace strcpy() with something that actually copies array into copy, it would still contain garbage, since array has NOT YET been filled with something - it contains whatever happens to be in that location of memory [1], and it's very unlikely that this matches your random number sequence.

    [1] Some compilers will actually fill unused memory with a recognisable pattern so that we can "see" that the memory has not been filled in, but the content is still not what you would like to have, so it's pretty irrelevant really.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by AvaGodess View Post
    I know at that point it has basically nothing. But arent I feeding it some data in this part
    Code:
    for ( i = 0; i < SIZE; i++ )
          array[ i ] = rand() % 90 + 10; /* give each element a value */
           
       printf( "Unsorted array:\n" );
        
       for ( i = 0; i < SIZE; i++ ) /* print the array */
          printf( "%d  ", array[ i ] );
    I'm just asking for some help thats all, how could this little program be settled. Without doing what you say. Basically i wanted to make copies of the elements given to the array, and work with them in the function.
    The two arrays are not "magically" linked together.
    They are separate arrays, with separate allocated space.
    What you do with one, does not affect the other.
    Therefore, if you want to duplicate the array, you should do so AFTER you've input the data.
    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.

  9. #9
    Registered User
    Join Date
    Sep 2008
    Posts
    54
    Quote Originally Posted by C_ntua View Post
    Think simple.
    Code:
    copy[i] = array[ i ] = rand() % 90 + 10;
    da da!

    Is your nickname inspired from Sin City (curious) ?

    EDIT: Yes, memcpy() would work. But, in any case, you can always just use a loop and copy each individual element of the array.
    Thats gorgeous, now im starting to think what my friends say bout blondes lol. How come i didnt see that, thats beautiful and simple, as soon as i get home near a compiler ill try it. Lol, hmm Sin City i heard when the movie was coming out but never actually went to see it so im not particularly sure about ur definition. But lad, my name well im Ava, and believe my beauty makes me a Godess, lol just kidding. Thank u very much love, ill let u know when i solve it at home, will be in a couple of hours. right now here at a cafe u see

  10. #10
    Registered User
    Join Date
    Sep 2008
    Posts
    54
    Quote Originally Posted by Elysia View Post
    The two arrays are not "magically" linked together.
    They are separate arrays, with separate allocated space.
    What you do with one, does not affect the other.
    Therefore, if you want to duplicate the array, you should do so AFTER you've input the data.
    Thank u very much Elysia, yes now im begning to understand the concepts of C. So im assuming if i make my copy after the array has been filled it will work right, like Matts had said before or simple declaring it as what he said arra[i]=copy[1]

    Oo im begning to love C, but its a bit tricky i think im gona enjoy this language!

  11. #11
    Registered User
    Join Date
    Sep 2008
    Posts
    54
    Quote Originally Posted by matsp View Post
    Yes, but that is AFTER you called strcpy(). My point was that if we assume that you replace strcpy() with something that actually copies array into copy, it would still contain garbage, since array has NOT YET been filled with something - it contains whatever happens to be in that location of memory [1], and it's very unlikely that this matches your random number sequence.

    [1] Some compilers will actually fill unused memory with a recognisable pattern so that we can "see" that the memory has not been filled in, but the content is still not what you would like to have, so it's pretty irrelevant really.

    --
    Mats
    Thank u very much Matt, i understand what u are saying now. As soon as get near a compiler ill follow all recommendations given. Thank u very much!!

  12. #12
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by AvaGodess View Post
    Oo im begning to love C, but its a bit tricky i think im gona enjoy this language!
    If you like C, then you're going to love C++.
    Just a friendly advice
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problems with passing an array of structures by pointer
    By raptor1770 in forum C Programming
    Replies: 9
    Last Post: 11-29-2008, 11:01 AM
  2. Problem(s) with an array!!
    By Leojeen in forum C Programming
    Replies: 6
    Last Post: 05-02-2008, 07:26 PM
  3. copying values of an array to another array?!
    By webznz in forum C Programming
    Replies: 8
    Last Post: 10-24-2007, 10:59 AM
  4. Help with an Array
    By omalleys in forum C Programming
    Replies: 1
    Last Post: 07-01-2002, 08:31 AM
  5. Hi, could someone help me with arrays?
    By goodn in forum C Programming
    Replies: 20
    Last Post: 10-18-2001, 09:48 AM