• Viernes 19 de Abril de 2024, 12:43

Autor Tema:  Problema con punteros el O.S. cierra el programa  (Leído 2038 veces)

tomas_sauron

  • Miembro activo
  • **
  • Mensajes: 56
    • Ver Perfil
Problema con punteros el O.S. cierra el programa
« en: Viernes 9 de Septiembre de 2011, 19:42 »
0
Buenas gtn tngo problemas con punteros es mi primera experiencia con ellos y c++ el programa es un producto de matrices con memoria dinamica pero cuando entra al tercer for de productoMatriz() el OS cierra el programa por lo poco que se debe estar tocando memoria q no db pero no veo bien el error si m pueden dar una mano mil gracias !! ak va el codigo :

Código: C++
  1.  *
  2.  *  Created on: 04/09/2011
  3.  *      Author: tomas
  4.  */
  5.  
  6. #include "reservaMemoria.h"
  7. #include "ingresoDatos.h"
  8. #include <string.h>
  9. #include <iostream>
  10. using namespace std;
  11.  
  12. void ingresoMatriz(int **A,int **B,int &filA,int &colA,int &filB,int &colB){
  13.  
  14.         int n;int m;
  15.  
  16.         cout<<"Ingrese filas de A"<<endl;
  17.         cin>>filA;
  18.         cout<<"Ingrese columnas de A"<<endl;
  19.         cin>>colA;
  20.         int datos;
  21.         reservaMemoria(A,filA,colA);
  22.         for(n=0;n<filA;n++){
  23.                 for(m=0;m<colA;m++){
  24.                                 cout<<"ingrese datos matriz A["<<n+1<<","<<m+1<<"]"<<endl;
  25.                                 cin>>datos;
  26.                                 A[n][m] = datos;
  27.  
  28.                 }
  29.         }
  30.         for(int n=0;n<filA;n++){
  31.                                  cout<<"  "<<endl;
  32.                                  for(int m=0;m<colA;m++){
  33.                                         cout<<A[n][m]<<"  ";
  34.  
  35.                                  }
  36.                                  cout<<"  "<<endl;
  37.                 }
  38.             cout<<" "<<endl;
  39.                 cout<<"Ingrese filas de B"<<endl;
  40.                 cin>>filB;
  41.                 cout<<"Ingrese columnas de B"<<endl;
  42.                 cin>>colB;
  43.                 reservaMemoria(B,filB,colB);
  44.             for(n=0;n<filB;n++){
  45.                 for(m=0;m<colB;m++){
  46.                                 cout<<"ingrese datos matriz B["<<n+1<<","<<m+1<<"]"<<endl;
  47.                                 cin>>B[n][m];
  48.  
  49.                 }
  50.  
  51.         }
  52.         for(int n=0;n<filB;n++){
  53.                                  cout<<"  "<<endl;
  54.                                  for(int m=0;m<colB;m++){
  55.                                         cout<<B[n][m]<<"  ";
  56.  
  57.                                  }
  58.                                  cout<<"  "<<endl;
  59.  
  60.         }
  61.         cout<<" "<<endl;
  62.  
  63.  
  64.  
  65.  
  66. }
  67.  
  68.  
  69.  
Código: C++
  1. /*
  2.  * productoMatricial.cpp
  3.  *
  4.  *  Created on: 21/08/2011
  5.  *      Author: tomas
  6.  */
  7. #include "reservaMemoria.h"
  8. #include "constantes.h"
  9. #include <iostream>
  10. #include "productoMatricial.h"
  11. using namespace std;
  12.  void productoMatriz(int **A,int **B,int **C,int filA,int colA,int filB,int colB){
  13.  
  14.                  int acumulador;
  15.          reservaMemoria(C,filA,colB);
  16.                  if(filB == colA){
  17.                          for(int n=0;n<filA;n++){
  18.                                  cout<<"Generando producto matricial..."<<endl;
  19.                                  for(int k=0;k<colB;k++){
  20.                                          C[n][k]=0;
  21.                                          acumulador=0;
  22.                                          for(int m=0;m<filB;m++){//aca seria el problema !!
  23.                                                  acumulador = ((A[n][m]) * (B[m][k]));
  24.                                                  C[n][k] = acumulador + C[n][k];
  25.  
  26.                                          }
  27.  
  28.                                  }
  29.  
  30.                          }
  31.                          cout<<"Fin de Producto Matricial"<<endl;
  32.  
  33.                  }else{
  34.                          cout<<"El producto de matrices no es valido,dado que el numero de filas de B es distinto al numero de columnas de A"<<endl;
  35.                          return;
  36.                  }
  37.  
  38.  
  39.  }
  40.  
