Hello, I think I'm almost finished with this program, it just needs a few things changed. When I choose any option from the menu, it begins spamming the screen. Anyone know what's wrong with it?
Code:
/* This program prompts user to choose an option from the menu
and performs the action on an array. */
#include <stdbool.h>
#include <stdio.h>
int fillArray (int range);
void printArray ();
int binarySearch (int list[], int end, int target, int *location);
int seqSearch (int list[], int last, int target, int *location);
void sortArray (int list[], int last);
int main()
{
bool sorted;
int option = -1;
int getOption;
int range;
int i;
int ary[50];
int list[50];
int last;
int target;
int *location;
int end;
while (option != 5)
{
printf("1- Fill Array\n2- Print Array\n3- Search Array\n4- Sort Array\n5- Quit\n");
scanf("%l", &option);
switch (option)
{
case 1:
fillArray(range);
sorted = false;
break;
case 2:
printArray();
break;
case 3:
if (sorted)
binarySearch(list[50], end, target, *location);
else
seqSearch(list[50], last, target, *location);
break;
case 4:
sortArray(list[50], last);
sorted = true;
break;
default:
printf("%c is an invalid choice.\n");
}
}
return 0;
}
int fillArray (int range)
{
srand(time(NULL));
range = (1, 999);
}
void printArray ()
{
int i;
int ary[50];
for(i=0; i<50; i++);
printf("%d", ary[i]);
return 0;
}
int binarySearch (int list[], int end, int target, int* location)
{
int first;
int mid;
int last;
first = 0;
last = end;
while (first <= last)
{
mid = (first + last) / 2;
if (target > list[mid])
first = mid + 1;
else if (target < list[mid])
last = mid - 1;
else
first = last + 1;
}
*location = mid;
return target == list[mid];
}
int seqSearch (int list[], int last, int target, int* location)
{
int looker;
int found;
looker = 0;
while (looker < last && target != list[looker])
looker++;
*location = looker;
found = (target == list[looker]);
return found;
}
void sortArray (int list[], int last)
{
int temp;
int walker;
for(int current = 0; current < last; current++)
{
for (int walker = last; walker > current; walker--)
if (list[walker] < list[walker - 1])
temp = list[walker];
list[walker] = list[walker - 1];
list[walker - 1] = temp;
}
return;
}
Thanks