Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 6 years ago.
Improve this question
I wanted to see better code/logic of the below questions from you guys.
Que: Say, there are two arrays - Input and Output array. Taking 4 elements in input array. O/p array will also have length of same as Input array. Output should have - first index value should product of all values from the rest of indexes except first. Similarly, second index value should have product of all other values from the rest of indexes except second. Likewise, the product should go till output array get the same length as that of input's. I have written code but I wanted to see different answers from different viewers. More customized and use of library functions is helpful. Customizing my code also is welcome.
System.out.println("Enter the size of the array..");
scan1 = new Scanner(System.in);
int n = scan1.nextInt();
int[] arr = new int[n];
System.out.println("Enter the array elements..");
for (int i = 0; i<n; i++)
arr[i] = scan1.nextInt();
System.out.println("Array is.." +Arrays.toString(arr));
int[] arr1 = new int[arr.length];
for (int i = 0;i<arr.length;i++){
int sum = 1, j = 0;
while (j<arr.length){
if(i==j)
System.out.println("I and J are equal");
else
sum = sum * arr[j];
j++;
}arr1[i] = sum;
}
System.out.println(Arrays.toString(arr1));`
Related
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 2 years ago.
Improve this question
I would like to write a program that reads an array of integers and finds the minimum value.
The first line contains the size of an array.
Output
An integer number representing the minimum in the input array.
Input:
5
5 1 4 2 3
My code:
import java.util.Arrays;
import java.util.Scanner;
class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int a = sc.nextInt();
int[] c = new int[a ];
for (int i = 0; i < c.length; i++) {
c[i] = sc.nextInt();
}
Arrays.sort(c);
int b= c[0] > 0 ? c[0]: c[c.length -1];
for (int i =1; i < c.length - 1; i++){
if(c[i] < 0){
if(c[i] > b){
b = c[i];
}
}
else{
if (c[i] < b){
b= c[i];
}
}
}
System.out.println(b);
}
}
My output:
3
-1 -2 -3
What is wrong with my code?
You are making it more complicated than necessary. No need to sort the array. Just set a variable to be equal to the first value in the array. Then scan through the array setting the variable to the actual array element if it is less than the present variable value.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I have a loop that displays 10 values: 1.0, 1.1, 1.2, etc.:
int i = 0;
int x = 10;
for(i;i>x;i++)
{
System.out.println(x);
}
but instead of displaying the values, I want to put them in an array. How do I do that?
How about:
// You want x ints.
int x = 10;
// Make an array big enough to hold x ints.
int[] array = new int[x];
// Loop x times.
for(int i = 0; i < x; i++) {
// Put the next number into the array.
array[i] = i;
}
first your way in writing for loop is need to be more clean
it should :
for(int i=0; i > x; i++){
System.out.println(x);
}
second your boolean condition in for loop isn't true because x=10 is always bigger than i=0 so it won't print any thing.
third to put the values in array :
simply define array : int[] numbers = new int[size of array];
then put each value inside the index i of array :
numbers[i] = i;
finally for loop will be like:
for(int i=0; i < x; i++){
numbers[i] = i;
}
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
Write pseudocode to Put Even & Odd Elements of an Array in 2 Separate Arrays
import java.util.Scanner;
public class InsertElementInArray
{
public static void main(String[] args)
{
int n, pos, x;
Scanner s = new Scanner(System.in);
System.out.print("Enter no. of elements you want in array:");
n = s.nextInt();
int a[] = new int[n+1];
System.out.println("Enter all the elements:");
for(int i = 0; i < n; i++)
{
a[i] = s.nextInt();
}
System.out.print("Enter the position where you want to insert element:");
pos = s.nextInt();
System.out.print("Enter the element you want to insert:");
x = s.nextInt();
for(int i = (n-1); i >= (pos-1); i--)
{
a[i+1] = a[i];
}
a[pos-1] = x;
System.out.print("After inserting:");
for(int i = 0; i < n; i++)
{
System.out.print(a[i]+",");
}
System.out.print(a[n]);
}
}
Code and pseudocode both are heading in same way, but the main difference is the second one is much easier to write and understand for humans.
Let's take an example: We have a function that takes an string array as an argument and performs some complicated operations like searching for some specified char chains or looking for a pattern with regex.
It can looks very simply in pseudocode:
function doLotsOfStuff(String array):
variable patternApperance
for each string in array:
if (string has "PATTERN"):
increment patternApperance
return patternApperance
The thing about pseudocode is that it doesn't have any specific way or convention of writing. It's something where you don't have to care if it is going to compile, parse etc. but only care about that if others understand your pseudocode. In short words pseudocode isnt made for computers, it's for humans to better understand what a piece of code is going to do.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 9 years ago.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Improve this question
array[0] = 1;
array[1] = 2;
array[2] = 3;
array[3] = 4;
items_arr = 4;
System.out.println("The elements in the array are: ");
for (int x = 0; x < items_arr; x++)
System.out.println("Array[" + x + "]=" + array[x]);
System.out.print("\n");
Scanner insert = new Scanner(System.in);
System.out.print("Enter an Element to Insert: ");
int input = insert.nextInt();
for (s = 0; s < items_arr; s++)
if (array[s] == input)
break;
items_arr++;
for (s = 0; s < items_arr; s++)
System.out.println("Array[" + s + "]=" + array[s]);
break;
The output is. The elements are
Array [0]= 1
Array [1]= 2
Array [2]= 3
Array [3]= 4
Enter an element to Insert: 5
Array [0]= 1
Array [1]= 2
Array [2]= 3
Array [3]= 4
Array [4]= 0
when I insert 5 it posts 0
any suggestions please.. thanks!
To insert in to the array you shuould be doing follwoing operation
array[s]=input
Two notes here
Arrays are fixed length, and you should be checking the array length before inserting values in to that,other wise you will get ArrayIndexOBException. Safer to sue List/Set
As better coding practise, and to improve the readablity, you should be enclosing the conditional/loop statements (such as if or for) - see eg below
eg: 1
for (int x = 0;x<items_arr;x++) {
System.out.println("Array["+x+"]="+array[x]);
}
eg 2:
for(int s = 0; s < items_arr; s++) {
if (array[s] == input) {
break;
}
}
You have not inserted 5 in your array,
do something after items_arr++
array[ items_arr] = input;
If you do not insert any thing then by default every element is 0
You should be using a Collection type; I would recommend an ArrayList - that is -
List<Integer> al = new ArrayList<Integer>();
for (int i = 1; i < 5; i++) {
al.add(i);
}
Scanner insert = new Scanner(System.in);
System.out.print("Enter an Element to Insert: ");
int input = insert.nextInt();
al.add(input); // And so on...
You are not updating/inserting the array with the new input.
for(s = 0; s < items_arr; s++)
if (array[s] == input)
break;
items_arr++;
just replace the above code with
array[ items_arr] = input;
items_arr++;
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Closed 9 years ago.
Improve this question
I'm confused about how to use Arrays.sort in Java.
In my Java code, I'm trying to sort the top 10 files in my directory, and so I'm going to use Arrays.sort method.
First I create the array to hold the file lengths:
int[] sortedArray = new int[11];
later on in my code, after I load numbers into sortedArray , I do this..
Arrays.sort(sortedArray);
but for some reason, it doesn't like that . I get this error:
TopTen.java:46: error: <identifier> expected
Arrays.sort(sortedArray);
Here is the rest of my code
File dir = new File("C:\\Users\\Code\\Desktop\\Work\\Oracle_Training\\Java_training\\Java_Challenge_Disk_10_files");
for(File child : dir.listFiles()){
System.out.print(child.getName() + " " + child.length() + " , ");
//puting into sortedArrya
for (int i = 0; i<11; i++){
sortedArray[i] = (int)child.length();
continue;
}
}
int[] array = new int[10];
Random rand = new Random();
for (int i = 0; i < array.length; i++)
array[i] = rand.nextInt(100) + 1;
System.out.println(Arrays.toString(array));
}
Arrays.sort(sortedArray);
Am i using Arrays.sort correctlY? thank you
You are calling Arrays.sort() correctly, but you forgot an opening brace after the start of the second for loop. In other words, change the last for loop to this:
for (int i = 0; i < array.length; i++) {
array[i] = rand.nextInt(100) + 1;
System.out.println(Arrays.toString(array));
}
To prevent this from happening again, you can get into the habit of adding the braces and not taking them out even if there is only one statement in the for loop.