I have home work due soon and have written most of the code there are only two parts i am having trouble with:

1.Can anyone tell me how to read the even values of an array, add them and then add the amount of even numbers there were in the array????

2.How can i move the values of the arrays around?

thank you in advance for your help, it is greatly appreciated!!! :cheesy:

11 Years
Discussion Span
Last Post by Lisi

Your first question is not very clear to me, what I understand is you want to find all even numbers in the array and add them together, plus you wish to keep a track of have many even numbers are in the array

int total = 0;
int evenCount = 0;
for(int i = 0; i < arr.length; i++)
     if(arr[i] % 2 == 0)
          total += arr[i];
          evenCount ++;

If I mis-understand please explain in more details

To swap value in array is simple just introduce new variable which will temporary store value from one array position

int temp = arr[i];
arr[i] = arr[i+1];
arr[i+1] = temp;
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.