Is this poor coding? copying arrays of doubles

This is a discussion on Is this poor coding? copying arrays of doubles within the C Programming forums, part of the General Programming Boards category; In my effort to learn C on my own, I am using Stephen Prata's book; it has some great assignments. ...

  1. #1
    Registered User
    Join Date
    May 2007
    Posts
    21

    Is this poor coding? copying arrays of doubles

    In my effort to learn C on my own, I am using Stephen Prata's book; it has some great assignments.

    This is the assignment:

    Write a program that initializes an array-of-double and then copies the contents of the array into two other arrays. (All three arrays should be declared in the main program.) To make the first copy, use a function with array notation. To make the second copy, use a function with pointer notation and pointer incrementing. Have each function take as arguments the name of the target array and the number of elements to be copied. That is, the function calls would look like this, given the following declarations:

    double source[5] = {1.1, 2.2, 3.3., 4.4, 5.5};
    double target1[5];
    double target2[5];
    copy_arr(source, target1, 5);
    copy_ptr(source, target1, 5);

    and here is my code solution:

    Code:
    #include <stdio.h>
    
    #define ITEMS 5
    
    double	source[ITEMS]	= {1.1, 2.2, 3.3, 4.4, 5.5};
    double	target1[ITEMS]	= {0.0};
    double	target2[ITEMS]	= {0,0};
    
    void copy_arr(double [], double [], int); 
    void copy_ptr(double *, double *, int);
    void print_results(void);
    
    int counter;
    
    int main(void)
    {
    	printf("BEFORE calling functions\n");
    	print_results();
    	
    	copy_arr(source, target1, ITEMS);
    	copy_ptr(source, target2, ITEMS);
    	
    	printf("AFTER calling functions\n");
    	print_results();
    
    	return 0;
    
    }
    
    void copy_arr(double source[], double target[], int items)
    {
    	for (counter = 0; counter < items; counter++) target[counter] = source[counter];
    	return;
    }
    
    void copy_ptr(double * source, double * target, int items)
    {
    	for (counter = 0; counter < items; counter++)
    	{
    		*target = *source;
    		source++; target++;
    	}
    
    	return;
    
    }
    void print_results(void)
    {
    	
    	printf("*----------------------------*\n");
    	printf("SOURCE  : ");
    	for (counter = 0; counter < ITEMS; counter++) printf("%1.1lf ", source[counter]); printf("\n");
    	printf("TARGET1 : ");
    	for (counter = 0; counter < ITEMS; counter++) printf("%1.1lf ", target1[counter]); printf("\n");
    	printf("TARGET2 : ");
    	for (counter = 0; counter < ITEMS; counter++) printf("%1.1lf ", target2[counter]); printf("\n");
    	printf("*----------------------------*\n");
    
    	return;
    }
    how does it look? it works fine -- here are the results I get:

    Code:
    BEFORE calling functions
    *----------------------------*
    SOURCE  : 1.1 2.2 3.3 4.4 5.5
    TARGET1 : 0.0 0.0 0.0 0.0 0.0
    TARGET2 : 0.0 0.0 0.0 0.0 0.0
    *----------------------------*
    AFTER calling functions
    *----------------------------*
    SOURCE  : 1.1 2.2 3.3 4.4 5.5
    TARGET1 : 1.1 2.2 3.3 4.4 5.5
    TARGET2 : 1.1 2.2 3.3 4.4 5.5
    *----------------------------*
    Press any key to continue . . .
    Is my code OK in your opinion? in terms of style etc.?

  2. #2
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Looks fine, in terms of indenting and all...
    A few things:
    * You can leave the names on your prototypes (this can save time later if you want to know the ordering of parameters)
    * Why is counter in the global scope?
    * Why not use double * source, rather than double source[]? You seem to alternate the usage of the two...
    * Use more line breaks, eg
    Code:
    for(counter = 0; counter < ITEMS; counter++)
        printf("&#37;1.1lf ", source[counter]);
    Last edited by zacs7; 05-25-2007 at 01:01 AM.

  3. #3
    Registered User
    Join Date
    May 2007
    Posts
    21
    Quote Originally Posted by zacs7 View Post
    Looks fine, in terms of indenting and all...
    A few things:
    * You can leave the names on your prototypes (this can save time later if you want to know the ordering of parameters)
    * Why is counter in the global scope?
    * Why not use double * source, rather than double source[]? You seem to alternate the usage of the two...
    * Use more line breaks, eg
    Code:
    for(counter = 0; counter < ITEMS; counter++)
        printf("%1.1lf ", source[counter]);
    Thanks!

    you are right, counter should not have global scope; you ask:
    * Why not use double * source, rather than double source[]? You seem to alternate the usage of the two...
    I did this simply because the assignment at the top asked me to do both; a pointer and an array indirection respectively.

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    I LOVE your style of coding, and can only hope that other posters follow it.

    Variables and declarations grouped together, alphabetically sorted, with one blank line between them, and CONSISTENCY in the MODERATE levels of indentations.

    Of course you used two different parameters for passing, because the assignment required it.

    Well done!

  5. #5
    Lean Mean Coding Machine KONI's Avatar
    Join Date
    Mar 2007
    Location
    Luxembourg, Europe
    Posts
    444
    Inspired by the strcpy source code, you could do this:

    Code:
    for (; (*target = *source) != 0; ++target, ++source);

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,758
    Inspired by the strcpy source code, you could do this:
    I have my doubts, strcpy() operates on null terminated strings, this is just a normal array. If the array has no zeros, you would end up with an array out of bounds error.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,709
    Perhaps this.
    Code:
    /*
        Copy an array using some cool pointer stuff.
        This version copies at most len items from old_arr to new_arr, assuming it
        is possible.  To make error handling easiest, copy_ptr() returns a useful
        pointer at the location where copying stopped.
    
        Preconditions: cpy_ptr() segfaults if you lie about the lengths of your arrays
        to promote bug free coding.  At least I hope it promotes that.
    */
    double *copy_ptr ( const double *old_arr, double *new_arr, size_t len ) {
      const double *iter;
      const double *end;
      double *where = NULL;
    
      if( old_arr && new_arr ) {
        for( iter = old_arr, end = old_arr + len; iter < end; iter++ ) {
          *new_arr++ = *iter;
        }
        where = new_arr;
      }
    
      return where;
    }

  8. #8
    Registered User pronecracker's Avatar
    Join Date
    Oct 2006
    Location
    netherlands
    Posts
    158
    Quote Originally Posted by KONI View Post
    Inspired by the strcpy source code, you could do this:

    Code:
    for (; (*target = *source) != 0; ++target, ++source);
    I believe you can always even omit !=0.

  9. #9
    Registered User pronecracker's Avatar
    Join Date
    Oct 2006
    Location
    netherlands
    Posts
    158
    Quote Originally Posted by Hansie View Post
    Code:
    double	target1[ITEMS]	= {0.0};
    double	target2[ITEMS]	= {0,0};
    Think you made a typo here, it doesn't really make a difference but a strict compiler will warn about 'assignment from int to double'.

  10. #10
    Registered User
    Join Date
    May 2007
    Posts
    21
    Quote Originally Posted by pronecracker View Post
    Think you made a typo here, it doesn't really make a difference but a strict compiler will warn about 'assignment from int to double'.
    good spot!!! and I even had VS2005 Pro on highest compiler warning (Level 4) -- and got 0 warnings!

  11. #11
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,682
    I can see that you have declared all your array and stuff as global, which is visible to all function on a global scrope. But you still send them as a parameter to the function.

    You can clearly see that the global variables are not save. The contents of them can be changed and can be called anywhere.

    But in your case it is fine. But in the real world global variables are quite expensive and not a good idea to use.

    Avoid global variables and send them as a parameter.

    ssharish2005

  12. #12
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    When I first read the assignment, I thought when it stated "create the three arrays in the main program", that what was meant was make all three arrays global.

    Rereading the OP, it seems what was meant was "create the three arrays in the main FUNCTION", and then send it to the other functions by using the specified methods.

    So those three arrays declaration & initialization should be moved into main(), do you agree?

  13. #13
    Registered User
    Join Date
    May 2007
    Posts
    21
    Quote Originally Posted by Adak View Post
    When I first read the assignment, I thought when it stated "create the three arrays in the main program", that what was meant was make all three arrays global.

    Rereading the OP, it seems what was meant was "create the three arrays in the main FUNCTION", and then send it to the other functions by using the specified methods.

    So those three arrays declaration & initialization should be moved into main(), do you agree?
    Yes, that would make sense!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Copying 2-d arrays
    By Holtzy in forum C++ Programming
    Replies: 11
    Last Post: 03-14-2008, 03:44 PM
  2. Copying character arrays
    By neandrake in forum C++ Programming
    Replies: 4
    Last Post: 02-27-2005, 05:32 PM
  3. Copying array's
    By big146 in forum C++ Programming
    Replies: 4
    Last Post: 06-15-2004, 01:25 PM
  4. Copying Arrays
    By conright in forum C++ Programming
    Replies: 5
    Last Post: 01-22-2003, 03:09 PM
  5. help on copying arrays
    By neversell in forum C Programming
    Replies: 3
    Last Post: 06-30-2002, 04:22 PM

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