add Incremented Number value to array based on common string values - arrays

What i'm trying to accomplish is trying to compare two strings in an array and output the number of occurances for each into a new array. I.E. Given a set constant Array of test0,test1,test2, and another array of test1,test1,test2 I want to search through the array to give a desired output of 0,2,1.
This is my code thus far but I am still at a loss on how to accomplish this.
private function findVal(arr1: Array, arr2: Array): Array {
var tempNum: Array = new Array();
for (var i: int = 0; i < arr1.length; i++) {
for (var ii: int = 0; ii < arr2.length; ii++) {
if (arr1[i] == arr2[ii]) {
var num: int = 0;
//not sure where to go forward from here
}
}
}
return tempNum;
}
Any help would be greatly appreciated, thank you.

This code simply counts up the number of occurrences of a string, then stores that in the tempNum array, and when the searching through arr1 is complete, the tempNum array is returned to the caller.
private function findVal(arr1:Array, arr2:Array): Array {
var tempNum:Array = new Array(arr1.length);
for (var i:int = 0; i < arr1.length; i++) {
var num:int = 0;
for (var j:int = 0; j < arr2.length; j++) {
if (arr1[i] == arr2[j]) {
num++; // count up the number of occurrences as you iterate through arr2 and compare with arr1
}
}
tempNum[i] = num; // store the number of occurrences of string at index i of arr1 that occurred in arr2
}
return(tempNum);
}

Related

adding an array table (bidimensional) to a List (Java)

I am doing a Bingo program and now, the section of the bingoCard, to do this I am using an a bidimensional array but I need to shuffle the numbers of each row. For the shuffle part I saw that the setList is much better, but I don't know how to relate the List with the array here is a part of the code:
public static Integer[][] bingoCard(){
Integer [][] bingoCard= new Integer[3][9];
for(int x =0; x<bingoCard.length; x++){
for(int y =0; y<bingoCard[x].length; y++){
if(y <5){
int random = (int)(Math.random()*90+1);
System.out.print((bingoCard[0][y] = random) + " ");
}
if(y >4 && y <9){
System.out.print((bingoCard[0][y] = 0) + " ");
}
}
System.out.println();
}
List<Integer[]> list =Arrays.asList(bingoCard);
Collections.shuffle(list);
list.toArray(bingoCard);
return bingoCard;
}
Any question please ask me!!
Thanks.
In your code, when you do List<Integer[]> list = Arrays.asList(bingoCard), you are converting the outer array into a List and then shuffling its contents. The effect of this will be that the order of the 3 rows are shuffled rather than the contents of the 3 rows which is what you want. You could achieve this by shuffling the contents of each row within your for-loop. Or, after constructing the 2D array, you can loop over each row again and shuffle them.
Also, you have another small bug. When you assign the value, you are doing bingoCard[0][y] = ... but it should be bingoCard[x][y]. Otherwise, you are only assigning the first row new values on each iteration.
I would recommend not converting the array to a List just to shuffle it and then converting it back. Instead, you can use Random.nextInt to pick indexes of the array to assign. That would look something like this:
public static Integer[][] bingoCard(){
int numRows = 3;
int numCols = 9;
int randomNumbersPerRow = 5;
int randomNumberBound = 90;
Random random = new Random();
Integer[][] bingoCard = new Integer[numRows][numCols];
for (int row = 0; row < numRows; row++) {
for (int col = 0; col < numCols; col++) {
// Initialize all spots to 0
bingoCard[row][col] = 0;
}
for (int i = 0; i < randomNumbersPerRow; i++) {
// Choose a spot to assign a random number
int indexToAssign = random.nextInt(numCols);
// Make sure we haven't already chosen it
while (bingoCard[row][indexToAssign] != 0) {
indexToAssign = random.nextInt(numCols);
}
int numToAssign = random.nextInt(randomNumberBound) + 1;
bingoCard[row][indexToAssign] = numToAssign;
}
}
// Print the bingo card
for (int row = 0; row < numRows; row++) {
for (int col = 0; col < numCols; col++) {
System.out.printf("%2d ", bingoCard[row][col]);
}
System.out.println();
}
return bingoCard;
}

how to append an element in the smallest available index in a 2D array in c?

