Documentos de Académico
Documentos de Profesional
Documentos de Cultura
Arreglo - Mi Primer Codigo
Arreglo - Mi Primer Codigo
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
#include<iostream>
using namespace std;
#define MAX 100
void numDatos(int &n,char mensaje[]);
void ingresoVector(float v[], int n,char mensaje[]);
void reporteVector(float v[], int n, char mensaje[]);
float mayor(float x[], int n);
float menor(float x[], int n);
float promedio(float x[], int n);
int main()
{
float x[MAX];
int n;
numDatos(n,"Numero de elementos del arreglo : ");
ingresoVector(x,n,"Ingreso de datos del arreglo");
reporteVector(x,n,"Vector Ingresado");
cout<<"El mayor : "<<mayor(x,n)<<endl;
cout<<"El menor : "<<menor(x,n)<<endl;
cout<<"El promedio es : "<<promedio(x,n)<<endl;
system("pause");
return 0;
}
void numDatos(int &n,char mensaje[])
{
do{
cout<<mensaje;
cin>>n;
}while(n<=0 || n>MAX);
}
void ingresoVector(float v[], int n,char mensaje[])
{
int i;
cout<<mensaje<<endl;
for(i=0;i<n;i++)
{
cout<<"v["<<i<<"]:";
cin>>v[i];
}
}
void reporteVector(float v[], int n, char mensaje[])
{
int i;
cout<<mensaje<<endl;
for(i=0;i<n;i++)
cout<<v[i]<<endl;
}
float mayor(float x[], int n)
{
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
int i;
float may;
may=x[0];
for(i=0;i<n;i++)
if(x[i]>may)
may=x[i];
return may;
}
float menor(float x[], int n)
{
int i;
float men;
men=x[0];
for(i=0;i<n;i++)
if(x[i]<men)
men=x[i];
return men;
}
float promedio(float x[], int n)
{
int i;
float s=0;
for(i=0;i<n;i++)
s=s+x[i];
return s/n;
}
1
2
3
4
5
6
7
8
#include<iostream>
#include<math.h>
using namespace std;
#define MAX 100
void numDatos(int &n,char mensaje[]);
void ingresoVector(float v[], int n,char mensaje[]);
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
suma=0;
for(i=0;i<n;i++)
suma=suma + pow(x[i]-p,2);
ds=sqrt(suma/(n-1));
return ds;
}
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
#include<iostream.h>
#define MAX 100
void main()
{
float x[MAX],temp;
clrscr();
int n,i,j;
do{
cout<<"Cantidad de elementos del arreglo : ";
cin>>n;
}while(n<=0 || n>MAX);
for(i=0; i<n;i++)
{
cout<<"x["<<i<<"]:";
cin>>x[i];
}
cout<<"Arreglo Ingresado"<<endl;
for(i=0; i<n;i++)
cout<<"x["<<i<<"]: "<<x[i]<<endl;
for(i=0,j=n-1;i<n/2;i++,j--)
{
temp=x[i];
x[i]=x[j];
x[j]=temp;
}
cout<<"Arreglo Invertido"<<endl;
for(i=0; i<n;i++)
cout<<"x["<<i<<"]: "<<x[i]<<endl;
}
29
30
31
4) Programa para ingresar 2 vectores de n elementos reales cada uno y reportar el producto
escalar de ellos.
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
#include<iostream>
using namespace std;
#define MAX 100
void numDatos(int &n,char mensaje[]);
void ingresoVector(float v[], int n,char mensaje[]);
void reporteVector(float v[], int n, char mensaje[]);
float productoEscalar(float x[], float y[], int n);
int main()
{
float x[MAX],y[MAX];
int n;
numDatos(n,"Numero de elementos de los vectores : ");
cout<<"Primer Vector"<<endl;
ingresoVector(x,n,"Ingreso de datos del primer vector");
cout<<"Segundo Vector"<<endl;
ingresoVector(y,n,"Ingreso de datos del segundo vector");
cout<<"El producto escalar es : "<<productoEscalar(x,y,n)<<endl;
system("pause");
return 0;
}
void numDatos(int &n,char mensaje[])
{
do{
cout<<mensaje;
cin>>n;
}while(n<=0 || n>MAX);
}
void ingresoVector(float v[], int n,char mensaje[])
{
int i;
cout<<mensaje<<endl;
for(i=0;i<n;i++)
{
cout<<"v["<<i<<"]:";
cin>>v[i];
}
}
void reporteVector(float v[], int n, char mensaje[])
{
int i;
cout<<mensaje<<endl;
for(i=0;i<n;i++)
cout<<v[i]<<endl;
}
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include<iostream>
#include<math.h>
using namespace std;
#define MAX 100
void numDatos(int &n,char mensaje[]);
void ingresoVector(float v[], int n,char mensaje[]);
void reporteVector(float v[], int n, char mensaje[]);
int busquedaSecuencial(float v[], int n,float dato);
int main()
{
float x[MAX],dato;
int n,p;
numDatos(n,"Numero de elementos del arreglo : ");
ingresoVector(x,n,"Ingreso de datos del arreglo");
reporteVector(x,n,"Vector Ingresado");
cout<<"dato a buscar : ";
cin>>dato;
p=busquedaSecuencial(x,n,dato);
if(p!=-1)
cout<<"El dato se encuentra en la posicion "<<p<<endl;
else
cout<<"El dato no se encuentra"<<endl;
system("pause");
return 0;
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
}
void numDatos(int &n,char mensaje[])
{
do{
cout<<mensaje;
cin>>n;
}while(n<=0 || n>MAX);
}
void ingresoVector(float v[], int n,char mensaje[])
{
int i;
cout<<mensaje<<endl;
for(i=0;i<n;i++)
{
cout<<"v["<<i<<"]:";
cin>>v[i];
}
}
void reporteVector(float v[], int n, char mensaje[])
{
int i;
cout<<mensaje<<endl;
for(i=0;i<n;i++)
cout<<v[i]<<endl;
}
int busquedaSecuencial(float v[], int n, float dato)
{
int i;
for(i=0;i<n;i++)
if(v[i]==dato) return i;
return -1;
}
1
2
#include<iostream>
#include<math.h>
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
for(i=0;i<n;i++)
cout<<v[i]<<endl;
}
int busquedaSecuencial(float v[], int n, float dato)
{
int i;
for(i=0;i<n;i++)
if(v[i]==dato) return i;
return -1;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include<iostream>
#include<math.h>
using namespace std;
#define MAX 100
void numDatos(int &n,char mensaje[]);
void ingresoVector(float v[], int n,char mensaje[]);
void reporteVector(float v[], int n, char mensaje[]);
int busquedaSecuencial(float v[], int n,float dato);
void eliminar(float v[], int &n, int p);
int main()
{
float x[MAX],dato;
int n,p;
numDatos(n,"Numero de elementos del arreglo : ");
ingresoVector(x,n,"Ingreso de datos del arreglo");
reporteVector(x,n,"Vector Ingresado");
cout<<"dato a eliminar : ";
cin>>dato;
p=busquedaSecuencial(x,n,dato);
if(p!=-1)
{
eliminar(x,n,p);
reporteVector(x,n,"Nuevo Arreglo");
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
}
else
cout<<"El numero no se encuentra"<<endl;
system("pause");
return 0;
}
void numDatos(int &n,char mensaje[])
{
do{
cout<<mensaje;
cin>>n;
}while(n<=0 || n>MAX);
}
void ingresoVector(float v[], int n,char mensaje[])
{
int i;
cout<<mensaje<<endl;
for(i=0;i<n;i++)
{
cout<<"v["<<i<<"]:";
cin>>v[i];
}
}
void reporteVector(float v[], int n, char mensaje[])
{
int i;
cout<<mensaje<<endl;
for(i=0;i<n;i++)
cout<<v[i]<<endl;
}
int busquedaSecuencial(float v[], int n, float dato)
{
int i;
for(i=0;i<n;i++)
if(v[i]==dato) return i;
return -1;
}
void eliminar(float v[], int &n, int p)
{
int i;
for(i=p;i<n-1;i++)
v[i]=v[i+1];
n=n-1;
}
71
72
73
74
75
76
8) Programa para ingresar n elementos en un arreglo y luego reportarlo en la pantalla.
Adems ingresar un nmero y la posicin donde desea insertarlo e insertarlo en el arreglo,
reportar el arreglo modificado.
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
#include<iostream>
using namespace std;
#define MAX 100
void
void
void
void
int main()
{
float x[MAX],dato;
int n,p;
numDatos(n,"Numero de elementos del arreglo : ");
ingresoVector(x,n,"Ingreso de datos del arreglo");
reporteVector(x,n,"Vector Ingresado");
cout<<"dato a insertar: ";
cin>>dato;
do{
cout<<"Posicion donde desea insertar : ";
cin>>p;
}while(p<0|| p>n);
insertar(x,n,dato,p);
reporteVector(x,n,"Nuevo Arreglo");
system("pause");
return 0;
}
void numDatos(int &n,char mensaje[])
{
do{
cout<<mensaje;
cin>>n;
}while(n<=0 || n>MAX);
}
void ingresoVector(float v[], int n,char mensaje[])
{
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
int i;
cout<<mensaje<<endl;
for(i=0;i<n;i++)
{
cout<<"v["<<i<<"]:";
cin>>v[i];
}
}
void reporteVector(float v[], int n, char mensaje[])
{
int i;
cout<<mensaje<<endl;
for(i=0;i<n;i++)
cout<<v[i]<<endl;
}
int busquedaSecuencial(float v[], int n, float dato)
{
int i;
for(i=0;i<n;i++)
if(v[i]==dato) return i;
return -1;
}
void insertar(float v[], int &n, float dato, int p)
{
int i;
for(i=n-1;i>=p;i--)
v[i+1]=v[i];
v[p]=dato;
n=n+1;
}
1
int main()
{
float x[MAX];
int n;
numDatos(n,"Numero de elementos del arreglo : ");
ingresoVector(x,n,"Ingreso de datos del arreglo");
reporteVector(x,n,"Vector Ingresado");
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
ordenar(x,n);
reporteVector(x,n,"Vector Ordenado");
system("pause");
return 0;
}
void numDatos(int &n,char mensaje[])
{
do{
cout<<mensaje;
cin>>n;
}while(n<=0 || n>MAX);
}
void ingresoVector(float v[], int n,char mensaje[])
{
int i;
cout<<mensaje<<endl;
for(i=0;i<n;i++)
{
cout<<"v["<<i<<"]:";
cin>>v[i];
}
}
void reporteVector(float v[], int n, char mensaje[])
{
int i;
cout<<mensaje<<endl;
for(i=0;i<n;i++)
cout<<v[i]<<endl;
}
void ordenar(float v[], int n)
{
int i,j;
float temp;
for(i=0;i<n-1;i++)
for(j=i+1;j<n;j++)
if(v[i]>v[j])
{
temp=v[i];
v[i]=v[j];
v[j]=temp;
}
}
131
132
133
134
135
136
137
138
139
140
141
142
143