answersLogoWhite

0


Best Answer

I don't know if I fully understand but here goes

int a[]={2,4,8,0,2};

int y = 0;

int x = 0;

while(y<a.length()){

x += a[y];

y++;

}

then you just reference x.

User Avatar

Wiki User

12y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: A Java method that takes an integer array as a formal parameter and returns the sum of integers contained within the array?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

What does the cons function do in clojure?

Returns a new sequence where the first parameter is the first element and the second parameter is the rest.


Which scanner class method would you use to read integer as input?

The method Scanner.nextInt() returns an integer obtained as user input.


D Write a function that returns 2n where n is an input parameter?

int 2n(int n){return n &lt;&lt; 1;)


When a function is overloaded there are multiple definitions of the functions What makes the various definitions of the function different from each other?

Their parameter-lists and returns values.


How do you write a program for counting and displaying occurrence of a number in an array?

Your first step is accepting input, which is done using the scanf() function:scanf("%d", &number);This means that you want scanf() to accept input, convert the input to a number, and store it in the memory at the address of number.Use a for() loop, counting from 0 to 9, and an array of integers to hold the numbers. Then simply scanf("%d", &intarray[counter]);The next step is a little tricky, but not very if you plan it out in advance.Each integer can contain 256, 65,536 or 4,294,967,296 different numbers. Creating an array to hold the count of each of those numbers is a waste of RAM.Instead, you'll want an "associative" array as follows:int numcount[MAXNUM][2];MAXNUM is 10, or the number of integers in the array you're checking. The second dimension, 2, consists of the number and its count.Obviously, you'll want a way to keep track of how many integers you've stored in numcount. An int called numcountnuminitialized to 0 would be the fastest way.Use a for() loop to iterate through the integers. If the integer does not exist in numcount, then set numcount[numcountnum][0] to the integer, set numcount[numcountnum][1] to 1, and increment numcountnum. Otherwise, if the integer exists, increase numcount[the integer index][1].Once the for() loop is finished, display the results. The only thing you have left to figure out is the function that searches the numcount array for an integer, and returns its index (or -1 if it's not found).

Related questions

What does the cons function do in clojure?

Returns a new sequence where the first parameter is the first element and the second parameter is the rest.


What is the use of the parameter in java?

A parameter is a variable that is passed to a method. It is not specific to Java, and is used in almost all programming languages. The parameter is used by the method in whatever way that it wants. Example: public void add(int x, int y) { return x + y; } In this case, the parameters are two integers, x and y. They are passed to the method 'add' which returns the sum of the parameters.


How can you check if a variable is a string or an integer in PHP?

Integers - The "is_int()" function can be used to check if a variable is has an integer for its value. ---- is_int($variable); // Returns true if $variable is an integer - otherwise false ---- Numeric Strings - Numeric strings are strings with numbers (or things that count as numbers) in them. Numeric-string variables are not integer variables. Numeric-string variables are passed on through forms, instead of integer variables - if you were wondering. Check form values using string formats, and not integer formats. The "is_numeric()" function can be used to check if a variable is a string with numbers - and only numbers - in it (except things that add up to be numbers). ---- is_numeric($variable); // Returns true if $variable is a string, and only contains numbers (broadly speaking) - false otherwise ---- Strings - String values are just text, basically. String variables can contain integers, but that does not make it an integer-type variable - it makes it a numeric string variable. The "is_string" function can be used to check if a variable contains the value of a string. ---- is_string($variable); // Returns true if $variable is a string - false otherwise


Which scanner class method would you use to read integer as input?

The method Scanner.nextInt() returns an integer obtained as user input.


D Write a function that returns 2n where n is an input parameter?

int 2n(int n){return n &lt;&lt; 1;)


When a function is overloaded there are multiple definitions of the functions What makes the various definitions of the function different from each other?

Their parameter-lists and returns values.


How do you write a program for counting and displaying occurrence of a number in an array?

Your first step is accepting input, which is done using the scanf() function:scanf("%d", &number);This means that you want scanf() to accept input, convert the input to a number, and store it in the memory at the address of number.Use a for() loop, counting from 0 to 9, and an array of integers to hold the numbers. Then simply scanf("%d", &intarray[counter]);The next step is a little tricky, but not very if you plan it out in advance.Each integer can contain 256, 65,536 or 4,294,967,296 different numbers. Creating an array to hold the count of each of those numbers is a waste of RAM.Instead, you'll want an "associative" array as follows:int numcount[MAXNUM][2];MAXNUM is 10, or the number of integers in the array you're checking. The second dimension, 2, consists of the number and its count.Obviously, you'll want a way to keep track of how many integers you've stored in numcount. An int called numcountnuminitialized to 0 would be the fastest way.Use a for() loop to iterate through the integers. If the integer does not exist in numcount, then set numcount[numcountnum][0] to the integer, set numcount[numcountnum][1] to 1, and increment numcountnum. Otherwise, if the integer exists, increase numcount[the integer index][1].Once the for() loop is finished, display the results. The only thing you have left to figure out is the function that searches the numcount array for an integer, and returns its index (or -1 if it's not found).


What day is it today or what day is today?

&lt;script&gt; var day; day = new Date(); var milli = day.getTime(); // returns milliseconds that have elapsed since 1/1/70 var year = day.getFullYear(); // returns the Year var month = day.getMonth(); // returns the month (0-11 January = 0) var dayofmonth = day.getDate(); // returns the day of the month var day2 = day.getDay(); // returns the integer day of the week (Sunday = 0) var hour = day.getHours(); // returns the integer hour (0 -&gt; 23) var minute = day.getMinutes(); // returns the Minute (0 - &gt; 59) var second = day.getSeconds(); // returns the Second (0 -&gt; 59) You can write out the date or time using document.write() document.write("The date is " + (month + 1) + "/" + dayofmonth + "/" + year); &lt;/script&gt;


What is integer in visual basic?

In the computer programming language Visual Basic, the data type integer is a whole number which can be used in calclations. It can be positive, negative, or zero. The default type of integer is 32-bit, but with "short" and "long" you can have 16- or 64- bit answers. The short data type works well with small numbers and saves RAM space. If overflow is a problem, long works better because of its larger capacity. To take the integer portion of a decimal, single, or double, use the int() function. int(3.925604) returns 3 as a decimal To convert a data type to integer, use cint() cint(int(3.925604)) returns 3 as an integer


Write a function prototype named test that accepts two parameters an integer and character and returns a float?

float test(int, char);


What do parameters and return values have to do with methods?

Parameters and return values are a major part of methods. When defining a method, you must include information about the data types of the return value and the parameters. An example of a method definition is this: public int getSumOfNumbers( int number1, int number2, int number3 ) { return ( number1 + number2 + number3 ); } The word "int" right after the word "public" is the return type. It describes what data type will be returned by the method. In this case, it was int, or integer. The sequence of words in between the parantheses, "int number1, int number2, int number3", is the parameter list. Each of the phrases separated by a comma in the parameter list is a parameter. The first word - in this case "int" - is the data type of the parameter. It describes what type of variable the parameter will be. The second word - "number1", "number2", or "number3" - is the name of the parameter. Every parameter must have a data type and a name, and every method must have a return type: even a method that returns nothing. For example: public void evaluateNumber(int number) { if ( number &gt; 0 ) { System.out.println( number + " is positive." ); } else if ( number &lt; 0 ) { System.out.println( number + " is negative." ); } else { System.out.println( number + " is zero." ); } } When a method does not return data, its return type must be defined as void, as it is above.


What is a variable that is used within a function?

If the variable is declared within the function body, it is a local variable, one that is local to the function. Local variables fall from scope when the function returns, they are only accessible within the function. However, local variables can be returned by value, which creates an automatic variable that is returned to the caller. If the caller does not store the return value, the automatic variable falls from scope when the expression containing the function call ends. However, the expression may evaluate the return value without storing it. Note that functions cannot return local variables by reference since the local variable falls from scope when the function returns. If the variable is passed as an argument to the function, then the variable is a parameter of the function. Arguments may be passed by value or by reference, depending upon the function signature. Passing by value means the function parameter is a copy of the argument (if the argument is an object, the object's copy constructor is invoked automatically). Thus any changes made to the parameter within the function are not reflected in the argument that was originally passed, and the parameter will fall from scope when the function returns. However, the value of the parameter can be returned as previously explained. Passing by reference means the function parameter refers directly to the argument that was passed. Thus any changes made to the parameter are reflected in the argument. Parameters that are declared as constant references assure the caller that the reference's immutable members will not be altered by the function. If the parameter is a non-const reference but the caller does not wish changes to be reflected in the argument, the caller should pass a copy of the argument instead.