Solved

float and long data type

Posted on 2004-05-01
6
396 Views
Last Modified: 2010-04-02
Dear Experts,

Seed = 0.000000
Press any key to continue . . .

The above are the results I receive after compiling the program below.  Can someone explain why I am getting the number 0.000000?  This is for a school assignment.  The objective here is to use Random function that was given to us, and send in the value "12345L".  Then output the result!  

The person with the most understandable explaination for a beginning programmer will be the one awarded with points.

Thanks for the response!
James

/*Below program is compiled and executed in Visual C++.NET*/
/*-----------------------------------------------------------------------------------------------*/
#include <stdio.h>
#include <stdlib.h>

float Random(long *seed);
/* Defining function Random*/
float Random(long *seed)
{
      float temp=0 ; //initialized to 0
      temp=(float)(*seed)/(65536L);
      *seed=(25173L*(*seed)+138496)%65536L;
      return(temp);
}

int main()
{
      float rn;
      long seed = 12345L;
      long *seedptr;
      
      seedptr = &seed;
      printf("Seed = %f \n", &seed);
      //printf("Seed = %f \n", *seedptr);
      //rn = Random(seedptr);

      //printf("The random number generated is: %f \n", rn);
      system("PAUSE");      
      return(0);
}
0
Comment
Question by:Charley420
  • 3
  • 2
6 Comments
 
LVL 46

Accepted Solution

by:
Sjef Bosman earned 200 total points
ID: 10968678
Printf cannot check whether the parameters passed are correct. The %f tells printf to get a float from its the stack, but you pushed an address of a long on it. A correct use of printf would be
    printf("Seed = %ld\n", seed)
or
    printf("Seed = %ld\n", *seedptr)
0
 
LVL 9

Expert Comment

by:tinchos
ID: 10968682
Just a comment Charley

Be aware that you're just printing the address of a local variable.
You're not calling the Random function.

(Note that the lines are commented)

Hope this helps

Tincho
0
 
LVL 1

Author Comment

by:Charley420
ID: 10969457
Tincho,
I had failed to mention in the original post, I commented out all of the other lines because I wanted to make sure I was passing in the correct value into the Random function.  Thanks for your response!

sjef_bosman,
I was searching on websites and non of them mentioned "%ld".   I understand that "%d" stands for signed integer written in decimal and "%l" specifies the variable is a long int*, but no one ever mentioned you concatenate the variables l and d together.  Thanks for your response!

James
0
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
LVL 46

Expert Comment

by:Sjef Bosman
ID: 10970942
Just some hints/links:
- search Google on "manual printf c", the second link is
- http://www.gnu.org/software/libc/manual/html_node/ and search for printf
- http://www.gnu.org/software/libc/manual/html_node/Formatted-Output.html#Formatted%20Output and click on Table of Output Conversions or Table of Integer Conversions
- the latter sends you to http://www.gnu.org/software/libc/manual/html_node/Integer-Conversions.html#Integer%20Conversions
- look on the second screen, where you will find anything on "type modifier"

And why a B?
0
 
LVL 1

Author Comment

by:Charley420
ID: 10974408
I fat fingered :o/....on the grading
I appreciated the links!

James
0
 
LVL 46

Expert Comment

by:Sjef Bosman
ID: 10975741
LOL
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now