Hello...
how would I find the largest number in an arry? I know it's some kind of a loop but I can't get it right...:rolleyes:
tahnks
-Emo
Printable View
Hello...
how would I find the largest number in an arry? I know it's some kind of a loop but I can't get it right...:rolleyes:
tahnks
-Emo
int high=-99999; //
for(int i=0; i<arraylength; i++)
{
if(array[i]>high)
high=array[i];
}
:cool:
as long as the highest number is above -99999
:p
heheh thanks a lot... ( and I hope the largest isn't below -99999 :D )
-Emo
I dont want to start a new thread so I just want to ask a quick question
I am playing with Pointers and I get an error saying:
'find_two_largest' : cannot convert parameter 1 from 'int *' to 'int'
the line that I have the error says:
find_two_largest(&Array[5], &An, *Largest, *sLargest);
and I can't find out what I'm doing wrong
thanks
-Emo
i never did well with that chapter....I just played around with adding and subtracting the & until it worked :rolleyes: don't think im much help there...sorry :)
hehe, thanks anyway...
I know the guy next to me had the same problem and the professor fixed it but I didnt see what he did :rolleyes:
-Emo
find_two_largest(int Array[], int &An, int *Largest, int *sLargest); ?
has no difference :(Quote:
Originally posted by Randy_Bartels
find_two_largest(int Array[], int &An, int *Largest, int *sLargest); ?
thanks though
-Emo
To ensure that you don't accidently have a completly wrong value you simply take the first array element:Quote:
Originally posted by SteveCRM
int high=-99999; //
for(int i=0; i<arraylength; i++)
{
if(array[i]>high)
high=array[i];
}
:cool:
Code:int high=array[0]; //
for(int i=1; i<arraylength; i++)
{
if(array[i]>high)
high=array[i];
}
good call...
Quote:
Originally posted by Emo
I dont want to start a new thread so I just want to ask a quick question
I am playing with Pointers and I get an error saying:
'find_two_largest' : cannot convert parameter 1 from 'int *' to 'int'
the line that I have the error says:
find_two_largest(&Array[5], &An, *Largest, *sLargest);
and I can't find out what I'm doing wrong
thanks
-Emo
You're passing a pointer while it expects an int.
The addressof operator & returns the address of the variable you're passing.Code:find_two_largest(Array[5], &An, *Largest, *sLargest);
What's the prototype of the function?
I thought integers only help up to 32,767?
http://www.vbforums.com/showthread.p...05#post1378205Quote:
Originally posted by dsheller
I thought integers only help up to 32,767?
well It's a function that reads the array (of 5 numbers) and then it returns the Largest number and the second largest number of those 5... that's allQuote:
Originally posted by Jop
What's the prototype of the function?
and thanks for clearning why I'm getting that error
-Emo
Ok it works now?
well it fixed my original error but it went on and instead of giving meQuote:
Originally posted by Jop
Ok it works now?
'find_two_largest' : cannot convert parameter 1 from 'int *' to 'int'
it was saying
'find_two_largest' : cannot convert parameter 2 from 'int *' to 'int'
and then a parameter 3... until I did this...
this gave me No errors but when I tried to build my app it gave me link errors saying:Code:find_two_largest(Array[5], An, &*Largest, &*sLargest);
so...I'm totaly lost now...Quote:
Linking...
App.obj : error LNK2001: unresolved external symbol "void __cdecl find_two_largest(int,int,int *,int *)" (?find_two_largest@@YAXHHPAH0@Z)
Debug/App.exe : fatal error LNK1120: 1 unresolved externals
Error executing link.exe.
-Emo
that doesn't look good.. you have a pointer, you then dereference it (get the value of the mem to which the pointer points) and then you take the address of that memory address (which is actually just the same as a pointer).Code:find_two_largest(Array[5], An, &*Largest, &*sLargest);
Also, I don't know what type of variable An is, but you're now passing it's passing a address (is An an array? then it's pointing to the memory address of the first item in the array). So you may need to dereference it. What kinda variable is An?
I don't know why this thread's gone on *quite* this long, but judging from the various replies, the code above should be sufficient (*nod* to CornedBee, I'd never thought to use the first array element, instead choosing INT_MAX or something equally horrible :)).Code:void v2max(const int *arr, size_t count, int *first, int *second) {
int i = 0, curmax = arr[0], curmaxb = curmax;
for(; i < count; ++i) {
if(arr[i] > curmax) {
curmaxb = curmax;
curmax = arr[i];
}
}
}
Then to use this:I was sticking to C here, feel free to replace any bits you don't like :) There could be some issues with very small arrays (two or less elements) giving dodgy results, but for something typed into a very small reply box it should be ok.Code:int array[4] = { 1, 5, 2, 8 };
int maxa, maxb;
v2max(array, 4, &maxa, &maxb);
printf("Maxes: %d, %d\n", maxa, maxb);
Thanks for everything guys! I got it working!
-Emo