Dev - C++ - Matriz Dinamica Simplificando Bucles

 
Vista:
sin imagen de perfil

Matriz Dinamica Simplificando Bucles

Publicado por Fucking (9 intervenciones) el 20/07/2015 03:53:39
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
/*quiero hacer correr dentro de el bucle for , ala funcion asi evito poner en cada funcion el bucle for cada vez que quiera hacer algo con la matriz aqui paso el codigo cual es el error?*/
#include <cstdio>
#include <cstdlib>
 
int suma_not_pair(int **pointmatriz){
int i=0,j=0,sumaimp=0;
 
if ((pointmatriz[i][j])%2!=0){
 
sumaimp+=pointmatriz[i][j];}
 
 
return sumaimp;
}
int main(){
 
 
//DECLARO LAS VARIABLES A UTILIZAR
int M=0,N=0 ,j=0,i=0,sumaimp;
printf("\n\tIngrese las filas\n\t");
scanf("%d",&M);
printf("\n\tIngrese las columnas \n\t");
scanf("%d",&N);
//DECLARO EL PUNTERO DE LA MATRIZ
int **pointmatriz;
//ALOJO EN LA MEMORIA LAS FILAS
pointmatriz=(int**) malloc(M*sizeof(int*));
//ALOJO EN LA MEMORIA LAS COLUMNAS
for(i=0;i<M;i++){
pointmatriz[i]=(int*)malloc(N*sizeof(int));
 
}
//LLENO LA MATRIZ
for(i=0;i<M;i++){
for(j=0;j<N;j++){
scanf("%d",&pointmatriz[i][j]);
 
 
}}
printf("\n\tLos valores de la matriz son\n\t");
//MUESTRO LA MATRIZ
for(i=0;i<M;i++){
for(j=0;j<N;j++){
 
printf("%d-",pointmatriz[i][j]);
 
}}
 
suma_not_pair(pointmatriz);
printf("\n\tLa suma de impares es de :%d\n\t",sumaimp);
for(i=0;i<M;i++){
free(pointmatriz[i]);
}
free(pointmatriz);
return EXIT_SUCCESS;
}
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder
Imágen de perfil de vangodp
Val: 73
Ha disminuido 1 puesto en Dev - C++ (en relación al último mes)
Gráfica de Dev - C++

Matriz Dinamica Simplificando Bucles

Publicado por vangodp (287 intervenciones) el 20/07/2015 07:18:58
Amigo: mire ese código:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
//UNAM
//AUTOR: Omar J.
//Fecha: 23092010
//Descripcion: Programa que guarda una matriz de tamanio mxn, usando memoria dinamica
 
#include<stdio.h>
#include<stdlib.h>
 
void getDatos ( int* rows, int* cols );
int** getMatrix ( int rows, int cols );
void fillMatrix ( int** matrix, int rows, int cols );
void printMatrix ( int** matrix, int rows, int cols );
void freeMemory ( int** matrix, int rows );
 
int main ( void ) {
    int rows = 0, cols = 0;
    int** matrix = NULL;
 
    getDatos ( &rows, &cols );
    matrix = getMatrix ( rows, cols );
    fillMatrix ( matrix, rows, cols );
    printMatrix ( matrix, rows, cols );
    freeMemory ( matrix, rows );
    return 0;
}
 
void getDatos ( int* rows, int* cols ) {
    printf ( "Renglones=" ); //Filas
    scanf ( "%d", rows );
    printf ( "Columnas=" );
    scanf ( "%d", cols );
}
 
int** getMatrix ( int rows, int cols ) {
    int i;
    int** matrix = NULL;
    matrix = ( int** ) malloc ( sizeof ( int* ) *rows );
 
    for ( i = 0; i < rows; i++ ) {
        * ( matrix + i ) = ( int* ) malloc ( sizeof ( int ) * cols );
    }
 
    return matrix;
}
 
void fillMatrix ( int** matrix, int rows, int cols ) {
    int i, j;
 
    for ( i = 0; i < rows; i++ ) {
        for ( j = 0; j < cols; j++ ) {
            * ( * ( matrix + i ) + j ) = i + j;
        }
    }
}
 
void printMatrix ( int** matrix, int rows, int cols ) {
    int i, j;
 
    for ( i = 0; i < rows; i++ ) {
        for ( j = 0; j < cols; j++ ) {
            printf ( "\t%d", * ( * ( matrix + i ) + j ) );
        }
 
        printf ( "\n" );
    }
}
 
