Test Foro de elhacker.net SMF 2.1

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.

int arr[5][8];

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

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


creo que debería ser así:

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


Corrige también los indices.


#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[i][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[i][j]);// arr[i j]
                      printf("\n");
                       
                }
}
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í.