How can I iterate a 2d array starting the last row? I've been trying a lot lately but I didn't find the solution, even google and stack overflow didn't gave me enough clues.
Here is my Sample code
C++:
String[][] array = {
{"a", "b", "c"},
{"d", "e"} // here at array[1][0] I want here to start the loop iteration
};
for (int row = 0; row < array.length; row++) {
for (int col = 0; col < array[x].length; col++) {
// do_Something
}
}
Should I change the row from int row = 0 to int row = -1? for row = -1; row < array.length; row++?
#include <iostream>
using namespace std;
int main() {
int test[3][2] = {{2, -5},
{4, 0},
{9, 1}};
// use of nested for loop
// access rows of the array
for (int i = 0; i < 3; ++i) {
// access columns of the array
for (int j = 0; j < 2; ++j) {
cout << "test[" << i << "][" << j << "] = " << test[i][j] << endl;
}
}
return 0;
}
Reverse one :
C++:
#include <iostream>
using namespace std;
int main() {
int test[3][2] = {{2, -5},
{4, 0},
{9, 1}};
// use of nested for loop
// access rows of the array
for (int i = 2; i > -1; i--) {
// access columns of the array
for (int j = 1; j > -1; j--) {
cout << "test[" << i << "][" << j << "] = " << test[i][j] << endl;
}
}
return 0;
}
Edit this method for yourself :
C++:
#include<iostream>
#include<conio.h>
using namespace std;
main() {
//clear the screen.
//declare variable type int
int a[3][3], i, j;
//Input the numbers
for (i = 0; i < 3; i++) {
for (j = 0; j < 3; j++) {
cout << "Enter number :";
cin >> a[i][j];
}
}
//display the matrix
for (i = 2; i >= 0; i--) {
for (j = 2; j >= 0; j--) {
cout << a[i][j] << "\t";
}
cout << endl;
}
return 0;
}
#include <iostream>
using namespace std;
int main() {
int test[3][2] = {{2, -5},
{4, 0},
{9, 1}};
// use of nested for loop
// access rows of the array
for (int i = 0; i < 3; ++i) {
// access columns of the array
for (int j = 0; j < 2; ++j) {
cout << "test[" << i << "][" << j << "] = " << test[i][j] << endl;
}
}
return 0;
}
Reverse one :
C++:
#include <iostream>
using namespace std;
int main() {
int test[3][2] = {{2, -5},
{4, 0},
{9, 1}};
// use of nested for loop
// access rows of the array
for (int i = 2; i > -1; i--) {
// access columns of the array
for (int j = 1; j > -1; j--) {
cout << "test[" << i << "][" << j << "] = " << test[i][j] << endl;
}
}
return 0;
}
Edit this method for yourself :
C++:
#include<iostream>
#include<conio.h>
using namespace std;
main() {
//clear the screen.
//declare variable type int
int a[3][3], i, j;
//Input the numbers
for (i = 0; i < 3; i++) {
for (j = 0; j < 3; j++) {
cout << "Enter number :";
cin >> a[i][j];
}
}
//display the matrix
for (i = 2; i >= 0; i--) {
for (j = 2; j >= 0; j--) {
cout << a[i][j] << "\t";
}
cout << endl;
}
return 0;
}
Ohh yes, it was just in the condition. I can also do like for (int i = arr.lenght - 1; i != -1; i--) which can be start at the end of the row of 2d array. Anyways, thnx for the suggestion, I could use your idea to apply it to my code.
Ohh yes, it was just in the condition. I can also do like for (int i = arr.lenght - 1; i != -1; i--) which can be start at the end of the row of 2d array. Anyways, thnx for the suggestion, I could use your idea to apply it to my code.