void freeMemory ( int** matrix, int rows ) {
    int i;
 
    for ( i = 0; i < rows; i++ ) {
        free ( * ( matrix + i ) );
    }
 
    free ( matrix );
}

No es mio ese código, pertenece al autor que pone en la cabecera, no recuerdo de donde lo saqué, pero representa la idea.

No sé exactamente lo que quieres hacer, al parecer quieres sumar todos las casillas impares de la matriz, seria modificar el método fillMatrix(...) para leer con scanf y hacer la funcion que suma lis impares:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
//UNAM
//AUTOR: Omar J.
//Fecha: 23092010
//Descripcion: Programa que guarda una matriz de tamanio mxn, usando memoria dinamica
 
#include<stdio.h>
#include<stdlib.h>
 
void getDatos ( int* rows, int* cols );
int** getMatrix ( int rows, int cols );
void fillMatrix ( int** matrix, int rows, int cols );
void printMatrix ( int** matrix, int rows, int cols );
void freeMemory ( int** matrix, int rows );
int obtenerSuma ( int** matrix, int rows, int cols );
 
int main ( void ) {
    int rows = 0, cols = 0, suma;
    int** matrix = NULL;
 
    getDatos ( &rows, &cols );
    matrix = getMatrix ( rows, cols );
    fillMatrix ( matrix, rows, cols );
    printMatrix ( matrix, rows, cols );
 
    suma = obtenerSuma( matrix, rows, cols );
    printf("Suma de impares: %d", suma);
 
    freeMemory ( matrix, rows );
    return 0;
}
 
void getDatos ( int* rows, int* cols ) {
    printf ( "Renglones=" ); //Filas
    scanf ( "%d", rows );
    printf ( "Columnas=" );
    scanf ( "%d", cols );
}
 
int** getMatrix ( int rows, int cols ) {
    int i;
    int** matrix = NULL;
    matrix = ( int** ) malloc ( sizeof ( int* ) *rows );
 
    for ( i = 0; i < rows; i++ ) {
        * ( matrix + i ) = ( int* ) malloc ( sizeof ( int ) * cols );
    }
 
    return matrix;
}
 
void fillMatrix ( int** matrix, int rows, int cols ) {
    int i, j;
 
    for ( i = 0; i < rows; i++ ) {
        for ( j = 0; j < cols; j++ ) {
            //scanf ( "matriz[i][j]= %d", *(*(matrix+i)+j) );
            //scanf("matriz[i][j]=%d", *(matrix + i) + j);
            printf("matriz[%d][%d]=", i, j);
            scanf("%d%*c", (*(matrix+i)+j) );
        }
    }
}
 
void printMatrix ( int** matrix, int rows, int cols ) {
    int i, j;
    printf ( "\n\n" );
    for ( i = 0; i < rows; i++ ) {
        for ( j = 0; j < cols; j++ ) {
            printf ( "matriz[%d][%d]=%d ", i, j, * ( * ( matrix + i ) + j ) );
        }
 
        printf ( "\n" );
    }
    printf ( "\n" );
}
 
void freeMemory ( int** matrix, int rows ) {
    int i;
 
    for ( i = 0; i < rows; i++ ) {
        free ( * ( matrix + i ) );
    }
 
    free ( matrix );
}
 
int obtenerSuma ( int** matrix, int rows, int cols ) {
    int i, j, suma = 0;
 
    for ( i = 0; i < rows; i++ ) {
        for ( j = 0; j < cols; j++ ) {
            if (*(*(matrix+i)+j)%2 != 0){
                suma += *(*(matrix+i)+j);
            }
        }
    }
    return suma;
}

Espero que sea eso lo que buscas. Para que no te lies, eso *(*(matrix+i)+j) es lo mismo que eso matrix[i][j] ok =), es aritmética de punteros, muy útil pero te puedes liar con ella XDD, lo puse para que vayas pillando el rollo jaja
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
1
Comentar
sin imagen de perfil

Matriz Dinamica Simplificando Bucles

Publicado por Fucking (9 intervenciones) el 21/07/2015 05:00:58
Gracias bro,lo que queria es evitar hacer en cada funcion el ciclo for,cuando quiera hacer algo con la matriz(por ejemplo sumar diagonales,la ultima columna ,etc).y si es posible hacerlo poniendo la llamada dentro del for cuando cargo la matriz en el main,bueno ahora cheko lo que me pasaste que esta genial .bueno grazie mille :P
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar