Вы находитесь на странице: 1из 10

2013

Arreglos

Arreglos

2 de mayo de 2013

Introduccin
Los arreglos en C# (tambin conocidos como Arrays) al igual que en C/C++, son indexados iniciando en cero (0). La forma de trabajo es muy similar a la mayora de lenguajes pero hay lagunas diferencias que notarn. Como declarar e inicializar un arreglo en C# Cuando vayamos a declarar un arreglo en debemos colocar los corchetes despus de tipo de dato. En C/C++ se estila poner los corchetes despus del identificador, eso causa un error de compilacin en C#int[] valores; //valores sin inicializar

valores = new int[100]; //100 elementos

valores = new int[20]; //ahora contiene 20 elementos

Arreglos multi-dimensionales En C# tambin podemos declarar arreglos multidimensionales, aqu unos ejemplos:

CODE: SELECCIONAR TODO //Arreglos unidimensionales o de dimensin simple

int[] valores1;

//sin inicializar

int[] valores2 = new int[50];

Arreglos

2 de mayo de 2013

//Arreglos multidimensionales

int[,] valores1;

//sin inicializar

int[,] valores2 = new int[3,7];

int[,,] valores3 = new int[3,4,2]; //Arreglo de tres dimensiones

//Arreglo de arreglos

int[][] matriz;

//sin inicializar

//Los arreglos de arreglos se inicializan de manera diferente

int[][] matriz = new int[3][];

for (int i = 0; i < matriz.Length; i++)

matriz[i] = new int[4];

Pueden combinarse los diferentes tipos de declaracin. 3

Arreglos Inicializacin Hay varias formas de inicializar los arreglos: CODE: SELECCIONAR TODO int[] valores = new int[10] {0,1,2,3,4,5,6,7,8,9};

2 de mayo de 2013

string[] paises = new string[5] {"Argentina", "Bolivia", "Peru","Chile","Colombia"};

//Inicializacion omitiendo el tamao de la matriz

int[] valores = new int[] {0,1,2,3,4,5,6,7,8,9};

string[] paises = new string[] {"Argentina", "Bolivia", "Peru","Chile","Colombia"};

//Tambien podemos omitir el operador new

int[] valores = {0,1,2,3,4,5,6,7,8,9};

string[] paises = {"Argentina", "Bolivia", "Peru","Chile","Colombia"};

Arreglos

2 de mayo de 2013

Desarrollo
/*NOMBRE DEL PROGRAMA: Arreglos */ /*FECHA: 2 de mayo del 2013*/ /*NOTAS*/ /*VERSIN: 1.0*/

#include<stdio.h> #include<stdlib.h> #include<conio.h> #include<math.h>

int main(void){ int tam;

float MA[5]={3,2,0,1,0}; float MB[5]={1,3,4,0,1}; float MC[5]={0,0,0,0,0}; int i=0; for (i=0; i<5; i++) { MC[i]=MA[i]+MB[i]; } printf("MA\t\tMB\t\tMC\n");

for (i=0; i<5; i++) { printf("%f\t%f\t%f\n",MA[i],MB[i],MC[i]); 5

Arreglos }

2 de mayo de 2013

printf("Por favor escriba los valores del primer arreglo: (5)\n"); scanf("%f %f %f %f %f", &MA[0], &MA[1], &MA[2], &MA[3],&MA[4]);

printf("Por favor escriba los valores del sgundo arreglo: (5)\n"); scanf("%f %f %f %f %f", &MB[0], &MB[1], &MB[2], &MB[3],&MB[4]);

for (i=0; i<5; i++) { MC[i]=MA[i]+MB[i]; printf("%f\t%f\t%f\n",MA[i],MB[i],MC[i]); }

printf("Por favor escriba los valores del primer arreglo: (5)\n"); scanf("%f %f %f %f %f", &MA[0], &MA[1], &MA[2], &MA[3],&MA[4]);

printf("Por favor escriba los valores del segundo arreglo: (5)\n"); scanf("%f %f %f %f %f", &MB[0], &MB[1], &MB[2], &MB[3],&MB[4]); for (i=0; i<5; i++) { MC[i]=sin(MA[i])+cos(MB[i]); } printf("la suma senA + cosB es:\n"); for (i=0; i<5; i++) { printf("%f\t%f\t%f\n",MA[i],MB[i],MC[i]); } 6

Arreglos

2 de mayo de 2013

_getche(); }

Cuarta parte #include <stdio.h>

int main() { int i,j; int n,m;

printf("Escribe el tamano de la matriz (n y m) "); scanf("%d %d",&n, &m); float arrA[n][m]; printf("tamano %d %d ",n,m); for (i = 0; i<n ; i++) { for (j = 0; j < m ; j++) { printf("Escribe el valor %i %i ",i,j); scanf("%f",&arrA[i][j]); } }

printf("Escribe el tamano de la segunda matriz (n y m) "); scanf("%d %d",&n, &m); float arrB[n][m]; printf("tamano %d %d ",n,m); 7

Arreglos for (i = 0; i<n ; i++) { for (j = 0; j < m ; j++) { printf("Escribe el valor %i %i ",i,j); scanf("%f",&arrB[i][j]); } } printf("Matriz A:\n"); for (i = 0; i<n ; i++) { for (j = 0; j < m ; j++) { printf("%f ",arrA[i][j]); } printf("\n"); } printf("Matriz B:\n"); for (i = 0; i<n ; i++) { for (j = 0; j < m ; j++) { printf("%f ",arrB[i][j]); } printf("\n"); } printf("La suma de las dos matrices: \n"); for (i = 0; i<n ; i++) { for (j = 0; j < m ; j++) { printf("%f ",arrA[i][j]+arrB[i][j]); } printf("\n"); }

2 de mayo de 2013

Arreglos return 0; }

2 de mayo de 2013

Conclusiones

Arreglos

2 de mayo de 2013

Parte cuatro utilizando el compilador gcc de Linux

10

Вам также может понравиться