Eastern Mediterranean University Department of Electrical and Electronic Engineering EENG 112 Introduction to Programming 5. Laboratory Handout Objective 1: To explore usage of Bubble Sort, Linear Search, Binary Search on arrays and multidimensional arrays. Definition: Bubble Sort: The bubble sort works by comparing each item in the array with the item next to it, and swapping them if required. The algorithm repeats this process until it makes a pass all the way through the array without swapping any items (in other words, all items are in the correct order). Linear Search: Search an array by checking items one at a time. Binary Search: Search a sorted array by repeatedly dividing the search interval in half. Begin with an interval covering the whole array. If the value of the search key is less than the item in the middle of the interval, narrow the interval to the lower half. Otherwise narrow it to the upper half. Repeatedly check until the value is found or the interval is empty. Prob. 1: The program below fills an array randomly in the range of 1-100. Then the program prompts the user to enter a value that he wants to search. The value that the user entered is searched linearly in the given array. Finally the program prints the location of the number in the array if the search key is in the array. Otherwise the user is warned that the number is not in the list. #include <stdio.h> #include <stdlib.h> #include <time.h> #include <conio.h> #define NUM_ITEMS 10 void fillArrayRandom( int numbers[] ); void showArray(int numbers[]); int Linear_Search(int numbers[],int searched_number); int main() { int numbers[NUM_ITEMS]; int search_number,count; srand( (unsigned)time( NULL ) ); //seed random number generator fillArrayRandom(numbers);//fill array with random integers //show the randomly filled integer array to the screen printf("\nOriginal Array.\n"); showArray(numbers); printf("\nEnter the number which you are looking for : "); scanf("%d",&search_number); count = Linear_Search(numbers,search_number); if( count != -1 ) printf("\nThe number which you are looking for is %d and it is in %d. location in the array" , search_number ,count ); else printf( "\nThe number that you are looking for is %d and is not in the array\n" , search_number ); getch(); } void fillArrayRandom( int numbers[] ) { int i; for( i = 0 ; i < NUM_ITEMS ; i++ ) { numbers[ i ] = rand() % 100 + 1; } } void showArray(int numbers[]) { int i; for (i = 0;i<NUM_ITEMS;i++) { printf("%6d ",numbers[i]); if(((i+1)%10)==0) printf("\n"); } } int Linear_Search(int numbers[],int searched_number) { int i; for (i = 0;i<NUM_ITEMS;i++) { if(searched_number == numbers[i]) { return( i ); } } return (-1); } Prob. 2: The program below prompts the user to enter an integer number in the range of 0 to 99, and shows index of the array in which entered integer is saved, searching is performed using binary search. #include<stdio.h> #include<stdlib.h> #include<math.h> #include<time.h> #include<conio.h> #define SIZE 64 void fill_array( int [ ] ); void display_array( int [ ] ); int binary_search( int [ ] , int ); void bubblesort( int [ ] ); void main( void ) { int array[ SIZE ] , number , i; srand((time(NULL))); fill_array( array ); bubblesort( array ); display_array( array ); printf( "Enter the element you are looking for : " ); scanf( "%d" , &number ); i = binary_search( array , number ); if( i >= 0 ) printf( "The element you are looking for is %d and its in %d. location" , array[ i ] , i ); else printf( "The number you are looking for is not in the array." ); getch(); } /*-------------------------------------------------------------------------------------------------------*/ void fill_array( int my_array[ ] ) { int j = 0; for( j = 0 ; j < SIZE ; j++ ) { my_array[ j ] = rand() % 100; } } /*-------------------------------------------------------------------------------------------------------*/ void display_array( int my_array[ ] ) { int k = 0; for( k = 0 ; k < SIZE ; k++ ) { printf( "%5d" , my_array[ k ] ); if( ( ( k + 1 ) % 8 ) == 0 ) printf( "\n" ); } } /*-------------------------------------------------------------------------------------------------------*/ void bubblesort(int my_array[ ] ) { int i, j, temp; for ( i = 1 ; i <= ( SIZE - 1 ) ; i++) { for ( j = 0 ; j <= ( SIZE - 2 ); j++ ) { if ( my_array[ j ] > my_array[ j + 1 ] ) { temp = my_array[ j ]; my_array[ j ] = my_array[ j + 1 ]; my_array[ j + 1 ] = temp; } } } } /*-------------------------------------------------------------------------------------------------------*/ int binary_search( int my_array[ ] , int num ) { int middle , high , low , flag = -1; high = SIZE - 1; low = 0; while( high >= low ) { middle = (int)( ( high + low ) / 2 ); if( num > my_array[ middle ] ) low = middle+1; if( num < my_array[ middle ] ) high = middle-1; if( num == my_array[ middle ] ) { return( middle ); } } return( flag ); } ASSIGNMENT DEFINITION 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 Find the largest number in each array according to the user choice o If user choice is L or l, use Linear Search o If user choice is B or b, use Binary Search Display the largest numbers in each array (ex the largest number in array A is 10, in array B is 20) Display the largest number. (the largest number is 20 in array B) You should have 5 separate functions for 1. Random Number Generator 2. Displaying the arrays 3. Linear Search 4. Bubble Sort (you can call the 2nd function to display sorted list) 5. Displaying the final result (largest in each array and the largest in general) Note: Your homework should be saved with your student number and attached as “.c” file (ex: 049521.c). Send the email to the following address: cprogramlab@gmail.com Homework submission date due after one week. You can discuss any difficulties you face in the homework with your lab instructor. But do not copy from your friends.