-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());
                        }
                    }
                }
            }
        }
    }
}
user2954279
  • 1
  • 1
  • 3
  • 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. – Roland Illig Nov 04 '13 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++. – Roland Illig Nov 04 '13 at 22:25
  • You can easily find sorting algorithms all over the place online. Bing it. – GodsCrimeScene Nov 04 '13 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 http://sscce.org/ – Michael J. Gray Nov 04 '13 at 22:36
  • @RolandIllig Here is the full code – user2954279 Nov 04 '13 at 23:14
  • In what sort of job do they use Bubble Sort? Fail....! – Mitch Wheat Nov 05 '13 at 02:16

2 Answers2

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);
    }
}
Erik Noren
  • 4,279
  • 1
  • 23
  • 29
  • 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 05 '13 at 00: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; – user2954279 Nov 05 '13 at 00: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: http://stackoverflow.com/questions/14768010/simple-bubble-sort-c-sharp http://stackoverflow.com/questions/1595244/whats-the-most-elegant-way-to-bubble-sort-in-c http://www.programcall.com/10/interview/write-a-sample-program-that-implements-bubble-sort-in-csnet.aspx – Erik Noren Nov 05 '13 at 00: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(); } – user2954279 Nov 05 '13 at 01: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 05 '13 at 02:50
  • It prints but it is not fully sorted. It also prints one number at a time – user2954279 Nov 05 '13 at 20:55
  • Take the write and read out of those loops. Leave them at the very end of the program after you did your bubble sorting. Make them separate parts of your program like I showed above. There shouldn't be any printing of the sort before the part of the code that says "print the results" – Erik Noren Nov 05 '13 at 21:04
  • never mind i have finally figured it out. Thank you very much – user2954279 Nov 05 '13 at 21:12
-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();
Attila
  • 3,206
  • 2
  • 31
  • 44
  • 2
    The title and tags indicate that they specifically want to utilize bubble sort. The `List.Sort` method may use some other sorting method depending on the implementation of the BCL being used. – Michael J. Gray Nov 04 '13 at 22:41