Results 1 to 5 of 5
  1. #1
    Join Date
    Jul 2012

    Question C# Lost and confused in Array land

    Hello i been watching the videon on arrays for 2 days now and still cant really fully understand them feeling like i have the last piece of the puzzle i know its the last piece and what it is i know what to do with it but i just dont know where to put it.

    I know the concept i understand it.
    I know how why you have to declare it and instantiate it.
    I know what and the importance of the int index is and how its used to increment or decrement.

    YET i just dont understand how to use it in a practical application no even in the console vending machine from week 2 video

    so can some one explain it to me pls and show some examples of a practical use pls?


  2. #2
    Join Date
    Oct 2011
    Say you have 2 horses.
    String horse1 = "Haste";
    String horse2 = "Silver";
    That seems easy to write withou arrays and it is.
    Say you want to show those names to an interested user.
    Console.WriteLine("name :" + horse1);
    Console.WriteLine("name :" + horse2);
    Again, seems easy to do without array, and it is.
    Say you want to let an user give them better names.
    horse1 = Console.ReadLine();
    horse2 = Console.ReadLine();
    This is also easy to do.

    So why use arrays?
    Instead of 2 horses think of 200 horses. You would have horse1, horse2, ... , horse200. The easy factor goes away because you add a factor of difficulty (declaring them, showing them to the client, modifying based on user input).

    Declare comparison:
    The classic requires some lines of code and carefulness of typing the horse numbers correctly.

    String horse1, horse2, horse3, ... , horse2000;
    The array permits a more relaxed approach, when the number is known beforehand.
    String [] horses = new string[2000];// the horse numbers are now accessible by saying horses[num].
    This is very great but still doesn't trully showcase the power of arrays (and collections).

    Comparison between displaying:
    Console.WriteLine(horse2000); // 2000 lines of code
    Array alternative
        Console.WriteLine("name: " + Horses[index]);
    } // just 3 lines.

    If you know the concepts of arrays, play with them and move on. The more you advance in the course, the more you get to see where and how to take advantage of them ( like loops).

    Written on the phone so excuse typos and such.

  3. #3
    Join Date
    Jul 2012
    as i said i know the concept and how/why use them Nelson made that pretty clear what i dont understand is how you use arrays in a practical manner like in the vending machine homework from week 2 say like when you get shown you ran out of money instead of just saying
    Console.WriteLine("You ran out of money")
    and after this say "you bought this items" and display in order of your purchase

    so again i dont understand where and how you use arrays in practical examples

  4. #4
    Join Date
    May 2014
    Vending machines....

    to me you already got a practical use of an array
    display in order of your purchase
    To do that, you need an array. Why? because an array is basically just a List of things. So if you want something listed, like items purchased then arrays are a perfect fit.

    Other use of arrays is for "if" statements. I am not currently familiar with C#, so I will just answer in JavaScript, but the functionality should exist in C#

    var itemsBought = new array();
    var item = "Fresh Milk";
    if (item in itemsBought) {
            console.log("Yes you have bought this item");

  5. #5
    Join Date
    Jun 2012
    probably at a PC
    An array is let's say: a basket

    Before you can use that basket to hold stuff, you have to define a few things: What will you call that basket and possibly define how much content it can hold. Best practices even suggest that you specify the type of content the basket will hold.

    So you could say... the new basket is called and Apple_Basket that can hold 100 apples.
    Or let's be more practical here and say: Array_of_productnames

    You could create a rather generic array:
    var array_of_productnames = new Array();
    or, be more specific: 100 names
    var array_of_productnames = new string[100];
    the type of the array will now be of type "string" and can hold 100 entries.

    let's add a couple:
    array_of_productnames[0] = "Cola";
    array_of_productnames[1] = "Milk";
    array_of_productnames[99] = "Energy Drink";
    now you have one variable/array called "array_of_productnames" that holds up to 100 names and almost the exact same way you added a value into one of the indexed slots, you can ask the array to read what's in that slot:

    This would display "Milk" in your console.

    So... instead of preparing your vending machines menus with tons of repeated lines of "string with names of products" or "integers with prices", you could simple pre-create an array with all of them... or have the user populate the array with some input (Console.ReadLine();). Then you only need to loop through the array or call specific indexes to display the information. No need to rewrite tons of "string with names of products" or "integers with prices". It also allows you to fairly simply update the price or name of something at a specific index and it would update correctly the next time you're calling your menu.

    Hope this helps a bit. I don't really wanna give away too much, as I can't remember what lesson it is and what the homework was... but the most important point to the homework, is to practice this stuff and build an understanding for it over time. It's essential in the end.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts