need help with analyzing some code

This is a discussion on need help with analyzing some code within the C Programming forums, part of the General Programming Boards category; Hey all. I'am using Teach Yourself C in 21 Days, and here is a sample code(p.214). The aim of the ...

  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
    22,265
    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().
    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

  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
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    to write this
    array[MAX] = 0;
    you need an array of at least MAX+1 elements
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  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, 11: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, 06:06 PM
  5. Replies: 0
    Last Post: 02-21-2002, 06:05 PM

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