Thread: what is wrong? how can i return array with values of proportion?

  1. #1
    Registered User
    Join Date
    Nov 2015
    Posts
    5

    what is wrong? how can i return array with values of proportion?

    Code:
     class Program
        {
            static int Sum(int[] a)
            {
                int sum = 0;
                for (int i = 0; i < a.Length; i++)
                {
                    sum += a[i];
                }
                return sum;
            }
            public static int[] Prop(int x, params int[] y)
            {
                int count = 0;
                if (y.Length > 1)
                    for (int i = 0; i < y.Length; i++)
                    {
                        count++;
                        int[] p = new int[count];
                        int f = x * y[i] / Sum(y);
                        p[count] = f;
                    }
                return p;
            }
            static void Main(string[] args)
            {
                Prop(2016, 5, 7, 9);
            }
        }
    }

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Returning an array from a function is not a problem, but you have made some other mistakes. First, you are declaring p in a place where it would only be in scope for the duration of the for loop. No problem, the easiest fix is to move the declaration of p. But the next issue is that you are doing weird things with count. As you have it written it will always cause an ArrayOverflow exception. I don't really understand the goal you have in mind, allocating and using the array this way. From the loop we can infer that p will not need more elements than y. Generally I prefer not to allocate the way you tried, anyway. Adding one element at a time is slow. The way you did it only compounds the problems. It loses the original array and none of the old elements are kept.

    So this is what I came up with:
    Code:
    using System.IO;
    using System;
    
    class Program
    {
        static int Sum(int[] a)
        {
            int sum = 0;
            for (int i = 0; i < a.Length; i++)
            {
                sum += a[i];
            }
            return sum;
        }
        public static int[] Prop(int x, params int[] y)
        {
            int[] p = new int[y.Length];
            if (y.Length > 1)
                for (int i = 0; i < y.Length; i++)
                {
                    int f = x * y[i] / Sum(y);
                    p[i] = f;
                }
            return p;
        }
            
        static void Main()
        {
            int[] res = Prop(2016, 5, 7, 9);
            for (int i = 0; i < res.Length; i++) {
                Console.WriteLine("res[{0,2:D}] = {1:D}", i, res[i]);
            }
        }
    }
    I didn't really check the math... hope this helps.

  3. #3
    Registered User
    Join Date
    Nov 2015
    Posts
    5
    Thanks a lot you really helped me, and also thanks for explaining to me, i really saw my mistake

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Wrong return type when returning char array as pointer
    By jwroblewski44 in forum C++ Programming
    Replies: 3
    Last Post: 04-25-2014, 01:10 PM
  2. write() and read() int values from socket returning wrong values.
    By Premjith P S in forum Linux Programming
    Replies: 8
    Last Post: 11-29-2012, 02:59 PM
  3. How to return certain values from an array?
    By Valandu in forum C Programming
    Replies: 1
    Last Post: 11-06-2011, 11:25 PM
  4. Scale back size of Numbers while maintaining proportion
    By kerrymaid in forum C Programming
    Replies: 5
    Last Post: 10-23-2011, 01:31 PM
  5. Replies: 2
    Last Post: 11-19-2008, 02:36 PM

Tags for this Thread