Fortran code giving error [closed] - fortran

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
I have taken this Fortran program from a book which basically runs a goodness to fit test for certain data and gives output. Code and its actual result/output are given as under:
real*4 x(50),xc(50,20),omega(50)
integer ir(50)
real*8 xx
c This code tests goodness of fit.
n=47
c The method of Bak, Nielsen, and Madsen is used.
data (x(i), i=1,47)/ 18, 22, 26, 16, 19, 21, 18, 22,
* 25, 31, 30, 34, 31, 25, 21, 24, 21, 28, 24, 26, 32,
* 33, 36, 39, 32, 33, 42, 44, 43, 48, 50, 56, 57, 59,
* 51, 49, 49, 57, 69, 72, 75, 76, 78, 73, 73, 75, 86/
do 999 icase=1,2
c Parameter icase =1 or 2 denotes SDE model 1 or 2.
xx=102038.
m=8
h=1.0
do 10 j=1,m+1
10 omega(j)=0.0
kk=4
akk=kk
h=h/akk
do 202 i=2,n
xs=x(i-1)
xe=x(i)
do 202 j=1,m
xk=xs
do 252 k=1,kk
call functs(icase,xk,f,g)
call random(xx,rand1,rand2)
252 xk=xk+h*f+sqrt(h)*g*rand1
xc(i,j)=xk
202 continue
do 402 i=2,n
irr=1
do 302 j=1,m
xe=x(i)
xcalc=xc(i,j)
if(xe.gt.xcalc) irr=irr+1
302 continue
402 ir(i)=irr
do 502 i=2,n
irr=ir(i)
omega(irr)=omega(irr)+1.0
502 continue
chi2=0.0
an=n
am=m
hlp=(an-1.0)/(am+1.0)
do 602 j=1,m+1
602 chi2=chi2+(omega(j)-hlp)**2/hlp
write(6,100) icase,chi2
100 format(5x,i7,5x,f9.2)
999 continue
stop
end
subroutine functs(icase,x,f,g)
th1=3510.0
th2=13500.0
f=th1/(x*x)
g=th2/(x*x)
if(icase.eq.1) goto 17
th1=.0361
th2=.6090
f=th1*x
g=sqrt(th2*x)
17 continue
return
end
subroutine random(xx,rand1,rand2)
real*8 xx,a,b,d,rng(2)
a=16807.
ib=2147483647
b=ib
do 55 i=1,2
id=a*xx/b
d=id
xx=a*xx-d*b
55 rng(i)=xx/b
pi=3.141592654
u1=rng(1)
u2=rng(2)
hlp=sqrt(-2.0*alog(u1))
rand1=hlp*cos(pi*2.0*u2)
rand2=hlp*sin(pi*2.0*u2)
return
end
Output of this program is:
1 18.57
2 4.09
However, even after using many online Fortran compilers i am not getting these results. It is giving errors like non standard type declaration etc.
I need help to get the same output as mentioned above.

