Results 1 to 20 of 20

Thread: Largest Num in an Array?

  1. #1

    Thread Starter
    Hyperactive Member Emo's Avatar
    Join Date
    Jul 2000
    Posts
    428

    Arrow 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²™¤»

  2. #2
    Frenzied Member
    Join Date
    Jul 1999
    Posts
    1,800
    int high=-99999; //

    for(int i=0; i<arraylength; i++)
    {
    if(array[i]>high)
    high=array[i];
    }



  3. #3
    Frenzied Member
    Join Date
    Jul 1999
    Posts
    1,800
    as long as the highest number is above -99999

  4. #4

    Thread Starter
    Hyperactive Member Emo's Avatar
    Join Date
    Jul 2000
    Posts
    428
    heheh thanks a lot... ( and I hope the largest isn't below -99999 )

    -Emo
    -=VB6 Enterprise Edition=-
    -=VC++6Enterprise Edition=-
    «¤E³m°O²™¤»

  5. #5

    Thread Starter
    Hyperactive Member Emo's Avatar
    Join Date
    Jul 2000
    Posts
    428
    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²™¤»

  6. #6
    Frenzied Member
    Join Date
    Jul 1999
    Posts
    1,800
    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

  7. #7

    Thread Starter
    Hyperactive Member Emo's Avatar
    Join Date
    Jul 2000
    Posts
    428
    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²™¤»

  8. #8
    Lively Member
    Join Date
    Mar 2001
    Location
    Massachusetts, USA
    Posts
    111
    find_two_largest(int Array[], int &An, int *Largest, int *sLargest); ?

  9. #9

    Thread Starter
    Hyperactive Member Emo's Avatar
    Join Date
    Jul 2000
    Posts
    428
    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²™¤»

  10. #10
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    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.

  11. #11
    Frenzied Member
    Join Date
    Jul 1999
    Posts
    1,800
    good call...

  12. #12
    Frenzied Member Jop's Avatar
    Join Date
    Mar 2000
    Location
    Amsterdam, the Netherlands
    Posts
    1,986
    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.

  13. #13
    Fanatic Member
    Join Date
    Dec 2002
    Location
    North Carolina
    Posts
    734
    I thought integers only help up to 32,767?

  14. #14
    Hyperactive Member
    Join Date
    Sep 2001
    Posts
    396
    Originally posted by dsheller
    I thought integers only help up to 32,767?
    http://www.vbforums.com/showthread.p...05#post1378205

  15. #15

    Thread Starter
    Hyperactive Member Emo's Avatar
    Join Date
    Jul 2000
    Posts
    428
    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²™¤»

  16. #16
    Frenzied Member Jop's Avatar
    Join Date
    Mar 2000
    Location
    Amsterdam, the Netherlands
    Posts
    1,986
    Ok it works now?
    Jop - validweb.nl

    Alcohol doesn't solve any problems, but then again, neither does milk.

  17. #17

    Thread Starter
    Hyperactive Member Emo's Avatar
    Join Date
    Jul 2000
    Posts
    428
    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²™¤»

  18. #18
    Frenzied Member Jop's Avatar
    Join Date
    Mar 2000
    Location
    Amsterdam, the Netherlands
    Posts
    1,986
    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.

  19. #19
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    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

  20. #20

    Thread Starter
    Hyperactive Member Emo's Avatar
    Join Date
    Jul 2000
    Posts
    428
    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
  •  



Click Here to Expand Forum to Full Width