#include <stdio.h>
#include <stdlib.h>
#include <time.h>
/* Prototipo de la función de búsqueda secuencial */
int busquedaSecuencial(int arreglo[], int tamano, int valorBuscado, int *comparaciones);
int main() {
/* 1. Datos del estudiante y fecha/hora usando funciones de C */
time_t tiempoActual;
struct tm *infoTiempo;
char bufferTiempo[80];
time(&tiempoActual);
infoTiempo = localtime(&tiempoActual);
strftime(bufferTiempo, sizeof(bufferTiempo), "%d/%m/%Y %H:%M:%S", infoTiempo);
printf("============================================================\n");
printf(" ESTRUCTURACION DE DATOS - ACTIVIDAD 4: BUSQUEDA SECUENCIAL\n");
printf("============================================================\n");
printf("Estudiante: Ruth Cristina Montellano Morales\n");
printf("Codigo de estudiante: [303216497]\n");
printf("Fecha y hora de ejecucion: %s\n", bufferTiempo);
printf("============================================================\n\n");
/* 2. Solicitar tamaño del arreglo entre 12 y 18 elementos */
int n;
do {
printf("Ingrese la cantidad de elementos a introducir (entre 12 y 18): ");
if (scanf("%d", &n) != 1) {
while (getchar() != '\n'); /* Limpiar buffer de entrada */
n = 0;
}
if (n < 12 || n > 18) {
printf("[!] Error: El numero de elementos debe estar estrictamente entre 12 y 18.\n");
}
} while (n < 12 || n > 18);
int arreglo[18];
/* 3. Captura de los elementos */
printf("\n--- Captura de %d numeros enteros ---\n", n);
for (int i = 0; i < n; i++) {
printf("Elemento [%d]: ", i);
scanf("%d", &arreglo[i]);
}
/* Mostrar el arreglo registrado */
printf("\nArreglo registrado: [ ");
for (int i = 0; i < n; i++) {
printf("%d ", arreglo[i]);
}
printf("]\n\n");
/* 4. Fase de pruebas de búsqueda */
printf("============================================================\n");
printf(" FASE DE PRUEBAS DE BUSQUEDA\n");
printf("============================================================\n");
/* Pruebas de 3 números existentes */
printf("\n>>> 1. BUSQUEDA DE 3 VALORES EXISTENTES <<<\n");
for (int i = 1; i <= 3; i++) {
int valor, comparaciones = 0;
printf("\n[Prueba existente #%d] Ingrese un numero que EXISTA en el arreglo: ", i);
scanf("%d", &valor);
int pos = busquedaSecuencial(arreglo, n, valor, &comparaciones);
if (pos != -1) {
printf(" -> RESULTADO: Valor %d ENCONTRADO en el indice %d (posicion ordinal %d).\n", valor, pos, pos + 1);
} else {
printf(" -> RESULTADO: El valor %d NO se encontro (verifique que exista).\n", valor);
}
printf(" -> Comparaciones realizadas: %d\n", comparaciones);
}
/* Pruebas de 2 números NO existentes */
printf("\n>>> 2. BUSQUEDA DE 2 VALORES NO EXISTENTES <<<\n");
for (int i = 1; i <= 2; i++) {
int valor, comparaciones = 0;
printf("\n[Prueba no existente #%d] Ingrese un numero que NO EXISTA en el arreglo: ", i);
scanf("%d", &valor);
int pos = busquedaSecuencial(arreglo, n, valor, &comparaciones);
if (pos != -1) {
printf(" -> RESULTADO: El valor %d SI se encontro en el indice %d (se esperaba que no existiera).\n", valor, pos);
} else {
printf(" -> RESULTADO: Valor %d NO ENCONTRADO en el conjunto de datos.\n", valor);
}
printf(" -> Comparaciones realizadas: %d (recorrio todo el arreglo)\n", comparaciones);
}
printf("\n============================================================\n");
printf(" FIN DE LA EJECUCION DEL PROGRAMA\n");
printf("============================================================\n");
return 0;
}
/*
* Implementacion del Algoritmo de Busqueda Secuencial (Lineal)
* Recorre de forma continua elemento por elemento y cuenta cada comparacion.
*/
int busquedaSecuencial(int arreglo[], int tamano, int valorBuscado, int *comparaciones) {
*comparaciones = 0;
for (int i = 0; i < tamano; i++) {
(*comparaciones)++;
if (arreglo[i] == valorBuscado) {
return i; /* Retorna el indice del elemento encontrado */
}
}
return -1; /* Retorna -1 si tras comparar todos los elementos no se encontro */
}
To embed this project on your website, copy the following code and paste it into your website's HTML: