Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

For Loops help needed!!

for loop loop

  • Please log in to reply
4 replies to this topic

#1 vrajesh

vrajesh

    CC Lurker

  • Just Joined
  • Pip
  • 2 posts

Posted 30 November 2007 - 09:39 AM

i just began programming in C++ i kno all the basics..

i am trying to code a factorization program..
Ax^2 + Bx + c = 0.

if you guys remember in high school algebra that you have to find two numbers that multiply to be c * A and also add up to be B. i ask the user for the A,B,C values.

i am stuck in finding the two numbers that would be c * A.

for this to happen i use a for loop ranging values are

for (x =1; x <50000 ; x++)
this will create the first number
for (y=1; y<5000; y++)
this will create the second.
the only problem is i do not know how to nest it right so x stays the same until y reaches 5000 then x changes to the next number.


i also used the if statement to determine when x * y = product(c*A).

thanks for reading please help all you can
thank you!
  • 0

#2 nikomaster

nikomaster

    CC Lurker

  • Just Joined
  • Pip
  • 3 posts

Posted 30 November 2007 - 06:12 PM

Use the math rules for factorization, and implement it into your program, try a better algorithm, find the number where the conditions are good, not needed to use a constant as 50000
for example if your equation is: x2 + 5x + 6 the rule is simple

W1 and W2 where the constants that multiply to get A

A=W1 * W2

C=Z1 * Z2

and B=W1*Z2 + W2 *Z1 the hard part is to get W1,W2,Z1,Z2 but is easy, is just the matter to find the numbers, the multiplies must be smaller than the number, for example, W1 and W2 must be smaller than A and also Z1 and Z2 must be smaller than C so you can start your tests.
  • 0

#3 vrajesh

vrajesh

    CC Lurker

  • Just Joined
  • Pip
  • 2 posts

Posted 30 November 2007 - 10:14 PM

nice explanation.. thanks for the help man.. i m going to have to think some codin sense in to me i

will update thanks..
  • 0

#4 driver

driver

    CC Lurker

  • Just Joined
  • Pip
  • 1 posts

Posted 01 December 2007 - 06:19 AM

If i understand your problem correct,there is a simplier way.
You want to find 2 numbers that solves the situation.
also for these numbers x1 and x2 issues x1*x2=c*a and x1+x2=b.
Well try this.

Define D=b^2-4*a*c
Then you have x1=(-b+ ?D)/2a and x2=(-b- ?d)/2a
where ?D i mean ?25 = 5

an example 2x^2+5x+2=0

D=25-2*4*2=25-16=9
then x1=(-5+3)/2*2=-0.5
and x2=(-5-3)/2*2=-2
well if D=0 then x1=x2
and if D<0 then there is no real number to match.

Well i think it is much easier to programming it now.
Send me to mali21nick@gmail.com what you succeed.
  • 0

#5 WingedPanther73

WingedPanther73

    A spammer's worst nightmare

  • Moderator
  • 17757 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others

Posted 03 December 2007 - 08:54 AM


for (x=1;x<sqrt(a*c);x++)

{

  if (a*c) % x = 0 then

  {

    if (a*c)/x + x = b then

      {

      cout<<x<<" "<<(a*c)/x;

      x=a*c;

      }

  }

}


  • 0

Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

My MineCraft server site: http://banishedwings.enjin.com/






Also tagged with one or more of these keywords: for loop, loop

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download