Thread: need help with analyzing some code

  1. #1
    In my head happyclown's Avatar
    Join Date
    Dec 2008
    Location
    In my head
    Posts
    391

    need help with analyzing some code

    Hey all.

    I'am using Teach Yourself C in 21 Days, and here is a sample code(p.214). The aim of the code is to show how to let the program know the size of the array, by setting the value of the last array member to 0. When the program finds a 0 in the array, it knows that the end of the array is reached.

    Here is the code
    Code:
    /* Passing an array to a function. Alternative way. */
    
    #include <stdio.h>
    
    #define MAX 10
    
    int array[MAX+1], count;
    
    int largest(int num_array[]);
    
    int main( void )
    {
        /* Input MAX values from the keyboard. */
    
        for (count = 0; count < MAX; count++)
        {
            printf("Enter an integer value: ");
            scanf("%d", &array[count]);
    
            if ( array[count] == 0 )
                count = MAX;               /* will exit for loop */
        }
        array[MAX] = 0;
    
        /* Call the function and display the return value. */
        printf("\n\nLargest value = %d\n", largest(array));
    
        return 0;
    }
    /* Function largest() returns the largest value */
    /* in an integer array */
    
    int largest(int num_array[])
    {
        int count, biggest = -12000;
    
        for ( count = 0; num_array[count] != 0; count++)
        {
            if (num_array[count] > biggest)
                biggest = num_array[count];
        }
    
        return biggest;
    }
    My question is, is the +1 in array[MAX+1] required at all?

    Thanks in advance.
    OS: Linux Mint 13(Maya) LTS 64 bit.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by happyclown
    My question is, is the +1 in array[MAX+1] required at all?
    Yes, if you want up to MAX elements and yet have a special element be designated as the terminator. This is akin to how null terminated strings work.

    By the way, both the array and count global variables should be local variables in main().
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    a newbie :p
    Join Date
    Aug 2008
    Location
    Zurich, Switzerland, Switzerland
    Posts
    91
    Quote Originally Posted by laserlight View Post
    Yes, if you want up to MAX elements and yet have a special element be designated as the terminator. This is akin to how null terminated strings work.
    Hello, laserlight, but I think it is not string.
    It is array of integer.
    I am sure it is not necessary...

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by auralius View Post
    Hello, laserlight, but I think it is not string.
    It is array of integer.
    I am sure it is not necessary...
    Yes, but it's a ZERO-terminated array of integers - a string is a zero-terminated array of characters - the concept is very similar.

    --
    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
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    to write this
    array[MAX] = 0;
    you need an array of at least MAX+1 elements
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  6. #6
    a newbie :p
    Join Date
    Aug 2008
    Location
    Zurich, Switzerland, Switzerland
    Posts
    91
    but it's not to be printed out...
    it only prints a single integer...

    to write this
    array[MAX] = 0;
    you need an array of at least MAX+1 elements
    I definitely agree... but trust me... it won't cause any error when you run...
    better omit it.. right?

  7. #7
    In my head happyclown's Avatar
    Join Date
    Dec 2008
    Location
    In my head
    Posts
    391
    Quote Originally Posted by auralius View Post


    I definitely agree... but trust me... it won't cause any error when you run...
    better omit it.. right?
    I tested the program without the +1, and it seems to run ok.
    OS: Linux Mint 13(Maya) LTS 64 bit.

  8. #8
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by happyclown View Post
    I tested the program without the +1, and it seems to run ok.
    That's because it's undefined behaviour. I can cross Waterloo road when the red-man [don't walk] is showing without looking and get across the road - once or twice. But if I constantly cross roads without looking at the signs, I will eventually get run over by a big red bus.

    Did you check the value of count after you set array[MAX] = 0?

    --
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Values changing without reason?
    By subtled in forum C Programming
    Replies: 2
    Last Post: 04-19-2007, 10:20 AM
  2. Obfuscated Code Contest
    By Stack Overflow in forum Contests Board
    Replies: 51
    Last Post: 01-21-2005, 04:17 PM
  3. Analyzing Code
    By smitsky in forum C++ Programming
    Replies: 13
    Last Post: 01-12-2005, 05:33 PM
  4. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM
  5. Replies: 0
    Last Post: 02-21-2002, 06:05 PM