The code is a written using the (old) Fortran 77 style with the addition of some common extensions. Since it uses the so called fixed-form the columns used by the source code are crucial to have a correct code. In particular for the case:
comments are defined by c character at the first column
continuation lines are defined by * at the sixth column
labels must use the first 5 columns
regular code must use 7-72 column range
Properly indenting your code allows to have it running on both GNU gfortran (tested using v.4.8.2) and Intel ifort (tested using version 15.0.2). To inform the compiler that you want to adopt the fixed-form for most compilers you have just to use .f extension for the source file. Otherwise you have suitable compilers options. For gfortran, compile specifying -ffixed-form. to The (minimally) indented code is provided below.
real*4 x(50),xc(50,20),omega(50)
integer ir(50)
real*8 xx
c This code tests goodness of fit.
n=47
c The method of Bak, Nielsen, and Madsen is used.
data (x(i), i=1,47)/ 18, 22, 26, 16, 19, 21, 18, 22,
* 25, 31, 30, 34, 31, 25, 21, 24, 21, 28, 24, 26, 32,
* 33, 36, 39, 32, 33, 42, 44, 43, 48, 50, 56, 57, 59,
* 51, 49, 49, 57, 69, 72, 75, 76, 78, 73, 73, 75, 86/
do 999 icase=1,2
c Parameter icase =1 or 2 denotes SDE model 1 or 2.
xx=102038.
m=8
h=1.0
do 10 j=1,m+1
10 omega(j)=0.0
kk=4
akk=kk
h=h/akk
do 202 i=2,n
xs=x(i-1)
xe=x(i)
do 202 j=1,m
xk=xs
do 252 k=1,kk
call functs(icase,xk,f,g)
call random(xx,rand1,rand2)
252 xk=xk+h*f+sqrt(h)*g*rand1
xc(i,j)=xk
202 continue
do 402 i=2,n
irr=1
do 302 j=1,m
xe=x(i)
xcalc=xc(i,j)
if(xe.gt.xcalc) irr=irr+1
302 continue
402 ir(i)=irr
do 502 i=2,n
irr=ir(i)
omega(irr)=omega(irr)+1.0
502 continue
chi2=0.0
an=n
am=m
hlp=(an-1.0)/(am+1.0)
do 602 j=1,m+1
602 chi2=chi2+(omega(j)-hlp)**2/hlp
write(6,100) icase,chi2
100 format(5x,i7,5x,f9.2)
999 continue
stop
end
subroutine functs(icase,x,f,g)
th1=3510.0
th2=13500.0
f=th1/(x*x)
g=th2/(x*x)
if(icase.eq.1) goto 17
th1=.0361
th2=.6090
f=th1*x
g=sqrt(th2*x)
17 continue
return
end
subroutine random(xx,rand1,rand2)
real*8 xx,a,b,d,rng(2)
a=16807.
ib=2147483647
b=ib
do 55 i=1,2
id=a*xx/b
d=id
xx=a*xx-d*b
55 rng(i)=xx/b
pi=3.141592654
u1=rng(1)
u2=rng(2)
hlp=sqrt(-2.0*alog(u1))
rand1=hlp*cos(pi*2.0*u2)
rand2=hlp*sin(pi*2.0*u2)
return
end
If you want to compile using an online resource be sure you properly copy-paste the code (with the right indentation) and use the option for the fixed form. For example using https://www.tutorialspoint.com/compile_fortran_online.php in the shell below compile typing: gfortran -ffixed-form *.f95 -o main.
Since Fortran 77 style is quite old now, if you are starting a new code I personally suggest to move to free-form source code and to use more recent Fortran features. A possible rewrite of the code using a modern style is given below:
module my_kinds
integer, parameter :: sp = selected_real_kind(9)
integer, parameter :: dp = selected_real_kind(18)
end module my_kinds
program test_from_book
use my_kinds
real(sp) :: x(50),xc(50,20),omega(50)
integer :: ir(50)
real(dp) :: xx
! This code tests goodness of fit.
n=47
! The method of Bak, Nielsen, and Madsen is used.
x = [ 18, 22, 26, 16, 19, 21, 18, 22, &
25, 31, 30, 34, 31, 25, 21, 24, 21, 28, 24, 26, 32, &
33, 36, 39, 32, 33, 42, 44, 43, 48, 50, 56, 57, 59, &
51, 49, 49, 57, 69, 72, 75, 76, 78, 73, 73, 75, 86, &
0 , 0, 0]
loop_999: do icase=1,2
! Parameter icase =1 or 2 denotes SDE model 1 or 2.
xx=102038.
m=8
h=1.0
do j=1,m+1
omega(j)=0.0
enddo
kk=4
akk=kk
h=h/akk
loop_202: do i=2,n
xs=x(i-1)
xe=x(i)
do j=1,m
xk=xs
do k=1,kk
call functs(icase,xk,f,g)
call random(xx,rand1,rand2)
xk=xk+h*f+sqrt(h)*g*rand1
enddo
xc(i,j)=xk
enddo
enddo loop_202
loop_402: do i=2,n
irr=1
do j=1,m
xe=x(i)
xcalc=xc(i,j)
if(xe.gt.xcalc) irr=irr+1
enddo
ir(i)=irr
enddo loop_402
do i=2,n
irr=ir(i)
omega(irr)=omega(irr)+1.0
enddo
chi2=0.0
an=n
am=m
hlp=(an-1.0)/(am+1.0)
do j=1,m+1
chi2=chi2+(omega(j)-hlp)**2/hlp
enddo
write(6,100) icase,chi2
100 format(5x,i7,5x,f9.2)
enddo loop_999
stop
end
subroutine functs(icase,x,f,g)
th1=3510.0
th2=13500.0
f=th1/(x*x)
g=th2/(x*x)
if(icase.ne.1) then
th1=.0361
th2=.6090
f=th1*x
g=sqrt(th2*x)
endif
end
subroutine random(xx,rand1,rand2)
use my_kinds
real(dp) :: xx,a,b,d,rng(2)
a=16807.
ib=2147483647
b=ib
do i=1,2
id=a*xx/b
d=id
xx=a*xx-d*b
rng(i)=xx/b
enddo
pi=3.141592654
u1=rng(1)
u2=rng(2)
hlp=sqrt(-2.0*alog(u1))
rand1=hlp*cos(pi*2.0*u2)
rand2=hlp*sin(pi*2.0*u2)
end

