Re-arranging array - java

I have an assignment they ask me to re-arrange an array from the even numbers to the odd numbers
like this:
Sample Output:
Please enter array of 5 integers:
1 2 5 6 4
the array after re-arranging:
2 6 4 1 5
I couldn't do it " I cant use methods" can anyone help me ?
this is my code:
public static void evenOdd(int[] arr){
int i=0;
int arr1[] = new int[5];
for (i=0;i<5;i++)
{
if (arr[i]%2==0)
arr1[i]=arr[i];
}
for (i=0;i<5;i++){
if(arr[i]%2!=0)
arr1[i]=arr[i];
}//end for
for(i=0;i<5;i++)
System.out.print(arr1[i]+" ");
System.out.println("");
}//end method
THANK YOU

The problem is when you add them into the new array you are putting them in the same position: i. Use a separate int to keep count of what part of the index you are on.
public static void evenOdd(int[] arr){
int i=0;
int count = 0;
int arr1[] = new int[5];
for (i=0;i<5;i++) {
if (arr[i]%2==0) {
arr1[count]=arr[i];
count++;
}
}
for (i=0;i<5;i++){
if(arr[i]%2!=0) {
arr1[count]=arr[i];
count++;
}
}//end for
for(i=0; i < 5; i++) {
System.out.print(arr1[i]+"\n");
}
}//end method

Related

Print out the numbers from the for loop

The Return type is void
No input parameters
Print out the numbers calculated results separated by a space using current number add the next number from 0 to (a+b).
An example would be if the numbers for the for loop are 0,1,2,3,4,5,6 then it would add 0+1, 1+2, 2+3, 3+4, 4+5, 5+6 and print out those values just like 0,1,2,3,4,5,6.
I honestly have no clue how to do this so I'm not going to lie about it so can someone help me code it and explain or just help me with it.
public class ForFogMe
{
public int a, b;
public String str;
public void addUp(){
for(a = 0; a <= 6; a ++){
System.out.print(a);
}
String s = Integer.toString(a);
System.out.println();
System.out.print(s.substring(0,2) );
}
public static void main(String args[]){
ForFogMe me = new ForFogMe();
me.addUp();
}
}
If you only want to print sum of the numbers from 0 to 6 you would do it simply like this:
public void addUp() {
for(a = 0; a < 6; a++) {
System.out.print(a+(a+1) + ",");
}
System.out.print("\b"); // to delete last comma
}
In first iteration a is 0 a+1 is 1 so you print their sum like (a+(a+1) + ",") which outputs "1,". It repeats until it reaches 6. At the end we have 1,3,5,7,9,11, so I used System.out.print("\b"); to delete last char, so we get 1,3,5,7,9,11
I believe this should do the trick:
public static void addUp(){
final int[] array = {0,1,2,3,4,5,6};
int[] result = new int[array.length-1];
for(int i = 0; i < array.length-1; i++) {
result[i]=array[i]+array[i+1];
}
result[3]=array[array.length-1];
for(int i = 0; i < result.length; i++) {
System.out.print(result[i]+" ");
}
}
Test case (array):
0,1,2,3,4,5,6
Outputs:
1 3 5 6 9 11
Note: The array size does not matter.

How to manipulate the array in Java?

Starting with a 1-indexed array of zeros and a list of operations, for each operation, I have to add value to each of the array elements between two given indices, inclusive. Once all operations have been performed, I have to return the maximum value in the array. But the wrong answer is coming every time for most test cases and for some test cases time limit is exceeding. Kindly help me to solve this problem.
I am using arrayManipulation function to get the number of array elements and queries array. And update function is used to update(add the element) the array.
import java.util.*;
import java.util.Arrays;
public class sample
{
public static void main(String args[])
{
int maximum=0;
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
int m=sc.nextInt();
int queries[][]=new int[m][3];
for(int x=0;x<m;x++)
{
for(int y=0;y<3;y++)
{
queries[x][y]=sc.nextInt();
}
}
maximum=arrayManipulation(n,queries);
System.out.println(maximum);
}
public static int arrayManipulation(int num,int qry[][])
{
int a=0,b=0,k=0;
int max=Integer.MIN_VALUE;
int arr[]=new int[num];
int arr2[]=new int[num];
for(int i=0;i<num;i++)
{
arr[i]=0;
}
for(int j=0;j<qry.length;j++)
{
for(int kl=0;kl<qry[0].length;kl++)
{
a=qry[j][kl];
b=qry[j][kl+1];
k=qry[j][kl+2];
break;
}
arr2=update(a,b,k,arr);
int lengtharr2=arr2.length;
max=Math.max(max,arr2[lengtharr2-1]);
}
return max;
}
public static int[] update(int a1,int b1, int k1,int array[])
{
for(int i=a1;i<b1;i++)
{
array[i]+=k1;
}
Arrays.sort(array);
return array;
}
}
Input: 10 means the number of array elements and 3 is no. of queries which consist of a,b,k values which mean like this: the left index, right index, and summand
10 3
1 5 3
4 8 7
6 9 1
My output:
11
expected output:
10
In this for loop:
for(int kl=0;k<qry[0].length;k++)
{
a=qry[j][kl];
b=qry[j][kl+1];
k=qry[j][kl+2];
break;
}
Why are you using two different variables (k and kl) in your loop condition?
Regardless, you should use a variable other thank k for the loop, as you are updating the value of k and then executing k++ at each iteration of your loop, which is very likely to mess up your output.
Consider something like:
for(int l=0;l<qry[0].length;l++)
{
a=qry[j][kl];
b=qry[j][kl+1];
k=qry[j][kl+2];
break;
}

