locked
vb homework question - help understanding loops RRS feed

  • Question

  • Consider an array named students of size 10 is declared.  This array holds the last name of all 10 students in the class.  It would then be possible to loop through this array and print out all the last names.  Why would this not be possible if using separate variable names (student1, student2, student3...etc.) in place of the array?  Explain
    • Edited by Reed KimbleMVP Sunday, July 8, 2018 12:25 PM improved title
    Saturday, July 7, 2018 3:52 PM

All replies

  • Most would use a collection a List(Of  T) with the collection using a custom type, a class, like a DTO.

    https://www.codeguru.com/vb/gen/vb_misc/oop/article.php/c7063/Data-Transfer-Object-Pattern-Goes-VBNET.htm

    The custom type can be using auto propertie that has its own private backing variable, that is exposed through its public property.

    https://docs.microsoft.com/en-us/dotnet/visual-basic/programming-guide/language-features/procedures/auto-implemented-properties

    But on the other hand, you don't have to use a custom type, a class, either. You can using primitive type string, int, double, and the others primitive types too in a List(of T).

    https://docs.microsoft.com/en-us/dotnet/visual-basic/programming-guide/concepts/collections

    <copied>

    For the type of elements in the List<T>, you can also define your own class. In the following example, the Galaxy class that is used by the List<T> is defined in the

    <end>

    The Galaxy is that custom type, that DTO, an object using public properties.

    https://www.dotnetperls.com/property-vbnet

    What you are being taught is one of the pillars of object oriented programming.

    https://standardofnorms.wordpress.com/2012/09/02/4-pillars-of-object-oriented-programming/

    https://en.wikipedia.org/wiki/Encapsulation_(computer_programming)

    <copied>

    Encapsulation is one of the fundamentals of OOP (object-oriented programming). It refers to the bundling of data with the methods that operate on that data.<sup class="reference" id="cite_ref-Rogers01_5-1" style="background-color:transparent;color:#222222;font-family:sans-serif;font-size:9.26px;font-style:normal;font-variant:normal;font-weight:400;letter-spacing:normal;line-height:9.26px;orphans:2;text-align:left;text-decoration:none;text-indent:0px;text-transform:none;-webkit-text-stroke-width:0px;white-space:nowrap;word-spacing:0px;">[5]</sup> Encapsulation is used to hide the values or state of a structured data object inside a class, preventing unauthorized parties' direct access to them. Publicly accessible methods are generally provided in the class (so-called getters and setters) to access the values, and other client classes call these methods to retrieve and modify the values within the object.

    <end>

    Capiche?

    Saturday, July 7, 2018 4:13 PM
  • This is a class assignment. You posted the question as-is from your instructor.

    This question is easy to solve if you simply understand it. Tell us what part you do not understand. Provide specific questions about it. Try asking the question using your own words.

    The best answer to this question is one that helps you understand well enough that the answer is clear to you.



    Sam Hobbs
    SimpleSamples.Info

    Saturday, July 7, 2018 6:49 PM
  • I just don't understand why it would not be possible using separate variables
    Saturday, July 7, 2018 9:21 PM
  • What is "it"? What are you referring to when you say "it"? If you think "it" is possible then give an example of doing it and then if that is not possible then we will explain why it is not possible. I don't know what it is that you don't understand.


    Sam Hobbs
    SimpleSamples.Info

    Saturday, July 7, 2018 9:59 PM
  • Also, the instructor's question is not clear. The question says "this not be possible" but it is not totally clear what "this" is. I assume "this" refers to "loop" but it would help if the question was explicit about that. I assume that understanding the question depends upon having heard what the instructor said in class.


    Sam Hobbs
    SimpleSamples.Info

    Saturday, July 7, 2018 10:03 PM
  • Why is it not possible to loop through and print out all of the students last names using separate variables, but you can loop through and print out all the students last names in an array? 
    Saturday, July 7, 2018 11:04 PM
  • What is "it"? What are you referring to when you say "it"? If you think "it" is possible then give an example of doing it and then if that is not possible then we will explain why it is not possible. I don't know what it is that you don't understand.


    Sam Hobbs
    SimpleSamples.Info

    I agree with Sam. One must consider the spirit of the question and then propose one's answer. 

    The question asks you to prove why. You need to come up with something like it or not.

    I suppose you could argue why the question hypothesis is wrong if it was. I mean you "could" do the printing code without a loop. But, that is not what the question is asking. The question asks you to explain why a loop is better?

    Saturday, July 7, 2018 11:42 PM
  • In a declared array name students of size 10, why can't I loop through and print using separate variables?
    Saturday, July 7, 2018 11:56 PM
  • In a declared array name students of size 10, why can't I loop through and print using separate variables?

    Show me your code and then I will decide. I am not sure what you are getting at. Show me yours and then I will show you mine. :)

    Sunday, July 8, 2018 12:07 AM
  • They is no code...only a question.

    Sunday, July 8, 2018 12:12 AM
  • They is no code...only a question.

    Hi

    So,you come to this forum only to get some form of cut and paste answer for your school work. Just how lazy and/or stupid are you?

    Well, I for one will definitely not help you.


    Regards Les, Livingston, Scotland

    Sunday, July 8, 2018 12:20 AM
  • They is no code...only a question.

    "why can't I loop through and print using separate variables?"

    Because using separate variables is not a loop.

    Define separate variables. Use code to show me.

    Sunday, July 8, 2018 12:22 AM
  • Why is it not possible to loop through and print out all of the students last names using separate variables, but you can loop through and print out all the students last names in an array? 

    That is the question, using your own words. The answer is simple to understand but you need to think about it. Programming might be difficult if you don't understand but will be much easier when you understand. You say "student1, student2, student3"; how would you use those in a loop? Think about it. How do you write a loop that prints each one? Try to write the code that does that.



    Sam Hobbs
    SimpleSamples.Info

    Sunday, July 8, 2018 12:46 AM
  • I just don't understand why it would not be possible using separate variables

    Oh, I didn't realize that this was a stupid school thing you where trying to seek an answer to.
    Sunday, July 8, 2018 1:17 AM
  • Why is it not possible to loop through and print out all of the students last names using separate variables, but you can loop through and print out all the students last names in an array? 

    Ask yourself these questions:

    • What is a loop?  What do you need to make a loop work?
    • How many variables do you have when the array is declared?
    • How many variables would you need if there were a variable for each student?  Do those variables exist?

    FWIW, the question is kind of ridiculous because you could do it...  it would just be ugly, pointless code.  But it could be done, so to say "why can't you do" is a terrible question. 


    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    • Proposed as answer by RLWA32 Sunday, July 8, 2018 1:02 PM
    Sunday, July 8, 2018 12:22 PM