Answer to Question #201336 in C for Halil Esendaglı

Question #201336

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 o If user choice is L or l, use Linear Search o 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 Searc


1
Expert's answer
2021-05-31T16:36:35-0400
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<time.h>
#include<conio.h>




void fillArray(int values[],int size);
void displayArray(int values[],int size);
int linearSearch(int values[],int size, int targetValue);
void bubbleSort(int values[],int size);
int binarySearch(int values[],int size, int targetValue);


void main( void){
	//You are given two arrays int array_A[12];  int array_B[8]; 
	int array_A[12]; 
	int array_B[8]; 


	int index;
	int targetValue;
	char choice;


	srand((time(NULL)));
	fillArray(array_A,12);
	fillArray(array_B,8);
	printf("Array A\n");
	displayArray(array_A,12);
	printf("\nArray B\n");
	displayArray(array_B,8);




	bubbleSort(array_A,12);
	bubbleSort(array_B,8);


	printf("\n\nAfter sorting\n\n");
	printf("Array A\n");
	displayArray(array_A,12);
	printf("\nArray B\n");
	displayArray(array_B,8);


	printf("\n\nL,l - Linear Search\n");
	printf("B,b - Binary Search\n");
	printf("Your choice: ");
	scanf("%c",&choice);
	printf( "Enter the element you are looking for: " );
	scanf( "%d" , &targetValue );
	//If user choice is L or l, use Linear Search 
	if(choice=='L' || choice=='l'){	
		index = linearSearch(array_A,12, targetValue);
		printf("\nArray A:\n");
		if(index >= 0){
			printf( "The element you are looking for is %d and its in %d location.\n", array_A[index],index);
		}else{
			printf( "The number you are looking for is not in the array." );
		}
		index = linearSearch(array_B,8, targetValue);
		printf("\nArray B:\n");
		if(index >= 0){
			printf( "The element you are looking for is %d and its in %d location.\n", array_B[index],index);
		}else{
			printf( "The number you are looking for is not in the array." );
		}
	}else if(choice=='B' || choice=='b'){//If user choice is B or b, use Binary Search
		index = binarySearch(array_A,12, targetValue);
		printf("\nArray A:\n");
		if(index >= 0){
			printf( "The element you are looking for is %d and its in %d location.\n", array_A[index],index);
		}else{
			printf( "The number you are looking for is not in the array." );
		}
		index = binarySearch(array_B,8, targetValue);
		printf("\nArray B:\n");
		if(index >= 0){
			printf( "The element you are looking for is %d and its in %d location.\n", array_B[index],index);
		}else{
			printf( "The number you are looking for is not in the array." );
		}
	}else{
		printf("Wrong choice\n\n");
	}


	getch();
	getch();
}


//1. Random Number Generator 
void fillArray(int values[],int size){
	int i;
	//Use random number generator to fill the arrays 
	for(i = 0;i < size;i++ ){
		values[i] = rand() % 100;
	}
}
//2. Displaying the arrays 
void displayArray(int values[],int size){
	int i;
	for(i = 0;i < size;i++ ){
		printf("%d ", values[i]);
	}
}
//3. Linear Search 
int linearSearch(int values[],int size, int targetValue){
	int i;
	for(i = 0;i < size;i++ ){
		if( values[i]==targetValue){
			return i;
		}
	}
	return -1;
}
//4. Bubble Sort
void bubbleSort(int values[],int size){
	int i, j, temp;
	for( i = 1 ; i <= (size -1 ) ; i++){
		for( j = 0 ; j <= ( size -2 ); j++ ){
			if( values[ j ] > values[ j + 1 ] ){
				temp = values[ j ];
				values[ j ] = values[ j + 1 ];
				values[ j + 1 ] = temp;
			}
		}
	}
}
//5. Binary Search
int binarySearch(int values[],int size, int targetValue){
	int middle;
	int high= size -1;
	int low=0;


	while( high >= low ){
		middle = (int)(( high + low ) / 2 );
		if( targetValue > values[ middle ] )
			low = middle+1;
		if( targetValue < values[ middle ])
			high = middle-1;
		if( targetValue == values[ middle ] ){
			return middle ;
		}
	}
	return -1;
}




Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS