Thread: exponential numbers

  1. #1
    Registered Abuser Loic's Avatar
    Join Date
    Mar 2007
    Location
    Sydney
    Posts
    115

    exponential numbers

    Hi, i am trying to write a program that produces an out put that looks like the fallowing, using "printf()" and "scanf()"
    Code:
    X                     X^2                  X^3
    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
    but the output that i am getting is nothing like it...
    Code:
    X                                 X^2                                X^3
    2.121996e-314          4.940656e-324              1.#QNAN0e+000
    8.487983e-314          3.952525e-323              1.#QNAN0e+000
    1.909796e-313          1.333977e-322              1.#QNAN0e+000
    3.395193e-313          3.162020e-322              1.#QNAN0e+000
    5.304989e-313          6.175821e-322              1.#QNAN0e+000
    7.639185e-313          1.067182e-321              1.#QNAN0e+000
    1.039778e-312          1.694645e-321              1.#QNAN0e+000
    1.358077e-312          2.529616e-321              1.#QNAN0e+000
    1.718817e-312          3.601739e-321              1.#QNAN0e+000
    my best guess is that the data i am trying to produce are exponential numbers (& the second column is the first one squared, and the third column is cubed) so i used the fallowing code... but i cant understand why it isn't working... please help
    Code:
    #include "stdafx.h"
    
    int _tmain(int argc, _TCHAR* argv[]) {
      // Declare varaibles
    	float tbStart, tbEnd;
    	
      // Prompt for input
    	// First Number
    	printf("Please enter the first number for the table: ");
    	scanf("%e", &tbStart);
    	
    	// Last Number
    	printf("Please enter the last number for the table: ");
    	scanf("%e", &tbEnd);
    	
      // print table header
    	printf("---------------------------------------------------------\n");
    	printf("Table of infomation\n\n");
    	printf("---------------------------------------------------------\n");
    	
      // print table information
    	printf("X                       X^2                     X^3\n");
    	for (int i=tbStart; i<=tbEnd; i++) {
    		int sqr = i*i; int cub = i*i*i;
    		printf("%e\t\t%e\t\t%e\n",i,sqr,cub);
    	}
    	
      // print table footer
    	printf("---------------------------------------------------------\n");
    	scanf("%f",&tbEnd);
      // end of program
    	return 0;
    }

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    > int sqr = i*i; int cub = i*i*i;
    > printf("&#37;e\t\t%e\t\t%e\n",i,sqr,cub);
    If you're going to print them out as floating point numbers, you might as well declare them as such (ie double).
    Code:
    		double sqr = i*i; double cub = i*i*i;
    		printf("%e\t\t%e\t\t%e\n",(double) i,sqr,cub);
    And depending on how big i gets, you may need to cast i in the calculations.
    Code:
    		double sqr = static_cast<double>(i)*i; double cub = static_cast<double>(i)*i*i;
    		printf("%e\t\t%e\t\t%e\n",(double) i,sqr,cub);

  3. #3
    Registered Abuser Loic's Avatar
    Join Date
    Mar 2007
    Location
    Sydney
    Posts
    115
    Quote Originally Posted by swoopy View Post
    > int sqr = i*i; int cub = i*i*i;
    > printf("%e\t\t%e\t\t%e\n",i,sqr,cub);
    If you're going to print them out as floating point numbers, you might as well declare them as such (ie double).
    Code:
    		double sqr = i*i; double cub = i*i*i;
    		printf("%e\t\t%e\t\t%e\n",(double) i,sqr,cub);
    And depending on how big i gets, you may need to cast i in the calculations.
    Code:
    		double sqr = static_cast<double>(i)*i; double cub = static_cast<double>(i)*i*i;
    		printf("%e\t\t%e\t\t%e\n",(double) i,sqr,cub);
    Thanks alot for that, that works... but im not to sure what you are doing with the

    Code:
    static_cast<double>

  4. #4
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Quote Originally Posted by Loic View Post
    but im not to sure what you are doing with the

    Code:
    static_cast<double>
    It casts (converts) i to a double, equivalent to:
    Code:
    (double) i
    If i gets big enough, for example say i = 1500, then your cubed results will overflow without the cast.

  5. #5
    Registered Abuser Loic's Avatar
    Join Date
    Mar 2007
    Location
    Sydney
    Posts
    115
    ah cool, i get ya... thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Writing unique numbers to an array
    By yardy in forum C Programming
    Replies: 6
    Last Post: 12-27-2006, 09:15 PM
  2. Logical errors with seach function
    By Taka in forum C Programming
    Replies: 4
    Last Post: 09-18-2006, 05:20 AM
  3. Replies: 4
    Last Post: 03-03-2003, 03:52 PM
  4. the definition of a mathematical "average" or "mean"
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 12-03-2002, 11:15 AM
  5. A (complex) question on numbers
    By Unregistered in forum C++ Programming
    Replies: 8
    Last Post: 02-03-2002, 06:38 PM