Foro de elhacker.net

Programación => Programación C/C++ => Mensaje iniciado por: Dent en 12 Abril 2011, 22:57 pm



Título: por que entrega cualquier valor ? .. gracias !!
Publicado por: Dent en 12 Abril 2011, 22:57 pm
#include <stdio.h>
        int main (){
                int j,i;
                int arr[5][8];

                for(i = 0; i <= 5; i++){
                        for(j = 0; j <= 8; j++){
                        arr[j] = i + j;
                        }
                }
                for(i=0; i<=5; i++){
                        for(j = 0; j <= 8; j++)
                        printf("valores arr [%d][%d]=%d\n ", i, j, arr[j]);// arr[i j]
                      printf("\n");
                       
                }
}

valores arr [3][0]=3
 valores arr [3][1]=4
 valores arr [3][2]=5
 valores arr [3][3]=6
 valores arr [3][4]=7
 valores arr [3][5]=8
 valores arr [3][6]=9
 valores arr [3][7]=10
 valores arr [3][8]=4
 
valores arr [4][0]=4
 valores arr [4][1]=5
 valores arr [4][2]=6
 valores arr [4][3]=7
 valores arr [4][4]=8
 valores arr [4][5]=9
 valores arr [4][6]=10
 valores arr [4][7]=11
 valores arr [4][8]=4
 
valores arr [5][0]=5
 valores arr [5][1]=1
 valores arr [5][2]=1307813
 valores arr [5][3]=14422064
 valores arr [5][4]=134513979
 valores arr [5][5]=2514932
 valores arr [5][6]=134513968
 valores arr [5][7]=0
 valores arr [5][8]=-1077657112
 
PORQUE LOS ULTIMOS VALORES NO CUMPLEN "i+j"   .. gracias 


Título: Re: por que entrega cualquier valor ? .. gracias !!
Publicado por: leogtz en 12 Abril 2011, 23:48 pm
No debiera compilarte si quiera.

Código:
int arr[5][8];

Es una matriz bidimensional, fijate lo que intentas hacer aquí:

Código:
for(j = 0; j <= 8; j++){
                        arr[j] = i + j;
                        }

creo que debería ser así:

Código:
for(j = 0; j <= 8; j++){
                        arr[j][i] = i + j;
                        }

Corrige también los indices.


Código
  1. #include <stdio.h>
  2.        int main (){
  3.                int j,i;
  4.                int arr[5][8];
  5.  
  6.                for(i = 0; i < 5; i++)
  7.                {
  8.                        for(j = 0; j < 8; j++)
  9.                        {
  10.                         arr[i][j] = i + j;
  11.                        }
  12.                }
  13.  
  14.                for(i=0; i<5; i++){
  15.                        for(j = 0; j < 8; j++)
  16.                        printf("valores arr [%d][%d] = %d\n", i, j, arr[i][j]);// arr[i j]
  17.                      printf("\n");
  18.  
  19.                }
  20. }
  21.  


Título: Re: por que entrega cualquier valor ? .. gracias !!
Publicado por: Dent en 13 Abril 2011, 05:44 am
Saludos.
Leo Gutiérrez. gracias por el interés, podrías pasarme tu email así te adjunto el archivo..
LA PAGINA NO MUESTRA TODO LO QUE SUBÍ
Atte. Dent .. muchas gracias !!


Título: Re: por que entrega cualquier valor ? .. gracias !!
Publicado por: leogtz en 13 Abril 2011, 16:15 pm
No, para eso está el foro, pregunta aquí.