This question already has answers here:
Format string for output dependent on a variable
(4 answers)
Closed 3 years ago.
I am trying to print a tree with the help of "format" and a variables. But couldn't figure out how to pass in variable and get an output in a generalized form. The below code works for a particular example but not for any tree.
I used hard coded approach for getting the horizontal spacing right using the if statement as below. But i want to just pass-in the variable "count" which contains the height of the tree, and get a output directly.
if (count .eq. 9) then
write(6,9) t%element
9 format (t12,I3)
else if (count .eq. 8) then
write(6,8) t%element
8 format (t9,I3)
else if (count .eq. 7) then
write(6,7) t%element
7 format (t6,I3)
else
write(6,6) t%element
6 format (t3,I3)
end if
The result you get is like this
1
2
3
4
5
6
7
What I usually do is to have the format identifier as a string, like this:
character(len=len("(T02, I3)")) :: fmt
write(fmt, '("(T", I2.2, ", I3)")') count ! if count=2, fmt will be "(T02, I3)"
write(*, fmt) t%element
Related
This question already has answers here:
Fortran: handling integer values of size: ~700000000000
(6 answers)
How do I do big integers in Fortran?
(1 answer)
Closed 8 months ago.
I'm trying to write a Fortran code which checks if a user input number is a prime or not. The code is given below,
program cp
implicit none
integer(kind=8) :: nmb, ind
real*16 :: sqnmb, real_nmb
print*, "Largest value for a is ", huge(nmb)
DO
print *,"Enter the number larger than 0:"
read (*,*) nmb
IF ( nmb <= 0) THEN
print *, "Please enter a number larger than 0."
END IF
IF ( nmb > 0) THEN
EXIT
END IF
END DO
sqnmb = sqrt(real(nmb))
do ind= 2, int(sqnmb), 1
IF (mod(nmb,ind) == 0) THEN
print *, "The number", nmb ,"is not a prime, it is divisible by",ind
STOP
END IF
end do
print *, "The number", nmb ,"is a prime"
end program cp
The program works fine for all numbers below 9223372036854775807(19 digits) which is the largest number that can be defined with the integer(kind=8) type. However, I want to calculate for numbers with 25 digits. How can we define larger integers in Fortran?
This question already has answers here:
JavaFX TextField Array max length of text value
(1 answer)
How to restrict TextField so that it can contain only one '.' character? JavaFX
(3 answers)
Java 8 U40 TextFormatter (JavaFX) to restrict user input only for decimal number
(1 answer)
Numeric TextField for Integers in JavaFX 8 with TextFormatter and/or UnaryOperator
(4 answers)
Closed 5 years ago.
I need to limit input length to 4 digits in JFX textField (field can be empty or have value 0-9999). Below solution works only partially - I can input only digits , but as many as i want - limit do not work. Even if I remove {0,4} from regex and
change IF condition to:
if(newText.matches("\d") && newText.length()>=0 && newText.length()<5)
it doesn't work too. Where is the error?
JFXtextField.textProperty().addListener((obs, oldText, newText) ->
{
if(newText.matches("\\d{0,4}"))
{
newText = newText;
}
else
{
newText = oldText;
}
});
This question already has answers here:
Algorithm to list unique permutations of string with duplicate letters
(3 answers)
Closed 6 years ago.
char ary[10] = "AAABB";
There are 6!/(3!*2!) possible ways to arrange them. How do I find them all?
First, there are 5! / (3! * 2!) = 10 possible ways, not 6! / (3! * 2!). But I think this is your typo.
For this specific string "AAABB", you can do something like this:
Take away "BB", and treat "A"s as separators of "B" slots. Let _ (underscore) be the slot we can insert "B" in.
_A_A_A_
First, treat "BB" as two separated char, and insert them in. We can insert them in slot 1 & 2 ("BABAA"), 1 & 3 ("BAABA"), 1 & 4, 2 & 3, 2 & 4, 3 & 4. (6 in total)
Then, treat "BB" as one, and insert it in. We can insert it in slot 1 ("BBAAA"), 2 ("ABBAA"), 3, 4. (4 in total)
All 10 possible ways iterated.
This question already has answers here:
R: convert list of numbers from character to numeric
(3 answers)
Closed 7 years ago.
Suppose I have the character string
x <- " 1.1325 -0.9022 -0.1832 -0.5479 0.1236 -0.6556 -1.0599 -0.8881 -0.2136"
and I want to extract the floats to end up with this vector as output:
c(1.1325, -0.9022, -0.1832, -0.5479, 0.1236, -0.6556, -1.0599, -0.8881, -0.2136)
What I managed to achieve is:
na.omit(as.numeric(strsplit(samp, split = " ")[[1]]))
My question: Is there a more efficient way?
We can use scan
scan(text=x, what=numeric(), quiet=TRUE)
#[1] 1.1325 -0.9022 -0.1832 -0.5479 0.1236 -0.6556 -1.0599 -0.8881 -0.2136
This question already has answers here:
How do I scale down numbers from rand()?
(9 answers)
Closed 8 years ago.
Hi. Is there any way to set the size of random numbers?( in random number generator "rand()")
For example I want to generate 10 digits random numbers.
and one more question, how can i set random function to generate numbers between 0 and 1 (for example 0100110110) ?
Im not sure about setting the size of the numbers. However I dont think it would be possible to get each digit to produce just a 0 or 1.
What you can do however is something like below:
ostringstream 10digitNumber;
for(int i = 0 ; i < 10 ; i ++){
v1 = rand() % 2;// generate o or 1
10digitNumber<< v1;// build up a string of 1 and 0
}
int real10DigitNumber = static_cast<int>10digitNumber); // typecast to integer
Please forgive me if my syntax isn't 100 %. Its being awhile since I used c++.