Thread: Noob Question....Any help appreciated

  1. #1
    Registered User RandomX's Avatar
    Join Date
    Nov 2006
    Posts
    19

    Noob Question....Any help appreciated

    Hey everyone!

    First off, I just found this board and I am impressed. It looks like you have a very intelligent community here. Anyways, I am a freshman studying Computer Science at Minnesota. I am in an entry level course for algorithms and information systems. We have a final project that is based around writing a program to find the lowest value for x of a given polynomial equestion, on a given range of numbers ( -5 <= x <= 5).

    "Develop an iterative algorithm to the minimum point of this function over a range of -5 to +5.

    y=x^2 -8x +30

    1. A generalized description of the algorithm
    2. Psuedo code for the algorithm
    3. Flowchart of the algorithm"



    That stuff should be a piece of cake.

    Then, the 2nd part is to make a "Little Man" program and write it out longhand, then copy it into an ascii text file named LMC.s

    The 3rd part is to make a C program that reads the file LMC.s and convert the source within that file to 8bit binary representation.


    -----------
    Now for my questions. Let me preface this by saying that Im not looking for someone to tell me exactly what i should do for everything. I wouldnt learn that way. I would like some advice to help push me in the right direction, and also some insight on my ideas I mention here:

    For the original psuedo code for the algorithm, my idea is to start with -5 and plug it in for X. This would be represented by -5*-5-8*-5+30. Then take that result and compare it to the next one, -4. Then just keep going like that.


    Lets just start with the psuedo code. What do you guys think about my plan? Could I possibly run into any issues structuring the algorithm this way?


    Thanks in advance,

    Matt N

  2. #2
    MFC killed my cat! manutd's Avatar
    Join Date
    Sep 2006
    Location
    Boston, Massachusetts
    Posts
    870
    I think that this is a good way to continue, particularly with only 10 numbers to test. Let me clarify what your proposing:
    -5: 25+40+30 = 95
    (store in variable)
    -4: 16+32+30 = 78
    (test if answer is less then previous, if it is, store it in variable)
    ...
    3: 9-24+30 = 15?
    (test if answer is less then previous, if it is, store it in variable)
    This should spit out your answers, no?
    Silence is better than unmeaning words.
    - Pythagoras
    My blog

  3. #3
    Registered User RandomX's Avatar
    Join Date
    Nov 2006
    Posts
    19
    yes that is what Im thinking. So, then my algorithm for this would be just a series of

    if variable is less than previous variable,
    then move onto the next variable

    It would keep doing that until it got to the comparison between 4 and 5 (this is assuming I start at -5). With the pattern Im going, the numbers keep decreasing until it reaches its lowest point of 4, then when it gets to 5 the value is larger than the previous. at this point it might looking something like this:

    if variable is less than previous variable,
    then move onto the next variable
    else this variable is the minimum point


    Do you follow what Im saying?

  4. #4
    MFC killed my cat! manutd's Avatar
    Join Date
    Sep 2006
    Location
    Boston, Massachusetts
    Posts
    870
    Psuedocode:
    Code:
    int num1, num2;
    for (int i = -5; i <= 5; i++)
    {
        num1 = getalgo(i);  
        if (num1 < num2)
        {
              num2 = num1;
        }
    }
    Silence is better than unmeaning words.
    - Pythagoras
    My blog

  5. #5
    Registered User RandomX's Avatar
    Join Date
    Nov 2006
    Posts
    19
    so first should I go through and store the value for each number from the equation?

    num1=95 (-5)
    num2=78 (-4)
    num3=63 (-3)
    ....
    and so on....


    By the way, thanks for the help with this so far.

  6. #6
    MFC killed my cat! manutd's Avatar
    Join Date
    Sep 2006
    Location
    Boston, Massachusetts
    Posts
    870
    No, getalgo would be a function, such that:
    Code:
    int getalgo( int num )
    {
         return (num*num) - (8*num) + 30;
    }
    Silence is better than unmeaning words.
    - Pythagoras
    My blog

  7. #7
    Registered User RandomX's Avatar
    Join Date
    Nov 2006
    Posts
    19
    Code:
       if (num1 < num2)
        {
              num2 = num1;
        }
    Would you mind explaining the logic behind this part? The num2 = num1 part doesnt make sense to me.

  8. #8
    Registered User
    Join Date
    Nov 2006
    Posts
    65
    Well of the bat keep in mind in linear equations the quadratic equations for max/min is (-B/2A,F(-B/2A) F(x)=ax^2+bx+c. If you wanted to find the roots its -b+- sqrt(b^2-4ac)/2a (use the pow function and have it to the 1/2 power).

  9. #9
    MFC killed my cat! manutd's Avatar
    Join Date
    Sep 2006
    Location
    Boston, Massachusetts
    Posts
    870
    Before these lines, I set num1 to the next iteration of the algorithm. Then in the if line I check if the new number is less than the old (smallest) number. If it is, the new number is smaller, so set the smallest number to the new number, then go again.
    Silence is better than unmeaning words.
    - Pythagoras
    My blog

  10. #10
    Registered User RandomX's Avatar
    Join Date
    Nov 2006
    Posts
    19
    Quote Originally Posted by manutd
    Before these lines, I set num1 to the next iteration of the algorithm. Then in the if line I check if the new number is less than the old (smallest) number. If it is, the new number is smaller, so set the smallest number to the new number, then go again.

    makes perfect sense now. this reminds me when we used these programs in class to sort out data sets

  11. #11
    MFC killed my cat! manutd's Avatar
    Join Date
    Sep 2006
    Location
    Boston, Massachusetts
    Posts
    870
    Yah, I feel all smart now
    Silence is better than unmeaning words.
    - Pythagoras
    My blog

  12. #12
    Registered User RandomX's Avatar
    Join Date
    Nov 2006
    Posts
    19
    Quote Originally Posted by KoG Metalgod
    Well of the bat keep in mind in linear equations the quadratic equations for max/min is (-B/2A,F(-B/2A) F(x)=ax^2+bx+c. If you wanted to find the roots its -b+- sqrt(b^2-4ac)/2a (use the pow function and have it to the 1/2 power).

    I just thought about this.....

    Instead of using the quadratic formula, why not just taking the derivative of the function then setting it equal to 0

    2x-8=0

    That gives me my answer I need.... 4


    If this works, how would i code it up to find the derivative?

  13. #13
    Registered User RandomX's Avatar
    Join Date
    Nov 2006
    Posts
    19
    So my options are:

    1. Compare the numbers one by one
    2. Take the derivative and set equal to 0
    3. Use the max/min formula

    I think the max/min formula might be the easiest to code up and the most efficient

  14. #14
    Fear the Reaper...
    Join Date
    Aug 2005
    Location
    Toronto, Ontario, Canada
    Posts
    625
    Why would he ask you something like that for a specific function ? A much better question would be for any function...it makes no sense to me.
    Teacher: "You connect with Internet Explorer, but what is your browser? You know, Yahoo, Webcrawler...?" It's great to see the educational system moving in the right direction

  15. #15
    Registered User RandomX's Avatar
    Join Date
    Nov 2006
    Posts
    19
    Quote Originally Posted by Happy_Reaper
    Why would he ask you something like that for a specific function ? A much better question would be for any function...it makes no sense to me.
    keep in mind this is a entry level course for the computer science major. Plus, I have a feeling Im looking too far into this, but I wouldnt mind making mine a little more complex... I enjoy a challenge.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. another noob question
    By clb2003 in forum C Programming
    Replies: 4
    Last Post: 02-12-2009, 01:28 PM
  2. Noob printf question
    By lolguy in forum C Programming
    Replies: 3
    Last Post: 12-14-2008, 08:08 PM
  3. Noob question (redeclaring a array)
    By Demon.killer in forum C Programming
    Replies: 8
    Last Post: 10-21-2006, 12:06 PM
  4. Noob question - Implementing INI Parsing class
    By Highland Laddie in forum C++ Programming
    Replies: 2
    Last Post: 07-04-2005, 05:47 PM
  5. Replies: 5
    Last Post: 11-01-2002, 06:09 PM