By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,109 Members | 969 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,109 IT Pros & Developers. It's quick & easy.

Formatting output with C++

P: 5
i need to form this table and i have no idea how to figure it out can somebody please help or at least give me a hint. please!

X X^2 X^3
1.0e+000 1.00e+000 1.00e+000
2.0e+000 4.00e+000 8.00e+000
3.0e+000 9.00e+000 2.70e+001
4.0e+000 1.60e+001 6.40e+001
5.0e+000 2.50e+001 1.25e+002
6.0e+000 3.60e+001 2.16e+002
7.0e+000 4.90e+001 3.43e+002
8.0e+000 6.40e+001 5.12e+002
9.0e+000 8.10e+001 7.29e+002
1.0e+001 1.00e+002 1.00e+003
1.1e+001 1.21e+002 1.33e+003
1.2e+001 1.44e+002 1.73e+003
1.3e+001 1.69e+002 2.20e+003
1.4e+001 1.96e+002 2.74e+003
1.5e+001 2.25e+002 3.38e+003
Aug 12 '07 #1
Share this Question
Share on Google+
9 Replies

Expert Mod 5K+
P: 9,197
It looks like you have already done it.

Column 1 is X (a double)
Column 2 is X squared (a double)
Column 3 is X cubed (double)

You don't actually need a table of all three values becuse you can create the squares and the cubes as needed.

Now you are down to a simple array of double with elements initialized to the values of X.
Aug 12 '07 #2

P: 113
If you are referring to formatting the table as your post shows, look at the library, "iomanip"--specifically the objects "setiosflags()", "setprecision()", and (possibly) "setw()".
Aug 12 '07 #3

P: 5
i have to actually format it and have a starting and final number for the table
Aug 13 '07 #4

Expert Mod 2.5K+
P: 4,677
i have to actually format it and have a starting and final number for the table
Well, it seems you have been helped in how to format it, now you just need to decide on your final number... Unless you have a more specific question?
Aug 13 '07 #5

P: 5
I have this at d moment but cant seem 2 get it into exponential format

Expand|Select|Wrap|Line Numbers
  1. #include "stdafx.h"
  2. #include<iostream>
  3. using namespace std;
  5. int main()
  6. {
  7.     int xA,xB,xTwo,xThree,i;
  9.     printf("Two numbers must be entered second number should be higher than first.");
  10.     printf("\nThey should both be whole number\n\n\n");
  11.     printf("Enter first number: ");
  12.     scanf("%d",&xA);
  13.     printf("Enter second number: ");
  14.     scanf("%d",&xB);
  15.     printf("\n\n\n---------------------------------\n");
  16.     printf("Table of Information\n\n");
  17.     printf("---------------------------------\n");
  18.     printf("X           X^2            X^3\n");
  19.     while(xA<=xB)
  20.     {
  21.                  xTwo = xA*xA;
  22.                  xThree = xTwo*xA;
  23.                  printf("%d          %d          %d\n",xA,xTwo,xThree);
  24.                  xA++;
  25.     }
  26.     printf("---------------------------------\n");
  27.     system("pause");
  28.     return 0;
  29. }
Aug 15 '07 #6

Expert Mod 2.5K+
P: 4,677

I'm gonna take a look at your code in just a minute here (some housekeeping things I need to do first), but if you could also read through the Posting Guidelines I'd appreciate it. You didn't do anything big, but we appreciate people using code tags and that sort of thing (I think I also changed the thread title on this, but I'm not sure - that's in there too). So if you could give that a look, we'd be grateful.


Aug 15 '07 #7

Expert Mod 2.5K+
P: 4,677

By "exponential" do you mean the scientific notation like 2.70e+001 ?
Aug 15 '07 #8

Expert 100+
P: 463
It looks like your problem is in the printf statement. You're using %d which deals with integers. Are you getting any decimals values at all?

Experiment with %e (or maybe f or g). The syntax is pretty weird but something like %12.6e3 should get you close.
Aug 16 '07 #9

P: 5
By exponential i do mean 1.0e+00.
I have tried using %e and %f etc but just doesnt seem to work
Aug 16 '07 #10

Post your reply

Sign in to post your reply or Sign up for a free account.