Código: C++
  1. /*
  2.  * reservaMemoria.cpp
  3.  *
  4.  *  Created on: 04/09/2011
  5.  *      Author: tomas
  6.  */
  7.  
  8. #include "reservaMemoria.h"
  9. #include <iostream>
  10. using namespace std;
  11.  
  12.  
  13. int** reservaMemoria(int fil,int col){
  14.  
  15.         int **p;
  16.     p = new int*[fil];
  17.     for(int n=0;n<fil;n++){
  18.         p[n] = new int[col];
  19.     }
  20.  
  21.         return p**;
  22. }
  23.  
Código: C++
  1. /*
  2.  * menu.cpp
  3.  *
  4.  *  Created on: 21/08/2011
  5.  *      Author: tomas
  6.  */
  7. #include <iostream>
  8. #include "menu.h"
  9. #include "ingresoDatosAleatorios.h"
  10. #include "ingresoDatos.h"
  11. #include "mostrarResultado.h"
  12. #include "productoMatricial.h"
  13. using namespace std;
  14.  
  15. void menu(){
  16.                  int i=-1;int filA,colA,filB,colB;
  17.                  int **A;int **B;int **C;
  18.                  while(i!=0){
  19.                              cout<<"***********MENU***********"<<endl;
  20.                                  cout<<"*1:Ingreso de datos manualmente******"<<endl;
  21.                                  cout<<"*2:Ingreso de datos aleatorios*******"<<endl;
  22.                                  cout<<"*3:Producto Matricial****************"<<endl;
  23.                                  cout<<"*4:Mostrar Resultado*****************"<<endl;
  24.                                  cout<<"*0:Salir*****************************"<<endl;
  25.                                  cin>>i;
  26.                                  switch(i){
  27.  
  28.                                          case 1:ingresoMatriz(A,B,filA,colA,filB,colB);break;
  29.                                          //case 2:ingresoAleatorio(A,B,maxColA,maxColB,maxFilA,maxFilB);break;
  30.                                          case 3:productoMatriz(A,B,C,filA,colA,filB,colB);break;
  31.                                          //case 4:mostrarResultado(C,colA,filB);break;
  32.  
  33.                                  }
  34.  
  35.                  }
  36.                  cout<<"Bye!!"<<endl;
  37.  
  38.  }
  39.  

espero m puedan dar una mano !!

saludos !!

alexg88

  • Miembro activo
  • **
  • Mensajes: 37
    • Ver Perfil
Re:Problema con punteros el O.S. cierra el programa
« Respuesta #1 en: Domingo 18 de Septiembre de 2011, 21:36 »
0
Aunque seguramente sea muy tarde, dejo la solución a los problemas:

