Arrays

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

Post Reply
polyneem
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 11
Joined: Tue May 11, 2010 7:23 pm

Arrays

Post 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?
User avatar
Ginto8
ES Beta Backer
ES Beta Backer
Posts: 1064
Joined: Tue Jan 06, 2009 4:12 pm
Programming Language of Choice: C/C++, Java

Re: Arrays

Post 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. ;)
Quit procrastinating and make something awesome.
Ducky wrote:Give a man some wood, he'll be warm for the night. Put him on fire and he'll be warm for the rest of his life.
User avatar
xiphirx
Chaos Rift Junior
Chaos Rift Junior
Posts: 324
Joined: Mon Mar 22, 2010 3:15 pm
Current Project: ******** (Unkown for the time being)
Favorite Gaming Platforms: PC
Programming Language of Choice: C++
Contact:

Re: Arrays

Post by xiphirx »

If you're hardcore, and want a faster way, do it tree-like ;)
StarCraft II Zerg Strategy, open to all levels of players!

Looking for paid work :< Contact me if you are interested in creating a website, need a web design, or anything else you think I'm capable of :)
User avatar
dandymcgee
ES Beta Backer
ES Beta Backer
Posts: 4709
Joined: Tue Apr 29, 2008 3:24 pm
Current Project: https://github.com/dbechrd/RicoTech
Favorite Gaming Platforms: NES, Sega Genesis, PS2, PC
Programming Language of Choice: C
Location: San Francisco
Contact:

Re: Arrays

Post 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.
Falco Girgis wrote:It is imperative that I can broadcast my narcissistic commit strings to the Twitter! Tweet Tweet, bitches! :twisted:
User avatar
zeid
Chaos Rift Junior
Chaos Rift Junior
Posts: 201
Joined: Fri Apr 24, 2009 11:58 pm

Re: Arrays

Post 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).
Axolotl Pop!
Image
Or, try it for free.

For many more free games online visit www.sam-zeid.com
MacJordan
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 4
Joined: Fri Oct 22, 2010 4:13 am

Re: Arrays

Post 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();
User avatar
Ginto8
ES Beta Backer
ES Beta Backer
Posts: 1064
Joined: Tue Jan 06, 2009 4:12 pm
Programming Language of Choice: C/C++, Java

Re: Arrays

Post 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.
Quit procrastinating and make something awesome.
Ducky wrote:Give a man some wood, he'll be warm for the night. Put him on fire and he'll be warm for the rest of his life.
User avatar
adikid89
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 94
Joined: Tue Apr 27, 2010 6:59 am
Current Project: small tiny-mini projects
Favorite Gaming Platforms: PC I guess...
Programming Language of Choice: c++

Re: Arrays

Post 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".
My first game C++/SDL Yoshi Combat! = http://www.youtube.com/watch?v=HQ9mMBEWSZg
==============================================================
Image
User avatar
dandymcgee
ES Beta Backer
ES Beta Backer
Posts: 4709
Joined: Tue Apr 29, 2008 3:24 pm
Current Project: https://github.com/dbechrd/RicoTech
Favorite Gaming Platforms: NES, Sega Genesis, PS2, PC
Programming Language of Choice: C
Location: San Francisco
Contact:

Re: Arrays

Post 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:
Falco Girgis wrote:It is imperative that I can broadcast my narcissistic commit strings to the Twitter! Tweet Tweet, bitches! :twisted:
Post Reply