Related

Compile time efficient remove duplicates from a boost::hana tuple

I use the boost::hana to_map function to remove duplicates from boost::hana tuple of types. See it at the compiler explorer. The code works very well but compiles very long (~10s). I wonder if there exist a faster solution that is compatible with boost::hana tuple.
#include <boost/hana/map.hpp>
#include <boost/hana/pair.hpp>
#include <boost/hana/type.hpp>
#include <boost/hana/basic_tuple.hpp>
#include <boost/hana/size.hpp>
using namespace boost::hana;
constexpr auto to_type_pair = [](auto x) { return make_pair(typeid_(x), x); };
template <class Tuple>
constexpr auto remove_duplicate_types(Tuple tuple)
{
return values(to_map(transform(tuple, to_type_pair)));
}
int main(){
auto tuple = make_basic_tuple(
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20
, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30
, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40
, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50
, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60
, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70
);
auto noDuplicatesTuple = remove_duplicate_types(tuple);
// Should return 1 since there is only one distinct type in the tuple
return size(noDuplicatesTuple);
}
I haven't run any benchmarks, but your example does not appear to take 10 seconds on Compiler Explorer. However, I can explain why it is a relatively slow solution, and suggest an alternative that assumes you are only interested getting a unique list of types and not retaining any run-time information in your result.
Creating large tuples and/or instantiating function templates that have large tuples in their prototypes are expensive compile-time operations.
Just your call to transform instantiates a lambda for each element which in turn instantiates pair. The input/output of this call are both large tuples.
The call to to_map makes an empty map and recursively calls insert for each element each time making a new map, but in this simple case the intermediate result will always be hana::map<int>. I'm willing to bet that this is exploding your compile-times if your actual use case is non-trivial. (It was certainly an issue when we were implementing hana::map so we made hana::make_map avoid this since it has all of its inputs up front).
All of this, and there is a significant penalty for these large function types being used in run-time code. You might notice a difference if you wrapped the operations in decltype and only used the resulting type.
Alternatively, using raw template metaprogramming can sometimes yield performance results over function template based metaprogramming. Here is an example for your use case:
#include <boost/hana/basic_tuple.hpp>
#include <boost/mp11/algorithm.hpp>
namespace hana = boost::hana;
using namespace boost::mp11;
int main() {
auto tuple = hana::make_basic_tuple(
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20
, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30
, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40
, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50
, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60
, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70
);
hana::basic_tuple<int> no_dups = mp_unique<std::decay_t<decltype(tuple)>>{};
}
https://godbolt.org/z/EnTWf6

How to fix an 'undefined symbol error'?

