none
C# Divide Number in specific way RRS feed

  • Question

  • this is my code which is not returning data as per my requirement. so please rectify my code which will give me desire output.

    static void Main(string[] args)
            {
                List<int> test1 = DivideInt(10,1).ToList().OrderBy(s=> s).ToList();
                List<int> test2 = DivideInt(10, 2).ToList().OrderBy(s => s).ToList();
                List<int> test3 = DivideInt(10, 3).ToList().OrderBy(s => s).ToList();
                List<int> test4 = DivideInt(10, 4).ToList().OrderBy(s => s).ToList();
                List<int> test5 = DivideInt(10, 5).ToList().OrderBy(s => s).ToList();
                List<int> test6 = DivideInt(10, 6).ToList().OrderBy(s => s).ToList();
                List<int> test7 = DivideInt(10, 7).ToList().OrderBy(s => s).ToList();
                List<int> test8 = DivideInt(10, 8).ToList().OrderBy(s => s).ToList();
                List<int> test9 = DivideInt(10, 9).ToList().OrderBy(s => s).ToList();
                List<int> test10 = DivideInt(10, 10).ToList().OrderBy(s => s).ToList();

            }

                public static IEnumerable<int> DivideInt(int pagetotal, int userinput)
                {
                    if(userinput==0)
                    {
                        yield   return  0;
                    }
                    else
                    {
                        int rest =pagetotal % userinput;
                        double  result =pagetotal / (double) userinput;

                        for(int i=0; i< userinput;i++)
                        {
                            if(rest-- >0)
                                yield   return (int) Math.Ceiling(result);
                            else
                                yield   return (int) Math.Floor(result);
                        }
                    }
                }
    My requirement as following.

    1) if total page is 10 and user split into 2 files then two files will be generated each file will have 5 pages

    2) if total page is 10 and user split into 3 files then 3 files will be generated. first 2 files will have 3 pages and last pages will have 4 pages.

    3) if total page is 10 and user split into 4 files then 4 files will be generated. first 3 files will have 3 pages and last pages will have 1 pages.

    4) if total page is 10 and user split into 5 files then 5 files will be generated. each files will have 2 pages

    5) if total page is 10 and user split into 6 files then first 5 files will be generated with 1 page. last file will have 5 pages

    6) if total page is 10 and user split into 7 files then first 6 files will be generated with 1 page. last file will have 4 pages.

    7) if total page is 10 and user split into 8 files then first 7 files will be generated with 1 page. last file will have 3 pages.

    8) if total page is 10 and user split into 9 files then first 8 files will be generated with 1 page. last file will have 2 pages.

    8) if total page is 10 and user split into 10 files then 10 files will be generated with 1 page. 

    again

    1) if total page is 9 and user split into 2 files then first file will be generated with 4 page. last file will have 5 pages.2) if total page is 9 and user split into 3 files then each 3 files will have 3 pages.

    3) if total page is 9 and user split into 4 files then first 3 files will have 2 pages and last page will have 3 pages.

    4) if total page is 9 and user split into 5 files then first 4 files will have 2 pages and last page will have 2 pages.5) if total page is 9 and user split into 6 files then first 5 files will have 1 pages and last page will have 4 pages.

    6) if total page is 9 and user split into 7 files then first 6 files will have 1 pages and last page will have 3 pages.

    7) if total page is 9 and user split into 8 files then first 7 files will have 1 pages and last page will have 2 pages.

    8) if total page is 9 and user split into 9 files then each file will have 1 pages.

    hopefully i am clear what i am looking for.

    • Edited by Sudip_inn Sunday, August 12, 2018 4:44 PM
    Friday, August 10, 2018 7:24 PM

Answers

  • Try an obvious thing:

    . . .
    int size = pagetotal / userinput;
    int rest = pagetotal % userinput;
    int last_size = size + rest;
    
    for( int i = 0; i < userinput - 1; i++ )
    {
       yield return size;
    }
    
    yield return last_size;
    

    • Marked as answer by Sudip_inn Sunday, August 12, 2018 4:42 PM
    Saturday, August 11, 2018 5:52 PM