multidimensional-array

How to write user input 2D array?

拥有回忆 提交于 2021-01-29 08:01:43
问题 I am new to java. Trying to make this into a user input 2D array which is 4*4. But when I try scanner, the row and col always got messed up. public static void main(String[] args) { String array = "1 2 2 1,1 3 3 1,1 3 3 2,2 2 2 2"; int[][] input = parseInt(array, 4, 4); } And I also want the user input can output as: 1 2 2 1 1 3 3 1 1 3 3 2 2 2 2 2 Appreciate for everybody's help! 回答1: Try this one : public static void main(String args[]) { String array = "1 2 2 1,1 3 3 1,1 3 3 2,2 2 2 2";

Map an array to create a new array of pairs

夙愿已清 提交于 2021-01-29 07:08:44
问题 I have two separate arrays and I'm trying to create a new nested array that has values grouped together. Can I use the map() method and pair each item inside the map method? There is a similar question here: Map an array of arrays However, he context is different because I don't have a nested array to begin with. var letters = [a, b, c]; var numbers = [1, 2, 3]; var lettersAndNumbers = letters.map((letter) => { numbers.forEach((number) => { return letter, number; ); }); // lettersAndNumbers =

How to pass a 2D array to a function using single pointer [closed]

风流意气都作罢 提交于 2021-01-29 06:21:48
问题 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 months ago . Improve this question I want to pass a 2D array to a function . I must receive the 2D array using a single pointer in the formal argument. 回答1: In C when you have a 2D array, you have an array of 1D arrays . On access, an array is converted to a pointer to its first element. C11 Standard - 6.3.2.1

Why my code give segmentation fault(core dumped) error?

不打扰是莪最后的温柔 提交于 2021-01-29 05:59:31
问题 I want to initialize a 5x5 array with 0 as content of each array index. But whenever i run my code, i get segmentation fault(core dumped error). Can you please help me what is the problem with my code. My code is as follows. #include <stdio.h> int main() { int a[5][5]; int i,j; for(i=0; i<=5; i++) { for(j=0; j<=5; j++) { a[i][j]=0; } } } 回答1: You have an array of 5 rows (indexed 0..4) and 5 cols (indexed 0..4), but you are accessing 6 rows (indexes 0..5) and 6 cols (indexes 0..5). You need to

Splitting text file into 2D array

我们两清 提交于 2021-01-29 05:20:56
问题 this is the text file that i want to split into a 2D array "YHOO",36.86,21,13873900,37.00 "GOOG",684.11,1114,1821650,686.72 "MSFT",50.54,3993,31910300,50.65 "AAPL",94.40,28201,39817000,94.26 and this is the code I have implemented to do this but it won't work String input = File.ReadAllText(@"..\..\Data\stockInfo.txt"); int i = 0, j = 0; string[,] result = new string[3, 5]; foreach (var row in input.Split('\n')) { j = 0; foreach (var col in row.Trim().Split(',')) { result[i, j] = string.Parse

Remove null from old 2d array and put the not null elements in a new 2d array

ぃ、小莉子 提交于 2021-01-29 05:06:41
问题 I am trying to remove the null elements from the old 2d array and put it in a new 2d array, but the new 2d array is outputting all null elements. String[][] status = new String[P][M]; String[][] newStatus = new String[P][M]; for (int i = 0; i < status.length; i++) { for (int j = 0; j < status.length; j++) { if (status[i][j] != null) { newStatus[i][j] = status[i][j]; } } } Original 2d array: [[O, X, null, O, O, null, null], [null, null, O, null, null, O, O]] I want it to look like this: [[O, X

How to extract perticular array value from multidimentional array

馋奶兔 提交于 2021-01-29 05:02:36
问题 How can I get the list of all ids from the array given below, $filteredZips=[{ "id": 21, "distance": "0" }, { "id": 20, "distance": "3.9399923305414037" }, { "id": 29, "distance": "8.33045537474091" }] Expected result will be : $id = array('21','20','29'); 回答1: There is a function called array_column that will grab one column in an array. First the string needs to be converted to array with Json_decode and second parameter to true. Then array_column returns your expected output. No looping is

Number spiral in C, code not working

我与影子孤独终老i 提交于 2021-01-29 04:30:55
问题 I want to make a spiral in C using a 2D matrix, such as the one shown below: This is the code that I worked out. But it keeps going into an infinite loop. I can't seem to get an output. Can anyone tell me what mistake I'm making in this logic? And I know its pretty cumbersome, but the assignment is to get the output for any "n" dimensional array, and we need to use all the row_left,row_right, etc variables, as they're given in the question. #include<stdio.h> int main(void) { int array[6][6]=

Create a Hosoya's Triangle in java

蹲街弑〆低调 提交于 2021-01-29 04:15:00
问题 So I'm trying to create a Hosoya's Triangle in Java, and I'm running into some math issues. I have started by making an empty 2D array that has a user input # of levels: int[][] tri = new int[levels][]; //build the empty triangle for (int row =0;row< tri.length;row++){ tri[row] = new int[row+1]; } this part is functional. The problem I'm having is in the next part, where I try to fill the triangle with Fibonacci numbers (I add the previous chunk of code because I'm thinking maybe building

Array from Database PHP

痴心易碎 提交于 2021-01-29 03:08:30
问题 Hi I need to create an array from database; myTable looks like this id | name | address | -------------+------------+--------------- 1 | Peter | Union Streeet| 2 | John | King Street | Then I have sql that will select values from database $record=mysql_query("SELECT * FROM myTable"); while($row=mysql_fetch_assoc($record)){ //fill array how to fill array that will look like bellow from database??? } I need to create array that will look like this $list = array ( array('$row[id]', '$row[name]',