CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5

Threaded View

  1. #2
    Join Date
    Feb 2011
    Location
    United States
    Posts
    1,016

    Arrow Re: Displaying only part of an Array

    To print out the last 20 elements of some array do something like (this example assumes the array has at least 20 elements):

    Code:
    void printArray(int[] data)
    {
        for(int i = data.Length - 20; i < data.Length; i++)
        {
            Console.WriteLine(data[i]);
        }
    }
    Do you see why this works and how to adapt this to print both sets?

    Part B will be easier if instead of cramming everything into the Main method you create separate methods like:

    Code:
    //Returns a sorted array (and the number of iterations as an output parameter)
    int[] bubbleSort(int[] data, out int iterations) { ... }
    
    //Returns a sorted array (and the number of iterations as an output parameter)
    int[] InsertionSort(int[] data, out int iterations) { ... }
    
    //Prints the first and last 20
    void printFirstAndLast(int[] data) { ... }
    
    //Creates a random array
    int[] generateRandomArray() { ... }
    Then in your main method, you can just do (pseudocode):

    Code:
    For 10 iterations:
        Generate random array
        Sort it
        Add the iterations count to average 
    Divide by 10 to get the average
    Display to user
    Writing short methods that are easy to understand makes it easy to debug your code. Try to make every method do one "verb" ("sort", "generate something", "calculate something", etc) and do it well.

    Also, I suggested you use out parameters. They're easy to use, but I should tell you how they work. Basically, using them tells the subroutine: "hey, you need to assign this variable before you're finished so the calling method can get it". Example:

    Code:
    //Some function with an out parameter
    int doSomething(int someInputParameter, out int anOutParameter)
    {
        //Does some stuff
        anOutParameter = 5;  //When the method returns, anOutParameter will return 5
    
        return 10;  //The function returns 10
    }
    
    //Example of how tocall
    void main()
    {
        int otherResult; //Declare a variable to hold the result of the out parameter
        int result = doSomething(50000, out otherResult);
    
        //Now result has value 10 and otherResult has value 5;
    }
    Does that make sense?

    If not, read the MSDN article on the out keyword: http://msdn.microsoft.com/en-us/libr...=vs.80%29.aspx

    Please also use [code] and [/code] tags in the future to keep your code formatted.

    I hope that helps. Good luck (and have fun)!
    Last edited by BioPhysEngr; February 18th, 2012 at 02:09 PM. Reason: hrm, some extraneous junk got tacked onto the end of my message; deleted
    Best Regards,

    BioPhysEngr
    http://blog.biophysengr.net
    --
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

Tags for this Thread

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured