Solved

Integer/Integer division

Posted on 2011-10-11
369 Views
I have two int variables :
int v1=43;
int v2=9;
I want v1/v2 to be of float or double value. How can this be done?
0
Question by:kites

LVL 47

Expert Comment

float flnum = (float)v1/(float)v2;
0

LVL 47

Expert Comment

Of cousrse teh same thing with double:

double dnum = (double)v1/(double)v2;

0

LVL 47

Accepted Solution

This is how it works:
``````       int v1=43;
int v2=9;

float flnum = (float)v1/(float)v2;

double dnum10 = (double)v1/(double)v2;

System.out.println("flnum: " + flnum);

System.out.println("dnum10: " + dnum10);
``````

Output:

``````flnum: 4.7777777
dnum10: 4.777777777777778
``````
0

Author Comment

Thanks!
How do we allocate memory for int[3][5] in C?
Also how do we time any C program?
0

LVL 47

Expert Comment

In C you can allocate memory for an array  of known size using statements such as:

char name[32];
int nums[100];
double coords[60][60]
char names[10][20][64];
double space[32][32][32][32];

http://www.phy.duke.edu/~marts/comp/ins.pdf

0

Author Comment

Dnt we use malloc?
0

LVL 47

Expert Comment

0

LVL 47

Expert Comment

This is if you want to allocate memory dynamically to more than one-dimensional array:
http://c-faq.com/~scs/cclass/int/sx9b.html
0

Author Comment

Thanks!!!
0

LVL 47

Expert Comment

You are always welcome.
0

Featured Post

Suggested Solutions

scoresAverage challenge 8 58
Java virtual machine monitoring tools 3 30
What is JNDI datasource in spring 1 19
JAVA part two 5 11
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements: