简体   繁体   中英

Generate list with random numbers. Sort and total sum

I am new to programming and i would be delighted if someone could help me with the following problem. I have problem with two methods. First sum all of 50 random numbers in a list, second sort list with 50 random numbers. I know how to do this when a list has for example four numbers{1,2,3,8} , but not with 50 random numbers.

It will be nice to use a constructor to this two methods, but I don't now how to do this.

Any help would be much appreciated.

class Class1
{
  var rand = new Random();

  public Class1( ) //  how to use a constructor ?


    public static List<double> TotalSum()
    {

        var alist = new List<double>();
        for (int i = 0; i < 50; i++)
        {
            alist.Add(rand.Next(10));
        }
        Console.WriteLine("Total sum:", alist);
    }

    public static List<double> Sort()
    {
        var slist = new List<double>();
        for (int i = 0; i < 50; i++)
        {
            slist.Sort(rand.Next(10));   // rand.Next does not work with Sort
            Console.WriteLine(slist);
        }
    }


    class Program
    {
        static void Main(string[] args)
        {
            Class1 show = new Class1(); 
            show.TotalSum();
            show.Sort();
        }
    }
}

just use the 2 methods

list.Sum(); 
list.Sort();

I suggest that you use rand.Next(Environment.TickCount) to get better random numbers, but at the end you need to use a different primitive value to store the result

class Program
{
    static void Main(string[] args)
    {
        var array = Class1.Sort();
        PrintArray(array);

        Console.WriteLine();

        Console.WriteLine("Total sum: {0}", Class1.TotalSum());

        Console.ReadLine();
    }

    static void PrintArray(List<double> array)
    {
        foreach (var a in array)
            Console.WriteLine(a);
    }
}

public class Class1
{

    public static double TotalSum()
    {
        var rand = new Random();

        var alist = new List<double>();
        for (int i = 0; i < 50; i++)
        {
            alist.Add(rand.Next(10));
        }

        return alist.Sum();
    }

    public static List<double> Sort()
    {
        var rand = new Random();

        var slist = new List<double>();
        for (int i = 0; i < 50; i++)
        {
            slist.Add(rand.Next(10)); // rand.Next does not work with Sort
        }

        slist.Sort();

        return slist;
    }
}

I think I got what you mean. You want a class that holds random list of numbers and you need some functions.

Its better to give more suitable name to your class. for example "RandomList" would be good.

You can't use Console.WriteLine(list) to print items of list. You have to iterate through items and print them in the way you want.

You can't use var to define fields. You have to write the type explicitly.

static keyword in other word is one for all instances . So if you dont use static it would be one per instance . Instance is just the object that you create with the constructor. Its better to declare random number generator as static, but other methods and list field should remain non static.

With the use of Linq Sum() you can get the sum of all the items from list. If you dont want to use Linq then you have to iterate through list and add each item to value sum .

I commented the code to explain things.

class Program
{
    static void Main(string[] args)
    {
        RandomList show = new RandomList(50, 10);
        show.TotalSum();
        show.Sort();
    }
}
class RandomList
{
    private static Random rand = new Random();

    private List<double> _list; // this field holds your list

    public RandomList(int length , int maxValuePerItem) // this is the constructor
    {
        _list = new List<double>(); 

        // populate list
        for (int i = 0; i < length; i++)
        {
            _list.Add(rand.Next(maxValuePerItem)); 
        }
    }

    public void TotalSum()
    {
        Console.WriteLine("Total sum: {0}", _list.Sum()); // {0} is required to specify the place to put _list.Sum() inside string.


        // without linq way
        // int sum = 0;
        // foreach(var i in _list) sum += i;
    }

    public void Sort()
    {
        _list.Sort();

        foreach (var d in _list) 
        {
            Console.Write(d + " , "); // you need to print this in the way you want.
        }
    }
}

A constructor is typically used to initialize some values. In your case, you should use it to initialize your list, that is, to load it with 50 random numbers. We will also change it so that the list is a property of the class.

Note that the methods shouldn't be static, since they are acting on a property of the class. We are also using the built-in methods of List ( Sum() and Sort() ) instead of rolling our own.

class Class1
{
    var rand = new Random();
    var alist;

    public Class1() // constructor creates a new list and initializes it
    {
        alist = new List<double>();
        for (int i = 0; i < 50; i++)
        {
            alist.Add(rand.Next(10)); 
        }
    }


    public List<double> TotalSum()
    {
        Console.WriteLine("Total sum:", alist.Sum());
    }

    public List<double> Sort()
    {
        alist.Sort();   
        for (double num in alist)
        {
            Console.WriteLine(num.ToString());
        }

    }


    class Program
    {
        static void Main(string[] args)
        {
            Class1 show = new Class1();
            show.TotalSum();
            show.Sort();
        }
    }
}

Because you asked how to use the constructor I tried to rewrite your code in a way that the constructor do something of usefull. I added a property and a local variable plus another function.

public class Class1
    {
        Random rand = new Random();
        List<double> alist { get; set; }

        public Class1(int howmany = 50)
        {
            alist = new List<double>();
            for (var i = 0; i < howmany; i++)
            {
                alist.Add(rand.NextDouble());
            }
        }

        public void Sort()
        {
            alist.Sort();
        }

        public void printTotalSum()
        {
            Console.WriteLine("Total sum: {0}", alist.Sum());
        }

        public void printList() {
            Console.WriteLine("The list contains:");
            for (int i = 0; i < alist.Count; i++)
            {
                Console.WriteLine(alist[i]);
            }
        }

    }

   class Program
    {
        static void Main(string[] args)
        {

            Class1 show = new Class1(10);
            show.printTotalSum();
            show.Sort();
            show.printList();
        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM