Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

3) PE06_03 (Linear Searching Arrays) Generate a random array with 15 integers fr

ID: 3908468 • Letter: 3

Question

3) PE06_03 (Linear Searching Arrays) Generate a random array with 15 integers from 50 to 99. Prompt the user to enter a valid integer search "key" between 50-99 and verify the user input. Use the linear search algorithm discussed in class to find the "key" value and array index location of the "key" value. If no value was found prompt the user to enter another number until a value is found. Display the random array to the screen only after the user finds a correct value for comparison. 4) PE 06 04 (Binary Searching Arrays) Generate a random array containing 15 integers from 10 to 25. Prompt the user to enter a valid integer "key" between 10 25 and verify the user input. Use the binary search algorithm discussed in class to find the value and location of the "key" value. If no value was found prompt the user to enter another number until a value is found. Display the random array to the screen only after the user finds a correct value for comparison 5) PE 06 05 (Two-by-Two Arrays) Create a two-by-two array A using random integers from1 to 10, a two-by-two array B using random integers from -10 to 0. Combine the elements of A + B using a for loop to create array C A B. Display array A, B and C to the screen for comparison. (Note that the sum of each element of A and B yields the corresponding element of C, c[0][e]-a[0]10]+b[e][e], etc.)

Explanation / Answer

ScreenShot

-----------------------------------------------------------

Program

//Header files for I/o and random generation
#include<stdio.h>
#include<stdlib.h>
//Linear search function prototype
int linearSearch(int arr[], int key);
//Main method
int main()
{
   //Variables for random array search number and for loop
   int randArray[15],i,num;
   //Generate random number between 50-99 and store in an array
   for (i = 0; i < 15; i++) {
       randArray[i]= (rand() % (99 - 50)) +50;
   }
   //User promt to enter a number
   printf("Enter a nymber between 50-99:");
   scanf("%d", &num);
   //Call search function
   i = linearSearch(randArray, num);
   //if search function not return 1 then prompt again until find the search number and it's index
   while (i != 1) {
       printf("Enter a nymber between 50-99:");
       scanf("%d", &num);
       i = linearSearch(randArray, num);
   }
   //Print array
   printf("Array is: ");
   for (i = 0; i < 15; i++) {
       printf("%d ", randArray[i]);
   }
   printf(" ");
    return 0;
}
//Linear search function implementation
int linearSearch(int arr[], int key) {
   int i, j;
   //loop to aerch the key , if found display the location
   for (i = 0; i < 15; i++)
   {
       if (arr[i] ==key)    /* If required element is found */
       {
           printf("%d is present at location %d. ", key, i + 1);
           return 1;
           break;
       }
   }
   return -1;

}

--------------------------------------------

Output

Enter a nymber between 50-99:52
Enter a nymber between 50-99:68
Enter a nymber between 50-99:64
64 is present at location 15.
Array is:
91 93 63 90 60 94 62 57 62 63 71 69 56 70 64
Press any key to continue . . .

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote