-2

Is it possible to show the time it took a linear search to find the key you entered to find in the program?

This is the task our teacher gave us

Requirements: Write a C++ program that will do the following in sequence: Linear Search 1. Ask the user to input a positive integer value that we will denote as n. Here n represents the size of the problem space, specifically, the number of random elements. 2. For linear search, the user will input the key/value that will be searched. 3. Once the key/value is found it will display the value including the index value. 4. It will also display the time that it searched in milliseconds.

  • To answer the question that you asked, yes, it is possible to show the time it took a linear search to find the key you entered. I see only three possibilities here: A) your teacher already explained to the class how to obtain the current system clock, and everything else needed to complete this assignment, but you were not in class that day, B) you were in class but either did not understand the material present or weren't paying attention, or C) You have an incompetent teacher who isn't teaching. If this is A or B, ask your teacher for help, that's his/her job; if C change classes. – Sam Varshavchik Mar 05 '17 at 03:01
  • To answer your question, yes. For assistance, demonstrate your own effort into trying this assignment on your own. – BusyProgrammer Mar 05 '17 at 03:06
  • The problem with our teacher is He is giving the codes to us , not letting us do it, all we need to do is to transfer his code into codeblocks. Now he gave us that project for prelim, and for all of the codes he GAVE us , the code for that isn't present. And the option C is not a choice , the whole 2nd year of our college is stuck with that guy as the professor for that subject – Sean Darwin Edon Sean Mar 05 '17 at 03:06
  • "code for that isn't present". Maybe this is where the problem solving, read learning really starts! – Mark Fitzgerald Mar 05 '17 at 04:38

1 Answers1

0

As stated in the comments of the question, I am not going to help you entirely as this is an assignment problem. Here is the starting point to do your work. Start looking to the clock() library function of c++ in the header time.h. That should do your work.

Sunil Kumar
  • 390
  • 1
  • 7
  • 25
  • I know this, so thats it? I'm just troubled because, it shows 0ms when I search.Because based on his explanation based on worst case of linear search N=Nseconds. Because when I search for it on an array of 1000 elements and search for the 999th element, it only shows like 8ms. Am I missing something? Because thats what he said about the time it takes a linear search to search for the element needed, or his instructions and lectures are just very bad. All of us college students are troubled with his explanation. Thats why I only asked if its possible and the not the code. Thank you sir. – Sean Darwin Edon Sean Mar 05 '17 at 06:05
  • I think you are confusing with the O(n) notation i.e linear in terms of input. Linear time doesn't mean that if there are n elements it would take n **seconds** rather it means that it would take n **units** (in worst case) where 1 unit is equal to time required to search for 1 element. Your professor might have used N seconds just for explanation purposes like if there are 10 elements then in worst case it would have to traverse 10 elements in the container to search which will take 10 seconds considering one element search takes 1 second. But in reality it takes quite less than 1 second. – Sunil Kumar Mar 05 '17 at 06:29
  • I've really appreciated your help and explanation sir, thank you very much. Its all clear to me now . – Sean Darwin Edon Sean Mar 05 '17 at 06:42