• 0

help with writing code with arrays


Question

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? names, and one containing the year of creation.

Write code to traverse the arrays and output each name along with the year of its creation to the List Box, lstOutput.  While traversing the array, determine character is the oldest.

 

Then output to lstOutput, the sentence:

    "The oldest character in this group is ____________________. "

The blank should contain the name of the oldest character.         

Link to comment
Share on other sites

4 answers to this question

Recommended Posts

  • 0

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.

Link to comment
Share on other sites

  • 0

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.
Link to comment
Share on other sites

  • 0

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 ;)

Link to comment
Share on other sites

This topic is now closed to further replies.