Thread: Partially filled array and output variable

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    May 2022
    Posts
    5

    Partially filled array and output variable

    Hi everyone. I've got example of partially filled array where two sizes of array are used, declared size and the actual size. What I'm confused about is the output variable *dbl_sizep. Is it pointer to in_use in main function? If it is indeed pointer to in_use variable then why it stands that *dbl_sizep = i if there will be condition in main function that i<in_use?

    This is the program I talk about:

    I - The fill_to_sentinel function

    insert
    Code:
     1. /*
    2.  * Gets data to place in dbl_arr until value of sentinel is encountered in  
    3.  * the input. 
    4.  * Returns number of values stored through dbl_sizep.  
    5.  * Stops input prematurely if there are more than dbl_max data values before  
    6.  * the sentinel or if invalid data is encountered. 
     7.  * Pre:  sentinel and dbl_max are defined and dbl_max is the declared size  
    8.  *       of dbl_arr  
    9.  */ 
    10. void 
    11. fill_to_sentinel(int    dbl_max,    /* input - declared size of dbl_arr      */ 
    12.                  double sentinel,   /* input - end of data value in 
    13.                                        input list                            */ 
    14.                  double dbl_arr[],  /* output - array of data                */ 
    15.                  int   *dbl_sizep)  /* output - number of data values
    16.                                                 stored in dbl_arr            */
    17. { 
    18.       double data; 
    19.       int    i, status; 
    20. 
    21.       /* Sentinel input loop                                                 */ 
    22.       i = 0; 
    23.       status = scanf("%lf", &data); 
    24.       while (status == 1 && data != sentinel && i < dbl_max) { 
    25.           dbl_arr[i] = data; 
    26.           ++i; 
    27.           status = scanf("%lf", &data); 
    28.       } 
    29. 
    30.       /* Issues error message on premature exit                              */ 
    31.       if (status != 1) { 
    32.             printf("\n*** Error in data format ***\n"); 
    33.             printf("*** Using first %d data values ***\n", i);
    34.  } else if (data != sentinel) { 
    35.             printf("\n*** Error: too much data before sentinel ***\n"); 
    36.             printf("*** Using first %d data values ***\n", i); 
    37.       } 
    38. 
    39.       /* Sends back size of used portion of array                            */ 
    40.       *dbl_sizep = i;

    II - Driver for testing fill_to_sentinel function

    insert
    Code:
     1. /* Driver to test fill_to_sentinel function */  
    2.  
    3. #define A_SIZE 20  
    4. #define SENT -1.0  
    5.  
    6. int  
    7. main(void) 
    8. {  
    9.       double arr[A_SIZE]; 
    10.       int    in_use,     /* number of elements of arr in use */ 
    11.              i; 
    12. 
    13.       fill_to_sentinel(A_SIZE, SENT, arr, &in_use); 
    14. 
    15.       printf("List of data values\n"); 
    16.       for  (i = 0; i < in_use; ++i) 
    17.           printf("%13.3f\n", arr[i]); 
    18.
     19.       return (0); 
    20. }
    Last edited by Programmin9; 05-31-2022 at 02:58 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 12-17-2016, 04:31 PM
  2. Checking to see if an array is filled
    By ljgerr93 in forum C Programming
    Replies: 5
    Last Post: 10-17-2011, 12:48 PM
  3. File handling with Array filled with a class
    By MarlonDean in forum C++ Programming
    Replies: 18
    Last Post: 06-27-2008, 10:53 AM
  4. Replies: 3
    Last Post: 11-16-2007, 03:10 PM
  5. Filled array
    By GaPe in forum C Programming
    Replies: 2
    Last Post: 12-11-2001, 05:36 AM

Tags for this Thread