Advertisement
Guest User

Untitled

a guest
Oct 25th, 2016
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.10 KB | None | 0 0
  1. #include <iostream>
  2. #include <conio.h>
  3. using namespace std;
  4. using namespace System;
  5. void generar_datos(int *Vnumeros, int nelementos)
  6. {
  7.     Random r;
  8.     for (int i = 0; i < nelementos; i++)
  9.         Vnumeros[i] = r.Next(1, 101);
  10. }
  11. void imprimir_datos(int *Vnumeros, int nelementos)
  12. {
  13.     int i;
  14.     cout << "impresion de datos generados\n";
  15.     for (i = 0; i < nelementos; i++)
  16.         cout << Vnumeros[i] << endl;
  17. }
  18. int mayor_elemento(int *Vnumeros, int nelementos)
  19. {
  20.     int mayor, i;
  21.     mayor = Vnumeros[0];
  22.     for (i = 1; i < nelementos; i++)
  23.         if (Vnumeros[i]>mayor)
  24.             mayor = Vnumeros[i];
  25.     return mayor;
  26. }
  27. int menor_elemento(int *Vnumeros, int nelementos)
  28. {
  29.     int menor, i;
  30.     menor = Vnumeros[0];
  31.     for (i = 1; i < nelementos; i++)
  32.         if (Vnumeros[i]<menor)
  33.             menor = Vnumeros[i];
  34.     return menor;
  35. }
  36. int buscar_elemento(int *Vnumeros, int nelementos, int datobuscado)
  37. {
  38.     int posicion, i;
  39.     posicion = -1;
  40.     for (i = 0; i < nelementos; i++)
  41.         if (Vnumeros[i] == datobuscado)
  42.             posicion = i;
  43.     return posicion;
  44. }
  45. int contar_repeticiones(int *Vnumeros, int nelementos, int datobuscado)
  46. {
  47.     int contador, i;
  48.     contador = 0;
  49.     for (i = 0; i < nelementos; i++)
  50.         if (Vnumeros[i] == datobuscado)
  51.             contador++;
  52.     return contador;
  53. }
  54. void main()
  55. {
  56.     int *Vnumeros;
  57.     int nelementos,datobuscado;
  58.     do
  59.     {
  60.         cout << "ingrese cantidad de elementos: "; cin >> nelementos;
  61.     } while (nelementos < 1 || nelementos>25);
  62.     Vnumeros = new int[nelementos];
  63.     generar_datos(Vnumeros, nelementos);
  64.     imprimir_datos(Vnumeros, nelementos);
  65.     cout << "el mayor elemento es " << mayor_elemento(Vnumeros, nelementos) << endl;
  66.     cout << "el menor elemento es " << menor_elemento(Vnumeros, nelementos) << endl;
  67.     cout << "ingrese dato a buscar: "; cin >> datobuscado;
  68.     if (buscar_elemento(Vnumeros, nelementos, datobuscado) != -1)
  69.         cout << "el dato se encuentra en " << buscar_elemento(Vnumeros, nelementos, datobuscado) << endl;
  70.     else
  71.     cout << "el dato no se encuentra\n";
  72.     cout << "ingrese dato a buscar: "; cin >> datobuscado;
  73.     cout << "el dato se repite : " << contar_repeticiones(Vnumeros, nelementos, datobuscado);
  74.     delete[]Vnumeros;
  75.     _getch();
  76. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement