Ques 1 : how to do?
Ans: The following function will do the work for you
int[] evenPositions(int[] nums)
{
int i,j,len;
if(nums.length%2==0)
{
len=nums.length/2;
}
else
{
len =
(nums.length+1)/2;
}
int[] newarray = new
int[len];
for(i=0,j=0;i<nums.length;i+=2,++j)
{
newarray[j]=nums[i];
}
return newarray;
}
------
To solve your question you first need to know the length of your array to be returned. For that first check if the length of given array is even or odd. In case it is even then the length of newarray should be length/2 else length of newarray should be (length+1)/2.
A loop is started to copy the values. Here we skip odd values of given array and copy the rest.
Then newarray is returned.
output
This is the output I got when my main function was
public static void
main(String[] args)
{
int[] array1 =
{8,1,7,4,6,10,5};
System.out.println("Initial
Array");
for(int
i=0;i<array1.length;++i)
{
System.out.print(array1[i]+" ");
}
int[] result =
evenPositions(array1);
System.out.println("\nResult");
for(int
i=0;i<result.length;++i)
{
System.out.print(result[i]+" ");
}
}
Question 2 : What is wrong with the code given?
Answer : I found 2 wrong things in your code
First, the line int[] newArray = new int[nums.length];
When this line was written there was no check on whether nums.length is odd or even. This is not the main problem.
Second, This one is the main problem. In the code you have written newArray[i] = nums[i] which is wrong as we are not copying at odd positions in newArray. So, for example nums is {1,2,3,4,5,6}.
newArray[0] = nums[0] = 1
newArray[2] = nums[2] = 3
Here newArray[1] is not given any value and loops move forward and tries newArray[4] = nums[4] = 5 which is also an error as we have exceeded the size of array newArray.
Two questions, 1) how do I do this, 2) what is wrong with my code norm...
How do I separate the method into different class? I try separate the testing and method class into 2 different class. And when I test it, it said that the method is undefined. And it ask me to create the method in the main class. I don't know what is the problem. This is the access to the code https://repl.it/@Teptaikorn/test Thank you very much MazeSolver.java MazeTerster.... TwoDim AutoA... testfile.txt Main.java x > 0 N Binary TreeN... X LinkedBinary... Maze.java 1...
What is wrong with my code, when I pass in 4 It will not run, without the 4 it will run, but throw and error. I am getting the error required: no arguments found: int reason: actual and formal argument lists differ in length where T is a type-variable: T extends Object declared in class LinkedDropOutStack public class Help { /** * Program entry point for drop-out stack testing. * @param args Argument list. */ public static void main(String[] args)...
I need help with these java codingbat questions please: Create a method that creates a user id, where the user id is made up of the first letter of the first name and the entire last name. e.g. john smith ->jsmith getUserld("bill", "gates")-"bgates" getUserId("steve", "jobs")"sjobs" getUserId("larry", "page") -"lpage" Go Save, Compile, Run (ctrl-enter) public String getUserId( String firstName, String lastName) Given a number n, create an array with elements starting from 1 to n. e.g. 5 > getNumArray(1) [1] getNumArray...
My code is not doing what I want it to do and I can't seem to figure out why it's not working. I commented some of the code to give some context to what I'm trying to do. Can someone check to see where my code is off. I'm assuming that I didn't use the counters properly or I'm using the pointers incorrectly. I can only use pointers to do this by the way. Code: #include <iostream> using namespace std;...
What am i doing wrong? can some take a look and tell me what should i change? in which line and how to fix it? Exercise 71130X WORK AREA RESULTS l import java.util.scanner; 3 class Numbersearch 5 public static void main (String args) 7 scanner scnew scanner (System.in); 9 System.out.println"Enter length of array 10 11 int size - sc.nextInt() 12 13 int arrnew intisize]; 14 15 for (int i -0; i < size; i+t) 17 System.out.println("Enter number for array index"+i:")i...
Programming C....... function code is clear but compile is wrong .. I input 8 and compiled 2 0 1 1 2 3 5 8 13.. look first number 2 is wrong. The correct answer 8 is 0 1 1 2 3 5 8 13 21.. Need fix code to compile correctly.. Here code.c --------------------------------------------------------------------------------------------------------------------- #include <stdio.h> #include <math.h> int fibonacciIterative( int n ) { int fib[1000]; int i; fib[ 0 ] = 0; fib[ 1 ] = 1; for (...
What's wrong with my code? : I'm trying to use recursive functions to display and count all the even numbers of an array. However, I can't seem get the program output the number of even integers . Here's the output I want: Here are the 5 even numbers: // Luckily, however, my code does output all the even numbers. But, I also want the program to tell me how may even integers there are. 2 8 14 18 22 MY...
this is part of my code im not sure how to do last 2 function. i think above last 2 functions need for making function please help me how to do it. #include <stdio.h> #include <stdlib.h> #include <stdbool.h> #include "list.h" typedef struct node { ElemType val; struct node *next; } NODE; struct list_struct { NODE *front; NODE *back; }; int lst_remove_first(LIST *l, ElemType x) { NODE *p; NODE *tmp; if(l->front == NULL) return 0; if(l->front->val == x) { lst_pop_front(l);...
Please answer these two questions with explanations. I do not know why my answers are wrong. Incorrect Question 10 0 13 pts Which of the following lines of Java calls a method called myMethod that is public and static, returns a 1D integer array, and passes a 2D array of double as a parameter: int] iArray myMethod(dArrayD0) int iArray-myMethod(dArray) int iArray myMethod(doublel0 dArray) nt iArray myMethod(dArray); nt iArray- myMethod(dArray[O]IO);
How do I add the space that this Zybooks code keeps asking me to add? 5.13 LAB: Output numbers in reverse Write a program that reads a list of integers, and outputs those integers in reverse. The input begins with an integer indicating the number of integers that follow. For coding simplicity, follow each output integer by a space, including the last one. Assume that the list will always contain fewer than 20 integers. Ex: If the input is: 5...