Sorry if I'm not specific, but I have my code in working order, but I do not know how to separate the two different array outputs(?). Here's my code:
package code;
public class ArrayPrinter{
public static void main(String[] args) {
int FirstArray[][] = { {5, 6, 7, 8,} , {2, 4, 6, 8} , {8, 7, 9, 1} , {3, 5, 1, 2} };
printarray(FirstArray);
}
public static void printarray(int a[][]) {
for( int row = 0 ; row < a.length ; row++ ) {
for ( int column = 0 ; column < a[row].length ; column++ ) {
System.out.print( a[row][column] + " ");
}
System.out.println();
}
int SecondArray[][] = { {1, 2} , {3, 4, 5} , {6} , {7, 8, 9} };
printarray1(SecondArray);
}
public static void printarray1(int b[][]) {
for( int row = 0 ; row < b.length ; row++ ) {
for ( int column = 0 ; column < b[row].length ; column++ ) {
System.out.print( b[row][column] + " ");
}
System.out.println();
}
}
}
Here is the output:
5 6 7 8
2 4 6 8
8 7 9 1
3 5 1 2
1 2
3 4 5
6
7 8 9
Here is what I want it to look like
5 6 7 8
2 4 6 8
8 7 9 1
3 5 1 2
(well it wont let me add spaces, but I want these two sperated)
1 2
3 4 5
6
7 8 9
Help please.
Put an extra System.out.println() after the outermost for loop like...
public static void printarray(int a[][]) {
for( int row = 0 ; row < a.length ; row++ ) {
for ( int column = 0 ; column < a[row].length ; column++ ){
System.out.print( a[row][column] + " ");
}
System.out.println();
}
System.out.println();
int SecondArray[][] = { {1, 2} , {3, 4, 5} , {6} , {7, 8, 9} };
printarray1(SecondArray);
}
Also your two print functions look identical and could be combined.
Related
char arrA[ 6 ] = { 1, 2, 3, 4, 5, 0 };
char arrB[ 6 ] = {};
void setup(){
strcpy( arrB, arrA );
}
Hi all, I obtained the code above from here. My case is I need to use int arrA[6] instead of char arrA[6] when initializing variables so that it remains as values and not string. May I ask how do I actually accomplish it with arduino code. The link above only uses string as array and not numbers. Thank you for reading and have a nice day !!!
You can write a for loop to copy the contents of arrA to arrB.
int arrA[ 6 ] = { 1, 2, 3, 4, 5, 0 };
int arrB[ 6 ];
void setup(){
for(int i = 0; i < (sizeof(arrA)/sizeof(arrA[0])); i++)
{
arrB[i] = arrA[i];
}
}
I have seen the problem of finding the next greater element for each element in an array and it can be easily solved using monotonic stack. But can it be solved using recursion?
For the input array [4, 5, 2, 25], the next greater elements for each element are as follows.
4 --> 5
5 --> 25
2 --> 25
25 --> -1
Using stack
#include <bits/stdc++.h>
using namespace std;
void printNGE(int arr[], int n)
{
stack<int> s;
int res[n];
for (int i = n - 1; i >= 0; i--) {
if (!s.empty()) {
while (!s.empty() && s.top() <= arr[i]) {
s.pop();
}
}
res[i] = s.empty() ? -1 : s.top();
s.push(arr[i]);
}
for (int i = 0; i < n; i++)
cout << arr[i] << " --> " << res[i] << endl;
}
int main()
{
int arr[] = { 11, 13, 21, 3 };
int n = sizeof(arr) / sizeof(arr[0]);
printNGE(arr, n);
return 0;
}
long long fun(long long i,vector<long long>&v,long long par)
{
if(i==v.size())
return -1;
if(v[i]>v[par])
{
return v[i];
}
return fun(i+1,v,par);
}
void solve(long long i,vector<long long>&v,vector<long long>&ans)
{
if(i==v.size())return ;
solve(i+1,v,ans);
long long temp=fun(i+1,v,i);
ans[i]=temp;
}
vector<long long> nextLargerElement(vector<long long> v, int n)
{
vector<long long>ans(n,-1);
solve(0,v,ans);
return ans;
}
I have a list of 4 2-by-2 arrays. I want them to be stored together in a larger 4-by-4 array. The first 2 arrays compose of the "first row", the last 2 arrays compose the "second row".
Code
static void Test(){
int[,] arr1 = {
{0,1},
{2,3}
};
int[,] arr2 = {
{4,5},
{6,7}
};
int[,] arr3 = {
{8,9},
{10,11}
};
int[,] arr4 = {
{12,13},
{14,15}
};
List<int[,]> arrList = new List<int[,]>();
int[,] result = new int[4,4];
arrList.Add(arr1);
arrList.Add(arr2);
arrList.Add(arr3);
arrList.Add(arr4);
int v = 0;
foreach(int[,] x in arrList){
for(int i = 0; i < 2; i++){
for(int j = 0; j < 2; j++){
result[v*i-1,v*j-1] = x[i,j]; //This needs to change
}
}
v += 1;
}
}
The end goal based on this example should be
0 1 4 5
2 3 6 7
8 9 12 13
10 11 14 15
Eventually I used the following function. What this function does is it loops over 2 by 2 or 3 by 3 arrays, and combines them to a larger n by n array.
private static string[,] Combine(List<string[,]> arrList){
int elements = arrList.Count * arrList[0].Length;
int n = (int) Math.Pow((double) elements, 0.5);
string[,] result = new string[n,n];
int k = 0;
int r = 0;
int by = 0;
int amt = 0;
if(arrList[0].Length % 2 == 0){
by = (int) Math.Pow(arrList[0].Length, 0.5);
amt = n/by;
}else if(arrList[0].Length % 3 == 0){
by = (int) Math.Pow(arrList[0].Length, 0.5);
amt = n/by;
}
for(int v = 0; v < arrList.Count; v++){
if(v%amt == 0 && v != 0){
k=0;
r +=1;
}
for(int i = 0; i < by; i++){
for(int j = 0; j < by; j++){
result[r*by+i, k*by+j] = arrList[v][i,j];
}
}
k+=1;
}
return result;
}
Traditional Longest Increasing Subsequence problem.
This is recursion version ( not DP version )
I realized that version1 code had a bug, so I changed it to version2.
I don't clearly understand why version2 works and version1 has a bug for input A0
Please see version 1 and version2 below:
static int lis1(int[] v) {
int maxLen = 1;
for(int i = 1; i < v.length; i++) {
List<Integer> w = new ArrayList<Integer>();
for( int j = 0; j < i; j++) {
if( v[j] < v[i] ) {
w.add(v[j]);
}
}
// it used to be the following one line which has bug for input A0
//cand = lis1(l2a(w)) + 1; // version1
// so I changed it to the following, but can't clearly understand why it works.
// without this part, it has but for input A0
int cand = 1; // version2
if(v[i-1] < v[i])
cand = lis1(l2a(w)) + 1;
else
cand = lis1(l2a(w));
maxLen = Math.max(maxLen, cand);
}
return maxLen;
}
public static void main(String[] args) {
int[] A0 = {3, 2, 5, 6}; // for this input version1 had a bug which printed out 4 ( instead of 3 )
int[] A1 = {1, 2, 3, 3, 2, 4, 6, 7}; // 6
int[] A2 = { 10, 22, 9, 33, 21, 50, 41, 60, 80 }; // 6
int[] A3 = { 5, 0, 4, 2, 3, 7, 1 }; // 4
int[] A4 = { 2, 7, 3, 4, 9, 8, 12 }; // 5
int[] A5 = {3, 4, 2, 5 }; // 3
Actually... neither of your version works. Try putting A0={3,2,7,6}, your v2 returns 2, obviously wrong.
As for v1, for v={3,2} the answer should be 1, right? Let's see what your code does. When index i=1, your w after inner for loop equals {}. Then you made a recursive call to w={}, which should've returned 0, but it returns 1. Why, because of your maxlen variable, which is wrongly initialized with 1. This error propagates to entire {3,2,5,6} and gives wrong answer.
v2 accidentally solves this problem because your if condition then fails (3<2), and it returns the previously returned 1.
Just delete entire version 2, correct maxlen initialization. And start outer loop for(int i = 1; i < v.length; i++) with i=0, else you will get 0 for single-element array.
static int lis1(int[] v) {
int maxLen = 0;
for(int i = 0; i < v.length; i++) {
List<Integer> w = new ArrayList<Integer>();
for( int j = 0; j < i; j++) {
if( v[j] < v[i] ) {
w.add(v[j]);
}
}
cand = lis1(l2a(w)) + 1; // version1
maxLen = Math.max(maxLen, cand);
}
return maxLen;
}
I need to get the length of filled rows ( filled rows means a rows which contains at least one 1 ) of 2D int array in c#.
For ex..
1 1 1 0 1
0 1 1 1 1
1 1 1 1 1
0 0 0 0 0
Now the filled rows length=3 and col=4.
That is what i need...
I am expecting that you are storing these values in two dimensional integer array e.g.
int[][] a = {new int[] {1, 1, 0, 1}, new int[] {0, 1, 1, 1}, new int[] {1, 1, 1, 1}, new int[] {0, 0, 0, 0}};
Following is the LINQ for counting rows which has atleast one 1.
int filledRowsCount = a.Count(i => i.Any(ii => ii == 1));
int[][] intarray = { new int[] { 1, 2, 3 },
new int[] { 1, 2, 3 },
new int[] { 1, 2, 3 },
new int[] { 1, 2, 3 } };
**int c = intarray.Count();**//for rows Length = 4
int b = intarray[0].Count();//for columns length =3
you can use also:
int d = intarray.GetLength(0);//for rows length = 4