Código: C++
  1. //#include <stdio.h>
  2. //#include <string.h>
  3. #include <iostream>
  4.  
  5. using namespace std;
  6.  
  7. #define INTENTOS 3
  8.  
  9. int** reservaMemoria(int fil,int col){
  10.  
  11.         int **p;
  12.     p = new int*[fil];
  13.     for(int n=0;n<fil;n++){
  14.         p[n] = new int[col];
  15.     }
  16.  
  17.         return p;
  18. }
  19.  
  20. void ingresoMatriz(int **&A,int **&B,int &filA,int &colA,int &filB,int &colB){
  21.  
  22.         int n;int m;
  23.  
  24.         cout<<"Ingrese filas de A"<<endl;
  25.         cin>>filA;
  26.         cout<<"Ingrese columnas de A"<<endl;
  27.         cin>>colA;
  28.         int datos;
  29.         A = reservaMemoria(filA,colA);
  30.         for(n=0;n<filA;n++){
  31.                 for(m=0;m<colA;m++){
  32.                                 cout<<"ingrese datos matriz A["<<n+1<<","<<m+1<<"]"<<endl;
  33.                                 cin>>datos;
  34.                                 A[n][m] = datos;
  35.  
  36.                 }
  37.         }
  38.         for(int n=0;n<filA;n++){
  39.                                  cout<<"  "<<endl;
  40.                                  for(int m=0;m<colA;m++){
  41.                                         cout<<A[n][m]<<"  ";
  42.  
  43.                                  }
  44.                                  cout<<"  "<<endl;
  45.                 }
  46.             cout<<" "<<endl;
  47.                 cout<<"Ingrese filas de B"<<endl;
  48.                 cin>>filB;
  49.                 cout<<"Ingrese columnas de B"<<endl;
  50.                 cin>>colB;
  51.                 B = reservaMemoria(filB,colB);
  52.             for(n=0;n<filB;n++){
  53.                 for(m=0;m<colB;m++){
  54.                                 cout<<"ingrese datos matriz B["<<n+1<<","<<m+1<<"]"<<endl;
  55.                                 cin>>B[n][m];
  56.  
  57.                 }
  58.  
  59.         }
  60.         for(int n=0;n<filB;n++){
  61.                                  cout<<"  "<<endl;
  62.                                  for(int m=0;m<colB;m++){
  63.                                         cout<<B[n][m]<<"  ";
  64.  
  65.                                  }
  66.                                  cout<<"  "<<endl;
  67.  
  68.         }
  69.         cout<<" "<<endl;
  70.  
  71.  
  72.  
  73.  
  74. }
  75.  
  76. void productoMatriz(int **A,int **B,int **C,int filA,int colA,int filB,int colB){
  77.  
  78.                  int acumulador;
  79.         C = reservaMemoria(filA,colB);
  80.                  if(filB == colA){
  81.                          for(int n=0;n<filA;n++){
  82.                                  cout<<"Generando producto matricial..."<<endl;
  83.                                  for(int k=0;k<colB;k++){
  84.                                          C[n][k]=0;
  85.                                          acumulador=0;
  86.                                          for(int m=0;m<filB;m++){//aca seria el problema !!
  87.                                                  acumulador = ((A[n][m]) * (B[m][k]));
  88.                                                  C[n][k] = acumulador + C[n][k];
  89.  
  90.                                          }
  91.  
  92.                                  }
  93.  
  94.                          }
  95.                          cout<<"Fin de Producto Matricial"<<endl;
  96.  
  97.                  }else{
  98.                          cout<<"El producto de matrices no es valido,dado que el numero de filas de B es distinto al numero de columnas de A"<<endl;
  99.                          return;
  100.                  }
  101.  
  102.  
  103.  }
  104.  
  105.  
  106. void menu(){
  107.                  int i=-1;int filA,colA,filB,colB;
  108.                  int **A = NULL;int **B = NULL;int **C = NULL;
  109.                  while(i!=0){
  110.                              cout<<"***********MENU***********"<<endl;
  111.                                  cout<<"*1:Ingreso de datos manualmente******"<<endl;
  112.                                  cout<<"*2:Ingreso de datos aleatorios*******"<<endl;
  113.                                  cout<<"*3:Producto Matricial****************"<<endl;
  114.                                  cout<<"*4:Mostrar Resultado*****************"<<endl;
  115.                                  cout<<"*0:Salir*****************************"<<endl;
  116.                                  cin>>i;
  117.                                  switch(i){
  118.  
  119.                                          case 1:ingresoMatriz(A,B,filA,colA,filB,colB);break;
  120.                                          //case 2:ingresoAleatorio(A,B,maxColA,maxColB,maxFilA,maxFilB);break;
  121.                                          case 3:productoMatriz(A,B,C,filA,colA,filB,colB);break;
  122.                                          //case 4:mostrarResultado(C,colA,filB);break;
  123.  
  124.                                  }
  125.  
  126.                  }
  127.                  cout<<"Bye!!"<<endl;
  128.  
  129.  }
  130.  
  131.  
  132. int main(){
  133.         menu();
  134. }
  135.  
  136.  

ProfesorX

  • Moderador
  • ******
  • Mensajes: 796
  • Nacionalidad: mx
    • Ver Perfil
Re:Problema con punteros el O.S. cierra el programa
« Respuesta #2 en: Lunes 19 de Septiembre de 2011, 00:12 »
0
Aunque seguramente sea muy tarde, dejo la solución a los problemas:


Nunca es tarde, a alguien mas le puede servir al informacion ;)

Por cierto, saludos brother :) y disculpa, no lei tu mensaje antes, si no te hubiera apoyado :P

NOTA:
==================================================================
Este foro es para ayudar, aprender, compartir... usenlo para eso,
NO SE RESUELVEN DUDAS POR MENSAJE PRIVADO Y MENOS POR CORREO
==================================================================

tomas_sauron

  • Miembro activo
  • **
  • Mensajes: 56
    • Ver Perfil
Re:Problema con punteros el O.S. cierra el programa
« Respuesta #3 en: Lunes 19 de Septiembre de 2011, 04:08 »
0
Si bien ya encontré la solución,nunk s tard ya q si s vrdad ,sirve para otra persona muchas gracias = por responder !!

PD : como sigo con C++ seguramente pronto estaré molestando con inquietudes nuevas !! jaja saludos !!