|
-
Mar 30th, 2003, 08:24 PM
#1
Thread Starter
Hyperactive Member
Largest Num in an Array?
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...
tahnks
-Emo
-=VB6 Enterprise Edition=-
-=VC++6Enterprise Edition=-
«¤E³m°O²™¤»
-
Mar 30th, 2003, 09:31 PM
#2
Frenzied Member
int high=-99999; //
for(int i=0; i<arraylength; i++)
{
if(array[i]>high)
high=array[i];
}
-
Mar 30th, 2003, 09:32 PM
#3
Frenzied Member
as long as the highest number is above -99999
-
Mar 30th, 2003, 09:46 PM
#4
Thread Starter
Hyperactive Member
heheh thanks a lot... ( and I hope the largest isn't below -99999 )
-Emo
-=VB6 Enterprise Edition=-
-=VC++6Enterprise Edition=-
«¤E³m°O²™¤»
-
Mar 30th, 2003, 09:58 PM
#5
Thread Starter
Hyperactive Member
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
-=VB6 Enterprise Edition=-
-=VC++6Enterprise Edition=-
«¤E³m°O²™¤»
-
Mar 30th, 2003, 10:18 PM
#6
Frenzied Member
i never did well with that chapter....I just played around with adding and subtracting the & until it worked don't think im much help there...sorry
-
Mar 30th, 2003, 10:19 PM
#7
Thread Starter
Hyperactive Member
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
-Emo
-=VB6 Enterprise Edition=-
-=VC++6Enterprise Edition=-
«¤E³m°O²™¤»
-
Mar 31st, 2003, 03:53 AM
#8
Lively Member
find_two_largest(int Array[], int &An, int *Largest, int *sLargest); ?
-
Mar 31st, 2003, 10:16 AM
#9
Thread Starter
Hyperactive Member
Originally posted by Randy_Bartels
find_two_largest(int Array[], int &An, int *Largest, int *sLargest); ?
has no difference
thanks though
-Emo
-=VB6 Enterprise Edition=-
-=VC++6Enterprise Edition=-
«¤E³m°O²™¤»
-
Mar 31st, 2003, 12:28 PM
#10
Originally posted by SteveCRM
int high=-99999; //
for(int i=0; i<arraylength; i++)
{
if(array[i]>high)
high=array[i];
}
To ensure that you don't accidently have a completly wrong value you simply take the first array element:
Code:
int high=array[0]; //
for(int i=1; i<arraylength; i++)
{
if(array[i]>high)
high=array[i];
}
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Mar 31st, 2003, 03:53 PM
#11
Frenzied Member
-
Mar 31st, 2003, 04:01 PM
#12
Frenzied Member
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.
Code:
find_two_largest(Array[5], &An, *Largest, *sLargest);
The addressof operator & returns the address of the variable you're passing.
What's the prototype of the function?
Jop - validweb.nl
Alcohol doesn't solve any problems, but then again, neither does milk.
-
Apr 1st, 2003, 08:10 PM
#13
Fanatic Member
I thought integers only help up to 32,767?
-
Apr 1st, 2003, 09:13 PM
#14
Hyperactive Member
Originally posted by dsheller
I thought integers only help up to 32,767?
http://www.vbforums.com/showthread.p...05#post1378205
-
Apr 2nd, 2003, 02:08 AM
#15
Thread Starter
Hyperactive Member
Originally posted by Jop
What's the prototype of the function?
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 all
and thanks for clearning why I'm getting that error
-Emo
-=VB6 Enterprise Edition=-
-=VC++6Enterprise Edition=-
«¤E³m°O²™¤»
-
Apr 2nd, 2003, 06:37 AM
#16
Frenzied Member
Jop - validweb.nl
Alcohol doesn't solve any problems, but then again, neither does milk.
-
Apr 2nd, 2003, 04:13 PM
#17
Thread Starter
Hyperactive Member
Originally posted by Jop
Ok it works now?
well it fixed my original error but it went on and instead of giving me
'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...
Code:
find_two_largest(Array[5], An, &*Largest, &*sLargest);
this gave me No errors but when I tried to build my app it gave me link errors saying:
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.
so...I'm totaly lost now...
-Emo
-=VB6 Enterprise Edition=-
-=VC++6Enterprise Edition=-
«¤E³m°O²™¤»
-
Apr 2nd, 2003, 06:38 PM
#18
Frenzied Member
Code:
find_two_largest(Array[5], An, &*Largest, &*sLargest);
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).
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?
Jop - validweb.nl
Alcohol doesn't solve any problems, but then again, neither does milk.
-
Apr 2nd, 2003, 07:02 PM
#19
Monday Morning Lunatic
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];
}
}
}
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 ).
Then to use this:
Code:
int array[4] = { 1, 5, 2, 8 };
int maxa, maxb;
v2max(array, 4, &maxa, &maxb);
printf("Maxes: %d, %d\n", maxa, maxb);
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.
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
Apr 3rd, 2003, 05:39 PM
#20
Thread Starter
Hyperactive Member
Thanks for everything guys! I got it working!
-Emo
-=VB6 Enterprise Edition=-
-=VC++6Enterprise Edition=-
«¤E³m°O²™¤»
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|