Los valores de un arreglo se pueden asignar directamente o por medio de lectura mediante el teclado como se muestra a continuacion en el ejemplo donde se utilizan todos los conceptos y funciones vistos anteriormente para asignar valores a una matriz con elementos de columnas y renglones solicitodos:
EJEMPLO:
| #include<stdio.h>
#include<conio.h> #include<dos.h>
int n,m,i,j,x,y; float a[15][15]; int borde(); char resp; void main(void) { do { clrscr(); x=1; y=0; do{ if(x>1) //if mensaje de error { clrscr(); for(y=28;y<=49;y++) { gotoxy(y,10); printf("*"); gotoxy(y,13); printf("*"); } textcolor(15+BLINK); gotoxy(30,11); cprintf("\a ERROR"); textcolor(14); cprintf(" EN LOS DATOS"); gotoxy(30,12); cprintf(" INTENTA OTRA VEZ"); sleep(2); textcolor(15);
} //fin del if del
mensaje de error
clrscr(); gotoxy(3,3); printf("DATOS DE UNA MATRIZ DE N x M \n"); gotoxy(3,4); printf("DAME EL NéMERO DE RENGLONES N<=10: "); scanf("%d",&n); gotoxy(3,5);printf("DAME EL NUMERO DE COLUMNAS M<=10: "); scanf("%d",&m);
x=x+1;
if(n<=10 &&
n>=1 && m<=10 && m>=1)break;
} while(1); //fin
del segundo do 'anidado'
gotoxy(30,12); printf("\a OK! DATOS ACEPTADOS"); sleep(2); clrscr();
borde();
/* SOLICITUD DE DATOS */ y=3; for(i=1;i<=n;i++) { x=4; for(j=1;j<=m;j++)
{ textcolor(11); gotoxy(1,1); cprintf("INGRESA EL VALOR DEL: [RENGLON %d] [COLUMNA %d]",i,j); gotoxy(x,y); //COLOCA EL CURSOR EN LA POSICION DE LOS VALORES ACTUALES DE X y Y scanf("%f",&a[i][j]); x=x+7; //ES COMO UN TABULADOR DE 7 ESPACIOS }
y=y+2;/* ES COMO 2 SALTOS DE LINEA */ }
gotoxy(2,25); printf("Repetir el proceso (Si/No): "); resp=getche(); if(resp=='n' ||
resp=='N') break;
} while(1); //fin
del primer do
} // fin del main()
borde() //funcion para realizar el margen de la matriz { textcolor(10); for(y=3;y<=n*2+1;y++) { gotoxy(2,y); cprintf("º"); gotoxy(m*7+6,y); cprintf("º"); if(y==n*2+1) { y=y+1; gotoxy(2,y); cprintf("ÈÍ"); gotoxy(m*7+5,y); cprintf("ͼ"); gotoxy(2,2); cprintf("ÉÍ"); gotoxy(m*7+5,2); cprintf("Í»"); } } return(0); } // fin de la funcion
borde();
//Este programa se realizo considerando el espacio de un monitor de 80x25 columnas |
