I have a loop to loop through 0-200 and if the number matches the number in the list. I will put it inside the freq[][]. However, I'm having problem into putting the numbers I found into the freq[][] considering that it needs to be in the size of [10][20].
public static void example(List<Integer> numbers, List<Integer> elements, int[][] list){
int index = 0;
int[][] freq = new int[10][20];
for (int i = 0; i < 200; i++){
for (int x = 0; x < list.length; x++){
for (int y = 0; y < list[x].length; y++){
if (list[x][y] == i){
freq[][index] = i;
}
}
}
}
}
Keep it as
if (list[x][y]== i){
freq[x][y] = i;
}
else {
freq[x][y] = 0; // if not matched
}
So that freq will be a two dimensional array with 10rows and 20 columns .
-Element at 5th index position in the list will be in 0*5th position in the 10*20 array.
-Element at 199 th index position in the list will be in 9*19th position in the 10*20 array.
first, you make a loop to read 200 numbers inside this loop you want to loop 2d array to compare its elements by every number and make condition if a number exist in list but it in freq[][] this code put every number Achieves the condition in freq array and otherwise but 0
for (int i = 0; i <= 200; i++) {
for (int j = 0; j < list.length; j++) {
for (int k = 0; k < list[j].length; k++) {
if(list[j][k]==i)
freq[j][k]=i;
}
}
}
if you use `
else
freq[j][k]=0;
`
that mean it start putting in array the number or 0, and finally, you get an array don't match you want, so let if condition only without else I test it and it work for me
Related
How can I get int a = 400; int b = 100; from 2-dimensional array 1000 x 1000 in Java, for example, mass[400][100] (row 400, column 100)? I found element in array and need numbers of his row/line and column. How can I get this numbers? Thanks.
Are you asking how to get the dimensions of an array?
If a is new int[400][100]; then you can get 400 by doing a.length and 100 by doing a[0].length.
If you need to find the position in the array based on the value, you have no other option but to brute-force loop through the whole array, breaking out when you find the first match:
int[][] massiveArray = new int[1000][1000];
final int valueTofind = 27;
// assign the value to find at position (400, 100)
massiveArray[400][100] = valueTofind;
int i_value = -1;
int j_value = -1;
// find the first occurrance of valueTofind by looping through the array
outer: for (int i = 0; i < massiveArray.length; i++) {
for (int j = 0; j < massiveArray[0].length; j++) {
if (massiveArray[i][j] == valueTofind) {
i_value = i;
j_value = j;
break outer;
}
}
}
System.out.println(String.format("First position for %d is at (%d, %d)",
valueTofind, i_value, j_value));
you can Work Around this .. To get a value in 2d array one way to do is
int[][] a = ...;
for (int r=0; r < a.length; r++) {
for (int c=0; c < a[r].length; c++) {
int value= a[r][c];
}
}
The following method uses some nested loops. I have a 2D array which I have taken row 1 and made a 1D array called keys. I am now iterating across the 2D array and comparing each value from keys to each item in the 2D array starting with row number 2.
So if I have the following 2D array:
1 1
1 0
1 0
upon the first iteration row 1 index 1 will compare and find that the first item in each following row is equal to the first item of row 1 (keys). However, when the next iteration begins since keys has another 1 in position 2 it will again be compared to the 1 in row 2, and this is the type of situation that I want to discriminate. The goal is to find common elements so I can't count the same element twice. I'm trying to convert my array collections to a 2D arraylist but was thinking it might not be necessary. What should be returned from above would be a 1D array with the values {1, null}. Here is the method
public Comparable[] findCommonElements(Comparable[][] collections){
Comparable[] keys = new Comparable[collections[0].length];
for(int row = 0; row < 1; row++){
for(int column = 0; column < collections[row].length; column++){
keys[column] = collections[row][column];
}
}
int comparison = 0;
Comparable[] result = new Comparable[keys.length];
for(int i = 0; i < keys.length; i++){
int found = 0;
for(int r = 1; r < collections.length; r++){
for(int c = 0; c < collections[r].length; c++){
comparison += 1;
if(keys[i].compareTo(collections[r][c]) == 0){
found += 1;
break;
}
}
if (found == collections.length - 1){
result[i] = keys[i];
}
}
}
for(int i = 0; i < result.length; i++){
System.out.print(result[i] + "\t");
}
System.out.println();
System.out.println("Comparisons made: " + comparison);
return result;
}
It sounds like you are just trying to find out if the entire column has alll of the same values, in which case the third nested loop is unnecesary because you just want to loop through each row but for one specific column at a time, so if you were to change it to:
for(int i = 0; i < keys.length; i++){
int found = 0;
for(int r = 1; r < collections.length; r++){
comparison += 1;
if(keys[i].compareTo(collections[r][i]) == 0){
found += 1;
}
if (found == collections.length - 1){
result[i] = keys[i];
}
}
}
That should give you the result you are looking for.
If you are actually doing it differently where you are checking that a certain number exists in every row, but you only want to check it once, then you can store what has already been checked and continue if that is repeated, as follows:
List<Comparable> checked = new ArrayList<Comparable>();
outer:
for(int i = 0; i < keys.length; i++)
{
for(Comparable c : list)
{
if(keys[i].compareTo(c) == 0) continue outer;
}
list.add(keys[i];
}
So, I generate a 100 numbers between the range of 0 and 9. I store these 100 numbers in an array called 'array'. Then I have the array called 'count'. It has 10 elements, and I wanted to check the following: for each element in 'array' if it equals to 0-9 then count[0-9] increments by 1, count[0] = how many times number 0 appears and so on count[1] = 1, count[2] = 2... . I just keep getting the output of around 20k numbers and i suppose? the sum of each element?, no idea why. I was wondering if there is something major wrong with my for loop?
import java.util.*;
class RandomInt {
public static void main(String[] args) {
int size = 100;
int max = 10;
int[] array = new int[size];
int[] count = new int[max]; //count[0,0,0,0,0,0,0,0,0,0]
int loop = 0;
Random generator = new Random();
for (int i = 0; i < size; i++) {
array[i] = generator.nextInt(max); // Generates 100 random numbers between 0 and 9 and stores them in array[]
System.out.print(array[i]);
for (int x = 0; x < size; x++) {// loop through 10 elements in count
for(int j = 0; j < 10; j++){ //loop through 100 elements in array
if (array[x] == j) {// loop through each 100 elements of array[x] and if element array[x] = value
count[j] += 1; // then count[x] = x + 1
System.out.print(count[j]);
}
}
}
}
System.out.println("0 appears " + count[0] + " times.");
}
}
Your Login is Perfect only mistake which i found u made is with the brackets........!
Generate the numbers using first loop and then count the number of occurrence using different for loop.
Here is your code's modified version which generates 10 numbers and counts the individual number occurrence count.....
public class RandomInt {
public static void main(String[] args) {
int size = 10;
int max = 10;
int[] array = new int[size];
int[] count = new int[max]; //count[0,0,0,0,0,0,0,0,0,0]
int loop = 0;
Random generator = new Random();
for (int i = 0; i < size; i++)
{
array[i] = generator.nextInt(max); // Generates 100 random numbers between 0 and 9 and stores them in array[]
System.out.print(array[i]+" ");
}
for (int x = 0; x < size; x++)
{// loop through 10 elements in count
for(int j = 0; j < 10; j++)
{ //loop through 100 elements in array
if (array[x] == j)
{// loop through each 100 elements of array[x] and if element array[x] = value
count[j] += 1; // then count[x] = x + 1
//System.out.print(count[j]);
}
}
}
System.out.println("3 appears " + count[3] + " times.");
}
}
There's a simpler way to do this without nested loops, so forgive me for suggesting this as a fix rather than finding the issue in the loop.
for(int i=0; i<size; i++){
int num = generator.nextInt(max);
array[i] = num;
count[num]++;
}
One loop, incrementing the count for each number as it appears. You may need to ensure all the entries in count start at 0, but even then an additional loop through 10 entries is MUCH faster.
To increment your counter, you don't need to have two nested for loops. Instead, you can use the value of array[x] as your counter.
for (int i = 0; i < size; i++) {
count[array[i]]++
}
You've nested your counting loop inside of your random number generating loop. Move the counting part outside.
Edit: The reason you're getting like 20k or whatever instances of zero is because when you set array[0] with a random value, you also check how many instances of 0 are in array[1] to array[99].
You probably shouldn't do your count until you have finished assigning your numbers, but here is how you could. Note that you want the value at array[i] to be your index to count.
for (int i = 0; i < size; i++) {
array[i] = generator.nextInt(max); // Generates random numbers
count[array[i]]++;
}
System.out.println(Arrays.toString(array));
System.out.println(Arrays.toString(count));
My goal is to
Create a 5x5 array and fill it with random integers in the range of 1 to 25.
Print this original array
Process the array, counting the number of odds, evens, and summing all of the elements in the array.
Print the total odds, evens, and the sum.
Im not sure how to do it and my teacher is very confused and cannot help me. I was wondering if i could get some guidance.
Here is my code:
public class Processing {
public static void main(String[] args) {
Random Random = new Random();
int[][] Processing = new int[5][5];
for (int x = 0; x < 5; x++) {
int number = Random.nextInt(25);
Processing[x] = number;
}
for (int i = 0; i < 5; i++) {
Processing[i] = new int[10];
}
}
}
Please follow naming conventions for your variables. See here: http://en.wikipedia.org/wiki/Naming_convention_(programming)#Java
Anyways, you have to nest your loops as follows:
for(int i = 0; i < 5; i ++) {
for(int j = 0; j < 5; j++) {
yourArray[i][j] = random.nextInt(25);
}
}
i is the row number and j is the column number, so this would assign values to each element in a row, then move on to the next row.
I'm guessing this is homework so I won't just give away the answer to your other questions, but to set you on the right track, here's how you would print the elements. Again, use two nested loops:
for(int i = 0; i < 5; i ++) {
for(int j = 0; j < 5; j++) {
// print elements in one row in a single line
System.out.print(yourArray[i][j] + " ");
}
System.out.println(); //return to the next line to print next row.
}
I am truly stuck here on how to do this. I got as far as creating the 10x10 array and making variables i and j - not far at all. I thought about the use of loops to initialize every element, but I just don't know how to go about doing it. Any help is appreciated, thanks.
public class arrays {
public static void main(String[] args) {
int[][] array = new int[10][10];
int i = 0, j = 0;
}
}
I was thinking of using a do while loop or for loop.
Psuedo-code:
for i = 0 to 9
for j = 0 to 9
array[i][j] = i*j
Converting this to Java should be a snap.
Create two nested for loops, one for i, and one for j, looping over all valid indices. In the body of the inner for loop, assign the computed product to the 2D array element.
You will need two for loops inside each other:
int[][] array = new int[10][10];
for (int x = 0; x < array.length; ++x)
{
for (int y = 0; y < array[y].length; ++y)
{
int product = x * y;
// put the value at the right place
}
}
You can read this as:
For each x value, iterate over the ten y values and do...
int [][] array = new int[10][10];
for (int i = 0; i < 10; i++) {
for (int j = 0; j < 10; j++) {
//initialize every element
array[i][j] = i + j;
}
}