Results 1 to 3 of 3

Math Help - Algorithms

  1. #1
    Newbie
    Joined
    Nov 2009
    Posts
    14

    Algorithms

    I am having trouble with this algorithm. I was wondering if anyone could offer assistance and also an explanation.

    Describe an algorithm that, upon input of n real numbers, a1,a2,...,an, and another number, x, determines how many ai are equal to x.

    I am totally lost. From reading my book, I am thinking that this is the answer.

    Step 1. Set C=0
    Step 2. For i=1 to n, if ai=x replace C by C+1
    Step 3. Output C

    Any help is appreciated.
    Last edited by discretec; December 16th 2009 at 11:02 AM. Reason: Wrote in an answer that I thought was correct.
    Follow Math Help Forum on Facebook and Google+

  2. #2
    MHF Contributor
    Joined
    Oct 2009
    Posts
    5,506
    Thanks
    765
    Your algorithm is correct.

    I don't know why you are totally lost, though. Programming a computer is not really different from being able to solve problems in real life.

    A long time ago I had a programmable calculator. Writing programs in it was really easy. I would first do some calculation myself ("by hands"), on some particular input. Then I had to remember the sequence of keys I pressed; that sequence was in fact the program. After I recorded the program, I could run it on any input.

    So, for me the first step in writing an algorithm is to imagine myself in place of the computer. Or, rather, I imagine computer objects (variables, lists, pointers) using some visual imagery. For example, I may think of variables as physical boxes where numbers are stored. Or, in the case of your problem, I could imagine that there is a sequence of boxes in a storehouse, with every box having a label that says what's inside. The manager asks me to count the number of boxes with Wii's. Let's forget for a moment about the computer; could I do this task? Could I do it repeatedly, without guessing, as opposed to having good luck one time and possibly bad luck next time? Could I explain how to do this to another person? If I think I could do it, I would record the sequence of steps that I perform. What is left is only to make a translation from physical objects to computer concepts. Then I would get the same algorithm as you wrote.
    Follow Math Help Forum on Facebook and Google+

  3. #3
    Newbie
    Joined
    Nov 2009
    Posts
    14
    Quote Originally Posted by emakarov View Post
    Your algorithm is correct.

    I don't know why you are totally lost, though. Programming a computer is not really different from being able to solve problems in real life.

    A long time ago I had a programmable calculator. Writing programs in it was really easy. I would first do some calculation myself ("by hands"), on some particular input. Then I had to remember the sequence of keys I pressed; that sequence was in fact the program. After I recorded the program, I could run it on any input.

    So, for me the first step in writing an algorithm is to imagine myself in place of the computer. Or, rather, I imagine computer objects (variables, lists, pointers) using some visual imagery. For example, I may think of variables as physical boxes where numbers are stored. Or, in the case of your problem, I could imagine that there is a sequence of boxes in a storehouse, with every box having a label that says what's inside. The manager asks me to count the number of boxes with Wii's. Let's forget for a moment about the computer; could I do this task? Could I do it repeatedly, without guessing, as opposed to having good luck one time and possibly bad luck next time? Could I explain how to do this to another person? If I think I could do it, I would record the sequence of steps that I perform. What is left is only to make a translation from physical objects to computer concepts. Then I would get the same algorithm as you wrote.
    Thank you very much for that explanation. That is a very helpful way of looking at it.
    Follow Math Help Forum on Facebook and Google+

Similar Math Help Forum Discussions

  1. Algorithms
    Posted in the Math Software Forum
    Replies: 2
    Last Post: November 4th 2010, 08:25 PM
  2. Algorithms
    Posted in the Discrete Math Forum
    Replies: 2
    Last Post: October 8th 2010, 09:48 PM
  3. algorithms
    Posted in the Discrete Math Forum
    Replies: 2
    Last Post: March 16th 2010, 02:25 PM
  4. Algorithms
    Posted in the Discrete Math Forum
    Replies: 1
    Last Post: December 4th 2009, 03:06 AM
  5. Algorithms
    Posted in the Discrete Math Forum
    Replies: 1
    Last Post: March 4th 2009, 11:33 AM

Search Tags


/mathhelpforum @mathhelpforum