How about, very good day, I've been starting to program, and I've had a problem with a program regarding functions.
It turns out that they ask me to make a program that stores elements in an array, and then asks the user to enter a number to look for it in the array. If the number entered is in the array, show it, and also show the location of that number.
The program works well for 4 elements in the array, but when you enter 5 elements, there is a problem, because by showing the location of the number entered for your search in the array, it always shows me that it is in position 4.
I hope you can help me, thank you very much.
I append the code below.
#include <stdio.h>
#include <iostream>
void buscarElemento(int a[], int size, int element_Busqueda);
int main()
{
int n; // Variable que determinará el número de elementos del arreglo.
int i; // Variable para recorrer el arreglo.
int elemento;
int array[n]; // Creamos un arreglo de enteros, el número de elementos del arreglo
// será el número asignado a la variable 'n'.
printf("Ingresa el numero de elementos del arreglo: ");
scanf("%d",&n);
for(i=0; i<n; i++)
{
printf("Ingresa el elemento numero %d del arreglo: ", (i+1) );
scanf("%d",&array[i]);
}
printf("\n\n");
printf("Ingresa el elemento a buscar en el arreglo: ");
scanf("%d",&elemento);
buscarElemento(array, n, elemento);
system("pause");
return 0;
}
void buscarElemento(int a[], int size, int element_Busqueda)
{
int j;
int b = 0;
int hold1,hold2;
while(b == 0)
{
for(j=0; j<size; j++)
{
if(element_Busqueda == a[j])
{
hold1 = a[j];
hold2 = j;
b=1;
}
}
}
if(b==1)
{
printf("El elemento encontrado es %d y se encuentra en la posicion %d.",hold1,hold2);
}
}