Thread: Partially filled array and output variable

  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.

  2. #2
    Registered User
    Join Date
    Dec 2017
    Posts
    1,626
    Yes, dbl_sizep is a pointer to in_use in main.
    The two i's in the different functions are different variables.
    Don't let that confuse you.
    You could change the name of i in fill_to_sentinel to x and the program would be the same.
    A little inaccuracy saves tons of explanation. - H.H. Munro

  3. #3
    Registered User
    Join Date
    May 2022
    Posts
    5
    In while loop will final value of i be 8 or 7? If I type in 8 values then value of i will be 7 because for the first element i will be equal to 0. How will *dbl_sizep be equal to i (*dbl_sizep=i) if the value of i is by 1 less than number of elements stored in array? If I type in sentinel value the value of i shouldn't be incremented because it breaks the condition.

  4. #4
    Registered User
    Join Date
    Dec 2017
    Posts
    1,626
    If I type in 8 values then value of i will be 7
    No, it will be 8. Although the first value of i will be 0, it will be incremented right after it is used as an index. The same thing happens on each iteration: the old value is used to index the array, then i is incremented.
    A little inaccuracy saves tons of explanation. - H.H. Munro

  5. #5
    Registered User
    Join Date
    May 2022
    Posts
    5
    Alright, now it's all much clearer to me! And what about for (i=0; i<in_use ; ++i)? Is it incremented at the same time it is used as index as this part lists the array values arr[0]-arr[7]?

  6. #6
    Registered User
    Join Date
    Dec 2017
    Posts
    1,626
    A for loop like this:
    Code:
    for (i = 0; i < in_use; ++i) {
        printf("%13.3f\n", arr[i]);
    }
    Is essentially "syntactic sugar" for a while loop like this:
    Code:
    i = 0;
    while (i < in_use) {
        printf("%13.3f\n", arr[i]);
        ++i;
    }
    As you can see, i is incremented after it is used as an index. So it is used as 0 then incremented to 1, used as 1 then incremented to 2, and as soon as it reaches in_use the loop stops, so it's never used as an index with that value. I.e., if in_use is 8, then i is used as an index from 0 to 7, just as it should be.
    A little inaccuracy saves tons of explanation. - H.H. Munro

  7. #7
    Registered User
    Join Date
    May 2022
    Posts
    5
    Thank you so much for choosing to spend part of your time in clearing these things up for me. I think I finally got it.

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