Storing Values for each iteration of a function.

I have a function for a bisection method.
I need to store the values in a string or array for each of the following,
a,b,c,solution(c), for each iteration.
How would i set that up?


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
#include <iostream>



using namespace std;
#define EP 0.0000000001

// Bisection Method function
double solution(double x) 
{
   return 2*x*x*x*x + 3*x*x*x - 11*x*x - 9*x + 15;
}

void bisection(double a, double b) 
{
   if (solution(a) * solution(b) >= 0)
    {
      cout << "Invalid a and b selection!!";
      return;
   }
   double c = a;
   
   while ((b-a) >= EP) {
      
      c = (a+b)/2;
      
      if (solution(c) == 0.0)
         break;
       
      else if (solution(c)*solution(a) < 0)
         b = c;
      else
         a = c;
   }
   cout << "The value of root is : " << c;
}
 // main function
int main() 
{
   double a, b;
   cout<< "Please Enter a Value for a and b\n"<<endl;
   cin >> a >> b ;
   bisection(a, b);
   return 0;
}
Last edited on
Hello Bkjohnson,


PLEASE ALWAYS USE CODE TAGS (the <> formatting button), to the right of this box, when posting code.

Along with the proper indenting it makes it easier to read your code and also easier to respond to your post.

http://www.cplusplus.com/articles/jEywvCM9/
http://www.cplusplus.com/articles/z13hAqkS/

Hint: You can edit your post, highlight your code and press the <> formatting button. This will not automatically indent your code. That part is up to you.

You can use the preview button at the bottom to see how it looks.

I found the second link to be the most help.



It is usually best to provide enough code to be able to compile and test the program. Some known test data for input and output is also helpful.

Try not to use single letter variable names. This is hard to read and follow. Give the variables names that mean something. The biggest benefit is to you in the future. But it also makes it easier for others to read and follow.

I do not see a "std::string" as being a viable choice for storing the numbers. To much converting the number to a string and then back to a number to use it.

My first thought is a vector of structs, but arrays can work also.

I need to know what you have learned and can use first.

Andy
I need to store the values in a string or array for each of the following, a,b,c,solution(c), for each iteration.
How would i set that up?

struct Solution {
double a {};
double b {};
double c {};
};
std::vector<Solution> sols;
Last edited on
Hello Andy;

I edited the post to include all the code i had. The bisection method works and produces the right answer, I just need to store the values of a, b and solution(c) for each iteration of the code. We have learned the basics of c++ with briefly going over arrays and strings. We have not talked about structures or anything like that although our teacher leaves it open for us to learn on our own and if we can find other ways of doing things he is ok with that. The goal is to take the stored values and make a table and then plot solution(c).

Thank you
I changed your function ... to one I could solve.

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
#include <iostream>
#include <vector>
using namespace std;

const double EP = 1.0e-10;

// Store trial data
struct Trial{ double left, right, mid, value; };


// Bisection Method function
double solution(double x) 
{
// return 2*x*x*x*x + 3*x*x*x - 11*x*x - 9*x + 15;
   return 9 - x * x;
}

void bisection(double a, double b) 
{
   vector<Trial> V;
   
   if (solution(a) * solution(b) >= 0)
    {
      cout << "Invalid a and b selection!!";
      return;
   }
   double c = a;
   
   while ((b-a) >= EP) {
      
      c = (a+b)/2;
      double fc = solution(c);
      V.push_back( { a, b, c, fc } );
      
      if (fc == 0.0)
         break;
       
      else if (fc*solution(a) < 0)
         b = c;
      else
         a = c;
   }
   cout << "The value of root is : " << c << '\n';
   cout << "The trials (left, right, mid, f(mid) ) were:\n";
   for ( Trial t : V ) cout << t.left << '\t' << t.right << '\t' << t.mid << '\t' << t.value << '\n';
}
 // main function
int main() 
{
   double a, b;
   cout<< "Please Enter a Value for a and b\n"<<endl;
   cin >> a >> b ;
   bisection(a, b);
   return 0;
}


Please Enter a Value for a and b

0 10
The value of root is : 3
The trials (left, right, mid, f(mid) ) were:
0	10	5	-16
0	5	2.5	2.75
2.5	5	3.75	-5.0625
2.5	3.75	3.125	-0.765625
2.5	3.125	2.8125	1.08984
2.8125	3.125	2.96875	0.186523
2.96875	3.125	3.04688	-0.283447
2.96875	3.04688	3.00781	-0.046936
2.96875	3.00781	2.98828	0.0701752
2.98828	3.00781	2.99805	0.0117149
2.99805	3.00781	3.00293	-0.0175867
2.99805	3.00293	3.00049	-0.00292993
2.99805	3.00049	2.99927	0.00439399
2.99927	3.00049	2.99988	0.000732407
2.99988	3.00049	3.00018	-0.00109867
2.99988	3.00018	3.00003	-0.000183106
2.99988	3.00003	2.99995	0.000274656
2.99995	3.00003	2.99999	4.57763e-05
2.99999	3.00003	3.00001	-6.86647e-05
2.99999	3.00001	3	-1.14441e-05
2.99999	3	3	1.71661e-05
3	3	3	2.86102e-06
3	3	3	-4.29153e-06
3	3	3	-7.15256e-07
3	3	3	1.07288e-06
3	3	3	1.78814e-07
3	3	3	-2.68221e-07
3	3	3	-4.47035e-08
3	3	3	6.70552e-08
3	3	3	1.11759e-08
3	3	3	-1.67638e-08
3	3	3	-2.79397e-09
3	3	3	4.19095e-09
3	3	3	6.98492e-10
3	3	3	-1.04774e-09
3	3	3	-1.74623e-10
3	3	3	2.61934e-10
Last edited on
Topic archived. No new replies allowed.