typecasting problem

R

Ray D.

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(double *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 = (double)random;
printf("%i\n", x);
}
}

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.
 
R

Ray D.

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

Ray D. said:
Here is my code:
void randomMatrix(double *x) {
int i, random;
x = (double)random;
printf("%i\n", x);


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.
 
M

Martin Ambuhl

Ray said:
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(double *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 = (double)random;
printf("%i\n", x);

^^
This is an error

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

#define SIZE 6

void randomMatrix(double *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 = random; /* useless cast removed */
printf("%g\n", x); /* fixed printf specifier; Was "%i",
which is for ints, but x 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(theArray);
putchar('\n');
randomMatrix(theArray);

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
 
C

CBFalconer

Ray D. said:
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(double *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 = (double)random;
printf("%i\n", x);
}
}


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);
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,070
Latest member
BiogenixGummies

Latest Threads

Top