Hey guys,

My teacher gave me a program to look at for using arrays with methods to which it creates a series of 10 random numbers and then uses a print method to print them out on one line then, using a selection sort method to arrange them in numerical order and then uses the print method to print the new sorted numbers that were generated one the next line. However, she did not explain how this works and I am rather new to C# and Object Oriented programming and methods. I see it looks like print array was declared twice above but called only once, below is the full code. I apologize for my ignorance, I just want someone who knows what they are doing to explain how this process works, so that I could perhaps write a similar program understanding the logic.
Thanks in advance!

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Sorting
{
    class sortedArray
    {
        
        static void Main(string[] args)
        {
            int[] myArray;          //declare the array
            myArray = new int[10]; //initialise the array size
            testArraySort(myArray); /*Call testArraySort() to initialise the memebrs 
                                    of the array to randomn values b/w 10 & 1000*/
            printArray(myArray);    //Print the un-sorted array
            sortArray(myArray);     //selection sort the array
            printArray(myArray);    //Print the sorted array
        }
       
                                    /// initialises the array memebers with random numbers b/w 10 & 1000
        
            static void testArraySort(int[] a)
            {
                Random rnd = new Random();
                for (int i = 0; i <= a.GetUpperBound(0); i++)
                {
                    a[i] = rnd.Next(10, 1000);
                }
            }

       
                                /// prints the bvalues held in the members of the array
        
            static void printArray(int[] a)
            {
                for (int i = 0; i <= a.GetUpperBound(0); i++)
                {
                    Console.Write("{0} ", a[i]);
                }
                Console.WriteLine();
            }


      
                                /// uses selection sort to sort the array
        
            static void sortArray(int[] a)
            {
                for (int i = 0; i <= a.GetUpperBound(0) - 1; i++)
                {
                    int min = i;
                    for (int j = i + 1; j <= a.GetUpperBound(0); j++)
                    {
                        if (a[j] < a[min])
                        {
                            min = j;
                        }
                    }
                    if (i != min)
                    {
                        int swap = a[i];
                        a[i] = a[min];
                        a[min] = swap;
                    }
                }

            }
    }
}

PrintArray is declared once on line 36.
It is called twice on lines 17 and 19.
Once to print out the unsorted array.
Once to print out the sorted array.

Thanks I understand now!

What does GetUpperBound do? And how does it what is doing in this case?

In your case a.GetUpperBound(0) equates to 9. The zero stands for the first dimension in your array. So in your for loops you are going from 0 to index 9 through the array. The array being myArray.
Perhaps you should better have asked this question in a new thread.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.