5
Name______________________________ Programming Lesson 7 - Programming Fundamentals – Arrays By now, you should know that information can be stored in a program using Variables. These are like boxes that we give a name to and the computer will put a value in. If the value never changes, then we can declare it as a constant. This is useful because we can give a piece of information a name – no mistakes will ever be made when using that value and, even better, it makes your code much easier to understand. This is good as far as it goes – but what if you want to store lots of pieces of information about the same thing? Example Make a program which asks 10 users for their names and stores them in a variable. We clearly know that a variable like this would be really useful to store a person’s name: Which is fine if we only needed to store the name of one person. But we don’t, we need to store 10! Using the method we currently know, things quickly get silly: The word “person” starts to look absurd. The situation gets worse if we wanted to store, say, 1000 names. There is a better way, right? Yes. Yes there is – Arrays. Look at this:

  · Web viewProgramming Lesson 7 - Programming Fundamentals ... The word “person” starts to look absurd. ... Program Plan: Names will be typed

Embed Size (px)

Citation preview

Page 1:   · Web viewProgramming Lesson 7 - Programming Fundamentals ... The word “person” starts to look absurd. ... Program Plan: Names will be typed

Name______________________________

Programming Lesson 7 - Programming Fundamentals – Arrays

By now, you should know that information can be stored in a program using Variables. These are like boxes that we give a name to and the computer will put a value in.

If the value never changes, then we can declare it as a constant. This is useful because we can give a piece of information a name – no mistakes will ever be made when using that value and, even better, it makes your code much easier to understand.

This is good as far as it goes – but what if you want to store lots of pieces of information about the same thing?ExampleMake a program which asks 10 users for their names and stores them in a variable.We clearly know that a variable like this would be really useful to store a person’s name:

Which is fine if we only needed to store the name of one person. But we don’t, we need to store 10!Using the method we currently know, things quickly get silly:

The word “person” starts to look absurd. The situation gets worse if we wanted to store, say, 1000 names.There is a better way, right? Yes. Yes there is – Arrays. Look at this:

That’s much better, isn’t it? This will store 10 pieces of information in an Array called “People.”To understand what’s going on here, a diagram:

Page 2:   · Web viewProgramming Lesson 7 - Programming Fundamentals ... The word “person” starts to look absurd. ... Program Plan: Names will be typed

Name______________________________

When you declare an array, instead of making one box and giving it a name, the computer makes a set of connected boxes and labels them like this:

People(0) People(1) People(2) People(3) People(4) People(5) People(6) People(7) People(8) People(9)

Notice how we count from ZERO, so we have 10 storage spaces in total.Lets fill it with data:

People(0) People(1) People(2) People(3) People(4) People(5) People(6) People(7) People(8) People(9)Dave Bob Ted John Eric Steve Jim Mike Roger Tony

Using an array is the same as a variable, but you need to include the right “array index” (the number in brackets)

Would set the name “Steve” as shown in our example.

What would this output to the label? _________________

Turn over for tasks

Page 3:   · Web viewProgramming Lesson 7 - Programming Fundamentals ... The word “person” starts to look absurd. ... Program Plan: Names will be typed

Name______________________________

Task 1 – Making an arrayMake a program which stores ten names in an array and then outputs these names to a ListboxHints:Use listboxname.Items.Add(YOURARRAY(index)) to add items to the list box

o Listboxname is obviously whatever you called your list boxo YOURARRAY(index) needs to be replaced with the name of your array and

appropriate indexHardcode your names in the array to begin withExtension:If you haven’t already, use a FOR loop to fill the list box.using array properties or the Array class, is there another way to set the bounds of the for loop?Task 2 – Filling arrays with user inputModify your program so that the array is filled by the user typing in names at run time.Hints:Use a FOR loop and YOURARRAY(index) = InputBox(“please enter a name”) to fill the array of names.You will have to use the counter in the FOR loop to change the index each time you go round the loop.

Task 3 – Searching an arrayAdd a button to your program that, when pressed, asks the user for a name to find in the list. Your program should show an appropriate message to the user to indicate whether or not that name is in the list.Hints:There is more than one way of doing this! Loops may be one way, but I bet there’s an easier way… There is something called the “array” class that you may want to investigate.Extension:Make the program tell you which index number the given name is stored in.

Task 4 – Changing elementsAdd another button and other relevant controls so that the user can enter a name to be changed and a name to change it to – i.e. Dave becomes Jane.Hints:As before, there is more than one way to do this – all methods are good practise! You can make your life easy or hard here. Think about the necessary steps to find and replace an element before you start.

Page 4:   · Web viewProgramming Lesson 7 - Programming Fundamentals ... The word “person” starts to look absurd. ... Program Plan: Names will be typed

Name______________________________

Task 5 – Sorting ArraysAdd a button that, when pressed, sorts the array of names in to alphabetical order. I highly recommend you look at what functions are available for your array. But, as usual, there are many ways to accomplish this task!

Challenge/Mini ProjectYou now know enough about arrays to make a high score table. This will be useful for any games we may create in the future.Make a new project called “High Scores” and lay out your form in a similar manner as shown below:

Program Plan:1. Names will be typed in to the text box – what error checking can you do to ensure a

name has been typed in?2. Scores will be selected from the drop down – pre-fill this in the form editor.3. Add Name will add the name and score to an array – you will need TWO arrays4. Show scores will fill the list box with names and scores.

Hints:You will need to use array.sort() This is incredibly useful but this is where things become complicated – this method has what are called “overloads” which means it can be used in more than one way. In fact, there are 17 different ways it can be used and one of them is going to be really useful here…!Now might also be time to use the breakpoint feature of Visual Studio so you can inspect your variables/arrays at run time to see what’s happening – we’ll help you with this.