function returns a pointer to the largest number in an array

Hi

I have a question about how can I write a function that returns a pointer to the largest number in an array of size 10.

the array is one dimention array.

I wrote a code but it didn't work!!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
int* largest_number(int *largest_array, int size)
{
    
    int maximum=largest_array[0];

    for(int i=0; i<size; i++)
    {
         maximum=random_array[i];
         *largest_array=maximum;
            
    }
    
    return largest_array;
}


help me please
http://cplusplus.com/reference/algorithm/max_element/ :P

No, in all seriousness, if you need/want to write your own, then I'm just going to comment that I don't see any comparisons, nor do I see the declaration of random_array...

-Albatross
this is the code but I don't know where is the problem

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
int* find_largest_number(int *random_array, int size)
{
    //set the value of maximum to the first element in the array
    int maximum=random_array[0];

    for(int i=0; i<size; i++)
    {
        if(random_array[i]>maximum);
        {
            maximum=random_array[i];
            *random_array=maximum;
        }
            
    }
    
    return random_array;
}
Last edited on
closed account (D80DSL3A)
Your code will always return a pointer to the first element of random_array.
Also, you probably don't want to overwrite values in random_array, as line 11 is doing.

Something like this may work better:
1
2
3
4
5
6
7
8
9
10
11
int* find_largest_number(int *random_array, int size)
{   
    // you want to return a pointer, so use a pointer
    int* pMax=random_array;// point to 1st array element initially

    for(int i=0; i<size; i++)    
        if(random_array[i]> *pMax)// comparing values here  
            // CODE here: assign pMax to point to the element with the larger value
    
    return pMax;// returning a pointer to the element with the largest value
}


Sorry, since this is probably a homework question I can't give the whole thing away, but that should get you closer.

EDIT: Sorry! My goof on the extra semicolon on line 7. I have fixed it now.
Last edited on
I just want to point out that passing in a pointer to the variable you are trying to modify AND returning that same variable is redundent. When you pass to a function by reference like that you are passing the actual address of the variable so it is modified by the function using it.

Your "if" on line 8 of your segment here only tests each element of the array against the first. You need a second "for" loop to change "maximum" so that each element gets tested against each other element. This is often called "Embedded For Loops" if that helps.
I wrote the function again, but I still get wrong output
this code displays the last element and the address of the first one !!

so, could you tell me where is the problem?

1
2
3
4
5
6
7
8
9
10
11
int* find_largest_number(int *random_array, int size)
{
    int *maximum=random_array;

    for(int i=0; i<size; i++)
    {
        if(random_array[i] > *maximum);
            *maximum=random_array[i];
    }
    return maximum;
}
First, there's one too many a semicolon here:

if(random_array[i] > *maximum);

Second, you're changing the value of the first element in the array instead of having maximum point to the largest number. You need to change line 8 to this:

maximum = &random_array[i];
Last edited on
finaaaally it works

Thanks to all of you :)
Topic archived. No new replies allowed.