How to populate int array dynamiclly by a given range? [closed] - java

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
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
Closed 9 years ago.
Improve this question
I would like to know how to populate an array dynamically given a start point and end point ? Assuming it's possible ?
my current way (hard coding ints)
int xValues[] = {340,347,348,349,352,355,359,364,369,376,383,392,400,410,421,431,443,455,467,480,492,505,519,532,545,559,572,585,599,612,625,637,648,658,667,675,681,686,690,693,696,697,698,700};
for (int i = 0; i < xValues.length; i++)
{
x=xValues[i];
Desired way
int xValues[] = Range(340, 700); // if possible unit of increment 1 or 10
Thank you and happy coding.

If you are looking for a code that generates random numbers between the given range, the below code is for you
import java.util.Scanner;
import java.util.Random;
public class Insert {
static Scanner input=new Scanner(System.in);
public static void main(String [] args){
Random rand=new Random();
int max,min;
System.out.println("enter the maximum number");
max=input.nextInt();
System.out.println("enter the minimum number");
min=input.nextInt();
int range=max-min+1;
int arr[]=new int[100];
for(int i=0;i<100;i++){
int random=rand.nextInt(max-min+1)+min;
arr[i]=random;
}
for(int i=0;i<100;i++){
System.out.println(arr[i]);
}
}

Related

How come this loop doesn't print out six numbers? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 months ago.
Improve this question
package p22_09_24;
public class Lotto2 {
public static void main(String[] args) {
for (int i=0; i <6; i++) {
i = (int)(Math.random()*45)+1;
System.out.println(i);
}
}
}
As you can see, the exit is under the 'for' which means it should loop six times, but the answer is only one num.
public class Random {
public static void main(String[] args) {
for (int i = 0; i < 6; i++)
{
int randomNumber = (int) (Math.random()*45)+1;
System.out.println("Random number is: " + randomNumber);
}
}
}
use different name to store the random number.

Reversing an Array: Please check this code out [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
I am struggling to understand why the following code cannot reverse an array.
I have made a copy of the array.
As you can see below, I have used a for loop and started the counter from 0 until the array's length. I set the first element of the original array to be equal with the last. And, the second from the beginning of the array to be equal with the second from the end. And, so on.
But the result gives me back the original array, not the reversed one.
I have pasted the whole code here so you can copy it into your IDE. Please help me figure this out.
Thank you very much in advance.
import java.util.Arrays;
import java.util.Scanner;
public class Main {
private static Scanner input = new Scanner(System.in);
public static void main(String[] args) {
int[] myArray = normal(5);
reverse(myArray);
}
public static int[] normal(int count){
int [] array = new int[count];
for(int i = 0; i < count; i++){
array[i] = input.nextInt();
}
return array;
}
public static void reverse(int[] array){
int[] reverse = Arrays.copyOf(array, array.length);
for(int i = 0; i < array.length; i++){
array[i] = reverse[(array.length - i)-1];
}
System.out.println(Arrays.toString(reverse));
}
}
You are printing reverse-
Replace this line
System.out.println(Arrays.toString(reverse));
with this
System.out.println(Arrays.toString(array));
This line is the culprit
System.out.println(Arrays.toString(reverse));
Replace it with
System.out.println(Arrays.toString(array));
A better way to interpret the result would be to use something similar
public static void main(String[] args) {
int[] myArray = normal(5);
System.out.println("Before reverse");
System.out.println(Arrays.toString(myArray));
reverse(myArray);
System.out.println("After reverse");
System.out.println(Arrays.toString(myArray));
}
and remove the print statements from the recursive loop completely.

What is wrong with this small for/if loop logic? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
This code is asking for how many numbers the user will input and then it takes each of those. In the end it should return the smallest value. I know about Math.min methods, I'm just struggling with why the logic below doesn't work, it always prints the last input instead the smallest one.
import java.util.Scanner;
public class Ch5_smallestValue {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Input how many numbers and then input each one");
int hMany = sc.nextInt();
int firstNum = sc.nextInt();
int smallest = firstNum;
for (int i = hMany; i > 1; i--){
int input = sc.nextInt();
if (smallest < input){
smallest = input;
}
}
System.out.println("smallest = " + smallest);
}
}
Change (smallest < input) to (smallest > input).

pseudocode to Put Even & Odd Elements of an Array in 2 Separate Arrays [closed]

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.

How to go through the deepest elements in an array of arrays? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
public static void main(String[] args) {
int [][] a={{1,2,3},{4,5,6}} ;
for(int x:a)
System.out.print(" "+x);
}
}
Could someone please tell me how would I print each value in the array?
You have to use for each loop twice. In first loop you iterate over all the array and in the inner loop you iterate over all the integers in the outer array.
int [][] a={{1,2,3},{4,5,6}} ;
for(int []x:a){
for(int y : x){
System.out.println(y);
}
}
As you're having a dual-dimension array, you need two loops:
public static void main(String[] args) {
int [][] a={{1,2,3},{4,5,6}} ;
for(int[] r:a) {
for (int x:r) {
System.out.print(" "+x);
}
}
}

Categories

Resources