Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8882057
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T20:28:58+00:00 2026-06-14T20:28:58+00:00

I am currently building a sorting program. I have made three different ways to

  • 0

I am currently building a sorting program. I have made three different ways to create an array: Random, In Order and Reverse. I am currently undergoing problems with the In Order and Reverse arrays. Every time an In Order array is created it starts with a 1, I am not sure how come is doing so. Also my Reverse array will display digits out of order not every time but after some clicks. How can I fix these two issues?

namespace sortingMachine
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        //Class Level Variables --------------------------------------------------------------------------------------

        Stopwatch sw = new Stopwatch();
        Random r = new Random();
        OpenFileDialog open1 = new OpenFileDialog();

        long operations = 0;
        int size;
        int max;
        int[] createArray;
        int[] sortArray;
        int[] copyArray;

        //Create Array Methods --------------------------------------------------------------------------------------

        public void RandomNumber()
        {
            size = Convert.ToInt32(textBoxSize.Text);
            max = Convert.ToInt32(textBoxMax.Text);
            createArray = new int[size];
            copyArray = new int[size];
            sortArray = new int[size];

            for (int i = 0; i < size; i++)
            {
                createArray[i] = r.Next(1, max);
            }

            textBoxResults.AppendText("-------------------------------------------------------------------------------" + Environment.NewLine + "Random" + Environment.NewLine + Environment.NewLine);

            DisplayArrays();
        }

        public void InOrder()
        {
            size = Convert.ToInt32(textBoxSize.Text);
            max = Convert.ToInt32(textBoxMax.Text);
            createArray = new int[size];
            copyArray = new int[size];
            sortArray = new int[size];

            createArray[0] = 1;

            for (int i = 1; i < size; i++)
            {
                createArray[i] = createArray[i - 1] + r.Next(1, max);
            }

            for (int i = 1; i < size; i++)
            {
                if (r.Next(1, 101) < Convert.ToInt32(textBoxPercentage.Text))
                {
                    for (int x = 1; x < size; x++)
                    {
                        createArray[x] = r.Next(1, createArray[size - 1]);
                    }
                }
            }

            textBoxResults.AppendText("-------------------------------------------------------------------------------" + Environment.NewLine + "In Order" + Environment.NewLine + Environment.NewLine);

            DisplayArrays();
        }

        public void ReverseOrder()
        {
            size = Convert.ToInt32(textBoxSize.Text);
            max = Convert.ToInt32(textBoxMax.Text);
            createArray = new int[size];
            copyArray = new int[size];
            sortArray = new int[size];

            createArray[size - 1] = 1;

            for (int i = size - 1; i > 0; i--)
            {
                createArray[i - 1] = createArray[i] + r.Next(1, max);
            }

            for (int i = size - 1; i > 0; i--)
            {
                if (r.Next(1, 101) < createArray[0])
                {
                    for (int x = size - 1; x > 0; x--)
                    {
                        createArray[x] = r.Next(1, createArray[0]);
                    }
                }
            }

            textBoxResults.AppendText("-------------------------------------------------------------------------------" + Environment.NewLine + "Reverse Order" + Environment.NewLine + Environment.NewLine);

            DisplayArrays();
        }



        private void buttonCreateArray_Click(object sender, EventArgs e)
        {

            if ((textBoxSortKey.Text != "") && (textBoxCreateKey.Text != ""))
            {
                if (radioButtonRandom.Checked == true)
                {
                    RandomNumber();
                }

                if (radioButtonInOrder.Checked == true)
                {
                    InOrder();
                }

                if (radioButtonReverseOrder.Checked == true)
                {
                    ReverseOrder();
                }
            }
            else
            {
                MessageBox.Show("Type a key into the Key textbox.");
            }
        }



    }
}

Display Results:

The order array I am not sure why it always start with one:

-------------------------------------------------------------------------------
In Order
1
2
4
6
10

There are times the reverse order array will be like this:

-------------------------------------------------------------------------------

    Reverse Order
    10
    2
    7
    6
    5

Windows Form:

enter image description here

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T20:28:59+00:00Added an answer on June 14, 2026 at 8:28 pm

    If you look at how you are assigning values to the array you’ll see that the first element is assigned zero, but in your loop you start at the second element (i.e. 1) so you never re-assign the first element.

            createArray[0] = 1;
    
            for (int i = 1; i < size; i++)
            {
                createArray[i] = createArray[i - 1] + r.Next(1, max);
            }
    

    Try writing the first element assignment like this:

            createArray[0] = r.Next(1, max);
    

    As for your reverse function, there’s too much weirdness in there to really see what you’re trying to do. Try thinking thru it a few more times. Especially be careful with code like this: if (r.Next(1, 101) < createArray[0]) – it has random behaviour and also magic numbers in it.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm building a node.js application with Mongoose and have a problem related to sorting
I'm currently building an NFL pick'em league site. I have a Users model, a
Im currently building a php framework... again. I have a class called config. its
I am currently building in Version 3.5 of the .Net framework and I have
I am currently building a C# application that is generating a random 9 digit
I am currently building something in JS and have run into the following scenario:
I'm currently building a simple ecommerce site and have ran into an interesting problem.
We are currently building a PostgreSQL database that allows us to create profiles for
I'm currently building a 6 page site and have finished the template for the
I am currently building a Safari Extension. I have a simple popover page with

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.