Hamoud
Hamoud

Reputation: 23

I'm creating a program that take five input from user and display the largest by using another function

I created the program that take an input from a user using an array of type int,, and the i want to use a user define function to display the largest element in the array, The problem is that i passed the array through the function but it didn't show me the largest element

#include <stdio.h>
#define size 5
void get_max(int list[size]);
int main()
{
    int list[size];
    printf("Enter five numbers : ");
    int i;
    for (i=0;i<5;i++)
    {
        scanf("%d",&list[i]);
    }
    printf("\n");
    for(i=0;i<5;i++)
    {
        printf("Element in array list[%d] = %d\n",i,list[i]);
    }
    get_max(list[size]);
}

void get_max(int list[size])
{
    int large,i;
    for (i=0;i<5;i++)
    {
        if(large < list[size])
        {
            large=list[size];
        }

    }
    printf("\nLargest element in array list  %d",large);
}


I want to print the largest value in the elements after getting the input from the user,,,,

thanks

Upvotes: 0

Views: 374

Answers (3)

j23
j23

Reputation: 3530

Mainly there are three errors in your code.

  1. You are calling the function with get_max(list[size]); instead of get_max(list);. When you give list[size] as argument, it points to the 6th element (list[5] array index start from 0 - size-1). So two errors are there itself. There is no 6th element and array will go out of bounds which can cause undefined behaviour. Also, even if you meant list[4] then there is a logical error i.e you are passing the address of the last element of the array. So it will definitely go out of bounds and cause undefined behaviour inside your function. get_max(list); is the correct way to do it.

  2. You have used large=list[size] inside get_max() function which will create undefined behaviour as explained in first point.(array index starts from 0 i.e 0-(size-1) is the valid accesses).
    eg: if(large < list[size]) has an undefined behaviour. It should be list[i] in the program. You have to iterate through all array elements to find the largest number.

  3. Variable large is undefined. You should set it before the if(large < list[size]) otherwise it can cause undefined behaviour in your application. So set large=list[0].

One additional hint is when you are trying to find the largest (or smallest) number you should set large=list[0] (or any element in the input array) as in below code. Otherwise if you set large=0 initially and if all the input numbers are negative, then the program will output wrong result.

Fixing the above errors will make your code run. See below:

#include <stdio.h>
#define size 5
void get_max(int list[size]);
int main()
{
    int list[size];
    printf("Enter five numbers : ");
    int i;
    for (i=0;i<5;i++)
    {
        scanf("%d",&list[i]);
    }
    printf("\n");
    for(i=0;i<5;i++)
    {
        printf("Element in array list[%d] = %d\n",i,list[i]);
    }
    get_max(list);
}

void get_max(int list[size])
{
    int large,i;
    large=list[0];
    for (i=1;i<5;i++)
    {
        if(large < list[i])
        {
            large=list[i];
        }

    }
    printf("\nLargest element in array list  %d",large);
}

Upvotes: 1

Adrian George
Adrian George

Reputation: 1

When you call get_max... As a parameter you have to put only list, not list[size]... An

Upvotes: 0

kiran Biradar
kiran Biradar

Reputation: 12732

  1. large is uninitialized and will have indeterminate value which will effect your caclucation.

  2. Accessing list[size] is undefined behavior, probably you meant list[i].

    void get_max(int list[size])
    {
        int large = 0,i = 0;
        for (i=0;i<5;i++)
        {
            if(large < list[i])
            {
                large=list[i];
            }

        }
        printf("\nLargest element in array list  %d",large);
    }

Upvotes: 0

Related Questions