xxxxxxxxxx
2-D Vectors
vector<vector<int>> vect;
for (int i = 0; i < vect.size(); i++)
{
for (int j = 0; j < vect[i].size(); j++)
{
cout << vect[i][j] << " ";
}
cout << endl;
}
xxxxxxxxxx
for (int row = 0; row < arr.length; row++)//Cycles through rows
{
for (int col = 0; col < arr[row].length; col++)//Cycles through columns
{
System.out.printf("%5d", arr[row][col]); //change the %5d to however much space you want
}
System.out.println(); //Makes a new row
}
//This allows you to print the array as matrix
xxxxxxxxxx
int[][] array = new int[rows][columns];
System.out.println(Arrays.deepToString(array));
xxxxxxxxxx
#My python lectures: https://cutt.ly/python-beginner-tutorials
numbers = [
[1,2,3],
[4,5,6],
[1,10,3],
[4,5,6],
]
for i in range(len(numbers)):
for j in range(len(numbers[i])):
print(numbers[i][j], end=" ")
print()
xxxxxxxxxx
// most of the time I forget that there should be matrix[i][j], not matrix[i]
#include <stdio.h>
// Abdullah Miraz
int main(){
int i, j;
int matrix[2][3] = {{2,3,4,5}, {7,8,9,1}};
for(i=0;i< 2 ; i++){
for(j=0;j<3;j++){
printf("%d ", matrix[i][j]);
}
}
}
xxxxxxxxxx
public class Sample {
public static void main(String[] args) {
String roles[][] = {
{ "admin", "customer", "cashier", "manager" },
{ "Jasmine", "lyka", "marbie", "soleen" },
{ "mama", "papa", "jenilyn", "efren" }
};
for (int i = 0; i < roles.length; i++) {
for (int j = 0; j < roles[i].length; j++) {
System.out.println(roles[i][j] + " ");
}
System.out.println("");
}
}
}
xxxxxxxxxx
for( auto &row : arr) {
for(auto col : row)
cout << col << " ";
cout<<endl;
}
xxxxxxxxxx
#include <stdio.h>
#define MAX 10
int main()
{
char grid[MAX][MAX];
int i,j,row,col;
printf("Please enter your grid size: ");
scanf("%d %d", &row, &col);
for (i = 0; i < row; i++) {
for (j = 0; j < col; j++) {
grid[i][j] = '.';
printf("%c ", grid[i][j]);
}
printf("\n");
}
return 0;
}
xxxxxxxxxx
void printElements(int* arr,int r,int c){
for (int i = 0; i < r; ++i){
for (int j = 0; j < c; ++j){
cout<<( *((arr + i * c) + j))<<" ";
}
cout<<"\n";
}
return;
}
// To call this function:
printElements(*array, row, column);