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...
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;
}
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.
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.
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: