- #1
leroyjenkens
- 616
- 49
Hello, this is sort of a big program and I wrote out the program, but I'm unsure about a lot of things.
The program is supposed to give the user a menu of choices:
1) Fill the array with random numbers.
2) Print the array.
3) Search the array.
4) Sort the array.
5) Quit.
Option one is supposed to fill an array with 50 random numbers. Option 2 prints the array. Option 3 searches the array. But it needs to use two different search methods depending on if the array is sorted or not. If it's not sorted, I'm supposed to use sequential searching, if it's sorted, then I'm supposed to use binary searching. Then 4 sorts the array. I chose bubble sorting arbitrarily.
I'm not sure how to make the program do one search method over another, so I just made two different functions for the two different search methods. How do I make it do the sequential search if it's unsorted and the binary search if it's sorted?
Thanks.
The program is supposed to give the user a menu of choices:
1) Fill the array with random numbers.
2) Print the array.
3) Search the array.
4) Sort the array.
5) Quit.
Option one is supposed to fill an array with 50 random numbers. Option 2 prints the array. Option 3 searches the array. But it needs to use two different search methods depending on if the array is sorted or not. If it's not sorted, I'm supposed to use sequential searching, if it's sorted, then I'm supposed to use binary searching. Then 4 sorts the array. I chose bubble sorting arbitrarily.
I'm not sure how to make the program do one search method over another, so I just made two different functions for the two different search methods. How do I make it do the sequential search if it's unsorted and the binary search if it's sorted?
Code:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int getOption (void)
{
int option;
printf("Fill Array With Random Numbers\n1- Print the Array\n2- Search the Array\n3- Sort the Array\n4- Quit\n\n");
scanf("%d", &option);
if (option<=0 || option>4)
printf("\nError: That number is not an option.\n");
if (option=4)
printf("Bye");
return option;
}
int fillArray (int option)
{
int range;
int getOption (void);
srand(time(NULL));
range = (999 - 0) + 1;
}
int printArray (int range)
{
printf("%d", rand() % range);
}
bool seqSearch (int list[], int last, int target, int* location)
{
int looker;
bool found;
looker = 0;
while (looker < last && target != list[looker])
looker++;
*location = looker;
found = (target == list[looker]);
return found;
}
bool 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];
}
void sortArray (int list[], int last)
{
int temp;
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;
}
}
Thanks.