0

I'm new to Java and I'm doing a uni course. I've been asked to design three functions.I have to find the difference between each adjacent numbers in an array, another to total the array and the last one to calculate the difference using the other functions then write a programme. I'm totally lost on the last function and my tutor has gone away on hols. Here is the code I have done so far. I don't want people doing the code for me but if anyone can advice me what I need to do I would appreciate your advice. I'm not sure how to loop the difference function into the array and store it into the new array I have made. If anyone could explain where I am going wrong I would love to hear from you!

var numberArray = [10,9,3,12];

// function difference will find the highest value of the two numbers,find the difference between them and return the value.

function difference(firstNumber, secondNumber)

{

if (firstNumber > secondNumber)
{
    return (firstNumber - secondNumber);
}
else
{   
    return (secondNumber - firstNumber);
}

}
// function sum will add the total numbers in the array and return the sum of numbers.

function sum(numberArray)
{
 numberTotal = 0
for (var total = 0; total < numberArray.length; total = total + 1)
{
numberTotal = numberTotal + numberArray[total]
}
{
    return numberTotal
}

/*code the process that calculates a new array containing the differences between all the pairs
of adjacent numbers, using the difference() function you have already written.
This function should be named calculateDifferences() and should accept an array numberArray. 
The function should first create a new empty array of the same size as numberArray
It should then calculate the differences between the pairs of adjacent numbers, 
using the difference() function, and store them in the new array. Finally, the function should return the new array.
The calculation of the differences should be done in a loop, at each step finding the difference between each 
array element and the next one along in the array, all except for the last difference,
which must be dealt with as a special case, because after the last element we have to wrap round to the start again. 
So the final difference is between the last and first elements in the array.*/


function calculateDifferences()
var createArray = new Array (numberArray.length);
{
createArray = 0;
for (var c = 0; c < numberArray.length; c = c + 1)
{
    createArray = difference(numberArray[c]);
}
{
return createArray
}
}
3
  • 1
    Are you sure it's not Javascript that you're doing? Commented Feb 18, 2010 at 12:01
  • That isn't Java...might want to make sure you have the language right. Also, proper indentation makes to world a better place so please do it Commented Feb 18, 2010 at 12:06
  • Yes it is homework as I stated in the above. I also stated that I'm not looking for someone to give me the answer I just need pointing in the right direction and told where I am going wrong. My books all say that this is Javascript so I am assuming that it is. I take on board you comment about indentations. I do use them in notepad but it didn't copy and paste over to this very well but should have taken some time to sort it out. Commented Feb 18, 2010 at 12:19

2 Answers 2

1

your implementation of function "calculateDifferences" is not correct.
this function should look like this:

function calculateDifferences()
{
var createArray = new Array (numberArray.length);
for (var c = 0; c < numberArray.length - 1 ; c = c + 1) {
/*
because of the function "difference" has two parameters (firstNumber, secondNumber) in its declaration, we should give two arguments. (that are adjacent elements in array)
*/
createArray[c] = difference(numberArray[c],numberArray[c+1]); }
/ *
calculating difference of first and last element of array and assign it to returning array's last element.
*/
createArray[numberArray.length - 1] = difference(numberArray[0],numberArray[numberArray.length - 1]);
return createArray;
}

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

1 Comment

Thank you so much for explaining that to me. I've had no idea where I have been going wrong but the way you have explained it now makes sense to me. I take it the -1 is to tell it to loop until the end of the array?
0

You should index createArray the same way you already do with numberArray[c].

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.