Sei sulla pagina 1di 15

ARRAYS (arreglos)

Ejercicio 3:

#include <iostream>
using namespace std;
const int MAX=30;
void func1(int n, int num[MAX]);
void func2(int n, int num[]);
void func3(int n, int *num);
int main()
{
int i, n, num [MAX];
cout << "Cuantos datos enteros para el arreglo?" << endl;
cin>>n;
cout<<"ingrese "<<n<<" datos:"<<endl;
for (i=0; i<n; i++)
cin>>num[i];
cout<<"\n Llamada a func1().como un array delimitado:"<<endl;
func1(n,num);
cout<<"llamada a func2(). Como un array no delimitado:"<<endl;
func2(n,num);
cout<<"llamada a func3. como una referencia o puntero:"<<endl,

func3(n,num);
return 0;
}
void func1 (int n, int num [MAX])
{
for(int i=0; i<n; i++)
cout<<num[i]<<" ";
cout<<endl;
}
void func2 (int n, int num [])
{
for(int i=0; i<n; i++)
cout<<num[i]<<" ";
cout<<endl;}
void func3 (int n, int *num )
{
for(int i=0; i<n; i++)
cout<<num[i]<<" ";
cout<<endl;
}

Ejercicio 4:

#include <iostream>
#include <cmath>
const int MAX=50;
void leerPuntos(float a[], float b[], int n);
void escribirpuntos(float a[], float b[], int n);
void distancias(float a[], float b[], int n);
using namespace std;

int main()
{
int n;
float x[MAX], y[MAX];
cout << "cuantos puntos en el array?:"<< endl;
cin>>n;
/*leerPuntos(x,y,n);
escribirpuntos(x,y,n);
distancias(x,y,n);*/
return 0;

}
void leerPuntos(float a[], float b[], int n)
{
cout<<"\nIngrese "<<n<<" puntos y sus respectivas coordenadas"<<endl;
for (int i=0; i<n; i++)
{
cout<<"coordenadas X e Y del punto "<<i+1<<" : ";
cin>>a[i]>>b[i];
}
}
void escribirpuntos(float a[], float b[], int n)
{
cout<<"\npuntos ingresados: "<<endl;
for ( int i=0; i<n; i++)
cout<<"Punto"<<i+1<<" :( "<<a[i]<<", "<<b[i]<<")"<<endl;
cout<<endl;
}
void distancias(float a[], float b[], int n)
{
float dist(n);
float max=-32767, min =32768;
float xmin, ymin, xmax, ymax;

for (int i=0; i<n; i++)


{
dist[i]=sqrt(a[i]*a[i]+b[i]*b[i]);
if (dist [i]<min)
{min =dist [i];
xmin=a[i];
ymin=b[i];

}
if (dist[i]>max)
{max=dist[i];
xmax=a[i];
ymax=b[i];}
}
cout<<"distancia mas cercana al origen: "<<min<<endl;
cout<<"Se dio en el punto: ("<<xmin<<","<<ymin<<")"<<endl;
cout<<"\nDistancia mas lejana al origen: "<<max<<endl;
cout<<"se dio en el punto:("<<xmax<<","<<ymax<<")"<<endl<<endl;
}

Parte II: Arrays Bidimensionales:


Ejercicio

#include <iostream>
using namespace std;
void leermatriz (int notas [5][4]);

void escribematriz (int notas [5][4]);


int mayornota (int notas [5][4]);
void promalum (int notas [5][4]);
void promcurso (int notas [5][4]);

int main()
{
int notas [5][4], mayor;
cout << "ingreso de notas" << endl;
leermatriz(notas);
escribematriz(notas);
mayor = mayornota(notas);
cout<<"la mayor nota es:"<<mayor1
1
<<endl;
promalum(notas);
promcurso(notas);
return 0;
}
void leermatriz(int notas[5][4])
{
for(int i=0; i<5; i++)
{for(int j=0; j<4; j++)
{
cout<<"ingrese nota ("<<i<<")("<<j<<"):";
cin>>notas[i][j];
}
}
}
void escribematriz(int notas [5][4])

{ //int mayor;
for(int i=0; i<5; i++)
{for(int j=0; j<4; j++)
cout<<notas[i][j]<<" ";
cout<<endl;
}
//return mayor;
}
int mayornota(int notas[5][4])
{
int mayor=-100;
for (int i=0; i<5; i++)
{for(int j=0; j<4;j++)
{
if (notas[i][j])
mayor=notas[i][j];
}

}
return mayor;
}

void promalum(int notas [5][4])


{
int acum;

float prom;

for(int i=0; i<5; i++)


{ acum=0;
for(int j=0; j<4; j++)
acum=acum+notas[i][j];
prom=acum/4;

cout<<"promedio de alumno "<<i+1<<"es:"<<prom<<endl;


}
}
void promcurso(int notas [5][4])
{
int acum; float prom;
for(int i=0; i<4; i++)
{ acum=0;
for(int j=0; j<5; j++)
acum=acum+notas[i][j];
prom=acum/5;
cout<<"promedio de curso"<<i+1<<"es: "<<prom<<endl;
}
}
Ejemplo 5:

#include <iostream>
#include <stdlib.h>
using namespace std;
const int MAX=20;
void leer_mat (int n, int elem[][MAX]);
void escribe_mat (int n, int elem[][MAX]);
void suma_mat (int n, int a[][MAX], int b[][MAX], int s[][MAX]);
void leer_mat(int elem[MAX][MAX], int n)

