Click here to Skip to main content
15,886,789 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
The function instead of receiving as input parameter a parameter of type int which will receive an array of ints and a int number that we will call b. The end of the function will be to return true or false in case some or all of the numbers in the array are exactly divisible by the number b passed by parameter. In parallel, this function will also show on the console of the Unity editor the number of numbers divisible by b in the array.

int testSumArray(int[] numbers = null, int b = 2)
    {
        numbers = new int[] { 8, 3, 9, 6, 7, 4 };
        int conta = 0;

        for (int i = 0; i < 5; i++)
        {
            if (numbers[i] / b == 0)
            {
                conta = conta + 1;
                return true;
            }
            else
            {
                return false;
            }
        }
        return conta;
    }
    

    // Start is called before the first frame update
    void Start()
    {
        Debug.Log("O numero de vezes é " + testSumArray());

    }


What I have tried:

I tried my best to do it, but I couldn't do a code with was right.
Posted
Updated 28-Apr-22 7:25am

C#
if (numbers[i] / b == 0)

You are checking if the answer is zero, but your question needs the remainder after the division. You should use the modulo operator:
C#
if (numbers[i] % b == 0)
 
Share this answer
 
While we are more than willing to help those that are stuck, that doesn't mean that we are here to do it all for you! We can't do all the work, you are either getting paid for this, or it's part of your grades and it wouldn't be at all fair for us to do it all for you.

So we need you to do the work, and we will help you when you get stuck. That doesn't mean we will give you a step by step solution you can hand in!
Start by explaining where you are at the moment, and what the next step in the process is. Then tell us what you have tried to get that next step working, and what happened when you did.

And I'd start by thinking about what you are supposed to do. For example, if your method is passed an array of data, is it really a good idea to just overwrite it immediately?
Then think about what "receiving as input parameter" means in the context of what you need to do when you call the method.

And why

If you are having problems getting started at all, then this may help: How to Write Code to Solve a Problem, A Beginner's Guide[^]
 
Share this answer
 
Quote:
I can understand this ex my teacher gave me

Programming is the art of details, You have to read carefully the requirement and find the logical order of every little pieces in order to make sense of it all.
As is, your code is nowhere near correct, advice do it all again.

Quote:
the number of numbers divisible by b in the array.

Divisible means that the remainder of an integer division is zero, this also means that number is multiple of b.
The operator that gives the reminder of division is modulo.

Inhope it will help you in future, learn one or more analyze methods, E.W. Djikstra/N. Wirth Stepwize Refinement/top-Down method is a good start.
Structured Programming.pdf[^]
https://en.wikipedia.org/wiki/Top-down_and_bottom-up_design[^]
https://en.wikipedia.org/wiki/Structured_programming[^]
https://en.wikipedia.org/wiki/Edsger_W._Dijkstra[^]
https://www.cs.utexas.edu/users/EWD/ewd03xx/EWD316.PDF[^]
Program Development by Stepwise Refinement[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900