Hello,I am new to the forum.i have an assignment and I could do only the half and need help to finish it.Here is the question:
Write a C program which does the following;
You are given two arrays
int array_A[12];
int array_B[8];
Use random number generator to fill the arrays
Display the content of the arrays
Ask the user for a search key and also ask for the searching method
If user choice is L or l, use Linear Search
If user choice is B or b, use Binary Search
Display the index of the search key entered by the user or display the error message if
the search key is not found.
You should have 5 separate functions for
1. Random Number Generator
2. Displaying the arrays
3. Linear Search
4. Bubble Sort
5. Binary Search
And here is my code:
Code:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int linears(int array_A[],int array_B[],int key);
int binarys(int array_A[],int array_B[],int key,int low,int high);
int bubles(int int array_A[],int array_B[]);
int main() {
int array_A[12]={};
int array_B[8]={};
int i;
int key;
int count1;
int count2;
char searchtype;
srand(time(NULL));
printf("Array A\n");
for(i=0;i<12;i++)
{
array_A[i]=1+rand()%100;
printf("%5d",array_A[i]);
}
printf("\nArray B\n");
for(i=0;i<8;i++)
{
array_B[i]=1+rand()%100;
printf("%5d",array_B[i]);
}
printf("\nEnter A number to search:\n");
scanf("%d",&key);
printf("\nEnter search type:\n");
scanf(" %c",&searchtype);
switch(searchtype) {
case 'L':
case 'l':
count1=linears(array_A,array_B,key);
if(count1!=-1)
printf("Searched key%d located at%d",key,count1+1);
else{
printf("Searched key%d is not presented in the array.\n",key);
}
case 'B':
case 'b':
count2=binarys(array_A,array_B,key,low,high1,high2);
if(count2!=-1)
printf("Searched key%d located at%d",key,array_A,count2+1);
else{
printf("Searched key%d is not presented in the array.\n",key);
}
break;
}
return 0;
}
int linears(int array_A[],int array_B[],int key) {
int i;
int j;
for(i=0;i<12;i++)
{
if(key==array_A[i])
return i;
}
for(j=0;j<8;j++)
{
if(key==array_B[j])
return j;
}
return -1;
}