I am trying to append value to the end of the 2D array say, ABC in a pointer A to the index with the smallest row index, and then the smallest col index e.g. if both indices [3][15] and [5][1] are empty, we append the value to [3][15], since it has the smaller row index.
imgr_result_t imgr_append(imgr_t* im, int expand_row, int val)
{
for(int i = 0; i < im->rows; i++)
{
for(int j = 0; j < im->cols; j++)
{
if(im->pixels[i][j] == 0)
{
im->pixels[i][j] = val;
}
}
}
}
I have written this snippet so far but I am unsure about what I am doing:/

What type of array sorting algorithm is this?

What type of sorting algorithm is this? It goes through each index, then gets the minimum value in the rest of the array and swaps.
private void Sort(int[] myArray)
{
for (int i = 0; i < myArray.Length; i ++)
{
var minValue = myArray[i];
var minIndex = i;
for(int j = i +1; j< myArray.Length; j++)
{
if (myArray[j] < minValue)
{
minIndex = j;
minValue = myArray[j];
}
}
var temp = myArray[i];
myArray[i] = myArray[minIndex];
myArray[minIndex] = temp;
}
}
It's called Selection sort. As you have already noticed, it selects the minimum element in the remaining list, swaps it in order and repeats until no more elements left.

Java: changing an already established array

So I am writing code that will change the elements of an array and print out ONLY the numbers that come after a certain target. In this case the target is the number 4.
meaning that if my original array contains the numbers
5 6 4 3 2 1
the new array will only contain 3 2 1 because those are the numbers that come after the number 4.
I have this all in a method and so far my code prints out the number that comes after 4 but keeps printing that same number over and over and I don't know how to keep it printing the elements that follow
boolean four;
int location = 0;
four = contains(newArray, 4);
if (four == false) { // this returns the same array if there is no 4
return newArray;
} else {
for (int i = 0; i < newArray.length; i++) {
if (newArray[i] == 4) {
location = i;
}
}
for (int j = 0; j < newArray.length; j++) {
newArray[j] = newArray[location + 1];
}
return newArray;
}
You could also make use of the native implementation of System.arraycopy like this
for (int i = 0; i < arr.length; i++) {
if (arr[i] == 4) {
location = i;
break;
}
}
int[] out = new int[arr.length-location-1];
System.arraycopy(arr, location+1, out, 0, arr.length-location-1);
return out;
You should create a new array, since you can't change an array's length :
for (int i = 0; i < newArray.length; i++) {
if(newArray[i] == 4) {
location = i;
break;
}
}
int [] arr = new int[newArray.length - location - 1];
for(int j = 0; j < arr.length; j++) {
arr[j] = newArray[location+1+j];
}
newArray = arr;

Converting Column Array to Rows Array in Flex

Im trying to make a multidimensional array to save the data but I obtain an error ("TypeError: Error #1010: A term is undefined and has no properties.").
var cols:int = sheet.cols;
var rows:int = sheet.values.length;
var ridx:int = 0;
var cidx:int = 0;
var out:Array = new Array();
var i:int;
for (i=0; i < cols; i++) {
out[i] = new Array();
var j:int;
for (j=0; j < rows; j++) {
out[ridx][cidx] = sheet.getCell(j, i).value;
ridx++;
if(ridx >= rows) {
cidx++;
ridx = 0;
}
}
}
what am I doing wrong?
Thanks in advance!
EDIT:
Now I have this code:
for (var i:int = 0; i < cols; i++) {
out[i] = new Array();
for (var j:int = 0; j < rows; j++) {
out[j][i] = sheet.getCell(j, i).value; //IM GETTING THE ERROR HERE
}
}
You're trying to assign a cell value to a nonexistent location in your output array.
In the first i loop, you assign out[0] = new Array();, then in the j loop, you'll set out[0][0] = sheet.getCell(0,0).value;
In the second j loop, you'll set out[1][0] = sheet.getCell(1,0).value;
In this case, sheet.getCell(1,0) may return null, but whether it does or not, out[1] is null, so your code translates to null[0] = ... -- which will trigger the error you're seeing.
You can probably fix this by swapping i and j in your inner loop.

Resources