Thread: How to identify the end of array

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Mar 2023
    Posts
    33
    The output is 5 because the array stops incrementing index when it reaches your element 0, as john.c explained with different words.

    Here is the working version of your program, "array[index]" is the value of the elements, and "index" can be used for the loop:

    Code:
    #include <stdio.h>
    
    
    unsigned int array[]={21,22,23,12,1,0,6,7};
    
    
    int main() 
    {
        // Write C code here
        unsigned int max=0;
        unsigned int index=0;
        while(index != 8)
        {
            if(array[index] > max)
            {
                max = array[index];
            }
            index++;
        }
        printf("max = %d\n number of elements = %d",max,index);
        return 0;
    }
    However, I'm sure you realize the issue with counting out the elements, so we could also make a version of your program that counts the number of integer elements so that it could be compared to the array even if you change the number of elements:

    Code:
    #include <stdio.h>
    
    
    unsigned int array[]={21,22,23,12,1,0,6,7};
    
    
    int main() 
    {
        // Write C code here
        unsigned int max=0;
        unsigned int index=0;
        unsigned int size = sizeof(array) / 4;
        
        while(index != size)
        {
            if(array[index] > max)
            {
                max = array[index];
            }
            index++;
        }
        printf("max = %d\n number of elements = %d",max,index);
        return 0;
    }
    What I did was pass the array to the sizeof() function, which counts the number of bytes in the array. Since integers are 4 bytes each, we divide the number by 4 to get the number of elements.

    You should also understand that null terminators are not necessary or used commonly for arrays of integers. Null terminators are just used to mark the end of a string, because otherwise your compiler will have no way of knowing when the loop ends otherwise. You don't actually need to add it yourself unless you are using a loop to make an array, if you have an array of chars, then the null terminator will get added automatically:

    Code:
    your_name[] = "Clearner123";
    When the string is declared like that, your compiler will add the null terminator.

    Also, having "while(array[index] != '\0')" is redundant, because while loops run until the condition is true or
    false anyways:

    Code:
    while(array[index])
    {
    ...
    }
    That while loop will run as long as "array[index]" does not equal zero...in programming terminology, it will
    run while the condition is true. To run the loop as long as the condition is equal to zero (false), then just
    add the exclamation point:

    Code:
    while(!array[index])
    {
    ...
    }

  2. #2
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    956
    Quote Originally Posted by C_me_run View Post
    Code:
    #include <stdio.h>
    
    
    unsigned int array[]={21,22,23,12,1,0,6,7};
    
    
    int main() 
    {
        // Write C code here
        unsigned int max=0;
        unsigned int index=0;
        unsigned int size = sizeof(array) / 4;
        
        while(index != size)
        {
            if(array[index] > max)
            {
                max = array[index];
            }
            index++;
        }
        printf("max = %d\n number of elements = %d",max,index);
        return 0;
    }
    What I did was pass the array to the sizeof() function, which counts the number of bytes in the array. Since integers are 4 bytes each, we divide the number by 4 to get the number of elements.
    What you should do instead of hardcoding the size of int as 4 (int is larger or smaller than 4 on some platforms) is to use sizeof (int), or better yet, sizeof array[0]:

    Code:
        unsigned int size = sizeof array / sizeof array[0];
    Using sizeof array[0] is useful in case you change the type of the array array.

  3. #3
    Registered User
    Join Date
    Apr 2021
    Posts
    143
    Quote Originally Posted by christop View Post
    What you should do instead of hardcoding the size of int as 4 (int is larger or smaller than 4 on some platforms) is to use sizeof (int), or better yet, sizeof array[0]:

    Code:
        unsigned int size = sizeof array / sizeof array[0];
    Using sizeof array[0] is useful in case you change the type of the array array.

    This macro is one of the most common utility macros in C programmers' toolboxes everywhere. Pretty much everybody needs it, and the standards committee has never bothered to help us out, because ... that's what they do.

    Create a header file named after yourself. So, @op might create clearner123.h. Then give it the standard include guard treatment, and then add this definition:

    Code:
    #define dimension_of(arr) (sizeof (arr) / sizeof *(arr))
    You can also put in all the other "cool" things you think might be useful, and have them all in one place.

    Code:
    #define elif else if
    #define is_power_of_2(intval) (((intval) & (intval) - 1) == 0)
    #define is_even(intval) (((intval) & 1) == 0)
    #define is_null(ptr) ((ptr) == NULL)
    #define STRCMP(a, op, b) (strcmp(a, b) op 0)
    #define str_eq(a, b) STRCMP((a), ==, (b))
    #define str_lt(a, b) STRCMP((a), <, (b))

  4. #4
    Registered User
    Join Date
    Mar 2023
    Posts
    33
    Quote Originally Posted by christop View Post
    What you should do instead of hardcoding the size of int as 4 (int is larger or smaller than 4 on some platforms) is to use sizeof (int), or better yet, sizeof array[0]:

    Code:
        unsigned int size = sizeof array / sizeof array[0];
    Using sizeof array[0] is useful in case you change the type of the array array.
    Yeah that's true, but it's also better to understand when you are starting out as a low-level programmer the size of elements of different data types, it's true your method is more flexible though and prevents you from having to make future changes to your code...

  5. #5
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    1,127
    Quote Originally Posted by C_me_run View Post
    Yeah that's true, but it's also better to understand when you are starting out as a low-level programmer the size of elements of different data types, it's true your method is more flexible though and prevents you from having to make future changes to your code...
    If the beginner, or "low-level" programmer, would first study a good up-to-date book on the C Programming Language, and do all the exercises at the end of each chapter, the programmer would then learn about all the data types available, such as char, int, long, float, double, etc.... The programmer would also learn about the header file, limits.h, and the minimum, and maximum range of values each type can contain.

    An advanced programmer could then study about the header file, stdint.h, to expand the types available.

    And of course, a study of the compiler documentation in use would provide the features and limits of the data types in the specific compiler.
    Last edited by rstanley; 01-02-2024 at 09:49 AM. Reason: typo correction

  6. #6
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    956
    Quote Originally Posted by C_me_run View Post
    Yeah that's true, but it's also better to understand when you are starting out as a low-level programmer the size of elements of different data types, it's true your method is more flexible though and prevents you from having to make future changes to your code...
    I disagree for the most part.

    A beginner programmer shouldn't really worry about how big each data type is (they should understand their ranges and when and where to use each type, though). And by the time a programmer learns about arrays, they should know how to use sizeof.

    A programmer also should learn from nearly the beginning not to use "magic numbers".

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. identify bug in code
    By acpower in forum C Programming
    Replies: 1
    Last Post: 06-17-2012, 07:02 AM
  2. Need help. Can't identify error.
    By bummielove in forum C Programming
    Replies: 23
    Last Post: 07-05-2011, 06:06 AM
  3. can you identify this car?
    By axon in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 07-19-2004, 02:59 PM

Tags for this Thread