Output the array so that 10 elements per line are printed

I'm very new to java (like a month old). This week we have a programming problem that I'm having difficulty with. We are asked to write a program that declares an array "alpha" of 50 elements of type "double". Initialize the array so that the first 25 elements are equal to the square of the index variable and the last 25 elements are equal to three times the index variable. Output the array so that 10 elements per line are printed. I've gotten the output right so far, but it's still only printing 1 value per line, here's what I have so far, what am I doing wrong? Any help is super appreciated!!
import java.util.*;
public class progprblm5{
public static void main(String[] args){
double alpha[] = new double[50];
for(int i =0;i<25;i++)
{alpha[i]= i*i;}
for(int i = 25;i<50;i++)
{alpha[i]= i*i*i;}
System.out.println( "The values are: ");
for(int i=0;i<50;i++)
System.out.println(alpha[i]);
}
void print(double array[])
{
for(int i=1; i <= array.length; i++)
{
System.out.print(array[i+1]+ " , ");
if(i%10==0)
System.out.print("\n");
}
}
}
You have not called print method from main method. One more mistake is in your code that, you mentioned about 3 times of index variable and in your code you are taking cube of index variable.
public class progprblm5{
public static void main(String []args){
double alpha[] = new double[50];
for(int i =0;i<25;i++){
alpha[i]= i*i;
}
for(int i = 25;i<50;i++){
alpha[i]= 3*i; // 3 times of index
}
System.out.println( "The values are: ");
new progprblm5().print(alpha); // method call
}
void print(double array[]){
for(int i=0; i < array.length; i++){ //iterate array from 0 index
System.out.print(array[i]+ " , "); // print ith element
if(i%10==0){
System.out.println();
}
}
}
}

Ascending Order Bubble sort java.lang.ArrayIndexOutOfBoundsException: 5 at wst2.calc(wst2.java:18)

This is the program that I have made and I even did a proper for loop starting from 0 to 4 as 5 numbers need to be sorted , Still getting arrayindexoutofboundsexception.
import java.util.*; class wst2 {
public static void calc()
{
Scanner sc= new Scanner(System.in);
int i,j , temp;
int a[]=new int [5];
for(i=0;i<=4;i++)
{
System.out.println("Enter 5 numbers");
a[i]=sc.nextInt();
}
for(i=0;i<=4;i++)
{
for(j=i;j<=4-i;j++)
{
if(a[j] > a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
for(i=0;i<=4;i++)
{
System.out.println(a[i]);
}
} }
This statement int a[]=new int [5]; defines an array with length of 5.
With an array of length 5, you can access elements with indices form 0 to 4 ({0, 1, 2, 3, 4}).
But at this line
a[j]=a[j+1];
in a possible and particular condition (j = 4 and i = 0), you are trying to access to the a[4+1] (means a[5]) which is not allowed.

sorting an array and counting its elements

I tried to sort an array accending and count the array elements
please help me find whats missing, have debugged many times. here is my code and the output i got. Thanks
package habeeb;
import java.util.*;
public class Habeeb {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int[] num = new int[30];
int i, count=0;
System.out.println("Enter the integers between 1 and 100" );
for( i=0; i<num.length; i++){
num[i]= input.nextInt();
if(num[i]==0)
break;
count++;
}
calling the function here
Sorting(num, i, count);
}
public static void Sorting(int[] sort, int a, int con){
if (a<0) return;
/*am sorting the array here*/
Arrays.sort(sort);
int j, count=0;
for(j=0; j<con; j++){
if(sort[a]==sort[j])
count++;
}
System.out.println(sort[a]+" occurs "+count+" times");
Sorting(sort, a-1, con);
}
}
Here is the output:
run:
Enter the integers between 1 and 100
2
5
4
8
1
6
0
0 occurs 6 times
0 occurs 6 times
0 occurs 6 times
0 occurs 6 times
0 occurs 6 times
0 occurs 6 times
0 occurs 6 times
Your problem is that the array is of size 30 and when you sort it you have all the values you did not assign to equal to 0 and thus they go in the front of the sorted array. Later on out of the first 6 numbers all are 0 so the output you have is correct.
To aviod the problem you face I suggest you use ArrayList instead of simple array so that you can add elements dynamically to it.
Try this
The counting method
public int count(int[] values, int value)
{
int count = 0;
for (int current : values)
{
if (current == value)
count++;
}
return count;
}
Then use
int[] sorted = Arrays.sort(num);
for (int value : sorted)
{
System.out.println("" + value + " occurs " + count(sorted, value) + " times");
}
This works for sure.
You are doing a bit more work than is necessary. I would solve this like so:
Map<Integer, Integer> countMap = new HashMap<Integer,Integer>();
for( i=0; i<num.length; i++){
int current = input.nextInt();
if(countMap.get(current) != null)
{
int incrementMe = countMap.get(current);
countMap.put(current,++incrementMe);
}
else
{
countMap.put(input.nextInt(),1);
}
}

Categories

Resources