Need help, figuring out which loop to use

Hi. I'm writing a quadratic program. But I need to allow the user to choose how many polynomials they want to solve. My program compiles and runs and gives the roots like I want, I just can't figure out the loop. :[
(and I can't just prompt the user to see if they want to run program again)

I was going to use a for loop, but I have trouble figuring out what to use for the initialization, conditional, and increment for my program.

I'm not even sure if that would be my best loop option.
Any help would be greatly appreciated!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
/* Hannah, N Polynomials
   This program determines the roots of a polynomial, 
   and allows the user to determine the number of 
   polynomials they would like to solve.
*/

#include <iostream>
#include <cmath>
using namespace std;

int main()
{

int n, a, b, c, discriminant;
float root1, root2;

// What the program does
cout << "This program determines the roots of a polynomial,\n"
      <<"and allows you to determine the number of polynomials\n"
	  <<"you want to figure out\n";

//Prompts for number of polynomials ans polynomial coefficients
	cout << "How many 2cnd order polynomials do you have to solve?\n";
		cin >> n;
for() or while()  // I  was wanting to place my loop here, but I'm not sure which type to use for, do-while, or while????

{
	cout << "Please input your polynomial coeffiecients in decreasing degree order.\n"
		 << "(i.e. If formula was Ax^2 + Bx + C, then your input would be A B C)\n";
		 cin >> a >> b >> c ;
discriminant = ((b*b)-4*a*c);  // Use quadratic formula to determine roots, to control imaginary numbers, first figure discrimiant

if (discriminant>0)
{
root1 = (-b+sqrt(discriminant))/ (2*a);
root2 = (-b-sqrt(discriminant))/ (2*a);
}

else if (discriminant ==0)
{
	root1 = root2 = (-1*b)/(2*a);
	
}
else if (discriminant < 0)
{
discriminant = -1*discriminant;
root1 = ((-1*b)+ sqrt(discriminant))/(2*a);
root2 = ((-1*b)- sqrt(discriminant))/(2*a);

cout << "The roots for your polynomial " << a <<"x^2 + " << b <<"x + " << c <<
		"\nare " << root1 << "i and " << root2 << "i" << endl; 
return 0;
}
cout << "The roots for your polynomial " << a <<"x^2 + " << b <<"x + " << c <<
		"\nare " << root1 << " and " << root2 << "\n" << endl; 
n=--n;	// I was even trying to have my n decrease ever time the loop ran, hoping that would give the desired result..... :[
} 
	return 0;
}
Last edited on
for loop would be best if it is a set amount of times to loop. Keep in mind to reset the variables each loop iteration otherwise they will contain old data.
Last edited on
That's the one I thought would be best but I'm not sure how to set it up

1
2
3

for ( ?; ?>=n; --n) //I'm not sure if even any of this is right, but it's as far as I can figure it
Last edited on
1
2
3
4
for(int i = 0; i < n; ++i) //will iterate n times
{
    //stuff to repeat
}

http://www.cplusplus.com/doc/tutorial/control/

The for loop
The for loop is designed to iterate a number of times. Its syntax is:

for (initialization; condition; increase) statement;

Like the while-loop, this loop repeats statement while condition is true. But, in addition, the for loop provides specific locations to contain an initialization and an increase expression, executed before the loop begins the first time, and after each iteration, respectively. Therefore, it is especially useful to use counter variables as condition.

It works in the following way:

initialization is executed. Generally, this declares a counter variable, and sets it to some initial value. This is executed a single time, at the beginning of the loop.
condition is checked. If it is true, the loop continues; otherwise, the loop ends, and statement is skipped, going directly to step 5.
statement is executed. As usual, it can be either a single statement or a block enclosed in curly braces { }.
increase is executed, and the loop gets back to step 2.
the loop ends: execution continues by the next statement after it.

Here is the countdown example using a for loop:
1
2
3
4
5
6
7
8
9
10
11
// countdown using a for loop
#include <iostream>
using namespace std;

int main ()
{
  for (int n=10; n>0; n--) {
    cout << n << ", ";
  }
  cout << "liftoff!\n";
}
10, 9, 8, 7, 6, 5, 4, 3, 2, 1, liftoff!


The three fields in a for-loop are optional. They can be left empty, but in all cases the semicolon signs between them are required. For example, for (;n<10;) is a loop without initialization or increase (equivalent to a while-loop); and for (;n<10;++n) is a loop with increase, but no initialization (maybe because the variable was already initialized before the loop). A loop with no condition is equivalent to a loop with true as condition (i.e., an infinite loop).

Because each of the fields is executed in a particular time in the life cycle of a loop, it may be useful to execute more than a single expression as any of initialization, condition, or statement. Unfortunately, these are not statements, but rather, simple expressions, and thus cannot be replaced by a block. As expressions, they can, however, make use of the comma operator (,): This operator is an expression separator, and can separate multiple expressions where only one is generally expected. For example, using it, it would be possible for a for loop to handle two counter variables, initializing and increasing both:

1
2
3
4
for ( n=0, i=100 ; n!=i ; ++n, --i )
{
   // whatever here...
}


This loop will execute 50 times if neither n or i are modified within the loop:



n starts with a value of 0, and i with 100, the condition is n!=i (i.e., that n is not equal to i). Because n is increased by one, and i decreased by one on each iteration, the loop's condition will become false after the 50th iteration, when both n and i are equal to 50.
Last edited on
The countdown loop makes since, starting with int n at 10 (int n=10), you want stop right before 0 (n>0), and n is decreasing by 1 (n--)

but for some reason I'm having difficulty applying this same reasoning to my program. I have read the material, looked at the examples.

I'm just frustrated, and I've tried the for loop every way I can think of, and it still only executes once

I tried the
1
2
3
4
5
6
7
8
9
10
11
12
13
....
{
for(int i = 0; i < n; ++n) //will iterate n times, even tried changing ++n to --n, because I need the program to run until n>0
    {
	cout << "Please input your polynomial coeffiecients in decreasing degree order.\n"
		 << "(i.e. If formula was Ax^2 + Bx + C, then your input would be A B C)\n";
		 cin >> a >> b >> c ;
               ........ etc
       (took off n=--n)
  }
retun 0
}


This only executed once as well
Last edited on
i < n; ++n this should be ++i, sorry I didn't catch the typo I had in my previous post :(
Last edited on
Thanks so much for all the help guys!
.....I just realized sometimes I'm a little dense, I figured out why my program wasn't working, it didn't matter what I was coding for the loop it only executed once.....
I had a piece of code that stops the loop with imaginary roots! o_o
1
2
3
4
5
6
7
8
9
10
else if (discriminant < 0)
{
discriminant = -1*discriminant;
root1 = ((-1*b)+ sqrt(discriminant))/(2*a);
root2 = ((-1*b)- sqrt(discriminant))/(2*a);

cout << "The roots for your polynomial " << a <<"x^2 + " << b <<"x + " << c <<
		"\nare " << root1 << "i and " << root2 << "i" << endl; 
return 0; // This is the reason my loop wasn't working
}


I had it working for non-imaginary soln, but not for imaginary one, and since I kept using imaginary examples to trial run the program I didn't catch it until re-coming through my code.

Thanks so much you were all a big help!
Topic archived. No new replies allowed.