1 Replies - 779 Views - Last Post: 14 March 2012 - 02:47 PM Rate Topic: -----

#1 eric4215  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 51
  • Joined: 09-September 10

Create 10 different arrays to sort

Posted 14 March 2012 - 02:34 PM

Hi, I'm writing a program that creates ten random arrays, sorts them using shell sort, and tracks the number of statements involved. However, my code loops the same number of statements ten times and then the Console Message after, I've tried moving the brackets around and have tried using a foreach statement, but I have a feeling that I am missing something that would make this work easier, any help would be appreciated! Here's my code:
   static int statements = 0;
        static void Main(string[] args)
        {
            
            int[] TestNumber = new int[1000];   //// create a new random array
            Random number = new Random();         //// to test statistics

            for (int i = 0; i < TestNumber.Length; i++)
            {
                TestNumber[i] = number.Next(100, 999);
                Console.WriteLine("\n To test sorting algorithms with 1,000, 3 digit numbers, ten times, press any key...");
                Console.ReadLine();

                for (int j = 0; j < 10; j++)
                {
                    ShellSort(TestNumber);
                }
                int average1 = 0;
                average1 = statements / 10;
                Console.WriteLine("The average number of statements in Shellsort is {0}", average1);
                 Console.ReadLine();
            }
            }
             public static void ShellSort(int [] values)
{
    int j; 
    int temp;
    
    int increment = 3;
    while (increment > 0)
    {
        for (int index = 0; index < values.Length; index++)
        {
            j = index;
            temp = values[index];
            while ((j >= increment) && values[j - increment] > temp)
                
            {
                values[j] = values[j] - increment;
                j = j - increment;
                statements++;
            }
            values[j] = temp;
            statements++;
        }
        if (increment / 2 != 0)
            increment = increment / 2;
        else if (increment == 1)
            increment = 0;
        else
            increment = 1;
        statements++;
    }
                Console.WriteLine(" There are a total of {0} statements in Shell Sort.", statements);
                Console.ReadLine();
                
            }
        }
    }



Here is with the for each: The method is changed to public static int [] ShellSort (int [] values), with a return values, then I tried a for each, I get an error because "i" has been declared in parent scope:
for (int i = 0; i < TestNumber.Length; i++)
            {
                for (int j = 0; j < 10; j++)
                {
                    TestNumber[i] = number.Next(100, 999);
                    Console.WriteLine("\n To test sorting algorithms with 1,000, 3 digit numbers, ten times, press any key...");
                    Console.ReadLine();
                    foreach (int i in ShellSort(TestNumber))
                    {
                        Console.Write("" + i.ToString());
                    }

                    int average1 = 0;
                    average1 = statements / 10;
                    Console.WriteLine("The average number of statements in Shellsort is {0}", average1);
                    Console.ReadLine();


I was wondering if doing it with loops would work best, or do I need to make another array for this?; Stuck and frustrated, there's more to this and I been working on it for three weeks now... Still feel lost. Thanks for your help.

Is This A Good Question/Topic? 0
  • +

Replies To: Create 10 different arrays to sort

#2 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5578
  • View blog
  • Posts: 11,924
  • Joined: 02-June 10

Re: Create 10 different arrays to sort

Posted 14 March 2012 - 02:47 PM

14 for (int j = 0; j < 10; j++)

you loop 10 times. Each time you increment 'statements'. THerefore it is always 10.

You never increment 'statements' so the result of statement/10 will always be the same.

This is obviously schoolwork so we can't just give you fixed code. All we can do is point out the error in your logic.



eric4215: What this shows us is that you aren't familiar with breakpoints and how to debug your own code. Learning to debug one's own code is an essential skill. Sadly, one that apparently few college courses teach. Silly if you ask me.

Placing breakpoints and walking through the code line by line allows you to actually WATCH it execute.

Visualizing what your code does will let you see why it behaves the way it does.

It would be well worth your time to do the tutorials on FAQ 2. A couple hours learning this skill will save you hundreds of hours of confusion in one project alone.


See FAQ # 2 & 25. (Click the SHOW button below)


TOP most asked:


What does this error message mean?

FAQ 2: How do I debug
FAQ 3: How do I make Class1/Form1 talk to Class2/Form2


FAQ (Frequently Asked Questions - Updated Feb 2012
Spoiler



Was This Post Helpful? 0
  • +
  • -

Page 1 of 1