Page 1 of 1

Arrays

Posted: Tue May 11, 2010 7:29 pm
by polyneem
what would the best way to sort through an array to find the largest number?

Code: Select all

int bottle[9] {1,3,2,5,6,2,3,5,3}; //here bottle[4] would be the largest
I was thinking about making a for loop, and setting 9 different variables to each number,
and changing the variables each time it goes through the loop, is there a better way to do this?

Re: Arrays

Posted: Tue May 11, 2010 7:58 pm
by Ginto8

Code: Select all

int max(int size,int array[]) {
    int ret = 0;
    for(int i=0;i<size;++i) {
        if(array[i] > array[ret]) {
            ret = i;
        }
    }
}
max(9,bottle) will return the index of the highest number in bottle. You should be able to figure out how it works pretty easily. ;)

Re: Arrays

Posted: Tue May 11, 2010 11:47 pm
by xiphirx
If you're hardcore, and want a faster way, do it tree-like ;)

Re: Arrays

Posted: Wed May 12, 2010 12:18 am
by dandymcgee
xiphirx wrote:If you're hardcore, and want a faster way, do it tree-like ;)
?? The only "tree" I know of is a binary search tree. Binary search would be great, if it were a sorted list.

Re: Arrays

Posted: Wed May 12, 2010 1:49 am
by zeid
If you are using an unsorted array;

Code: Select all

int largestNumber=0;
for(int i=0; i < 9; i++)
{
   if(bottle[i] > largestNumber)
      largestNumber = bottle[i];
}
There are quicker ways of finding particular values, however you would have to sort the information in some way first.

If you are at the stage where you are having trouble working out how to get the highest value from an array of integers you are not yet ready for more advanced data structures (just in case after reading this you go look into them polyneem, don't be disheartened).

Re: Arrays

Posted: Fri Oct 22, 2010 4:44 am
by MacJordan
polyneem wrote:what would the best way to sort through an array to find the largest number?

Code: Select all

int bottle[9] {1,3,2,5,6,2,3,5,3}; //here bottle[4] would be the largest
I was thinking about making a for loop, and setting 9 different variables to each number,
and changing the variables each time it goes through the loop, is there a better way to do this?
you can use the bubble sort method to find out the maximum number fro array. i

this method is very simple and easy to use.

in this method element element is compare to next element of array.

consider part of code,

int MAX = 0;

for(i= 0 ;i<=9;i++)
{
if (bottle >MAX)
MAX = bottle;
}

printf(Largest Number : %d",MAX);

getch();

Re: Arrays

Posted: Fri Oct 22, 2010 8:02 am
by Ginto8
MacJordan wrote:
polyneem wrote:what would the best way to sort through an array to find the largest number?

Code: Select all

int bottle[9] {1,3,2,5,6,2,3,5,3}; //here bottle[4] would be the largest
I was thinking about making a for loop, and setting 9 different variables to each number,
and changing the variables each time it goes through the loop, is there a better way to do this?
you can use the bubble sort method to find out the maximum number fro array. i

this method is very simple and easy to use.

in this method element element is compare to next element of array.

consider part of code,

int MAX = 0;

for(i= 0 ;i<=9;i++)
{
if (bottle >MAX)
MAX = bottle;
}

printf(Largest Number : %d",MAX);

getch();

congratulations on 2 things. First, you have managed to reiterate what was said at least once before (just in a slightly different way), and you also have been able to necro a 5-month old thread. Though 5 months isn't that bad, it's still a necro.

Re: Arrays

Posted: Fri Oct 22, 2010 8:46 am
by adikid89
Ginto8 wrote:congratulations on 2 things. First, you have managed to reiterate what was said at least once before (just in a slightly different way), and you also have been able to necro a 5-month old thread. Though 5 months isn't that bad, it's still a necro.

:lol: Spot on! ... also
MacJordan wrote: you can use the bubble sort method to find out the maximum number fro array.

That's not bubble sort, that's just a basic linear search. And by method I guess you mean "algorithm".

Re: Arrays

Posted: Fri Oct 22, 2010 12:42 pm
by dandymcgee
adikid89 wrote:
Ginto8 wrote:congratulations on 2 things. First, you have managed to reiterate what was said at least once before (just in a slightly different way), and you also have been able to necro a 5-month old thread. Though 5 months isn't that bad, it's still a necro.

:lol: Spot on! ... also
MacJordan wrote: you can use the bubble sort method to find out the maximum number fro array.

That's not bubble sort, that's just a basic linear search. And by method I guess you mean "algorithm".

Omg, did he really did just say that? Any post that is a 5 month necro and has the phrase "bubble sort" in it should warrant a punch in the face for ignorance. :nono: