-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvectores.cpp
70 lines (59 loc) · 1.49 KB
/
vectores.cpp
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
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
#include <iostream>
using namespace std;
void cargar(double a[], int t);
void mostrar(double a[], int t);
void invertir(double a[], double b[], int t);
int main()
{
int n;
cout << "Ingrese el número de elementos en el vector: ";
cin >> n;
double arr[n];
cargar(arr, n);
// Crear un nuevo vector para almacenar la inversa
double arrInvertido[n];
// Invertir el vector y mostrarlo
invertir(arr, arrInvertido, n);
cout << "Vector Original:" << endl;
mostrar(arr, n);
cout << "Vector Invertido:" << endl;
mostrar(arrInvertido, n);
return 0;
}
void cargar(double a[], int t)
{
cout << "Ingrese los elementos del vector: ";
for (int i = 0; i < t; i++)
{
cin >> a[i];
}
}
void mostrar(double a[], int t)
{
for (int i = 0; i < t; i++)
{
cout << a[i] << endl;
}
}
void invertir(double a[], double b[], int t)
{
// Invertir el vector 'a' en el vector 'b'
for (int i = 0; i < t; i++) {
b[i] = a[t - 1 - i];
}
}
void invertirVectorUsandoMismoVector(double a[], int t) {
// Invertir el vector 'a' en el mismo vector 'a'
double aux;
for (int i = 0; i < t / 2; i++) {
aux = a[i]; // se almacena el primer elemento
a[i] = a[t - 1 - i]; // se sobreescribe el primer elemento con el último
a[t - 1 - i] = aux; // se sobreescribe el último elemento con el primero
}
}
int main() {
double a[10];
int n;
invertirVectorUsandoMismo(a, n);
mostrar(a, n);
}