473,414 Members | 1,989 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,414 software developers and data experts.

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(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[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.

Oct 26 '07 #1
5 3400
In article <11**********************@19g2000hsx.googlegroups. com>,
Ray D. <ra************@gmail.comwrote:
>Here is my code:
>void randomMatrix(double *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 indistinguishable from a feature."
-- Rich Kulawiec
Oct 26 '07 #2
Touchee, can't believe I didn't notice that!

On Oct 26, 2:19 pm, rober...@ibd.nrc-cnrc.gc.ca (Walter Roberson)
wrote:
In article <1193421934.207754.326...@19g2000hsx.googlegroups. com>,

Ray D. <ray.delvecc...@gmail.comwrote:
Here is my code:
void randomMatrix(double *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 indistinguishable from a feature."
-- Rich Kulawiec

Oct 26 '07 #3
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>
Oct 26 '07 #4
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(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[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(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[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(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
Oct 26 '07 #5
"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(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[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

Oct 26 '07 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Kapil Khosla | last post by:
Hi, I have been trying to understand this concept for quite sometime now somehow I am missing some vital point. I am new to Object Oriented Programming so maybe thats the reason. I want to...
7
by: Nicolay Korslund | last post by:
Hi! I'm having a little trouble with the typecast operator, can anybody help me with the rules for when the this operator is invoked? In the class 'Test' in the code below, the typecast operator...
2
by: Arun Prasath | last post by:
Hi all, I have the following question regd pointer typecasting. Is the following type of pointer typecasting valid? #define ALLOC(type,num) ((type *)malloc(sizeof(type)*num)) /*begin...
63
by: andynaik | last post by:
Hi, Whenever we type in this code int main() { printf("%f",10); } we get an error. We can remove that by using #pragma directive t direct that to the 8087. Even after that the output is...
11
by: Vinod Patel | last post by:
I have a piece of code : - void *data; ...... /* data initialized */ ...... struct known_struct *var = (struct known_struct*) data; /*typecasting*/ How is this different from simple...
3
by: jdm | last post by:
In the sample code for the SortedList class, I see the use of a string typecasting macro consisting of a single letter "S". i.e.: Sortedlist->Add(S"Keyval one", S"Item one"); Now I have...
7
by: Raghu | last post by:
Hello All, I need some help regarding overloading operation. Is there any way to overload typecasting? I mean if i have a piece of code as below. int a = 2: float b; b = (float)a;
8
by: Mark Fink | last post by:
I try to port a server application to Jython. At the moment I use Jython21\Lib\socket.py Currently I do face problems with casting the string "localhost" to the desired value:...
12
by: bwaichu | last post by:
What is the best way to handle this warning: warning: cast from pointer to integer of different size I am casting in and out of a function that requires a pointer type. I am casting an...
3
by: sritejv | last post by:
Hello everyone, I am having a problem with typecasting void pointers.I have read the pointer basics but still cant understand why the following test code doesnt work. void *xyz; struct abcd...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.