Sign in to follow this  
Followers 0

help with writing code with arrays


5 posts in this topic

Posted

Given the following data:

Cartoon Character              Year of Creation

Bugs Bunny                                   1938

Homer Simpson                             1987

Fred Flintstone                              1960

Charlie Brown                                1950

Mickey Mouse                               1928

 

Write a program to:

 

Define two parallel arrays, one containing the Characters

Share this post


Link to post
Share on other sites

Posted

Set a variable to keep track of the oldest character, initialize it to 99999 or something.

 

Inside the loop, if the year of creation for that character is less than the oldestCharacter, update oldestCharacter to that character's year of creation and save the character's name to a separate variable, output it at the end.

Share this post


Link to post
Share on other sites

Posted

Here's a pseudo-code of how it might be done.

 

# Define 2 Arrays
Names are [bugs, homer, fred]
Years are [1938, 1987, 1960]

OldestName is Unknown
OldestYear is 99999

# Traverse the array while printing & determining oldest characters
With index from FirstIndex(Names) to LastIndex(Names)

    AddToList(Names[index], Years[index])

    If Years[index] less than OldestYear
        OldestYear is Years[index]
        OldestName is Names[index]

# Print final message
AddToList("The oldest character is", OldestName)
Now it's up to you to implement it in any language you like.
1 person likes this

Share this post


Link to post
Share on other sites

Posted

It really looks like someone doesn't want to do his homework...

1 person likes this

Share this post


Link to post
Share on other sites

Posted

If you have two coloumn data , better you use with other datastructure like hashtable. In hashtable you can input values like key and value and easy to retrieve . following link shows the hashtable implementation in c# http://csharp.net-informations.com/collection/csharp-hashtable.htm c# hashtable , hope this will help you.

 

Lee.

Very true, but the way that the question is written, I suspect that this is homework on arrays. In the real world as you correctly say, an associative container would be better :)

P.S. Welcome to Neowin!

P.P.S. Please don't resurrect two month old topics ;)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0

  • Recently Browsing   0 members

    No registered users viewing this page.