Untitled
unknown
plain_text
2 years ago
1.8 kB
2
Indexable
#include <iostream> #include <time.h> #include <stdlib.h> using namespace std; void vecRandom(int vec[], int tam, int limInf1, int LimSup1, int limInf2, int LimSup2); void printVec(int vec[], int tam); bool repetidos(int vec[], int tam); void Ordenar(int vec[], int tam); void intercambio(int vec[], int tam, int pos1, int pos2); int tam = 6; int LimInf1 = 1; int LimSup1 = 43; int LimInf2 = 1; int LimSup2 = 16; int main() { int* vector = new int[tam]; srand(time(NULL)); vecRandom(vector, tam, LimInf1, LimSup1, LimInf2, LimSup2); cout << "Vector generado" << endl; printVec(vector, tam); Ordenar(vector, tam); cout << "Vector ordenado" << endl; printVec(vector, tam); } void vecRandom(int vec[], int tam, int LimInf1, int LimSup1, int limInf2, int LimSup2) { do { for (int i = 0; i < tam-1; i++) { vec[i] = LimInf1 + rand() % (LimSup1 - LimInf1 + 1); } } while (repetidos(vec, tam)); vec[tam - 1] = LimInf2 + rand() % (LimSup2 - LimInf2 + 1); } void printVec(int vec[], int tam) { for (int i = 0; i < tam; i++) { cout << vec[i] << " "; } cout << endl; } bool repetidos(int vec[], int tam) { bool r = false; for (int a = 0; a < tam - 1; a++) { for (int b = a + 1; b < tam; b++) { if (vec[a] == vec[b]) { r = true; } } } return r; } void intercambio(int vec[], int tam, int pos1, int pos2) { int aux = vec[pos1]; vec[pos1] = vec[pos2]; vec[pos2] = aux; } void Ordenar(int vec[], int tam) { for(int i = 0; i < tam;i++){ for (int j = 0; j < tam - 2; j++) { if (vec[j] > vec[j + 1]) { intercambio(vec, tam, j, j + 1); } } } }
Editor is loading...