0

May i know why is int count, biggest = -12000;? Why must it be -12000 and I do not understand this statement biggest = -12000 If I put biggest = 10000, it can still compile. Appreciate your advise as I am currently learning c programming. Can you please understand as clearly as possible? Thanks in advance!

#include <stdio.h>

#define MAX 10

int array[MAX], count;

int largest(int x[], int y);

int main()
{
    /* Input MAX values from the keyboard. */

    for (count = 0; count < MAX; count++)
    {
        printf("\nEnter an integer value:\n ");
        scanf_s("&d", &array[count]);
    }

    /* Call the function and display the return value. */
    printf("\n\nLargest value = %d\n", largest(array, MAX));

    return 0;
}

/* Function largest() returns the largest value in an integer array */

int largest(int x[], int y)
{
    int count, biggest = -12000;

    for (count = 0; count < y; count++)
    {
        if (x[count] > biggest)
            biggest = x[count];
    }

    getchar();

    return biggest;

}
1
  • 3
    Read more C programming books till you understand what "arrays are decayed to pointers, e.g. when passed as arguments" means. Also, edit your question to improve formatting (add four spaces in front of every code line) Commented Jun 5, 2015 at 7:57

2 Answers 2

6

If you want to find the largest number in an array you compare all elements against the currently 'biggest' value. Whenever you find a value that's larger you put it in biggest.

To make sure that you find the proper value you must initialize biggest to a sensible value.

Your code initializes biggest to -12000, and therefore it will fail if all elements in the array have values lower than -12000 (unless you know something about the values in the array, but then that should be mentioned in a comment, to explain the unusual initialization value).
Sure it will compile, but that does not mean it will work correctly.

You could initialize biggest to the lowest integer value possible (INT_MIN),

int largest(int x[], int y)
{
    int count, biggest = INT_MIN; // lowest integer value possible

    for (count = 0; count < y; count++)
    {

but a smart trick is to initialize it to the first value in your array.

int largest(int x[], int y)
{
    int count, biggest = x[0]; // first value in your array

    for (count = 1; count < y; count++) // starting with 2nd element
    {

You can work this all out on a piece of paper with e.g. 3 array values, or step through your debugger and see what values the respective variables get.

Sign up to request clarification or add additional context in comments.

Comments

0

Instead of assigning the value in starting to biggest, you can compare two elements of the array and after comparing it store maximum value in biggest and after it swap the numbers if greater it would be good approach. if you use like:

if(x[count]>x[count+1])
    biggest=x[count]; 
    x[count]=x[count+1];
    x[count+1]=biggest;

code above line in loop.

What you tried assigned a very high value to biggest. It's not a worthy idea.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.