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());
}
}
}
}
}
}
}
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:
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);
}
}