{
cout << "Ingrese "<<n<<"datos por fila:\n"<<endl;
for (int i=0; i<n; i++)
{ cout<<"fila "<<i<<" ";
for (int j=0; j>n; j++)
cin>>elem[i][j];
}
}
void escribe_mat(int elem[MAX][MAX], int n)
{
for (int i=0; i<n; i++)
{
for (int j=0; j<n; j++)
{
cout<<elem[i][j]<<" ";
}
cout<<endl;
}
}
void suma_mat(int a[][MAX], int b[][MAX], int s[][MAX], int n)
{
for (int i=0; i<n; i++)
{
for (int j=0; j<n; j++)
s[i][j]=a[i][j]+b[i][j];
}
}
int main ()
{
int a[MAX][MAX], b[MAX][MAX], s[MAX][MAX], n;

cout<<"ingrese orden N de las matrices: ";


cin>>n;
cout<<"\nMatriz A: ";
leer_mat(a, n);
cout<<"\nMatriz B: ";
leer_mat(b, n);
cout<<"\nMatriz Suma S=A+B: "<<endl;
escribe_mat(s, n);
cout<<"\nFin del programa!"<<endl;
return 0;
}
Ejemplo 6:

#include <iostream>
using namespace std;
const int MAX=20;
void leer_mat (int elem[][MAX], int n, int m);
void escribe_mat (int elem[][MAX], int n, int m );
void prod_mat (int a[][MAX], int b[][MAX], int s[][MAX], int p[][MAX], int n, int l,
int m );
void leer_mat(int elem[MAX][MAX], int n, int m)
{

cout << "Ingrese "<<m<<"datos por fila:\n"<<endl;


for (int i=0; i<n; i++)
{ cout<<"fila "<<i<<" ";
for (int j=0; j>m; j++)
cin>>elem[i][j];
}
}
void escribe_mat(int elem[][MAX], int m, int n)
{
for (int i=0; i<n; i++)
{for (int j=0; j<m; j++)
{
cout<<elem[i][j]<<" ";
}
cout<<endl;
}
}
void prod_mat(int a[][MAX], int b[][MAX], int p[][MAX], int n, int m, int l)
{
for (int i=0; i<n; i++)
{for (int j=0; j<m; j++)
//p[i][j]=0;
for (int k=0; k<l; k++)
p[i][j]=p[i][j]+a[i][k]*b[k][j];
}
}
int main ()
{
int a[MAX][MAX], b[MAX][MAX], p[MAX][MAX], n, l, m;
cout<<"ingrese Numero de filas y de columnas de A y ";

cout<<"tambien numero de columnas de B: ";


cin>>n>>l>>m;
cout<<"\nMatriz A: ";
leer_mat(a,n,l);
cout<<"\nMatriz B: ";
leer_mat(b,l,m);
prod_mat(a,b,p,n,l,m);
cout<<"\nMatriz A"<<endl;
escribe_mat(a,n,l);
cout<<"\nMatriz B"<<endl;
escribe_mat(b,n,l);
cout<<"\nMatriz producto P=A*B:"<<endl;
escribe_mat(p,n,m);
cout<<"\nFin del programa!"<<endl;
return 0;
}
Ejemplo 7:
#include <iostream>
#include <cmath>
const int MAX=20;
void leer_vect(int x[], int n);
void leer_mat(int n[], float elem[][MAX]);
void escribe_mat(int n, float elem[][MAX]);
float norma(int x[MAX], int n);
double norma(int a[MAX],int n);
float norma(int x[MAX],int n, int p);
float norma(int a[MAX],int n);
using namespace std;

int main()

{
int n, x[MAX], p;
float a[MAX][MAX];
cout << "\nNorma euclidiana del vector X="<<norma(x,n)<< endl;
cout<<"Ingree el valor de p: ";
cin>>p;
cout<<"\nNorma-"<<p<<"del vector x ="<<norma(x,n,p)<<endl;
cout<<"\nIngrese orden N de la matriz: ";
cin>>n;
cout<<"Matriz A: ";
leer_mat(n,a);
cout<<"\nNorma euclidiana de la matriz A = "<<norma(a,n)<<endl;
cout<<"\nNorma maxima de la columna suma= "<<norma(a,n)<<endl;
return 0;
}
void leer_vect(int x[], int n)
{
cout<<"Ingrese "<<n<<"datos para el vector:\n";
for (int i=0; i<n; i++)
cin>>x[i];
}
void leer_mat(int n, float elem[][MAX])
{ cout<<"Ingrese "<<n<<" datos por fila:\n";
for (int i=0; i<n; i++)
{
cout<<"Fila "<<i<<": ";
for (int j=0; j<n; j++)
cin>>elem[i][j];
}
}

void escribe_mat(int n, float elem[][MAX])


{ for (int i=0; i<n; i++)
{
for (int j=0; j<n; j++)
{
cout<<elem[i][j]<<" ";
}
cout<<endl;
}
}
float norma(int x[MAX], int n)
{
int suma=0;
for(int i=0; i<n; i++)
suma+=x[i]*x[i];
return (sqrt (suma));
}
double norma(int a[][MAX], int n)
{
int suma=0;
for(int i=0; i<n; i++)
for (int j=0; j<n; j++)
suma+=a[i][j]*a[i][j];
return (sqrt (suma));
}
float norma(int x[MAX], int n, int p)
{
float suma=0;
for(int i=0; i<n; i++)
suma+=pow(fabs(x[i]),p);

return (pow(suma, 1.0/p));


}
float norma(float a[][MAX], int n)
{
float suma, max=-1000;
for(int j=0; j<n; j++)
{suma=0;
for (int i=0; i<n; i++)
suma +=fabs(a[i][j]);
if (suma>max)
max=suma;
return max;
}

Potrebbero piacerti anche