• 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
https://www.neowin.net/forum/topic/1172793-help-with-writing-code-with-arrays/
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.

  • 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.
  • 0
  On 23/10/2013 at 06:37, bikithalee said:

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

This topic is now closed to further replies.
  • Posts

    • I talked about OnlyOffice. And stop insulting people.
    • I’m hugely disappointed by how far this site has drifted from what its community actually came here for. An occasional speculative piece is fine, but for few months large number of the feed feels like the click-bait “future hype” (not even related to tech) that used to clutter the click bait sites. Guess it's time to start looking for new venue, if its keep going down this path...
    • They probably mean File Pilot, which this colorful rainbow program can never compete with, because this WinUI crap isn’t made for apps like that. My advice? You should just switch the framework already
    • Scientists uncover bizarre new material that's breaking one of the laws of this Universe by Sayan Sen Image by Ron Lach via Pexels Scientists from the University of Chicago and UC San Diego have discovered a group of materials that behave in surprising ways when put under heat, pressure or electricity. Instead of responding like most materials, these can shrink when heated, expand when compressed, and even bounce back to their original state with the right electric charge. The work focuses on oxygen-redox (OR) materials—types that can help batteries store more energy but typically suffer from stability problems due to structural disorder. In their normal state, the materials follow the usual rules of thermodynamics. But in what's called a “metastable” state, a kind of temporary balance, they behave in reverse. “When heated, the material shrinks instead of expanding,” said Prof. Shirley Meng, senior author of the study published in Nature. This is linked to what’s known as a disorder–order transition inside the material’s structure. The team recorded a negative thermal expansion rate of −14.4(2) × 10⁻⁶ °C⁻¹, which means the material actually contracts when warmed up. This goes against a common theory called the Grüneisen relationship, which usually explains why materials expand with heat. And pressure? Even stranger. When they pushed the material on all sides at levels seen in Earth's tectonic plates, it expanded instead of getting smaller. “Negative compressibility is just like negative thermal expansion,” explained Prof. Minghao Zhang. “If you compress a particle of the material in every direction… it will expand.” They also found that electricity can reset the material’s structure. By tweaking the voltage limits, they recovered almost 100% of the original structure and performance. This has big potential for battery tech, especially electric vehicles (EVs). “When we use the voltage, we drive the material back to its pristine state. We recover the battery,” said Zhang. He added: “You just do this voltage activation… your car will be a new car. Your battery will be a new battery.” The research may lead to materials with zero thermal expansion, helpful in everything from buildings to aircraft. Zhang noted, “Take every single building, for example. You don't want the materials making up different components to change volume that often.” As they move forward, the team wants to understand how redox chemistry can further control these effects and expand practical uses. “One of the goals is bringing these materials from research to industry,” said co-first author Bao Qiu. Their work opens up a new way of thinking about material design, where energy doesn’t just power devices, but reshapes the building blocks themselves. Source: University of Chicago, Nature This article was generated with some help from AI and reviewed by an editor. Under Section 107 of the Copyright Act 1976, this material is used for the purpose of news reporting. Fair use is a use permitted by copyright statute that might otherwise be infringing.
    • "Elon Musk says a kid-friendly 'Baby Grok' is on the way" No kid wants to be a baby anymore. "Kid friendly" I would have named it "Kid Grok"
  • Recent Achievements

    • Rookie
      Snake Doc went up a rank
      Rookie
    • First Post
      nobody9 earned a badge
      First Post
    • One Month Later
      Ricky Chan earned a badge
      One Month Later
    • First Post
      leoniDAM earned a badge
      First Post
    • Reacting Well
      Ian_ earned a badge
      Reacting Well
  • Popular Contributors

    1. 1
      +primortal
      495
    2. 2
      Michael Scrip
      203
    3. 3
      ATLien_0
      197
    4. 4
      Xenon
      137
    5. 5
      +FloatingFatMan
      116
  • Tell a friend

    Love Neowin? Tell a friend!