Array unidimensional,bidimensional y multidimensional en C++
El uso de arrays en C++, a continuación se muestran como implementarlos:
Array unidimensional:
#include <cstdlib>
#include <iostream>
using namespace std;
int main()
{
char A[10]; int i=0; for(char letra='A'; letra<='J'; letra++) { A[i]=letra; i++; } int j; for(j=0;j<=i;j++) { cout << A[j]; }
return 0;
}
Array bidimensional:
#include <cstdlib>
#include <iostream>
using namespace std;
int main()
{
int num; int bidi[2][3]; int i,j; for(i=0;i<2;i++) { for(j=0;j<3;j++) { cout << "\n DAME UN NUMERO ENTERO:"; cin >> num; bidi[i][j]=num; } } int x,y; for(x=0;x<2;x++) { for(y=0;y<3;y++) { cout << "\n bidi " << "[" << x << "]" << "[" << y << "]" << " = " << bidi[x][y]; } }
return 0;
}
Array multidimensional:
#include <cstdlib>
#include <iostream>
using namespace std;
int main()
{
int multi[2][3][5]; multi [0] [0] [0] = 1; multi [0] [1] [1] = 2; multi [0] [2] [2] = 3; multi [0] [0] [3] = 4; multi [0] [1] [4] = 5; multi [0] [2] [0] = 6; multi [0] [0] [1] = 7; multi [0] [1] [2] = 8; multi [0] [2] [3] = 9; multi [0] [0] [4] = 10; multi [0] [1] [0] = 11; multi [0] [2] [1] = 12; multi [0] [0] [2] = 13; multi [0] [1] [3] = 14; multi [0] [2] [4] = 15; multi [1] [0] [0] = 16; multi [1] [1] [1] = 17; multi [1] [2] [2] = 18; multi [1] [0] [3] = 19; multi [1] [1] [4] = 20; multi [1] [2] [0] = 21; multi [1] [0] [1] = 22; multi [1] [1] [2] = 23; multi [1] [2] [3] = 24; multi [1] [0] [4] = 25; multi [1] [1] [0] = 26; multi [1] [2] [1] = 27; multi [1] [0] [2] = 28; multi [1] [1] [3] = 29; multi [1] [2] [4] = 30; int i,j,k; for (i=0;i<2;i++) { for (j=0;j<3;j++) { for(k=0;k<5;k++) { cout << "\n" << "multi " << "[" << i << "]" << "[" << j << "]" << "[" << k << "]" << " = " << multi[i][j][k]; } } }
return 0;
}
No hay comentarios:
Publicar un comentario