Hello,

I've been working on this random number generator code and got it to work using integer outputs, however I need scientific notation. I read on a site that I can use %E for scientific notation, however I get wierd results that aren't really random, and results that are extremely small like 4.880590E-313 (This is the form I need) when I use it. In addition to this, the output numbers that are on the same line don't seem to change value as they did when they were integers. Can someone please help me, I'm really stuck, I have included my working code that displays integers below. Thanks,

-Al

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <cstdlib>
int main ()
{
  
  srand ( time(NULL) );
   
 
  printf (")O+_06 Big-body initial data  (WARNING: Do not delete this line!!\n)"); 
  printf (") Lines beginning with `)' are ignored.\n");
  printf (")---------------------------------------------------------------------\n");
  printf (" style (Cartesian, Asteroidal, Cometary) = Cartesian\n");
  printf (" epoch (in days) = 2451000.5\n");
  printf (")---------------------------------------------------------------------\n"); 
  printf ("JUPITER     m=%d r=20.d0 d=%d\n", rand()%20, rand()%10);
  printf ("%d %d %d \n", rand()%10, rand()%10, rand()%10);
  printf ("%d %d %d \n", rand()%10, rand()%10, rand()%10);
 // printf ("SATURN      m=%E r=20.d0 d=%d \n", rand()%100, rand()%5);
 // printf ("%E %E %E \n", rand()%10+20, rand()%10+20, rand()%10+20);
 // printf ("%E %E %E \n", rand()%10+20, rand()%10+20, rand()%10+20);
 // printf ("URANUS      m=%E r=20.d0 d=%d \n", rand()%100, rand()%5);
 // printf ("%E %d %E \n", rand()%10+20, rand()%10+20, rand()%10+20);
 // printf ("%E %E %E \n", rand()%10+20, rand()%10+20, rand()%10+20);
 // printf ("NEPTUNE     m=%E r=20.d0 d=%d \n", rand()%100, rand()%5);
 // printf ("%E %E %E \n", rand()%10+20, rand()%10+20, rand()%10+20);
 // printf ("%E %E %E \n", rand()%10+20, rand()%10+20, rand()%10+20);
    

  return 0;

}

<< moderator edit: added [code][/code] tags >>

Recommended Answers

All 6 Replies

%E expects a double, rand()%10+20 is an int.

I'm still pretty new at c++, that's what I was thinking but I don't know how to fix the problem. Thanks

How about breaking things up into a couple of lines. Perhaps have variables a, b, c declared as double. Calculate their values. Then print a, b, and c.

The thing is that I need the format that it's in, I can't really move things around. The ouput is going to be used by another program and that's the format it expects.

Who said anything about changing the format?

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main ()
{
   double a, b, c;
   int i, j, k;
  srand ( time(NULL) );
   
 
  printf (")O+_06 Big-body initial data  (WARNING: Do not delete this line!!\n)"); 
  printf (") Lines beginning with `)' are ignored.\n");
  printf (")---------------------------------------------------------------------\n");
  printf (" style (Cartesian, Asteroidal, Cometary) = Cartesian\n");
  printf (" epoch (in days) = 2451000.5\n");
  printf (")---------------------------------------------------------------------\n"); 
  
  i = rand()%20; j = rand()%10;
  printf ("JUPITER     m=%d r=20.d0 d=%d\n", i, j);
  i = rand()%10; j = rand()%10; k = rand()%10;
  printf ("%d %d %d \n", i, j, k);
  i = rand()%10; j = rand()%10; k = rand()%10;
  printf ("%d %d %d \n", i, j, k);
  
  a = rand()%100; j = rand()%5;
  printf ("SATURN      m=%E r=20.d0 d=%d \n", a, j);
  a = rand()%10+20; b = rand()%10+20, c = rand()%10+20;
  printf ("%E %E %E \n", a, b, c);
  a = rand()%10+20; b = rand()%10+20, c = rand()%10+20;
  printf ("%E %E %E \n", a, b, c);

  a = rand()%100; j = rand()%5;
  printf ("URANUS      m=%E r=20.d0 d=%d \n", a, j);
  a = rand()%10+20; j = rand()%10+20; c = rand()%10+20;
  printf ("%E %d %E \n", a, j, c);
  a = rand()%10+20; j = rand()%10+20; c = rand()%10+20;
  printf ("%E %d %E \n", a, j, c);
  
  a = rand()%100; j = rand()%5;
  printf ("NEPTUNE     m=%E r=20.d0 d=%d \n", a, j);
  a = rand()%10+20; j = rand()%10+20; c = rand()%10+20;
  printf ("%E %d %E \n", a, j, c);
  a = rand()%10+20; j = rand()%10+20; c = rand()%10+20;
  printf ("%E %d %E \n", a, j, c);

  return 0;

}

/* my output
)O+_06 Big-body initial data  (WARNING: Do not delete this line!!
)) Lines beginning with `)' are ignored.
)---------------------------------------------------------------------
 style (Cartesian, Asteroidal, Cometary) = Cartesian
 epoch (in days) = 2451000.5
)---------------------------------------------------------------------
JUPITER     m=0 r=20.d0 d=8
1 9 2 
3 0 4 
SATURN      m=3.900000E+01 r=20.d0 d=1 
2.400000E+01 2.100000E+01 2.400000E+01 
2.300000E+01 2.500000E+01 2.400000E+01 
URANUS      m=6.900000E+01 r=20.d0 d=0 
2.400000E+01 29 2.200000E+01 
2.800000E+01 22 2.700000E+01 
NEPTUNE     m=4.600000E+01 r=20.d0 d=4 
2.500000E+01 22 2.300000E+01 
2.700000E+01 22 2.200000E+01 
*/

Thank you so much, now I can continue modifying the code. I really appreciate your willingness to help me out.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.