2

I'm learning C# and I have a problem with a function.

It should multiply elements of the int array "tab" by the int value of "mnoznik" and return new int array "wyj".

I tried many things but I still have no idea on how to solve it.

static int mnozenie(int[] tab, int mnoznik)
{
    int[] wyj;
    wyj = new int[tab.Length];
    if (mnoznik != 0)
    {
        for (int i = 0; i < tab.Length; i++)
            wyj[i] = tab[i] * mnoznik;
        return wyj[];
    } else
        return 0;
1
  • Have your return type as int[][] and return null in else part. Commented Mar 30, 2015 at 18:00

5 Answers 5

7

try

static int[] mnozenie(int[] tab, int mnoznik)
{
    int[] wyj = new int[tab.Length];
    if (mnoznik != 0)
    {
        for (int i = 0; i < tab.Length; i++)
            wyj[i] = tab[i] * mnoznik;
    }
    return wyj;

}
Sign up to request clarification or add additional context in comments.

2 Comments

The OP wants the value 0 if the input mnoznik is 0. IMHO, that means it would make more sense to always return wyj; and just skip the multiplication loop if mnoznik is 0.
This worked perfectly (to create and return an int array)...
1
static int[] mnozenie(int[] tab, int mnoznik)
{
    if (tab.Length > 0)
    {
        for (int i = 0; i < tab.Length; i++)
        {
            tab[i] *= mnoznik;
        }
        return tab;
    }
    return null;
}

Or alternatively instead of return null just return tab back.

The if block just protects from trying to access a null collection, or you could just handle the exception in whatever way you choose.

7 Comments

or alternatively instead of return null just return tab back, the if block just protects from trying to access a null collection, or just handle the exception in whatever way you choose.
where u store result of multiplication ?
yes u assign result to local field item which is never declared ... in the original method is result of multiplication stored in array
I'm sorry, but thats just not accurate. If the original int[] tab is never declared then that is handled with the if block, if it is declared then I store the result of the multiplication for each element in the element and pass the new int[] tab back. In fact declaring another int[] or any more variables would be a complete waste of resources as they are unneeded.
furthermore, you are welcome and even encouraged to test the above code to see if it works.
|
0

You're trying to return both an array and an integer, which you can't do. You can wrap it all in a Tuple<,> and return both, or you can return null in the case mnoznik is 0.

Something like:

static Tuple<bool,int[]> mnozenie(int[] tab, int mnoznik)
{
    return (mnoznik == 0)
           ? new Tuple<bool,int[]>(false, new [] { 0 })
           : new Tuple<bool,int[]>(true, tab.Select(i => i*mnoznik).ToArray());
}

or

static int mnozenie(int[] tab, int mnoznik)
{
    return (mnoznik == 0)
           ? null
           : tab.Select(i => i*mnoznik).ToArray();
}

2 Comments

in your second example has method int return type and u returned int array
That was a typo. I should've written int[]. Another thing is that instead of null, you might return a well-defined array, like I did in the first example: new [] { 0 };
0

correct me if I'm wrong but I'm something like 90% sure that in case of returning arrays or any kind of element in c#/c++. The compiler actually doesn't actually move the value itself, in the stack from one memory location to another, it just pushes the pointer/handle to that specific memory, where the information is stored up the stack.

by specifying

return wyj[];

you're actually trying to return the array itself, and not the pointer. Which would be illegal, but my simply removing the the square brackies, you're moving the pointer and complier is happycamper. [EDIT: missed words]

Comments

0

Try to see if this code might be able to help you!

static int[] m(int[] a , int low , int big) 
{
    int[] re = new int[2];
    int min, max, mid ;
    if (low == big)
    {
        min = max = a[low];
        re[0] = min;
        re[1] = max;
        return (re);
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.