-4

I cannot get the program to print the numbers properly and sort properly. I need help on making the program run properly. This project's deadline is tonight at 12. Please Help

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

namespace Sort
{
    class Program
    {
        static void Main(string[] args)
        {
            List<int> ints = new List<int>() { };

            int sort = 0;

            for (int i = 1; i > 0; i++)
            {
                Console.WriteLine("Enter a number");

                int number = Convert.ToInt32(Console.ReadLine());

                if (number == -1)
                    break;
                {
                    for (int j = 0; j > ints.Count; j++)
                    {
                        if (ints[i] < ints[j])
                        {
                            sort = ints[j];
                            ints[j] = ints[i];
                            ints[i] = sort;

                            Console.WriteLine(ints[i].ToString());
                        }
                    }
                }
            }
        }
    }
}
6
  • Please post a useful piece of code. In your example the { and } are not even balanced. And please indent your code properly. A good way is to use the source code formatter from Visual Studio. Nov 4, 2013 at 22:23
  • 4
    Oh, and if it's for the job: Someone else has already figured out how to sort things. Search for "C# sort list". Oh, and your programming language is C#, not C++. Nov 4, 2013 at 22:25
  • You can easily find sorting algorithms all over the place online. Bing it. Nov 4, 2013 at 22:34
  • 1
    I am recommending that this post be closed as it does not demonstrate an understanding of the problem at hand and does not include information about expected and actual behavior. Please see sscce.org Nov 4, 2013 at 22:36
  • @RolandIllig Here is the full code Nov 4, 2013 at 23:14

2 Answers 2

0

It's best if you ask a specific question about the nature of your problem and what you'd like to have fixed.

But looking at your code I see some strange behavior. It appears you are abusing a for loop to loop forever and ask the user for a number. If the number isn't -1 you loop over a blank List<int> and print every line after doing a swap if the inner loop's indexed value is greater than the outer loop.

If the input is -1, then you break out of your loop and end the program.

Is your problem that you never seem to have any numbers to sort? Is it that your sort gets an index out of bounds because you're starting with i == 1 but indexes of arrays start at 0? Is it that you're writing numbers without completing your sort first? All of the above?

You'd best be served by looking up some pseudo-code that explains the flow of a bubble sort and then implement it yourself. Failing that you can readily find C# implementations of bubble sort with simple Internet searches.

But to give you a jump start, my guess is you meant to:

  1. while(true)
  2. prompt user for number
  3. if number != -1 store number into array
  4. repeat until number == -1 which will break out of the while loop
  5. sort elements of list or array based on bubble sort
  6. loop your sorted elements and print them out

EDIT: Since you asked for help on how to fix your program, I will give you the below untested code to get you started. You need to implement the bubble sort according to your "work" instructions.

static void Main(string[] args)
{
    List<int> ints = new List<int>();

    //capture numbers from user input
    while(true)
    {
        Console.WriteLine("Enter a number");
        int number = Convert.ToInt32(Console.ReadLine());

        if (number == -1) //If user enters magic number, we break out of while loop
            break;

        ints.Add(number); //Unless we've broken out, add the number to the list
    }

    //do your bubble sort here
    //this is up to you to implement!

    //print the results
    foreach(int sortedNumber in ints)
    {
        Console.WriteLine(sortedNumber);
    }
}
8
  • The numbered steps are pretty straightforward. I added some untested code to get you started. You will still need to do the bubble sort yourself but the code is marked where it should go. It's untested as well. It's meant to give you a start on fixing your problem - it's not meant to do your work for you!
    – Erik Noren
    Nov 5, 2013 at 0:10
  • thanks @eriknoren my bubble sort does not work for (int j = 0; j < ints.Count; j++) { if (ints[i] < ints[j]) { sort = ints[j]; ints[j] = ints[i]; ints[i] = sort; Nov 5, 2013 at 0:29
  • 1
    That's correct - your existing bubble sort code doesn't work - it's incorrectly written. You need to implement it where I left room in the code using an existing algorithm or one you've created based on an implementation. You should be able to find one easily and put the code right where there's room for it. Here's some to start: stackoverflow.com/questions/14768010/simple-bubble-sort-c-sharp stackoverflow.com/questions/1595244/… programcall.com/10/interview/…
    – Erik Noren
    Nov 5, 2013 at 0:52
  • int temp = 0; for (int write = 0; write < ints.Count; write++) { for (int sort = 0; sort < ints.Count - 1; sort++) { if (ints[sort] < ints[sort + 1]) { temp = ints[sort]; ints[sort] = ints[sort + 1]; ints[sort + 1] = temp; } } Console.Write(ints[write].ToString()); Console.ReadLine(); } Nov 5, 2013 at 1:51
  • It's hard to read code in a comment but it looks like you've got your Console.Write inside the bubble sort loop. It needs to be outside of that like I've shown above. You might even try updating your question with your current code and where you're experiencing the problem this time. It sounds like you're a lot closer than you were before.
    – Erik Noren
    Nov 5, 2013 at 2:50
-2

If you would like to sort a list in C# you can use the built in method of List:

List<int> list = new List();

// add some elements

list.Sort();
1
  • 2
    The title and tags indicate that they specifically want to utilize bubble sort. The List<T>.Sort method may use some other sorting method depending on the implementation of the BCL being used. Nov 4, 2013 at 22:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.