# Thread: Noob Question....Any help appreciated

1. ## 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?

Matt N

2. 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)

3. 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. Psuedocode:
Code:
```int num1, num2;
for (int i = -5; i <= 5; i++)
{
num1 = getalgo(i);
if (num1 < num2)
{
num2 = num1;
}
}```

5. 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. No, getalgo would be a function, such that:
Code:
```int getalgo( int num )
{
return (num*num) - (8*num) + 30;
}```

7. 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. 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. 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.

10. 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. Yah, I feel all smart now

12. 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).

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. 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. 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.

15. 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.