Question

Unit 4 Assignment 5: Coding Project using C# Binary Search Scenario Use the same integer array...

Unit 4 Assignment 5: Coding Project using C#

Binary Search

Scenario

Use the same integer array named partNumbers that you used for task 3. Sort the array in ascending order.

1065, 1095, 1075, 1055, 1056, 1090, 1098, 1088, 1097, and 1078.

Java: use Array.sort()

C#: use Array.Sort()

PHP: use sort()

Write code that asks the user to enter two part numbers. For C#, use console input.

Implement a binary tree search function called binarySearch() to search the array for the part number entered. Use the following variable names: use a variable named x to represent the array passed to the function and an integer variable named key to represent the key being searched for.

Pass to this function the array partNumbers, the lower array index, the upper array index, and the variable key.

Define an integer variable in the binarySearch function called position. Use this variable for the value of the index of the middle position.

If the part number is found, display to the console the index that part number is in.

Depending on whether or not the part number exists, display to the console that the part number is or is not in stock.

To test your program and get the expected output, enter 1088 for the first value and 1067 for the second value.

Expected Output

The part number was found at index 5.

1088 is in stock.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Please find the answer below, all the details are mentioned in the comments.

Program.cs

using System;

//main class
public class Program{
   static public void Main (){
       //array to hold the part numbers
       int[] partNumbers = { 1065, 1095, 1075, 1055, 1056, 1090, 1098, 1088, 1097, 1078};
      
       //Sort the array first
       Array.Sort(partNumbers);
      
       //get the 2 part input from the user
       int part1 = Convert.ToInt32(Console.ReadLine());
       int part2 = Convert.ToInt32(Console.ReadLine());
      
       //get the index from the binarySearch function
       int index = binarySearch(partNumbers,0,partNumbers.Length-1,part1);
      
       //if element is present then index would be some value else -1
       if(index != -1){
       Console.WriteLine("The part number was found at index " + index + ".");
       Console.WriteLine(part1 + " is in stock.");
       } else{
       Console.WriteLine(part2 + " is not in stock.");
       }
      
       //search for the second part from the user
       index = binarySearch(partNumbers,0,partNumbers.Length-1,part2);
       if(index != -1){
       Console.WriteLine("The part number was found at index " + index + ".");
       Console.WriteLine(part2 + " is in stock.");
       } else{
       Console.WriteLine(part2 + " is not in stock.");
       }
      
   }
  
   //function for the binary Search
   static public int binarySearch(int [] x, int lower, int upper, int key){
   //to check if the valid partition is possible or not
   if(lower < upper){
   //get the middle element of the array
   int position = (lower) + (upper - lower) / 2;
   //if middle element is greater then the key then Search in the lower part
   if(x[position] > key){
   return binarySearch(x,lower,position,key);
   }
   //if middle element is lesser then the key then Search in the upper part
   else if(x[position] < key){
   return binarySearch(x,position + 1,upper,key);
   }
   //if middle element is the key then return the index
   else if(x[position] == key){
   return position;
   }
   // else return -1
   return -1;
   }
   // else return -1
   return -1;
   }
}

Output:

Please let us know in the comments if you face any problems.

