Show numbers not incluided in an Array [closed] - java

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 2 years ago.
Improve this question
I've an issue that I couldn't fix, I tried different ways. I filled an Array with ok 100 positions with randoom numbers (Range: 1-100). Then I've to show by console the numers that aren't incluides in the array. I try two loopes nested but it didn't work neither.
int arraySize = 100, aux=0;
int numeros [] = new int [arraySize];
for (int i = 0; i < numeros.length; i++) {
numeros[i] = (int) ((Math.random()*100)+1);
Here, Somebody know how to get it? Thanks

I'd just stream all the numbers and check which ones aren't included in the array. Note that in order to get better performance it may be useful to convert the array to a Set, but it isn't strictly required:
Set<Integer> existing = Arrays.stream(numeros).boxed().collect(Collectors.toSet());
IntStream.rangeClosed(1, 100)
.boxed()
.filter(i -> !existing.contains(i))
.forEach(System.out::println); // or collect them to use later

Instead of nested for loops, for better performance, try one of the following options:
Sort the array using Arrays.sort(numeros), then iterate and print missing values whenever a gap is detected.
Add all the numbers to a HashSet<Integer>, then iterate 1-100 and print any value not found in the Set.
Since your question is obviously an exercise or test, I'll leave the actual code writing to you.

Well I could fix it with an counter, It was easier more than i think, but in stressfull peaks we don't think in a right way. I'll let the Code below.
int numeros [] = new int [arraySize];
for (int i = 0; i < numeros.length; i++) {
numeros[i] = (int) ((Math.random()*100)+1);
}
//Loop to show not numbers in the array
for (int i = 0; i < numeros.length; i++) {
for (int j = 0; j < numeros.length; j++) {
if (i==numeros[j]) {
cont++;
}
}
if (cont==0) {
System.out.println(i);
}
cont=0;
}

Related

How can i count how many times the keywords user enter is the used in the essay. its doesn't check on the entire keyword [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
public static int keywordsChecker(String essay,String key) {
int count = 1;
String[] k=key.split(",");
for (int i = 0; i < k.length-1; i++) {
if (essay.contains(k[i])) {
count++;
}
}
return count;
}
To take into account that each keyword searched for may occur more than once, and to count such occurrences, you may use this inside your for loop:
int indexOfOccurrence = essay.indexOf(k[i]);
while (indexOfOccurrence > -1) {
count++;
indexOfOccurrence = essay.indexOf(k[i], indexOfOccurrence + 1);
}
There are a couple of other issues in your code: I believe you need to initialize count to 0 (not 1). And to count also the last keyword in key your for loop should be for (int i = 0; i < k.length; i++) (without subtracting 1 from k.length). If you want, using <= would also work: for (int i = 0; i <= k.length-1; i++), but this is non-standard, so I would not recommend it.

I have a loop that displays 10 values: 1.0, 1.1,1.2, etc. how can I put those values into an array? [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 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;
}

i created 500k Array size and i want fill it with random numbers without duplicate [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
i need to fill this array with 500000 int numbers this my code
public class lab_02 {
public static void main(String[] args) {
// 500000 Array
Array array = new Array (500000);
}
}
i don't know what should i do use loop or Recursion
thank you
1) If you don't need duplicates use a Set
Set<Integer> intSet = new HashSet<Integer>();
2) Then you can add your random generated int add it to the Set
while (intSet.size() < DESIRED_SIZE) {
//generate the randomInteger - DESIRED_SIZE is 500k in your case
intSet.add(randomInteger);
}
The tricky part here is ensuring uniqueness. Checking if an item is unique can be expensive. Since the OP does not state what the bounds on the random numbers are I am assuming min 1 and max 500000 for this solution.
One approach would be to enter the numbers sequentially 0 to 500000 and then shuffle the array.
int size = 500000;
List<Integer> list = new ArrayList<Integer>();
for (int i = 0; i < size; i++) {
list.add(i);
}
Collections.shuffle(list);
for (int i = 0; i < size; i++) {
System.out.println(list.get(i));
}

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.

Difference between prefix and postfix operators in different situations [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 8 years ago.
Improve this question
I have gone through all the questions in this forum that are corresponding to this topic, and I felt to raise a different question as this question is not answered clearly. Here is my scenario:
I have this class Test1:
public class Test1 {
public static void main(String args[]) {
int i=0, j=2;
do {
i= i+1;
j--;
} while(j>0);
System.out.println(i);
}
}
Now this is my question:
a) If I replace the increment operation of 'i= i+1' with ++i, I get the desired output as
1
2
But if I replace the increment operation of 'i= i+1' with i++, I get the desired output as
0
0
I do understand the difference of using prefix and postfix operators in for loop, but why is the value not getting incremented at all in do-while loop?
Using i = i++; will never change i value.
Use just:
i++;
Why? because:
i = i++;
is similar to doing something like this:
temp = i; // temp is 0.
i = i+1; // increment i
i = temp; // assign temp (which is 0) to i.
Take a look at a similar post-increament question and even another one.
++i
This will increment i, then return it.
i++
This will return i, then increment it.
Example
Here is a link to an ideone example of this.
int i = 5;
System.out.println(i++);
// Outputs 5
int j = 5;
System.out.println(++j);
// Outputs 6.
x = i++; is a shortcut for x = i; i = i + 1;
x = ++i; is a shortcut for i = i + 1; x = i;
that's it.
Actually in your sample, there should be no difference between i++ and ++i, if you replace i = i+1 by one of two. Chris noted the difference correctly, I'll just add a sample, where this is important:
int i = 0;
method(i++);
Will call method with the original value of 0 and increment i afterwords. In method, however, you'll see the original value 0.
int i = 0;
method(++i);
Will increment i and then call method. So in method you'll get the value of 1. That does not change the fact that after method, i has the value 1 in both cases.
Others have noted correctly that
i = ++i;
and
i = i++;
are different things actually!

Categories

Resources