typecasting problem

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Ray D.

    #1

    typecasting problem

    Okay, so I'm trying to typecast a random integer into a double, and
    store that in an array of doubles. Here is my code:


    void randomMatrix(do uble *x) {

    int i, random;

    // Generate pseudo-random sequence
    // Restrict range to 9
    for(i = 0; i < SIZE; i++){
    random = rand()%9;
    printf("random is %i, ", random);

    x[i] = (double)random;
    printf("%i\n", x[i]);
    }
    }

    I call this function twice, and here is the output for each time (Note
    that SIZE = 6 in the loop above)

    random is 8, 1075838976
    random is 7, 1075576832
    random is 6, 1075314688
    random is 1, 1072693248
    random is 1, 1072693248
    random is 2, 1073741824

    random is 6, 1075314688
    random is 3, 1074266112
    random is 3, 1074266112
    random is 0, 0
    random is 4, 1074790400
    random is 5, 1075052544


    As you can see, I first print out the integer returned from the rand()
    function, then I attempt to typecast to a double and print this, but
    it's pretty clear they do not match, any idea what is going on?? I
    appreciate any help you may have! Thanks.

  • Walter Roberson

    #2
    Re: typecasting problem

    In article <1193421934.207 754.326800@19g2 000hsx.googlegr oups.com>,
    Ray D. <ray.delvecchio @gmail.comwrote :
    >Here is my code:
    >void randomMatrix(do uble *x) {
    int i, random;
    x[i] = (double)random;
    printf("%i\n", x[i]);
    You are passing a double to printf, but telling printf that you
    are passing an integer. If you want to see the stored double value,
    try %f as the format.
    --
    "Any sufficiently advanced bug is indistinguishab le from a feature."
    -- Rich Kulawiec

    Comment

    • Ray D.

      #3
      Re: typecasting problem

      Touchee, can't believe I didn't notice that!

      On Oct 26, 2:19 pm, rober...@ibd.nr c-cnrc.gc.ca (Walter Roberson)
      wrote:
      In article <1193421934.207 754.326...@19g2 000hsx.googlegr oups.com>,
      >
      Ray D. <ray.delvecc... @gmail.comwrote :
      Here is my code:
      void randomMatrix(do uble *x) {
      int i, random;
      x[i] = (double)random;
      printf("%i\n", x[i]);
      >
      You are passing a double to printf, but telling printf that you
      are passing an integer. If you want to see the stored double value,
      try %f as the format.
      --
      "Any sufficiently advanced bug is indistinguishab le from a feature."
      -- Rich Kulawiec

      Comment

      • Default User

        #4
        Re: typecasting problem - TPA

        Ray D. wrote:
        Touchee, can't believe I didn't notice that!
        Please don't top-post. Your replies belong following or interspersed
        with properly trimmed quotes. See the majority of other posts in the
        newsgroup, or:
        <http://www.caliburn.nl/topposting.html >

        Comment

        • Martin Ambuhl

          #5
          Re: typecasting problem

          Ray D. wrote:
          Okay, so I'm trying to typecast a random integer into a double, and
          store that in an array of doubles. Here is my code:
          Note that the cast is completely worthless.
          >
          void randomMatrix(do uble *x) {
          >
          int i, random;
          >
          // Generate pseudo-random sequence
          // Restrict range to 9
          for(i = 0; i < SIZE; i++){
          random = rand()%9;
          printf("random is %i, ", random);
          >
          x[i] = (double)random;
          printf("%i\n", x[i]);
          ^^
          This is an error
          }
          }
          #include <stdio.h>
          #include <stdlib.h>
          #include <time.h>

          #define SIZE 6

          void randomMatrix(do uble *x)
          {

          int i, random;

          for (i = 0; i < SIZE; i++) {
          random = 9. * rand() / (1. + RAND_MAX); /* restrict range to
          0..8 */
          printf("random is %i, ", random);

          x[i] = random; /* useless cast removed */
          printf("%g\n", x[i]); /* fixed printf specifier; Was "%i",
          which is for ints, but x[i] is a
          double. */
          }
          }

          int main(void)
          {
          double theArray[SIZE];
          srand(time(0)); /* you may want to fix this to handle
          cases where the value of a time_t
          may be outside the range of an
          unsigned int */
          randomMatrix(th eArray);
          putchar('\n');
          randomMatrix(th eArray);

          return 0;
          }

          random is 8, 8
          random is 7, 7
          random is 0, 0
          random is 4, 4
          random is 5, 5
          random is 7, 7

          random is 5, 5
          random is 5, 5
          random is 3, 3
          random is 3, 3
          random is 2, 2
          random is 3, 3

          Comment

          • CBFalconer

            #6
            Re: typecasting problem

            "Ray D." wrote:
            >
            Okay, so I'm trying to typecast a random integer into a double,
            and store that in an array of doubles. Here is my code:
            >
            void randomMatrix(do uble *x) {
            >
            int i, random;
            >
            // Generate pseudo-random sequence
            // Restrict range to 9
            for(i = 0; i < SIZE; i++){
            random = rand()%9;
            printf("random is %i, ", random);
            >
            x[i] = (double)random;
            printf("%i\n", x[i]);
            }
            }
            Try printing a double format, rather than an integer format. Also
            make sure that the passed in matrix (*x) is big enough to hold all
            the values. That means malloc it with:

            x = malloc(SIZE * sizeof *x);

            --
            Chuck F (cbfalconer at maineline dot net)
            Available for consulting/temporary embedded and systems.
            <http://cbfalconer.home .att.net>



            --
            Posted via a free Usenet account from http://www.teranews.com

            Comment

            Working...