Fandom

Scratchpad

PLE:Unidad4Ejercicio15

215,853pages on
this wiki
Add New Page
Discuss this page0 Share

Ad blocker interference detected!


Wikia is a free-to-use site that makes money from advertising. We have a modified experience for viewers using ad blockers

Wikia is not accessible if you’ve made further modifications. Remove the custom ad blocker rule(s) and the page will load as expected.

/*
Programa:         Unidad4Ejercicio15.cpp
Fecha:            10/01/2006
Descripción:      Búsqueda secuencial sobre un array.

*/
#include <iostream>
#include <conio>
#include <stdlib>
using namespace std;

const int MAX = 100;
int busca(int array[], int num);
void muestra(int array[]);

void main()
{
      int num[MAX], i, n, pos;
      // Asignamos valores aleatorios
      for(i = 0; i < MAX; i++)
            num[i] = random(100);

      // Mostramos en pantalla el array
      muestra(num);

      cout << " Introduzca un numero ";
      cin >> n;

      pos = busca(num, n);

      if(pos == -1)
            cout << "No existe el elemento buscado ";
      else
            cout << "El numero buscado ocupa la posicion " << pos;

      cin.ignore();
      cin.get();
}

int busca(int array[], int num)
{
	int i=0,pos = -1;
	while (i < MAX && pos==-1)
	{
		if(array[i]==num)
			pos = i;
		i++;
	}
	return pos;
}

void muestra(int array[])
{
      int i;
      for(i = 0; i < MAX; i++)
            cout << array[i] << '\t';
}

Also on Fandom

Random wikia