I am quite new to both abaqus and fortran programming. I am working on a project where I am tying phase field to fracture propagation where the void phase is taken into the kinematics of the crack propagation. I will not go too much into detail!
I have attached 3 codes/messages with my question here:
a) umat_sdvini3.f (my user subroutine file that has both sdvini and umat in it)
b) Job-User-mod-9.inp (the .inp input file that I created with the help of Abaqus CAE)
c) Job-User-15.msg (the file that contains the error message!)
When I am telling Abaqus to run the job with the user subroutine, the job gets aborted for some reason! In the previous job runs (11 to 14), when in the subroutine file, the if/else statement for the state variable was commented out, everything ran fine!
With the second if/else statement uncommented in the attached fortran file, there is an error and the job is getting aborted!
The fortran 77 code for my user subroutine: (it is the second if/else statement command block not running properly! I thought I could access the values of the solution dependent state variables via coords(1) which is the x-coordinates!
subroutine sdvini(statev,coords,nstatv,ncrds,noel,npt,layer,kspt)
include 'aba_param.inc'
dimension statev(nstatv), coords(ncrds)
statev(1)=5.d0
return
end
subroutine umat(stress,statev,ddsdde,sse,spd,scd,
1 rpl,ddsddt,drplde,drpldt,
2 stran,dstran,time,dtime,temp,dtemp,predef,dpred,cmname,
3 ndi,nshr,ntens,nstatv,props,nprops,coords,drot,pnewdt,
4 celent,dfgrd0,dfgrd1,noel,npt,layer,kspt,kstep,kinc)
include 'aba_param.inc'
character*80 cmname
dimension stress(ntens),statev(nstatv)
dimension ddsdde(ntens,ntens),ddsddt(ntens),drplde(ntens)
dimension stran(ntens),dstran(ntens),time(2),predef(1),dpred(1)
dimension props(nprops),coords(3),drot(3,3),dfgrd0(3,3),dfgrd1(3,3)
C real E, xnu, bulk, shear
integer i, j
ddsdde = 0.d0
E = props(1)
xnu = props(2)
statev(1)=5.d0
shear = E/2.d0/(1.d0+xnu)
bulk = E/3.d0/(1.d0-2.d0*xnu)
real f_i=1.0e-10
real w_bar=0.25d0
real walpha=2.d0
a_1 = (-2)*(1.d0-f_i)/0.25d0
a_2 = (1.d0-f_i)/(0.25**2)
C omega = statev(1)
if(ndi.eq.2 .and. nshr.eq.1) then
ddsdde(1,1) = bulk + (10.d0/9.d0)*shear
ddsdde(1,2) = bulk - (8.d0/9.d0)*shear
ddsdde(2,1) = bulk - (8.d0/9.d0)*shear
ddsdde(2,2) = bulk + (10.d0/9.d0)*shear
ddsdde(3,3) = 2.d0*shear
ddsdde = ddsdde*statev(1)
end if
do i=1,ntens
do j=1,ntens
stress(i) = stress(i) + ddsdde(i,j)*dstran(j)
end do
end do
if(coords(1).le.0 .and. coords(2).eq.0) then
statev(1) = (w_bar*(1-tanh(w_alpha*coords(1))))
statev(1)=1.d0+a_1*(statev(1))+a_2*(statev(1)**2)
elseif(coords(1).gt.0 .and. coords(2).eq.0) then
statev(1) = (w_bar*(1+tanh(w_alpha*coords(1))))
statev(1)=1.d0+a_1*(statev(1))+a_2*(statev(1)**2)
else
statev(1)=0.d0
end if
statev(1)=state(1) + kinc
write(6,*) ddsdde
return
end
This is the .inp file of the Abaqus (Standard):
*Heading
** Job name: Job-User-9 Model name: Model-1
** Generated by: Abaqus/CAE 6.14-2
*Preprint, echo=NO, model=NO, history=NO, contact=NO
**
** PARTS
**
*Part, name=Plate
*Node
1, 0., 100.
2, 0., 99.5
3, 0., 99.
4, 0., 98.5
5, 0., 98.
6, 0., 97.5
7, 0., 97.
8, 0., 96.5
9, 0., 96.
10, 0., 95.5
11, 0., 95.
12, 0., 94.5
13, 0., 94.
14, 0., 93.5
15, 0., 93.
16, 0., 92.5
17, 0., 92.
18, 0., 91.5
19, 0., 91.
20, 0., 90.5
21, 0., 90.
22, 0., 89.5
23, 0., 89.
24, 0., 88.5
25, 0., 88.
26, 0., 87.5
27, 0., 87.
28, 0., 86.5
29, 0., 86.
30, 0., 85.5
31, 0., 85.
32, 0., 84.5
33, 0., 84.
34, 0., 83.5
35, 0., 83.
36, 0., 82.5
37, 0., 82.
38, 0., 81.5
39, 0., 81.
40, 0., 80.5
41, 0., 80.
42, 0., 79.5
43, 0., 79.
44, 0., 78.5
45, 0., 78.
46, 0., 77.5
47, 0., 77.
48, 0., 76.5
49, 0., 76.
50, 0., 75.5
...
...
...
*Element, type=CPS4R
1, 1, 2, 203, 202
2, 2, 3, 204, 203
3, 3, 4, 205, 204
4, 4, 5, 206, 205
5, 5, 6, 207, 206
6, 6, 7, 208, 207
7, 7, 8, 209, 208
8, 8, 9, 210, 209
9, 9, 10, 211, 210
10, 10, 11, 212, 211
11, 11, 12, 213, 212
12, 12, 13, 214, 213
13, 13, 14, 215, 214
14, 14, 15, 216, 215
15, 15, 16, 217, 216
16, 16, 17, 218, 217
17, 17, 18, 219, 218
18, 18, 19, 220, 219
19, 19, 20, 221, 220
20, 20, 21, 222, 221
21, 21, 22, 223, 222
22, 22, 23, 224, 223
23, 23, 24, 225, 224
24, 24, 25, 226, 225
25, 25, 26, 227, 226
26, 26, 27, 228, 227
27, 27, 28, 229, 228
28, 28, 29, 230, 229
29, 29, 30, 231, 230
30, 30, 31, 232, 231
31, 31, 32, 233, 232
32, 32, 33, 234, 233
33, 33, 34, 235, 234
34, 34, 35, 236, 235
35, 35, 36, 237, 236
36, 36, 37, 238, 237
37, 37, 38, 239, 238
38, 38, 39, 240, 239
39, 39, 40, 241, 240
40, 40, 41, 242, 241
41, 41, 42, 243, 242
42, 42, 43, 244, 243
43, 43, 44, 245, 244
44, 44, 45, 246, 245
45, 45, 46, 247, 246
46, 46, 47, 248, 247
47, 47, 48, 249, 248
48, 48, 49, 250, 249
49, 49, 50, 251, 250
50, 50, 51, 252, 251
...
...
...
*Nset, nset=Set-1, generate
1, 40401, 1
*Elset, elset=Set-1, generate
1, 40000, 1
** Section: Section-1
*Solid Section, elset=Set-1, controls=EC-1, material=Material-1
1.,
*Hourglass Stiffness
100., , 0., 0.
*End Part
**
**
** ASSEMBLY
**
*Assembly, name=Assembly
**
*Instance, name=Plate-1, part=Plate
*End Instance
**
*Nset, nset=Left_edge, instance=Plate-1, generate
1, 201, 1
*Elset, elset=Left_edge, instance=Plate-1, generate
1, 200, 1
*Nset, nset=bottom-edge, instance=Plate-1, generate
2211, 40401, 201
*Elset, elset=bottom-edge, instance=Plate-1, generate
2200, 40000, 200
*Elset, elset=_Surf-1_S3, internal, instance=Plate-1, generate
39801, 40000, 1
*Elset, elset=_Surf-1_S4, internal, instance=Plate-1, generate
1, 39801, 200
*Surface, type=ELEMENT, name=Surf-1
_Surf-1_S3, S3
_Surf-1_S4, S4
*End Assembly
**
** ELEMENT CONTROLS
**
*Section Controls, name=EC-1, hourglass=STIFFNESS
1., 1., 1.
**
** MATERIALS
**
*Material, name=Material-1
*Depvar
1,
*User Material, constants=2
1e+06, 0.3
**
*INITIAL CONDITIONS, TYPE=SOLUTION,USER
** BOUNDARY CONDITIONS
**
** Name: BC-1 Type: Displacement/Rotation
*Boundary
Left_edge, 1, 1
** Name: BC-2 Type: Displacement/Rotation
*Boundary
bottom-edge, 2, 2
** ----------------------------------------------------------------
**
** STEP: Step-1
**
*Step, name=Step-1, nlgeom=NO
*Static
1., 1., 1e-05, 1.
**
** LOADS
**
** Name: Load-1 Type: Pressure
*Dsload
Surf-1, P, -100.
**
** OUTPUT REQUESTS
**
*Restart, write, frequency=0
**
** FIELD OUTPUT: F-Output-1
**
*Output, field
*Node Output
CF, COORD, RF, U
*Element Output, directions=YES
E, S, SDV
**
** FIELD OUTPUT: F-Output-2-bottomEdge
**
*Node Output, nset=bottom-edge
COORD, U
*Element Output, elset=bottom-edge, directions=YES
S, SDV
**
** HISTORY OUTPUT: H-Output-1
**
*Output, history, variable=PRESELECT
*End Step
The .msg file is below: (I do not understand the error well!!! at all!!!)
Abaqus JOB Job-User-15
Abaqus 6.14-2
Abaqus License Manager checked out the following licenses:
Abaqus/Standard checked out 5 tokens from Flexnet server lm.rcc.psu.edu.
<255 out of 300 licenses remain available>.
Begin Compiling Abaqus/Standard User Subroutines
Thu 13 Jul 2017 02:38:34 PM EDT
Intel(R) Fortran Intel(R) 64 Compiler XE for applications running on
Intel(R) 64, Version 15.0.0.090 Build 20140723
Copyright (C) 1985-2014 Intel Corporation. All rights reserved.
Intel(R) Fortran 15.0-1684
End Compiling Abaqus/Standard User Subroutines
Begin Linking Abaqus/Standard User Subroutines
Intel(R) Fortran Intel(R) 64 Compiler XE for applications running on
Intel(R) 64, Version 15.0.0.090 Build 20140723
Copyright (C) 1985-2014 Intel Corporation. All rights reserved.
ifort: command line remark #10148: option '-i-dynamic' not supported
GNU ld version 2.20.51.0.2-5.36.el6 20100205
End Linking Abaqus/Standard User Subroutines
Thu 13 Jul 2017 02:38:35 PM EDT
Begin Analysis Input File Processor
Thu 13 Jul 2017 02:38:35 PM EDT
Run pre
Thu 13 Jul 2017 02:38:39 PM EDT
End Analysis Input File Processor
Begin Abaqus/Standard Analysis
Thu 13 Jul 2017 02:38:39 PM EDT
Run standard
/gpfs/apps/x86_64-rhel6/abaqus/6.14-2/6.14-2/code/bin/standard: symbol
lookup error: /tmp/mfg5310_Job-User-15_1727/libstandardU.so: undefined
symbol: state_
Thu 13 Jul 2017 02:38:43 PM EDT
Abaqus Error: Abaqus/Standard Analysis exited with an error - Please see the
message file for possible error messages if the file exists.
Abaqus/Analysis exited with errors
Could any of you please help me with what it could be? Please!
Thank you,
Mousumi
As indicated by the error message, you have a typo at the undefined variable state.
statev(1)=state(1) + kinc
!! Should be:
statev(1)=statev(1) + kinc
It's possible the compiler didn't catch that error during compilation prior to runtime since you are using implicit typing (that is, not explicitly defining the variable types before using them). Perhaps you can verify that yourself.

Array of Structs cannot read intergers assigned to them c++

I am trying to assign values to the Arrival, BurstTime, and IOTime arrays. It worked fine when I didn't have Arrival[9]. However trying to pass multiple ints into the variable doesn't work. I get -86638*** numbers. I have been trying to find an answer for days, No luck.
This is my struct.
struct ReadyQueue
{
//static const int MAX_NUMBER = 10;
int ArrivalTime[10];
int BurstTime[10];
int IOTime[10];
std::string Name;
};
and my variables
P[0].ArrivalTime[10] = 0, 98, 221, 327, 423, 530, 628, 719, 788 ;
P[1].ArrivalTime[8] = 17, 116, 208, 320, 437, 554, 665, 754 ; // P2
P[2].ArrivalTime[7] = 27, 125, 238, 364, 468, 579, 680; // P3
P[3].ArrivalTime[8] = 45, 155, 276, 392, 515, 642, 739, 820 ; // P4
P[4].ArrivalTime[10] = 62, 186, 343, 489, 603, 715, 807, 887, 952, 997; // P5
P[5].ArrivalTime[7] = 67, 174, 262, 348, 446, 566, 654; // P6
P[6].ArrivalTime[9] = 77, 148, 216, 302, 359, 461, 546, 622, 697; // P7
P[7].ArrivalTime[8] = 83, 196, 306, 409, 499, 608, 702, 773 ; // P8
P[8].ArrivalTime[9] = 92, 192, 296, 386, 492, 599, 692, 734, 803; // P9
P[0].BurstTime[8] = 17 , 18, 17, 16, 14, 16, 14, 15, 15;
P[0].IOTime[8] = 24, 73, 31, 27, 33, 43, 64, 19 ;
P[1].BurstTime[8] = 10, 9, 8, 7, 9, 12, 15, 19 ;
P[1].IOTime[7] = 31, 35, 42, 43, 47, 43, 51 ;
P[2].IOTime[6] = 51, 53, 61, 31, 43, 31 ;
P[2].BurstTime[7] = 18, 23, 24, 22, 21, 20, 12 ;
P[3].BurstTime[8] = 17, 19, 20, 17, 15, 12, 15, 14 ;
P[3].IOTime[7] = 42, 55, 54, 52, 67, 72, 66 ;
P[4].BurstTime[10] = 5, 6, 5, 3, 5, 4, 3, 4, 3, 5 ;
P[4].IOTime[9] = 61, 82, 71, 61, 62, 51, 77, 61, 42 ;
P[5].BurstTime[7] = 10, 12, 14, 11, 15, 13, 11 ;
P[5].IOTime[6] = 35, 41, 33, 32, 41, 29 ;
P[6].BurstTime[7] = 6, 7, 5, 4, 5, 7, 8, 6, 5 ;
P[6].IOTime[8] = 18, 21, 19, 16, 29, 21, 22, 24 ;
P[7].BurstTime[8] = 9, 12, 14, 14, 16, 14, 13, 15 ;
P[7].IOTime[7] = 52, 42, 31, 21, 43, 31, 32 ;
P[8].BurstTime[9] = 6, 4, 6, 6, 7, 4, 5, 5, 4 ;
P[8].BurstTime[8] = 35, 41, 33, 32, 41, 29, 16, 22 ;
Thank you very much
I want to clarify what those assignments do, because I see some wrong answers.
Let's see it with an example:
P[0].ArrivalTime[10] = 0, 98, 221, 327, 423, 530, 628, 719, 788 ;
First, that statement is assigning 0 in the eleventh element of the ArrivalTime array and I don't think you wanted to do this due to ArrivalTime[9] is the last element.
Second, the comma operator evaluates the first operand, discard the result, and then evaluates the second operand and returns his value. In the example you could think this evaluation returns 788, but assign operator have more precedence than comma, so, the statement will evaluate like this:
((P[0].ArrivalTime[10] = 0), 98, 221, 327, 423, 530, 628, 719, 788 ;
Change this:
struct ReadyQueue
{
//static const int MAX_NUMBER = 10;
int ArrivalTime[10];
int BurstTime[10];
int IOTime[10];
std::string Name;
};
to this:
struct Item
{
int ArrivalTime;
int BurstTime;
int IOTime;
};
struct Ready_queue
{
string name;
queue<Item> items;
};
where queue is std::queue.
If that doesn't suit your higher level purpose, then something similar that does suit that (unexplained) purpose.
The main point is an inversion of the logical structure, putting related data together.
Do note that e.g. x = 6, 4, 6, 6, 7, 4, 5, 5, 4 ; is parsed as (x = 6), 4, 6, 6, 7, 4, 5, 5, 4 ; and thus is equivalent to just = 6;. The longwinded expression after the assignment is using the comma operator, which evaluates the expressions in ordinary reading order, producing the value of the last one. Due to the parsing also that final value is discarded.
Please note that my answer contained a mistake which I corrected it: equal operator has a higher priority than the comma operator (it might still contain traces of it)
This is not how you assign an array to a variable.
what you are doing is simply:
assign 788 0 to p[0].ArrivalTime[10] //the 11th element of p[0] which is outside the reserved space for the array
because the comma operator what it does is that it evaluates every one from left to right and return the last value: example:
int i=0,j;
j=i++,(i+=5),i; //j=0 and i=6 //because it is in fact (j=i++),(i+=5),i;
j=(i++,(i+=5),i); //j=6 and i=6
what you are trying to do is assign p[0].arrivalTime to an array of 10. You should do it this way:
P[0].ArrivalTime[0] = 0 ;
P[0].ArrivalTime[1] = 98 ;
...
P[0].ArrivalTime[9] = 788 ;
I don't know any method that assign an array to an array variable after declaration
You cannot do assignments to arrays like this in c/c++. You can use a similar syntax with {} for initializing arrays, but it won't work for what you want to do here. You could use memcpy to copy the arrays into the memory locations in the struct.
memcpy( p[0].ArrivalTime,
(const int[9]) {0, 98, 221, 327, 423, 530, 628, 719, 788},
sizeof(int [9])
);
Note that the comma is an operator which evaluates a list of expressions and returns the value of the last expression. This means that
P[0].ArrivalTime[10] = 0, 98, 221, 327, 423, 530, 628, 719, 788 ;
assigns 0 to the value at index 10 of the array named ArrivalTime and ignores the rest of the list.

Permuting on a schedule python

I'm trying to implement simplified DES for learning purposes in python, but I am having trouble figuring out how to do the permutations based on a "schedule." Essentially, I have a tuple with the appropriate permutations, and I need to bit shift to the correct location.
For example, using a key:
K = 00010011 00110100 01010111 01111001 10011011 10111100 11011111 11110001
Would move the 57st bit to the first bit spot, 49th bit to the second bit spot, etc...
K+ = 1111000 0110011 0010101 0101111 0101010 1011001 1001111 0001111
Current code:
def keyGen(key):
PC1table = (57, 49, 41, 33, 25, 17, 9,
1, 58, 50, 42, 34, 26, 18,
10, 2, 59, 51, 43, 35, 27,
19, 11, 3, 60, 52, 44, 36,
63, 55, 47, 39, 31, 23, 15,
7, 62, 54, 46, 38, 30, 22,
14, 6, 61, 53, 45, 37, 29,
21, 13, 5, 28, 20, 12, 4)
keyBinary = bin(int(key, 16))[2:].zfill(64)
print keyBinary
permute(PC1table, keyBinary)
def permute(permutation, permuteInput):
elements = list(enumerate(permutation))
for bit in permuteInput:
***magic bitshifting goes here***
keyGen("133457799BBCDFF1")
The logic I thought would work was to enumerate the tuple of permutations, and for each bit of my old key, look in the enumeration to find the index corresponding the the bit, and bit shift the appropriate number of times, but I just can't figure out how to go about doing this. It may be that I am approaching the problem from the wrong angle, but any guidance would be greatly appreciated!
Ok, I ended up figuring a way to make this work, although this probably isn't the most efficient way...
prior to calling the function, turn the binary number into a list:
keyBinary = bin(int(key, 16))[2:].zfill(64)
keyBinary = [int(i) for i in keyBinary]
Kplus = permute(PC1table, keyBinary)
def permute(mapping, permuteInput):
permuteOutput = []
for i in range(len(mapping)):
permuteOutput.append(permuteInput[mapping[i % 56] - 1])
return permuteOutput
if anyone has a better way of tackling this, I'd love to see your solutions!

Numbers between a and b without their permutations

I've written a similar question which was closed I would like to ask not the code but an efficiency tip. I haven't coded but if I can't find any good hint in here I'll go and code straightforward. My question:
Suppose you have a function listNums that take a as lower bound and b as upper bound.
For example a=120 and b=400
I want to print numbers between these numbers with one rule. 120's permutations are 102,210,201 etc. Since I've got 120 I would like to skip printing 201 or 210.
Reason: The upper limit can go up to 1020 and reducing the permutations would help the running time.
Again just asking for efficiency tips.
I am not sure how you are handling 0s (eg: after outputting 1 do you skip 10, 100 etc since technically 1=01=001..).
The trick is to select a number such that all its digits are in increasing order (from left to right).
You can do it recursively. AT every recursion add a digit and make sure it is equal to or higher than the one you recently added.
EDIT: If the generated number is less than the lower limit then permute it in such a way that it is greater than or equal to the lower limit. If A1A2A3..Ak is your number and it is lower than limit), then incrementally check if any of A2A1A3...Ak, A3A1A2...Ak, ... , AkA1A2...Ak-1 are within limit. If need arises, repeat this step to with keeping Ak as first digit and finding a combination of A1A2..Ak-1.
Eg: Assume we are selecting 3 digits and lower limit is 99. If the combination is 012, then the lowest permutation that is higher than 99 is 102.
When the lower bound is 0, an answer is given by the set of numbers with non-decreasing digits (0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 12, 13, 14, 15, 16, 17, 18, 19, 22, 23, 24, 25, 26, 27, 28, 29, 33, 34, 35, 36, 37, 38, 39, 44, 45, 46, 47, 48, 49, 55, 56, 57, 58, 59, 66, 67, 68, 69, 77, 78, 79, 88, 89, 99, 111, 112...) that fall in the requested range.
This sequence is easily formed by incrementing an integer, and when there is a carry, replicate the digit instead of carrying. Exemple: 73 is followed by 73+1 = 74 (no carry); 79 is followed by 79+1 = 80 (carry), so 88 instead; 22356999 is followed by 22356999+1 = 22357000, hence 22357777.
# Python code
A= 0 # CAUTION: this version only works for A == 0 !
B= 1000
N= A
while N < B:
# Detect zeroes at the end
S= str(N)
P= S.find('0')
if P > 0:
# Replicate the last nonzero digit
S= S[:P] + ((len(S) - P) * S[P-1])
N= eval(S)
# Next candidate
print N
N+= 1
Dealing with a nonzero lower bound is a lot more tricky.