10 Replies Latest reply: Aug 6, 2007 3:15 AM by 807605

Incrementing an array variable!!!

Hello,

I wanna know if we can increment an array variable by more than 1.
When I use for(i=0;i<10;i+4) it is giving me an error.Just wanted to know if we can increment an array variable by any value other than 1.

Thanks
• 1. Re: Incrementing an array variable!!!
``for(i = 0; i < 10; i += 4)``
• 2. Re: Incrementing an array variable!!!
``for(i = 0; i < 10; i += 4)``
• 3. Re: Incrementing an array variable!!!
very impressive Mayank. We now know that you can copy pbrockway.
• 4. Re: Incrementing an array variable!!!
very impressive Mayank. We now know that you can copy pbrockway.

Please be sending me the dukem thingummies now. Vely goodly. ;-)
• 5. Re: Incrementing an array variable!!!
Thank you gents :-)

Message was edited by:
Mayank_03
• 6. Can any body solve all these three problem
Hello Guys,

I am new in java and don't have sufficient knowledge. I really appriciate your any help to solve these three problem.

here are the problem:

1. An array with an odd number of elements is said to be centered is if all elements (except the middle one) are strictly greater than the value of the middle element.

Note that only arrays with an odd number of elements have a middle element (a[a.length/2]).

Write a function named isCentered that accepts an integer array and returns 1 if it is a centered array, otherwise it returns 0.

the function signature int isCentered (int[]a)

Examples

if the input array is
{1,2,3,4,5} 0(the middle element 3 is not strictly less than all other elements)
{3,2,1,4,5} 1(the middle element 1 is strictly less than all other elements)
{3,2,1,4,1} 0(the middle element 1 is strictly less than all other elements)
{3,2,1,14,6} 0(no middle element since array has even number of elements)
{} 0(no middle element)
{1} 1(satisfies the condition vacuously)

2. An array is said to be dual if it has even number of elements and each pair of consecutive even and odd elements sum to the same value. write a function named isDual that accepts an array of integers and returned 1 if the array is dual, otherwise it returns 0.

the function signature int isDual (int[]a)

Examples:

If the input array is:
{1,2,3,0} 1(because 1+2 = =3+0= =3)
{1,2,2,1,3,0} 1(because 1+2 = =2+1= =3+0= =3)
{1,1,2,2} 0(because 1+1 = =2 != 2+2)
{1,2,1} 0(because array does not have any even number of elements)
{} 1

3. An array is defined to be odd-heavy if it contains at least one odd element and every element and every element whose value is odd is grater than every even-valued element. So{11,4,9,2,8} is odd-heavy because the two odd element (11 and 9) are greater then all the even elements. And {11,4,9,2,3,10}
is not odd-heavy because the even element 10 is greater then the odd element 9.

Write a function called isOddHeavr that accepts an integers array and returns 1 if the array is odd-heavy; otherwise it returns 0.

the function signature is int isOddheavy(int[]a)

Some other example:

if the input array is isOddHeavy should return
{1} 1(true vacuously)
{2} 0(contains no odd elements)
{1,1,1,1,1,1} 1
{2,4,6,8,11} 1
{-2,-4,-6,-8,-11} 0
• 7. Re: Can any body solve all these three problem
So you are new, well you need to learn a few things.

2. This is not www.domyhomeworkforme.com. Don't paste you assignment and then expect someone to do it for you. Write code and paste it here (use code tags) so we can see you have made an attempt and then ask a specific question.
• 8. Re: Can any body solve all these three problem
Ok sorry guys for pasting the assignment. I have done some thing but me not sure weather I am doing right way or not. here is my code

Code for Q. 1)

import java.io.*;

public class isCenteredArray{

public static void main(String args[]){

isCentered(new int[]{1, 2, 3, 4, 5});

isCentered(new int[]{3, 2, 1, 4, 5});

isCentered(new int[]{3, 2, 1, 4, 1});

isCentered(new int[]{3, 2, 1, 1, 4, 6});

isCentered(new int[]{});

isCentered(new int[]{1});

}
static int isCentered(int[] a){

int ar=0;

if(a.length%2 ==0){

return ar;
}
int j=1;
for (int i=0; i<a.length; i++){

if(j != a.length/2){

if(a[i] > a[a.length/2]){

ar = 1;

}else{

ar = 0;

return ar;

}

}
j=j+1;     }

return ar;

}

}

Code for Q. 2)

import java.io.*;

public class isDualArray{

public static void main(String args[]){

isDual(new int[]{1, 2, 3, 0});

isDual(new int[]{1, 2, 2, 1, 3, 0});

isDual(new int[]{1, 1, 2, 2});

isDual(new int[]{1, 2, 1});

isDual(new int[]{});

}
static int isDual(int[] a){

int ar=0;

for(int i=0; i<=a.length; i++){

if(i%2 !=0){

if((a[i-1] + a) == a[i+1]){

ar = 1;

}else{

ar = 0;

return ar;
}
}

}

return ar;

}

}

