I put the following code in, compiled the program and ran it, but it doesnt find and return any numbers to me! Can anyone point me in the right directions here? I've tried other boards but all they said was initialize my ints...I did and it didnt make any difference. Any help would be greatly appreciated!
Code:
#include <stdio.h>
#define MAXNUM 10
int search (int x, int v[], int n);
void main()
{
int a[MAXNUM];
int j=0;
int x=0;
int found=0;
int cnt=0;
printf("Enter a number to input into array or -1 to quit.\n");
scanf("%d",&x);
while (x != -1 && cnt<MAXNUM)
{
a[cnt++]=x;
{if(cnt==MAXNUM)
printf("The array is full.\nEnter -1 to begin searching the array for a number.\n");
else
printf("Enter a number to input into array or -1 to quit.\n");
scanf("%d", &x);
} /* End of if */
} /* End of while */
printf("Numbers entered %d\n", cnt);
/* Echo array */
for (j=0; j<cnt;j++)
printf("%d %d\n", j, a[j]);
printf("Enter a number to search for in the array or -1 to quit\n"); /* Get queries */
scanf("%d", &x);
while (x != -1)
{
found = search (x,a, cnt); /* Query array count */
if(found=-1)
printf("Number not found.\n");
else
printf("Number found at %d\n", found);
printf("Enter a number to search for or -1 to quit.\n");
scanf("%d", &x);
} /*End while */
} /* End main */
int search (int x, int v[], int n)
{
int cnt=0;
int found=0;
while(cnt<n && !found)
if (x==v[cnt])
found=1;
else
cnt++;
if(found)
return cnt; /*Where found */
else
return -1;
} /* End search */
Thanks so much!!!