#include <stdlib.h>
#include <time.h>
#include <iomanip>
#include <fstream>
using namespace std;
void generarNumerosAleatorios(int [10][10],int , int );
void almacenaValoresArchivo(int [10][10],int , int);
void leervaloresArchivo(int [10][10],int, int);
void desplegarValores(int [10][10],int ,int);
int main()
{
int valores[10][10],n,m;
do
{
cout<<"introduce el numero de renglones o filas (1 a 10) = ? ";
cin>>n;
if(n<1 || n>10)
{
cout<<"error al introducir valor de renglon debe estar en el rango 1 a 10 :\n\n ";
system("Pause");
//system("cls");
}
}
while(n<0 || n>10);
do
{
cout<<"introduce el numero de columnas (1 a 10) = ? ";
cin>>m;
if(m<1 || m>10)
{
cout<<"error al introducir valor de columna debe estar en el rango 1 a 10 :\n\n ";
system("Pause");
//system("cls");
}
}
while(m<0 || m>10);
generarNumerosAleatorios(valores,n,m);
almacenaValoresArchivo(valores,n,m);
leervaloresArchivo(valores,n,m);
desplegarValores(valores,n,m);
return 0;
}
void generarNumerosAleatorios(int a[10][10],int nr, int nc)
{
int r,c;
srand(time(NULL));
for(r=0;r<nr;r++)
{
for(c=0;c<nc;c++)
{
a[r][c]=rand()%100+1;
}
cout<<endl;
}
}
void almacenaValoresArchivo(int a[10][10],int n, int m)
{
int r,c;
ofstream arch1;
arch1.open("valores.dat");
for(r=0;r<n;r++)
{
for(c=0;c<m;c++)
{
arch1<<setw(6)<<a[r][c];
}
arch1<<endl;
cout<<endl;
}
arch1.close();
}
void leervaloresArchivo(int m[10][10],int nr, int mc)
{
int r,c;
ifstream arch2;
arch2.open("valores.dat");
for(r=0;r<nr;r++)
{
for(c=0;c<mc;c++)
{
arch2>>m[r][c];
}
cout<<endl;
}
arch2.close();
}
void desplegarValores(int b[10][10], int n, int m)
{
int r,c;
cout<<"Despliegue de valores de una matriz de "<<n<<" x "<<m<<endl<<endl;
for(r=0;r<n;r++)
{
for(c=0;c<m;c++)
{
cout<<setw(6)<<b[r][c];
}
cout<<endl;
}
}
No hay comentarios:
Publicar un comentario