Hola, saludos laneros, este es el primer tema que publico y es que necesito un poco de ayuda. He estado leyendo otros temas y muchos siempre dicen que este foro no es para hacernos las tareas y yo no espero eso. De hecho ya trabaje en el programa que necesito pero hay algo que me esta faltando.
Necesito hacer un programa que escriba una matriz en un archivo de texto.
Este es el codigo que llevo hasta el momento:
#include <iostream>
#include <fstream>
using namespace std;
typedef double Escalar;
typedef Escalar* Vector;
typedef Vector* Matriz;
void CreaMatriz(Matriz& M, int Filas, int Cols)
{
M= new Vector [Filas];
for(int f=0; f<Filas; f++)
M[f]= new Escalar[Cols];
}
void EscribeEnMatriz(Matriz M, int Filas, int Cols, string N)
{
for(int f=0; f<Filas; f++)
for(int c=0; c<Cols; c++)
{
cout<<N<<"("<<f+1<<","<<c+1<<")=";
cin>>M[f][c];
}
}
void EscribeMatrizEnArchivo(Matriz M, int Filas, int Cols)
{
// cout<<"La Matriz "<<nombre<<" es: "<<endl;
for(int f=0; f<Filas; f++)
{
for(int c=0; c<Cols; c++)
cout<<M[f][c]<<" ";
cout<<endl;
ofstream fout("C:\\Documents and Settings\\Administrador\\Escritorio\\matriz.txt");
fout<<M;
}
}
int main()
{
cout<<"Matriz"<<endl;
cout<<"Entre nombre de la Matriz:"<<endl;
string name;
cin>>name;
cout<<"Entre numero de Filas:"<<endl;
int fil;
cin>>fil;
cout<<"Entre numero de Columnas"<<endl;
int col;
cin>>col;
Matriz MA;
CreaMatriz(MA,fil,col);
EscribeEnMatriz(MA,fil,col,name);
EscribeMatrizEnArchivo(MA,fil,col);
system("PAUSE");
return 0;
}
Se que solo es una cosita que me falta y de hecho el error esta en la funcion "EscribeMatrizEnArchivo" si crea el archivo pero me esta escribiendo basura, nada que ver con la matriz. Espero puedan hecharle un ojo, y comentar alguna sugerencia.
Por su atencion. Gracias!
Necesito hacer un programa que escriba una matriz en un archivo de texto.
Este es el codigo que llevo hasta el momento:
#include <iostream>
#include <fstream>
using namespace std;
typedef double Escalar;
typedef Escalar* Vector;
typedef Vector* Matriz;
void CreaMatriz(Matriz& M, int Filas, int Cols)
{
M= new Vector [Filas];
for(int f=0; f<Filas; f++)
M[f]= new Escalar[Cols];
}
void EscribeEnMatriz(Matriz M, int Filas, int Cols, string N)
{
for(int f=0; f<Filas; f++)
for(int c=0; c<Cols; c++)
{
cout<<N<<"("<<f+1<<","<<c+1<<")=";
cin>>M[f][c];
}
}
void EscribeMatrizEnArchivo(Matriz M, int Filas, int Cols)
{
// cout<<"La Matriz "<<nombre<<" es: "<<endl;
for(int f=0; f<Filas; f++)
{
for(int c=0; c<Cols; c++)
cout<<M[f][c]<<" ";
cout<<endl;
ofstream fout("C:\\Documents and Settings\\Administrador\\Escritorio\\matriz.txt");
fout<<M;
}
}
int main()
{
cout<<"Matriz"<<endl;
cout<<"Entre nombre de la Matriz:"<<endl;
string name;
cin>>name;
cout<<"Entre numero de Filas:"<<endl;
int fil;
cin>>fil;
cout<<"Entre numero de Columnas"<<endl;
int col;
cin>>col;
Matriz MA;
CreaMatriz(MA,fil,col);
EscribeEnMatriz(MA,fil,col,name);
EscribeMatrizEnArchivo(MA,fil,col);
system("PAUSE");
return 0;
}
Se que solo es una cosita que me falta y de hecho el error esta en la funcion "EscribeMatrizEnArchivo" si crea el archivo pero me esta escribiendo basura, nada que ver con la matriz. Espero puedan hecharle un ojo, y comentar alguna sugerencia.
Por su atencion. Gracias!