Code for Q. 3)

import java.io.*;

public class idOddHeavyArray{

public static void main(String args[]){

isOddHeavy(new int[]{1});

isOddHeavy(new int[]{2});

isOddHeavy(new int[]{1, 1, 1, 1, 1, 1});

isOddHeavy(new int[]{2, 4, 6, 8, 11});

isOddHeavy(new int[]{-2, -4, -6, -8, -11});

}
static int isOddHeavy(int[] a){

int ar=0;

int oddheavynumber=0;

for (int i=0; i<a.length; i++){

if(a[i]>oddheavynumber){
oddheavynumber = a[i];

}else{
oddheavynumber=oddheavynumber;

}

}
if(oddheavynumber%2==0){
ar=0;
}else{

ar=1;
}
System.out.println(ar);

return ar;

}

}

Please check the way out how I did and let me know if I am going the wrong way...

Thanks
• 9. Re: Can any body solve all these three problem
Hi,

as flounder asked, please use code tags. Thatis, insert your indented code (copied and pasted from your Java editor) between the tags [ code ] and [ /code ] -- you can obtain them by clicking the code button above.

If you don't do so, code is not-indented and therefore difficult to read; furthermore, pieces like array indexes [ i ] are parsed as style formatting and disappear from your code (notice that in fact your code goes italic in some point).
• 10. Re: Can any body solve all these three problem
Your are right, I see that thank you

Ok sorry guys for pasting the assignment. I have done some thing but me not sure weather I am doing right way or not. here is my code. And by the way how to return the integer to the main function "public static void main(String args[]){" from static integer function "static int isCentered(int[] a){"

Code for Q. 1)

Out put:
{1,2,3,4,5} => 0(the middle element 3 is not strictly less than all other elements)
{3,2,1,4,5} => 1(the middle element 1 is strictly less than all other elements)=>
{3,2,1,4,1} => 0(the middle element 1 is strictly less than all other elements)
{3,2,1,14,6} => 0(no middle element since array has even number of elements)
{} => 0(no middle element)
{1} => 1(satisfies the condition vacuously)

``````import java.io.*;

public class isCenteredArray{

public static void main(String args[]){

isCentered(new int[]{1, 2, 3, 4, 5});

isCentered(new int[]{3, 2, 1, 4, 5});

isCentered(new int[]{3, 2, 1, 4, 1});

isCentered(new int[]{3, 2, 1, 1, 4, 6});

isCentered(new int[]{});

isCentered(new int[]{1});

}
static int isCentered(int[] a){

int ar=0;

if(a.length%2 ==0){

return ar;
}
int j=1;
for (int i=0; i<a.length; i++){

if(j != a.length/2){

if(a[i] > a[a.length/2]){

ar = 1;

}else{

ar = 0;

return ar;

}

}
j=j+1;     }

return ar;

}

}``````
Code for Q. 2)

Out put:

{1,2,3,0} => 1(because 1+2 = =3+0= =3)
{1,2,2,1,3,0} => 1(because 1+2 = =2+1= =3+0= =3)
{1,1,2,2} => 0(because 1+1 = =2 != 2+2)
{1,2,1} => 0(because array does not have any even number of elements)
{} => 1

``````import java.io.*;

public class isDualArray{

public static void main(String args[]){

isDual(new int[]{1, 2, 3, 0});

isDual(new int[]{1, 2, 2, 1, 3, 0});

isDual(new int[]{1, 1, 2, 2});

isDual(new int[]{1, 2, 1});

isDual(new int[]{});

}
static int isDual(int[] a){

int ar=0;

for(int i=0; i<=a.length; i++){

if(i%2 !=0){

if((a[i-1] + a) == a[i+1]){

ar = 1;

}else{

ar = 0;

return ar;
}
}

}

return ar;

}

}

Code for Q. 3)

Out put:

{1} => 1(true vacuously)
{2} => 0(contains no odd elements)
{1,1,1,1,1,1} => 1
{2,4,6,8,11} => 1
{-2,-4,-6,-8,-11} => 0

import java.io.*;

public class idOddHeavyArray{

public static void main(String args[]){

isOddHeavy(new int[]{1});

isOddHeavy(new int[]{2});

isOddHeavy(new int[]{1, 1, 1, 1, 1, 1});

isOddHeavy(new int[]{2, 4, 6, 8, 11});

isOddHeavy(new int[]{-2, -4, -6, -8, -11});

}
static int isOddHeavy(int[] a){

int ar=0;

int oddheavynumber=0;

for (int i=0; i<a.length; i++){

if(a>oddheavynumber){
oddheavynumber = a[i];

}else{
oddheavynumber=oddheavynumber;

}

}
if(oddheavynumber%2==0){
ar=0;
}else{

ar=1;
}
System.out.println(ar);

return ar;

}

}

Please check the way out how I did and let me know if I am going the wrong way...

Thanks,

Indoman ``````