Add a comment
Know the answer?
Add Answer to:
Unit 4 Assignment 5: Coding Project using C# Binary Search Scenario Use the same integer array...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • #include <stdio.h> // Define other functions here to process the filled array: average, max, min, sort,...

    #include <stdio.h> // Define other functions here to process the filled array: average, max, min, sort, search // Define computeAvg here // Define findMax here // Define findMin here // Define selectionSort here ( copy from zyBooks 11.6.1 ) // Define binarySearch here int main(void) { // Declare variables FILE* inFile = NULL; // File pointer int singleNum; // Data value read from file int valuesRead; // Number of data values read in by fscanf int counter=0; // Counter of...

  • In C++ 1. Write a program that allows a user to enter 10 integer values from...

    In C++ 1. Write a program that allows a user to enter 10 integer values from the keyboard. The values should be stored in an array. 2. The program should then ask the user for a number to search for in the array: The program should use a binary search to determine if the number exists in a list of values that are stored in an array (from Step #1). If the user enters a number that is in the...

  • This project will allow you to practice with one dimensional arrays, function and the same time...

    This project will allow you to practice with one dimensional arrays, function and the same time review the old material. You must submit it on Blackboard and also demonstrate a run to your instructor. General Description: The National Basketball Association (NBA) needs a program to calculate the wins-losses percentages of the teams. Write a complete C++ program including comments to do the following: Create the following: •a string array that holds the names of the teams •an integer array that...

  • Just Q3 and Q4 Q1] Write a C function to implement the binary search algorithm over...

    Just Q3 and Q4 Q1] Write a C function to implement the binary search algorithm over an array of integer numbers and size n. The function should return the index of the search key if the search key exists and return - 1 if the search key doesn't exist. [10 Points] Q2] Write a C function to implement the selection sort algorithm, to sort an array of float values and size n. The function should sort the array in ascending...

  • Using c++ 1 of 2 Assignment 5 Lab Section 3 write a program create a vector...

    Using c++ 1 of 2 Assignment 5 Lab Section 3 write a program create a vector with random numbers. Use merge sort to reorder the vector Prompt user to enter a number to search in the vector. If there are more than one number in the vector equal to the search value, display the indices remove the repeated numbers. If found just one matching number, display the index. If no matching number, prompt user for 2 options: add to the...

  • Write an object-oriented C++ program (i.e. a class and a main function to use it), using...

    Write an object-oriented C++ program (i.e. a class and a main function to use it), using pointer variables, that program that performs specific searching and sorting exercises of an array of integers. This program has six required outputs. Start by initializing an array with the following integers, in this order: 23, 17, 5, 90, 12, 44, 38, 84, 77, 3, 66, 55, 1, 19, 37, 88, 8, 97, 25, 50, 75, 61, and 49. Your array input may be hardcoded...

  • Stuck on this computer science assignment Write a program that demonstrates binary searching through an array...

    Stuck on this computer science assignment Write a program that demonstrates binary searching through an array of strings and finding specific values in an corresponding parallel double array. In all cases your output should exactly match the provided solution.o. Provided files: Assignment.cpp - starter assignment with function prototypes companies.txt - file for program to read. earnings.txt - file for program to read. Input1.txt Input2.txt - Test these inputs out manually, or use stream redirection Input3.txt - These inputs are based...

  • All those points need to be in the code Project 3 Iterative Linear Search, Recursive Binary...

    All those points need to be in the code Project 3 Iterative Linear Search, Recursive Binary Search, and Recursive Selection Sort Class River describes river's name and its length in miles. It provides accessor methods (getters) for both variables, toString() method that returns String representation of the river, and method isLong() that returns true if river is above 30 miles long and returns false otherwise. Class CTRivers describes collection of CT rivers. It has no data, and it provides the...

  • For this c++ assignment, Overview write a program that will process two sets of numeric information....

    For this c++ assignment, Overview write a program that will process two sets of numeric information. The information will be needed for later processing, so it will be stored in two arrays that will be displayed, sorted, and displayed (again). One set of numeric information will be read from a file while the other will be randomly generated. The arrays that will be used in the assignment should be declared to hold a maximum of 50 double or float elements....

  • Use c-strings for the following project: Write a C++ program that declares an array containing up...

    Use c-strings for the following project: Write a C++ program that declares an array containing up to a maximum of 20 sentences, each sentence of maximum 81 characters long, using c-strings. Continue reading sentences from the user and store them in the array of sentences, until the user enters a NULL string for the sentence or 20 sentences have been entered. Then, one by one, display each sentence entered by the user and present the following menu of operations on...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT