rootTable help

My task is to write a program which reads in the number of roots, a value increment, and a precision and outputs a table of roots x1/2, x1/3, … for the given number of roots and values of x equal to i times the increment up to and including 100.

So far I have managed to get everything right but the calculation of the roots themselves so the output is just 1s like this:

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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
Enter number of roots: 5
Enter value increment (integer): 5
Enter precision: 3
Value   x^1/2   x^1/3   x^1/4   x^1/5   x^1/6
    5   1.000   1.000   1.000   1.000   1.000
   10   1.000   1.000   1.000   1.000   1.000
   15   1.000   1.000   1.000   1.000   1.000
   20   1.000   1.000   1.000   1.000   1.000
   25   1.000   1.000   1.000   1.000   1.000
   30   1.000   1.000   1.000   1.000   1.000
   35   1.000   1.000   1.000   1.000   1.000
   40   1.000   1.000   1.000   1.000   1.000
   45   1.000   1.000   1.000   1.000   1.000
   50   1.000   1.000   1.000   1.000   1.000
   55   1.000   1.000   1.000   1.000   1.000
   60   1.000   1.000   1.000   1.000   1.000
   65   1.000   1.000   1.000   1.000   1.000
   70   1.000   1.000   1.000   1.000   1.000
   75   1.000   1.000   1.000   1.000   1.000
   80   1.000   1.000   1.000   1.000   1.000
   85   1.000   1.000   1.000   1.000   1.000
   90   1.000   1.000   1.000   1.000   1.000
   95   1.000   1.000   1.000   1.000   1.000
  100   1.000   1.000   1.000   1.000   1.000

Here is my code: 

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

int main ()
{ 

  // Variable declarations 

  int numRoots, i, p, c, c1, c2, pWidth(0);

  // Prompt for inputs 

  cout << "Enter number of roots: ";
  cin >> numRoots;

  cout << "Enter value increment (integer): ";
  cin >> i;

  cout << "Enter precision: "; 
  cin >> p;

  // Calculate table width 
 
  if (p > 2) {
     pWidth = 6 + (p-2);
             }
  else {
     pWidth = 6;
       }

  // Table header 

  cout << "Value"; 
  for (int c = 2 ; c <= numRoots + 1 ; c++) {
      cout << setw(pWidth) << "x^1/" << c; 
                                            }

  cout << endl;
    
  c2 = i;
  while (c2 <= 100) {
      cout << setw(5) << c2;
      c1 = 1;
      while (c1 <= numRoots) { 
          cout.setf(ios::fixed);
	  cout.precision(p);
	  cout << setw(pWidth + 1) << pow(c2, (1 / (c1 + 1)));
	  c1++;
		             }
      cout << endl;
      c2 = c2 + i; 
                    }
 
  return 0;
}


I can't figure out what wrong with my cout statement,

cout << setw(pWidth + 1) << pow(c2, (1 / (c1 + 1)));

that makes it only print out 1s. I would appreciate any quick help that can be offered. Thanks

Last edited on
c1 and c2 are ints. You're doing integer division to compute the exponent. 1 divided by any int is 0. c2 to the 0 power is 1.

PLEASE USE CODE TAGS (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your post.
http://www.cplusplus.com/articles/jEywvCM9/
Hint: You can edit your post, highlight your code and press the <> formatting button.
Last edited on
Thank you!
Topic archived. No new replies allowed.