├── .gitattributes ├── 9781484261279.jpg ├── Contributing.md ├── LICENSE.txt ├── README.md ├── ch10sources ├── cofm5a.c ├── cofm5b.c └── cofmc.c ├── ch11sources └── browntest3.c ├── ch12sources └── vaca.c ├── ch13sources └── chain1.c ├── ch1sources ├── ch1arith.c ├── ch1arr.c ├── ch1filecreate.c ├── ch1filecreate2.c ├── ch1fileread.c ├── ch1fileread2.c ├── ch1func.c ├── ch1funcans.c ├── ch1math.c ├── ch1strings.c └── ch1sw.c ├── ch2sources ├── capm.c └── regyonx2.c ├── ch3sources ├── pmccf.c ├── regboth.c ├── regbothip.c └── regbotht2.c ├── ch4sources ├── asseta2.c └── assetalgorithm.c ├── ch5sources ├── createmarket.c └── markettest4.c ├── ch6sources ├── createflightsb.c └── flightsg.c ├── ch7sources ├── createplantb.c ├── plantb.c └── plantbam.c ├── ch8sources └── peke.c ├── ch9sources ├── pendme.c └── pendme2.c └── errata.md /.gitattributes: -------------------------------------------------------------------------------- 1 | # Auto detect text files and perform LF normalization 2 | * text=auto 3 | -------------------------------------------------------------------------------- /9781484261279.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Apress/practical-numerical-c-programming/994a8e56be8e6a02846d45552fa8997c1ab1aed5/9781484261279.jpg -------------------------------------------------------------------------------- /Contributing.md: -------------------------------------------------------------------------------- 1 | # Contributing to Apress Source Code 2 | 3 | Copyright for Apress source code belongs to the author(s). However, under fair use you are encouraged to fork and contribute minor corrections and updates for the benefit of the author(s) and other readers. 4 | 5 | ## How to Contribute 6 | 7 | 1. Make sure you have a GitHub account. 8 | 2. Fork the repository for the relevant book. 9 | 3. Create a new branch on which to make your change, e.g. 10 | `git checkout -b my_code_contribution` 11 | 4. Commit your change. Include a commit message describing the correction. Please note that if your commit message is not clear, the correction will not be accepted. 12 | 5. Submit a pull request. 13 | 14 | Thank you for your contribution! -------------------------------------------------------------------------------- /LICENSE.txt: -------------------------------------------------------------------------------- 1 | Freeware License, some rights reserved 2 | 3 | Copyright (c) 2020 Philip Joyce 4 | 5 | Permission is hereby granted, free of charge, to anyone obtaining a copy 6 | of this software and associated documentation files (the "Software"), 7 | to work with the Software within the limits of freeware distribution and fair use. 8 | This includes the rights to use, copy, and modify the Software for personal use. 9 | Users are also allowed and encouraged to submit corrections and modifications 10 | to the Software for the benefit of other users. 11 | 12 | It is not allowed to reuse, modify, or redistribute the Software for 13 | commercial use in any way, or for a user’s educational materials such as books 14 | or blog articles without prior permission from the copyright holder. 15 | 16 | The above copyright notice and this permission notice need to be included 17 | in all copies or substantial portions of the software. 18 | 19 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 20 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 21 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 22 | AUTHORS OR COPYRIGHT HOLDERS OR APRESS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 23 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 24 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 25 | SOFTWARE. 26 | 27 | 28 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Apress Source Code 2 | 3 | This repository accompanies [*Practical Numerical C Programming*](https://www.apress.com/9781484261279) by Philip Joyce (Apress, 2020). 4 | 5 | [comment]: #cover 6 | ![Cover image](9781484261279.jpg) 7 | 8 | Download the files as a zip using the green button, or clone the repository to your machine using Git. 9 | 10 | ## Releases 11 | 12 | Release v1.0 corresponds to the code in the published book, without corrections or updates. 13 | 14 | ## Contributions 15 | 16 | See the file Contributing.md for more information on how you can contribute to this repository. -------------------------------------------------------------------------------- /ch10sources/cofm5a.c: -------------------------------------------------------------------------------- 1 | /* cofm5a.c 2 | Centre of Mass Calculation. 3 | Calculates c of m for 2D shape y = x^^2 4 | */ 5 | #define _CRT_SECURE_NO_WARNINGS 6 | #include 7 | #include 8 | #include 9 | #include 10 | double randfunc(); 11 | main() 12 | { 13 | 14 | int I,outcount; 15 | float area,total,count; 16 | FILE *fptr; 17 | time_t t; 18 | /* Local Arrays */ 19 | double x, y,xout[3500],yout[3500],xcofm,ycofm; 20 | 21 | fptr=fopen("cofm5a.dat","w"); 22 | 23 | 24 | 25 | /* Intializes random number generator */ 26 | srand((unsigned) time(&t)); 27 | 28 | /* clears arrays to zero */ 29 | for( I = 0; I<3500;I++) 30 | { 31 | xout[I] = 0.0; 32 | yout[I] = 0.0; 33 | 34 | } 35 | /* set x and y cofm accumulators to zero */ 36 | xcofm=0.0; 37 | ycofm=0.0; 38 | 39 | 40 | total = 0.0; 41 | count = 0.0; 42 | outcount = 0; 43 | for( I = 1;I<= 3500;I++) 44 | { 45 | /* get x values between -2 and +2 */ 46 | /* get y values between 0 and +4 */ 47 | x = randfunc()*4.0-2.0; 48 | y = randfunc()*4.0; 49 | 50 | /* If the generated x and y values are above */ 51 | /* the curve y=x^2 then add 1 to count */ 52 | /* and update the x and y cofm values */ 53 | 54 | if(y>pow(x,2)) 55 | { 56 | xcofm=xcofm+x; 57 | ycofm=ycofm+y; 58 | 59 | total = total+1; 60 | outcount = outcount +1; 61 | xout[outcount] = x; 62 | yout[outcount] = y; 63 | } 64 | count = count+1; 65 | 66 | } 67 | 68 | area=(total/count)*16;/* area is part of the square which is 4x4 or 16 sq units */ 69 | printf("total is %f count is %f\n",total,count); 70 | 71 | xcofm=xcofm/total; 72 | ycofm=ycofm/total; 73 | 74 | printf("area is %lf\n",area); 75 | printf("cofm is %lf,%lf",xcofm,ycofm); 76 | 77 | 78 | /* Plot the data */ 79 | 80 | if(outcount >= 2700) 81 | outcount = 2700; 82 | 83 | 84 | for(I = 1; I<=outcount-1;I++) 85 | fprintf(fptr,"%lf %lf\n",xout[I],yout[I]); 86 | fclose(fptr); 87 | 88 | } 89 | 90 | double randfunc() 91 | { 92 | /* get a random number 0 to 1 */ 93 | double ans; 94 | 95 | 96 | ans=rand()%1000; 97 | ans=ans/1000; 98 | 99 | return ans; 100 | 101 | } 102 | 103 | 104 | 105 | 106 | -------------------------------------------------------------------------------- /ch10sources/cofm5b.c: -------------------------------------------------------------------------------- 1 | /* cofm5b.c */ 2 | /* 3 | Centre of Mass Calculation. 4 | Calculates c of m for 2D shape between y = x^2, y = x^2 + 1 and y = 4 5 | */ 6 | #define _CRT_SECURE_NO_WARNINGS 7 | #include 8 | #include 9 | #include 10 | #include 11 | double randfunc(); 12 | main() 13 | { 14 | 15 | int I, outcount; 16 | float area,total,count; 17 | FILE *fptr; 18 | time_t t; 19 | /* Local Arrays */ 20 | double x, y,xout[3500],yout[3500],xcofm,ycofm; 21 | 22 | fptr=fopen("cofm5b.dat","w"); 23 | 24 | 25 | /* Intializes random number generator */ 26 | srand((unsigned) time(&t)); 27 | 28 | /* clears arrays to zero */ 29 | for( I = 1; I<=3500;I++) 30 | { 31 | xout[I] = 0.0; 32 | yout[I] = 0.0; 33 | 34 | } 35 | /* set x and y cofm accumulators to zero */ 36 | xcofm=0.0; 37 | ycofm=0.0; 38 | 39 | total = 0.0; 40 | count = 0.0; 41 | outcount = 0; 42 | for( I = 0;I< 3500;I++) 43 | { 44 | /* get x values between -2 and +2 */ 45 | /* get y values between 0 and +4 */ 46 | x = randfunc()*4.0-2.0; 47 | y = randfunc()*4.0; 48 | 49 | /* If the generated x and y values are above */ 50 | /* the curve y=x^2 and below y=x^2 + 1 then add 1 to count */ 51 | /* and update the x and y cofm values */ 52 | 53 | if(y>pow(x,2) && y<(pow(x,2)+1)) 54 | { 55 | xcofm=xcofm+x; 56 | ycofm=ycofm+y; 57 | 58 | total = total+1; 59 | outcount = outcount +1; 60 | xout[outcount] = x; 61 | yout[outcount] = y; 62 | } 63 | count = count+1; 64 | 65 | } 66 | 67 | area=(total/count)*16;/* area is part of the square which is 4x4 or 16 sq units */ 68 | printf("total is %f count is %f\n",total,count); 69 | 70 | 71 | xcofm=xcofm/total; 72 | ycofm=ycofm/total; 73 | 74 | 75 | printf("area is %lf\n",area); 76 | printf("cofm is %lf,%lf",xcofm,ycofm); 77 | 78 | 79 | /* Plot the data */ 80 | 81 | 82 | if(outcount >= 2700) 83 | 84 | outcount = 2700; 85 | 86 | 87 | for(I = 1; I<=outcount-1;I++) 88 | fprintf(fptr,"%lf %lf\n",xout[I],yout[I]); 89 | fclose(fptr); 90 | 91 | } 92 | 93 | double randfunc() 94 | { 95 | /* get a random number 0 to 1 */ 96 | double ans; 97 | 98 | 99 | ans=rand()%1000; 100 | ans=ans/1000; 101 | 102 | return ans; 103 | } 104 | 105 | 106 | 107 | 108 | -------------------------------------------------------------------------------- /ch10sources/cofmc.c: -------------------------------------------------------------------------------- 1 | /* cofm5c.c 2 | Centre of Mass Calculation. 3 | Calculates c of m for 4 | circle centre = (0,0) radius = 2 5 | */ 6 | #define _CRT_SECURE_NO_WARNINGS 7 | #include 8 | #include 9 | #include 10 | #include 11 | double randfunc(); 12 | main() 13 | { 14 | 15 | int I,outcount; 16 | float area,total,count; 17 | FILE *fptr; 18 | time_t t; 19 | /* Local Arrays */ 20 | double x, y,xout[3500],yout[3500],xcofm,ycofm; 21 | 22 | fptr=fopen("cofmc.dat","w"); 23 | 24 | 25 | 26 | /* Intializes random number generator */ 27 | srand((unsigned) time(&t)); 28 | 29 | /* clears arrays to zero */ 30 | for( I = 0; I<3500;I++) 31 | { 32 | xout[I] = 0.0; 33 | yout[I] = 0.0; 34 | 35 | } 36 | /* set x and y cofm accumulators to zero */ 37 | xcofm=0.0; 38 | ycofm=0.0; 39 | 40 | 41 | total = 0.0; 42 | count = 0.0; 43 | outcount = 0; 44 | for( I = 1;I<= 3500;I++) 45 | { 46 | /* get x values between -2 and +2 */ 47 | /* get y values between -2 and +2 */ 48 | x = randfunc()*4.0-2.0; 49 | y = randfunc()*4.0-2.0; 50 | 51 | /* If the generated x and y values are above */ 52 | /* the curve y=x^2 then add 1 to count */ 53 | /* and update the x and y cofm values */ 54 | 55 | if(y>-sqrt(4-pow(x,2)) && y= 2700) 82 | outcount = 2700; 83 | 84 | 85 | for(I = 1; I<=outcount-1;I++) 86 | fprintf(fptr,"%lf %lf\n",xout[I],yout[I]); 87 | fclose(fptr); 88 | 89 | } 90 | 91 | double randfunc() 92 | { 93 | /* get a random number 0 to 1 */ 94 | double ans; 95 | 96 | 97 | ans=rand()%1000; 98 | ans=ans/1000; 99 | 100 | return ans; 101 | 102 | } 103 | 104 | 105 | 106 | 107 | -------------------------------------------------------------------------------- /ch11sources/browntest3.c: -------------------------------------------------------------------------------- 1 | /* Brownian motion (2D) Simulation (Monte Carlo)*/ 2 | /* */ 3 | #define _CRT_SECURE_NO_WARNINGS 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | 10 | #define PI 3.141592654 11 | 12 | 13 | main() 14 | { 15 | FILE *fptr; 16 | time_t t; 17 | 18 | 19 | int i; 20 | int collisions; 21 | double anglerand; 22 | double xvals[5950],yvals[5950]; 23 | double cosval,sinval; 24 | 25 | fptr=fopen("browntest3.dat","w"); 26 | srand((unsigned) time(&t));/* set the random number seed */ 27 | 28 | collisions=1000; 29 | xvals[0]=00.0; 30 | yvals[0]=00.0; 31 | 32 | for(i=0;i<1000;i++) 33 | { 34 | 35 | anglerand=rand()%1000; 36 | anglerand=anglerand/1000; 37 | 38 | /* Get a random angle between 0 and PI radians */ 39 | 40 | anglerand=2*PI*anglerand; 41 | 42 | /* length of jump is 1 */ 43 | /* So the cos and sin of the angle */ 44 | /* will be the distance moved in */ 45 | /* that direction (+ or -) */ 46 | 47 | xvals[i+1]=xvals[i]+cos(anglerand); 48 | cosval = cos(anglerand); 49 | 50 | 51 | yvals[i+1]=yvals[i]+sin(anglerand); 52 | sinval = sin(anglerand); 53 | 54 | 55 | fprintf(fptr,"%lf %lf\n", xvals[i], yvals[i]); 56 | /*printf("cosval = %lf sinval = %lf\n",cosval,sinval);*/ 57 | 58 | 59 | } 60 | 61 | 62 | 63 | } 64 | -------------------------------------------------------------------------------- /ch12sources/vaca.c: -------------------------------------------------------------------------------- 1 | /* PROGRAM vaca.c 2 | C VACANCY DIFFUSION MODEL. 3 | C TRACES ALL ATOMS (2D VERSION) 4 | C NB - FOR MORE THAN 1 ATOM IN LATTICE, THE TRACE ATOM 5 | C MAY MOVE MORE THAN ONCE IN ONE MONTE CARLO CYCLE.*/ 6 | #include 7 | #include 8 | #include 9 | int IRND(); 10 | int IFOURRND(); 11 | main() 12 | { 13 | int test; 14 | float FT,XMSD,TEMP,MOVES; 15 | int N,I,N1,N2,ATOM,FILLEDSITES; 16 | int FOURRNDTEST[4],N1N,N2N,J,K,L,MCC,M; 17 | int LATTICE2[20][20]; 18 | int FIRSTTIME,MCCMAX; 19 | int N1R,N2R,IX,IY,Q,P,NOTFOUND,INC,IXY; 20 | FILE *fptr; 21 | 22 | time_t t; 23 | 24 | /* Intializes random number generator */ 25 | srand((unsigned) time(&t)); 26 | fptr=fopen("vaca.dat","w"); 27 | 28 | /*MCCMAX = 500;*/ 29 | /*MCCMAX = 100000;*/ 30 | /*MCCMAX = 10000; this value fills the whole lattice */ 31 | /*MCCMAX = 50; this value demonstrates partially filled lattice */ 32 | MCCMAX = 50;/* this demonstrates msd */ 33 | MCCMAX = 1000; 34 | MCCMAX = 50; 35 | MCCMAX = 1000; 36 | MCCMAX = 10000; 37 | MOVES = 0.0; 38 | MCCMAX = 50;/* test 13/2/20 */ 39 | XMSD = 0.0; 40 | ATOM = 0; 41 | 42 | 43 | for(P=0;P<20;P++) 44 | { 45 | for(Q=0;Q<20;Q++) 46 | { 47 | /*C FILL THE ARRAY*/ 48 | 49 | LATTICE2[P][Q] = 0; 50 | 51 | } 52 | } 53 | FILLEDSITES = 399; 54 | 55 | 56 | /*C SELECT ANY SITE AS THE INITIAL VACANCY SITE*/ 57 | /*N1N = IRND(); 58 | N2N = IRND();*/ 59 | N1N=1;/* Start x value */ 60 | N2N=10;/* Start y value */ 61 | /*N1N=1; 62 | N2N=1;*/ 63 | 64 | LATTICE2[N1N][N2N] = 1; 65 | 66 | 67 | 68 | 69 | 70 | for(MCC=1;MCC<=MCCMAX;MCC++) 71 | { 72 | 73 | 74 | 75 | 76 | N1=N1N; 77 | N2=N2N; 78 | 79 | if(LATTICE2[N1][N2] == 1) 80 | { 81 | /*C VACANCY SITE (= 1 RANDOM NUMBER SELECTION) 82 | C */ 83 | 84 | INC = IFOURRND(); 85 | 86 | N1R = 0; 87 | N2R = 0; 88 | FOURRNDTEST[INC] = FOURRNDTEST[INC] + 1; 89 | /* test mechanism- instead of going from 19 to 1 etc you bounce off the boundary. Go from 19 to 18 etc */ 90 | if(INC == 1)/* UP */ 91 | { 92 | if(N1 == 19) 93 | N1N = 18; 94 | else 95 | N1N = N1+1; 96 | 97 | N1R = -1; 98 | }else if(INC == 2)/* RIGHT */ 99 | { 100 | if(N2 == 19) 101 | N2N = 18; 102 | else 103 | N2N = N2+1; 104 | 105 | N2R = -1; 106 | }else if(INC == 3)/* DOWN */ 107 | { 108 | if(N1 == 1) 109 | N1N = 2; 110 | else 111 | N1N = N1-1; 112 | 113 | N1R = 1; 114 | }else if(INC == 4)/* LEFT */ 115 | { 116 | if(N2 == 1) 117 | N2N = 2; 118 | else 119 | N2N = N2-1; 120 | 121 | N2R = 1; 122 | } 123 | printf("INC %d N1N %d N2N %d N1R %d N2R %d\n",INC,N1N,N2N,N1R,N2R); 124 | 125 | test=0; 126 | 127 | 128 | if(LATTICE2[N1N][N2N] == 0) 129 | { 130 | LATTICE2[N1N][N2N] = 1; 131 | 132 | 133 | } 134 | else 135 | printf("not found\n"); 136 | } 137 | 138 | 139 | } 140 | 141 | for(P=0;P<20;P++) 142 | { 143 | for(Q=0;Q<20;Q++) 144 | { 145 | 146 | 147 | 148 | if(LATTICE2[P][Q] == 1) 149 | fprintf(fptr," %d\t%d\n",P,Q); 150 | 151 | 152 | 153 | 154 | } 155 | } 156 | 157 | fclose(fptr); 158 | 159 | } 160 | 161 | 162 | int IRND() 163 | { 164 | float TOT,DIV,X; 165 | int ANS,I; 166 | 167 | TOT=rand()%1000; 168 | TOT=TOT/1000; 169 | DIV = 20.0; 170 | X = 1.0; 171 | for(I=0;I<20;I++) 172 | if(TOT < X/DIV) 173 | ANS = I; 174 | 175 | else 176 | X = X+1.0; 177 | 178 | 179 | return ANS; 180 | } 181 | 182 | 183 | 184 | int IFOURRND() 185 | { 186 | float TOT; 187 | int ANS,I; 188 | 189 | 190 | TOT=rand()%1000; 191 | TOT=TOT/1000; 192 | 193 | 194 | if(TOT < 0.25) 195 | ANS = 1; 196 | else if(TOT < 0.5) 197 | ANS = 2; 198 | else if(TOT < 0.75) 199 | ANS = 3; 200 | else 201 | ANS = 4; 202 | 203 | return ANS; 204 | 205 | } 206 | 207 | 208 | 209 | 210 | -------------------------------------------------------------------------------- /ch13sources/chain1.c: -------------------------------------------------------------------------------- 1 | /* chain1.c 2 | Chain Reaction Simulation.Cubic Shape 3 | Predefined values of a & b (varied values 4 | of a & b) */ 5 | #define _CRT_SECURE_NO_WARNINGS 6 | #include 7 | #include 8 | #include 9 | #include 10 | double randfunc(int N); 11 | int checkin(double x, double y, double z, double a, double b); 12 | 13 | 14 | main() 15 | { 16 | FILE *fptr; 17 | FILE *fptr2; 18 | 19 | 20 | /* Local variables */ 21 | 22 | int K, P, N, Ninp, Q; 23 | 24 | /* x0, y0, z0 is the position of the fission nucleus */ 25 | /* x1, y1, z1,phi1,d1,costheta1 are positions of first neutron */ 26 | /* x2, y2, z2,phi2,d2,costheta2 are positions of second neutron */ 27 | double f, x0, y0, z0, phi1, phi2, d1, d2, costheta1, costheta2; 28 | 29 | double a, b, x1, y1, z1, x2, y2, z2; 30 | double pi; 31 | time_t t; 32 | 33 | pi = 3.142; 34 | P = 0; 35 | 36 | /* Select output file for error messages */ 37 | fptr = fopen("chain1.err", "w"); 38 | 39 | /* initialise random number generator */ 40 | srand((unsigned)time(&t)); 41 | 42 | /* Ask the user for the number of fissions */ 43 | printf("Enter number of fissions \n"); 44 | scanf("%d", &N); 45 | 46 | 47 | /* Create results file */ 48 | fptr2 = fopen("chain1.dat", "w"); 49 | if (fptr2 == NULL) 50 | { 51 | fprintf(stderr, "Error writing to %s\n", "chain1.dat"); 52 | return(1); 53 | } 54 | 55 | 56 | /* Start values for dimensions of box */ 57 | a = 2.0; 58 | b = 0.1; 59 | 60 | for (Q = 1;Q <= 20;Q++) 61 | { 62 | Ninp = 0; 63 | for (K = 1;K <= N;K++) 64 | { 65 | /* find a random position within the box */ 66 | /* for the nucleus */ 67 | x0 = a * (randfunc(P) - 0.5); 68 | y0 = a * (randfunc(P) - 0.5); 69 | z0 = b * (randfunc(P) - 0.5); 70 | phi1 = 2 * pi*randfunc(P); 71 | costheta1 = 2 * (randfunc(P) - 0.5); 72 | phi2 = 2 * pi*randfunc(P); 73 | costheta2 = 2 * (randfunc(P) - 0.5); 74 | d1 = randfunc(P); 75 | d2 = randfunc(P); 76 | 77 | /* calculate the position of first neutron */ 78 | x1 = x0 + d1 * sin(acos(costheta1))*cos(phi1); 79 | y1 = y0 + d1 * sin(acos(costheta1))*sin(phi1); 80 | z1 = z0 + d1 * costheta1; 81 | 82 | /* calculate the position of second neutron */ 83 | x2 = x0 + d2 * sin(acos(costheta2))*cos(phi2); 84 | y2 = y0 + d2 * sin(acos(costheta2))*sin(phi2); 85 | z2 = z0 + d2 * costheta2; 86 | 87 | /* Find out if first neutron is inside the box */ 88 | if (checkin(x1, y1, z1, a, b) == 1) 89 | Ninp = Ninp + 1; 90 | 91 | /* Find out if second neutron is inside the box */ 92 | if (checkin(x2, y2, z2, a, b) == 1) 93 | Ninp = Ninp + 1; 94 | 95 | 96 | 97 | } 98 | 99 | /*printf("\na is : %d", a); 100 | printf("\nb is : %d", b);*/ 101 | 102 | f = (float)Ninp / (float)N; 103 | 104 | fprintf(fptr2, "%lf %lf\n", b / a, f); 105 | 106 | /* make a smaller and b larger */ 107 | /* We will show that a cube (a=b) */ 108 | /* produces the best survival fraction */ 109 | a = a - 0.1; 110 | b = b + 0.1; 111 | 112 | } 113 | fclose(fptr2); 114 | } 115 | double randfunc(int N) 116 | { 117 | /* find a random number between 0 and 1 */ 118 | double TOT; 119 | 120 | TOT = rand() % 1000; 121 | TOT = TOT / 1000; 122 | 123 | return TOT; 124 | 125 | } 126 | 127 | 128 | int checkin(double x, double y, double z, double a, double b) 129 | { 130 | 131 | /* If the coordinates are within the box return 1 */ 132 | /* Otherwise return 0 */ 133 | int I; 134 | if (x > -a / 2 && x < a / 2 135 | && y > -a / 2 && y < a / 2 136 | && z>-b / 2 && z < b / 2) 137 | I = 1; 138 | else 139 | I = 0; 140 | 141 | return I; 142 | 143 | } 144 | -------------------------------------------------------------------------------- /ch1sources/ch1arith.c: -------------------------------------------------------------------------------- 1 | /* ch1arith.c */ 2 | /* Read, display and arithmetics */ 3 | /* Input data from the command line */ 4 | /* and read it into the program. */ 5 | /* Also write the data back to the */ 6 | /* command line. Basic arithmetic */ 7 | /* done on input data. */ 8 | #define _CRT_SECURE_NO_WARNINGS 9 | 10 | #include 11 | int main () 12 | { 13 | char c; /* store for character entered */ 14 | int this_is_a_number1, this_is_a_number2, total; /* store for two integers entered */ 15 | float float_number1, float_number2,float_total; /* store for two decimals entered */ 16 | 17 | 18 | /* read and display a character */ 19 | 20 | printf("Enter character: "); /* tell the user to enter a character */ 21 | c = getchar(); /* read the character in */ 22 | 23 | printf("Character entered: ");/* tell the user what was entered */ 24 | putchar(c); /* write the character */ 25 | 26 | 27 | 28 | 29 | /* Read in two integers , add them and display the answer */ 30 | 31 | 32 | printf("\nPlease enter an integer number:\n "); 33 | scanf("%d", &this_is_a_number1); /* read number into this_is_a_number1 */ 34 | printf("You entered %d\n", this_is_a_number1); 35 | 36 | printf("Please enter another integer number: \n"); 37 | scanf("%d", &this_is_a_number2); /* read number into this_is_a_number2 */ 38 | printf("You entered %d\n", this_is_a_number2); 39 | 40 | total = this_is_a_number1 + this_is_a_number2;/* add two numbers */ 41 | printf("sum of your two integer numbers is %d\n", total); /* write result to command line */ 42 | 43 | 44 | 45 | 46 | /* Add two floating point numbers */ 47 | 48 | 49 | printf("Please enter a decimal number:\n "); 50 | scanf("%f", &float_number1); /* read decimal number into float_number1 */ 51 | printf("You entered %f\n", float_number1); 52 | 53 | printf("Please enter another decimal number: \n"); 54 | scanf("%f", & float_number2); /* read decimal number into float_number2 */ 55 | printf("You entered %f\n", float_number2); 56 | 57 | float_total = float_number1+float_number2;/* add the numbers */ 58 | printf("sum of your two decimal numbers is %f\n", float_total);/* write result to command line */ 59 | 60 | 61 | 62 | 63 | 64 | /* multiply two floating point numbers */ 65 | 66 | 67 | 68 | 69 | float_total = float_number1 * float_number2;/* multiply the numbers */ 70 | printf("product of your two decimal numbers is %f\n", float_total);/* write result to command line */ 71 | 72 | 73 | 74 | /* divide two floating point numbers */ 75 | 76 | 77 | float_total = float_number1 / float_number2;/* divide the numbers */ 78 | printf("quotient of your two decimal numbers is %f\n", float_total);/* write result to command line */ 79 | 80 | return 0; 81 | } 82 | 83 | -------------------------------------------------------------------------------- /ch1sources/ch1arr.c: -------------------------------------------------------------------------------- 1 | /* ch1arr.c */ 2 | /* Array use and nested forloops */ 3 | 4 | #define _CRT_SECURE_NO_WARNINGS 5 | 6 | #include 7 | 8 | /* program to show array use */ 9 | 10 | int main() 11 | 12 | { 13 | int arr1[8];/* define an array of 8 integers */ 14 | 15 | int arr2[7][8];/* 2D array of integers 7 rows and 8 columns*/ 16 | 17 | 18 | int i, j, k, l; 19 | 20 | 21 | 22 | printf("enter 8 integer numbers\n"); 23 | 24 | for (i = 0;i < 8;i++) 25 | { 26 | scanf("%d", &arr1[i]);/* read into arr1[i] */ 27 | } 28 | printf("Your 8 numbers are \n"); 29 | 30 | for (i = 0;i < 8;i++) 31 | { 32 | printf("%d ", arr1[i]);/* write contents of arr1 to command line */ 33 | } 34 | printf("\n"); 35 | 36 | 37 | printf("enter number of rows and columns (max 7 rows max 8 columns) \n"); 38 | scanf("%d %d", &k, &l); 39 | if (k > 7 || l > 8) 40 | { 41 | /* User tried to enter more than 7 rows or 8 columns */ 42 | printf("error - max of 8 for rows or columns\n"); 43 | 44 | } 45 | 46 | else 47 | { 48 | printf("enter array\n"); 49 | /* read i rows and j columns using nested forloop */ 50 | for (i = 0;i < k;i++) 51 | { 52 | for (j = 0;j < l;j++) 53 | { 54 | scanf("%d", &arr2[i][j]); 55 | } 56 | } 57 | printf("Your array is \n"); 58 | /* print entered 2D array using nested forloop */ 59 | for (i = 0;i < k;i++) 60 | { 61 | for (j = 0;j < l;j++) 62 | { 63 | printf("%d ", arr2[i][j]); 64 | } 65 | printf("\n"); 66 | 67 | } 68 | } 69 | 70 | 71 | 72 | 73 | 74 | } 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | -------------------------------------------------------------------------------- /ch1sources/ch1filecreate.c: -------------------------------------------------------------------------------- 1 | /* ch1filecreate.c */ 2 | /* filecreate */ 3 | /* Creates a file freom data preset */ 4 | /* into the 2D array of the program. */ 5 | /* Then prints the data to the user */ 6 | #define _CRT_SECURE_NO_WARNINGS 7 | #include 8 | #include 9 | int main() 10 | { 11 | struct record 12 | { 13 | double matrix[3][5]; 14 | }; 15 | int /*counter,*/ i, j; 16 | FILE *ptr; 17 | struct record data_record; 18 | size_t r1; 19 | 20 | double inmat[3][5] = { 21 | {2.6,3.1,7.4,0.6,9.3}, 22 | {4.9,9.3,0.6,7.4,3.1}, 23 | {8.3,8.8,5.2,2.7,0.8} 24 | 25 | }; 26 | /* Copy preset array to output array */ 27 | for (i = 0;i < 3;i++) 28 | { 29 | for (j = 0;j < 5;j++) 30 | { 31 | data_record.matrix[i][j] = inmat[i][j]; 32 | } 33 | } 34 | /* Open output file (write/binary) */ 35 | ptr = fopen("testaug.bin", "wb"); 36 | if (!ptr) 37 | { 38 | /* Error when trying to open file */ 39 | printf("Can not open file"); 40 | return 1; /* quit the program */ 41 | } 42 | /* Write output matrix to output file */ 43 | r1 = fwrite(data_record.matrix, sizeof(data_record.matrix), 1, ptr); 44 | printf("wrote %d elements \n", r1); 45 | printf("size of data_record.matrix is %d \n", sizeof(data_record. 46 | matrix)); 47 | /* Print matrix written to file */ 48 | for (i = 0;i < 3;i++) 49 | { 50 | for (j = 0;j < 5;j++) 51 | { 52 | data_record.matrix[i][j] = inmat[i][j]; 53 | printf("data_record.matrix[%d][%d] = %lf \n", i, j, 54 | data_record.matrix[i][j]); 55 | } 56 | } 57 | 58 | fclose(ptr); /* Close the file */ 59 | return 0; 60 | } 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | -------------------------------------------------------------------------------- /ch1sources/ch1filecreate2.c: -------------------------------------------------------------------------------- 1 | /* ch1filecreate2.c */ 2 | /* filecreate2 */ 3 | /* reads from file */ 4 | /* prints out the records sequentially */ 5 | /* Finds specific records and prints them */ 6 | #define _CRT_SECURE_NO_WARNINGS 7 | #include 8 | 9 | /* structure containing information */ 10 | /* for each record in the file. */ 11 | /* Each record contains data for one patient. */ 12 | /* The data is the patients ID,name and blood pressure */ 13 | struct Patient { 14 | int PatientID; 15 | char name[13]; 16 | int BloodPressure; 17 | }; 18 | int main() 19 | { 20 | int i, numread; 21 | FILE *fp; 22 | struct Patient s1; 23 | struct Patient s2; 24 | /* Preset the data for each patient */ 25 | struct Patient s10 = { 10,"Brown ",50 }; 26 | struct Patient s11 = { 11,"Jones ",51 }; 27 | struct Patient s12 = { 12,"White ",52 }; 28 | struct Patient s13 = { 13,"Green ",53 }; 29 | struct Patient s14 = { 14,"Smith ",54 }; 30 | struct Patient s15 = { 15,"Black ",55 }; 31 | struct Patient s16 = { 16,"Allen ",56 }; 32 | struct Patient s17 = { 17,"Stone ",57 }; 33 | struct Patient s18 = { 18,"Evans ",58 }; 34 | struct Patient s19 = { 19,"Royle ",59 }; 35 | struct Patient s20 = { 20,"Stone ",60 }; 36 | struct Patient s21 = { 21,"Weeks ",61 }; 37 | struct Patient s22 = { 22,"Owens ",62 }; 38 | struct Patient s23 = { 23,"Power ",63 }; 39 | struct Patient s24 = { 24,"Bloom ",63 }; 40 | struct Patient s28 = { 28,"Haver ",68 }; 41 | struct Patient s29 = { 29,"James ",69 }; 42 | 43 | /* Open the Patients file */ 44 | fp = fopen("patients.bin", "w"); 45 | 46 | /* Write details of each patient to file*/ 47 | /* From the structures defined above */ 48 | fwrite(&s10, sizeof(s1), 1, fp); 49 | fwrite(&s11, sizeof(s1), 1, fp); 50 | fwrite(&s12, sizeof(s1), 1, fp); 51 | fwrite(&s13, sizeof(s1), 1, fp); 52 | fwrite(&s14, sizeof(s1), 1, fp); 53 | fwrite(&s15, sizeof(s1), 1, fp); 54 | fwrite(&s16, sizeof(s1), 1, fp); 55 | fwrite(&s17, sizeof(s1), 1, fp); 56 | fwrite(&s18, sizeof(s1), 1, fp); 57 | fwrite(&s19, sizeof(s1), 1, fp); 58 | fwrite(&s20, sizeof(s1), 1, fp); 59 | fwrite(&s21, sizeof(s1), 1, fp); 60 | fwrite(&s22, sizeof(s1), 1, fp); 61 | fwrite(&s23, sizeof(s1), 1, fp); 62 | fwrite(&s24, sizeof(s1), 1, fp); 63 | fwrite(&s28, sizeof(s1), 1, fp); 64 | fwrite(&s29, sizeof(s1), 1, fp); 65 | 66 | /* Close the file */ 67 | fclose(fp); 68 | 69 | /* Reopen the file */ 70 | fopen("patients.bin", "r"); 71 | /* Read and print out all of the records on the file */ 72 | for (i = 0;i < 17;i++) 73 | { 74 | numread = fread(&s2, sizeof(s2), 1, fp);/* read into numread */ 75 | 76 | if (numread == 1) 77 | { 78 | /* print data for one patient */ 79 | printf("\nPatientID : %d", s2.PatientID); 80 | printf("\nName : %s", s2.name); 81 | printf("\nBloodPressure : %d", s2.BloodPressure); 82 | } 83 | else { 84 | /* If an error occurred on read then print out message */ 85 | if (feof(fp)) 86 | printf("Error reading patients.bin : unexpected end of file fp is %p\n", fp); 87 | 88 | else if (ferror(fp)) 89 | { 90 | perror("Error reading patients.bin"); 91 | } 92 | } 93 | } 94 | /* Close the file */ 95 | fclose(fp); 96 | } 97 | 98 | 99 | 100 | 101 | 102 | 103 | 104 | 105 | 106 | 107 | 108 | 109 | 110 | 111 | 112 | 113 | 114 | 115 | 116 | 117 | 118 | 119 | 120 | 121 | 122 | -------------------------------------------------------------------------------- /ch1sources/ch1fileread.c: -------------------------------------------------------------------------------- 1 | /* ch1fileread.c */ 2 | /* fileread */ 3 | /* read the data from a file and write it to command line */ 4 | #define _CRT_SECURE_NO_WARNINGS 5 | #include 6 | #include 7 | struct record 8 | { 9 | double matrix[3][5]; 10 | }; 11 | int main() 12 | { 13 | int counter, i; 14 | FILE *ptr; 15 | struct record data_record; 16 | size_t r1; 17 | /* Open input file (read/binary) */ 18 | ptr = fopen("testaug.bin", "rb"); 19 | if (!ptr) 20 | { 21 | /* error trying to open the file */ 22 | printf("Can not open file"); 23 | return 1; 24 | } 25 | /* Read input matrix from input file */ 26 | r1 = fread(data_record.matrix, sizeof(data_record.matrix), 1, ptr); 27 | printf("read %d elements \n", r1); 28 | printf("size of struct record is %d \n", sizeof(struct record)); 29 | /* Print matrix read from file */ 30 | /* using nested forloop */ 31 | for (counter = 0; counter < 3; counter++) 32 | { 33 | for (i = 0; i < 5; i++) 34 | { 35 | printf("matrix[%d][%d] = %lf \n", counter, i, data_record.matrix[counter][i]); 36 | 37 | } 38 | } 39 | fclose(ptr); /* close the file */ 40 | return 0; 41 | } 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | -------------------------------------------------------------------------------- /ch1sources/ch1fileread2.c: -------------------------------------------------------------------------------- 1 | /* ch1fileread2.c */ 2 | /* fileread2 */ 3 | /* reads from file */ 4 | /* reads and prints sequentially */ 5 | /* reads and prints specific records */ 6 | #define _CRT_SECURE_NO_WARNINGS 7 | #include 8 | struct Patient { 9 | int PatientID; 10 | char name[13]; 11 | int BloodPressure; 12 | }; 13 | int main() 14 | { 15 | FILE *fp; 16 | struct Patient s2; 17 | int numread, i; 18 | /* Open patients file */ 19 | fp = fopen("patients.bin", "r"); 20 | for (i = 0;i < 17;i++) 21 | { 22 | /* Read each patient data from file sequentially */ 23 | fread(&s2, sizeof(s2), 1, fp); 24 | /* Print patient ID, name and Blood Pressure for each patient */ 25 | printf("\nPatientID : %d", s2.PatientID); 26 | printf("\n Name : %s", s2.name); 27 | printf("\nBloodPressure : %d", s2.BloodPressure); 28 | } 29 | fclose(fp); 30 | /* Re-open the patients file */ 31 | fp = fopen("patients.bin", "r"); 32 | for (i = 0;i < 17;i++) 33 | { 34 | /* Search the file for patient with ID of 23 */ 35 | fread(&s2, sizeof(s2), 1, fp); 36 | if (s2.PatientID == 23) 37 | { 38 | /* Found the patient. Print their name */ 39 | printf("\nName : %s", s2.name); 40 | break; 41 | } 42 | } 43 | /* Go back to the beginning of the file */ 44 | fseek(fp, sizeof(s2), SEEK_END); 45 | rewind(fp); 46 | /* Find all patients with Blood Pressure reading above 63 */ 47 | for (i = 0;i < 17;i++) 48 | { 49 | fread(&s2, sizeof(s2), 1, fp); 50 | if (s2.BloodPressure > 63) 51 | { 52 | /* Print out name of each patient with Blood pressure above 63 */ 53 | 54 | printf("\nName : %s", s2.name); 55 | } 56 | } 57 | /* Go back to the beginning of the file */ 58 | rewind(fp); 59 | /* Read and print out the first 3 patients in the file */ 60 | numread = fread(&s2, sizeof(s2), 1, fp); 61 | if (numread == 1) 62 | { 63 | printf("\nPatientID : %d", s2.PatientID); 64 | printf("\nName : %s", s2.name); 65 | printf("\nBloodPressure : %d", s2.BloodPressure); 66 | } 67 | numread = fread(&s2, sizeof(s2), 1, fp); 68 | if (numread == 1) 69 | { 70 | printf("\nPatientID : %d", s2.PatientID); 71 | printf("\nName : %s", s2.name); 72 | printf("\nBloodPressure : %d", s2.BloodPressure); 73 | } 74 | numread = fread(&s2, sizeof(s2), 1, fp); 75 | if (numread == 1) 76 | { 77 | printf("\nPatientID : %d", s2.PatientID); 78 | printf("\nName : %s", s2.name); 79 | printf("\nBloodPressure : %d", s2.BloodPressure) ; 80 | } 81 | /* Close the file */ 82 | fclose(fp); 83 | } 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | 100 | 101 | 102 | 103 | 104 | 105 | 106 | 107 | 108 | 109 | 110 | 111 | 112 | -------------------------------------------------------------------------------- /ch1sources/ch1func.c: -------------------------------------------------------------------------------- 1 | /* ch1func.c */ 2 | /* demonstrate function */ 3 | #define _CRT_SECURE_NO_WARNINGS 4 | 5 | #include 6 | 7 | /* This code demonstrates what a function does */ 8 | /* The function here compares two numbers and says which is bigger */ 9 | /* The user enters three numbers and gets told which is bigger than which !*/ 10 | 11 | void myfunction(int a,int b); /* decaration of your function and its parameters */ 12 | 13 | int first , second, third; /* data fields to hold entered data */ 14 | main() 15 | { 16 | printf( "Please enter first integer number: " ); 17 | scanf( "%d", &first ); 18 | printf( "Please enter second integer number: " ); 19 | scanf( "%d", &second ); 20 | printf( "Please enter third integer number: " ); 21 | scanf( "%d", &third ); 22 | 23 | /* call "myfunction" with different parameters for each call */ 24 | myfunction(first , second); 25 | myfunction(first , third); 26 | myfunction(second , third); 27 | } 28 | void myfunction(int a,int b) 29 | /* the function is outside the main{} part of the program */ 30 | /* The function just compares the two parameters, a and b, and says which is greater*/ 31 | { 32 | 33 | if(a>b) 34 | printf("%d is greater than %d\n", a,b); 35 | else if (a 6 | 7 | 8 | /* Function which returns an answer */ 9 | /* finds the pupil in one year of the school with the highest marks */ 10 | 11 | #include 12 | double getmarks(double pupils[]); 13 | 14 | int main() 15 | { 16 | double pupil; 17 | /* Array with marks for class is preset in the main part of the program */ 18 | double marks[] = { 10.6, 23.7, 67.9, 93.0, 64.2, 33.8 ,57.5 ,82.2 ,50.7 ,45.7 }; 19 | /* Call function getmarks. The function returns the max marks which is then stored in pupil */ 20 | pupil = getmarks(marks); 21 | printf("Max mark is = %f", pupil); 22 | return 0; 23 | } 24 | 25 | double getmarks(double pupils[]) 26 | { 27 | int i; 28 | double highest; 29 | highest = 0; 30 | /* Go through all the pupils in turn and store the highest mark */ 31 | for (i = 0; i < 6; ++i) 32 | { 33 | if (highest < pupils[i]) 34 | highest = pupils[i]; 35 | 36 | } 37 | return highest; /* returns the value in highest to where the function was called */ 38 | } 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | -------------------------------------------------------------------------------- /ch1sources/ch1math.c: -------------------------------------------------------------------------------- 1 | /* ch1math.c */ 2 | /* demonstrate mathematics functions */ 3 | #define _CRT_SECURE_NO_WARNINGS 4 | #include 5 | 6 | #include 7 | /* Illustration of the common trigonometric functions */ 8 | /* also exponent, natural log, log to base 10 */ 9 | /* power, square root and find absolute value */ 10 | 11 | int main() 12 | { 13 | #define PI 3.14159265 14 | double angle, radianno, answer; 15 | 16 | double arccos, arcsin, arctan; 17 | double expno, natlog, lb10; 18 | double pownum, power, sqroot, fabsno; 19 | 20 | /* The cosine function */ 21 | printf("cosine function:\n "); 22 | printf("Please enter angle in degrees:\n "); 23 | scanf("%lf", &angle); 24 | printf("You entered %lf\n", angle); 25 | radianno = angle * (2 * PI / 360); 26 | answer = cos(radianno);/* returns cos value to answer */ 27 | printf("cos of %lf is %lf\n", angle, answer); 28 | 29 | /* The sine function */ 30 | printf("sine function:\n "); 31 | printf("Please enter angle in degrees:\n "); 32 | scanf("%lf", &angle); 33 | printf("You entered %lf\n", angle); 34 | radianno = angle * (2 * PI / 360); 35 | answer = sin(radianno);/* returns sin value to answer */ 36 | printf("sin of %lf is %lf\n", angle, answer); 37 | 38 | /* The tangent function */ 39 | printf("tangent function:\n "); 40 | printf("Please enter angle in degrees:\n "); 41 | scanf("%lf", &angle); 42 | printf("You entered %lf\n", angle); 43 | radianno = angle * (2 * PI / 360); 44 | answer = tan(radianno);/* returns tan value to answer */ 45 | printf("tan of %lf is %lf\n", angle, answer); 46 | 47 | /* The arccos function */ 48 | printf("arccos function:\n "); 49 | printf("Please enter arccos:\n "); 50 | scanf("%lf", &arccos); 51 | printf("You entered %lf\n", arccos); 52 | radianno = acos(arccos);/* returns arccos value to radianno (in radians) */ 53 | answer = radianno * (360 / (2 * PI)); 54 | printf("arccos of %lf in degrees is %lf\n", arccos, answer); 55 | 56 | /* The arcsin function */ 57 | printf("arcsin function:\n "); 58 | printf("Please enter arcsin:\n "); 59 | scanf("%lf", &arcsin); 60 | printf("You entered %lf\n", arcsin); 61 | radianno = asin(arcsin);/* returns arcsin value to radianno (in radians) */ 62 | 63 | answer = radianno * (360 / (2 * PI)); 64 | printf("arcsin of %lf in degrees is %lf\n", arcsin, answer); 65 | 66 | /* The arctan function */ 67 | printf("arctan function:\n "); 68 | printf("Please enter arctan:\n "); 69 | scanf("%lf", &arctan); 70 | printf("You entered %lf\n", arctan); 71 | radianno = atan(arctan);/* returns arctan value to radianno (in radians) */ 72 | answer = radianno * (360 / (2 * PI)); 73 | printf("arctan of %lf in degrees is %lf\n", arctan, answer); 74 | 75 | 76 | /* showing use of exp, log and log10 functions */ 77 | /* find exponent of entered number */ 78 | printf("exponental function:\n "); 79 | printf("Please enter number:\n "); 80 | scanf("%lf", &expno); 81 | printf("You entered %lf\n", expno); 82 | 83 | answer = exp(expno);/* returns exponent value to answer */ 84 | printf("exponent of %lf is %lf\n", expno, answer); 85 | 86 | /* find natural logarithm of entered number */ 87 | printf("natural logarithm function:\n "); 88 | printf("Please enter number:\n "); 89 | scanf("%lf", &natlog); 90 | printf("You entered %lf\n", natlog); 91 | answer = log(natlog);/* returns natural log value to answer */ 92 | printf("natural logarithm of %lf is %lf\n", natlog, answer); 93 | 94 | 95 | /* find log to base 10 of entered number */ 96 | printf("log to base 10 function:\n "); 97 | printf("Please enter number:\n "); 98 | scanf("%lf", &lb10); 99 | printf("You entered %lf\n", lb10); 100 | answer = log10(lb10);/* returns log to base 10 value to answer */ 101 | 102 | printf("log to base 10 of %lf is %lf\n", lb10, answer); 103 | 104 | 105 | /* showing use of pow, sqrt and fabs functions */ 106 | /* find x raised to power y number */ 107 | printf("power:\n "); 108 | printf("Please enter number:\n "); 109 | scanf("%lf", &pownum); 110 | printf("You entered %lf\n", pownum); 111 | printf("Please enter power:\n "); 112 | scanf("%lf", &power); 113 | printf("You entered %lf\n", power); 114 | 115 | answer = pow(pownum, power);/* returns power of pownum value to answer */ 116 | printf("%lf raised to power %lf is %lf\n", pownum, power, answer); 117 | 118 | /* find square root of number */ 119 | 120 | printf("square root:\n "); 121 | printf("Please enter number:\n "); 122 | scanf("%lf", &sqroot); 123 | printf("You entered %lf\n", sqroot); 124 | 125 | answer = sqrt(sqroot);/* returns square root of sqroot value to answer */ 126 | printf("The square root of %lf is %lf\n", sqroot, answer); 127 | 128 | /* find absolute value of number */ 129 | printf("absolute value:\n "); 130 | printf("Please enter number:\n "); 131 | scanf("%lf", &fabsno); 132 | printf("You entered %lf\n", fabsno); 133 | 134 | answer = fabs(fabsno);/* returns absolute value of fabsno to answer */ 135 | printf("The absolute value of %lf is %lf\n", fabsno, answer); 136 | 137 | 138 | 139 | 140 | 141 | 142 | return 0; 143 | 144 | } 145 | 146 | 147 | 148 | 149 | -------------------------------------------------------------------------------- /ch1sources/ch1strings.c: -------------------------------------------------------------------------------- 1 | /* ch1strings.c */ 2 | /* demonstrate strings */ 3 | 4 | #define _CRT_SECURE_NO_WARNINGS 5 | #include 6 | #include 7 | /* Program to demonstrate string operations strlen, strcpy, strcat, strcmp */ 8 | 9 | int main() { 10 | char select[7] = { 's', 'e', 'l', 'e', 'c', 't','\0' }; 11 | char string1[32] = "This is string1"; 12 | char string2[16] = "This is string2"; 13 | char string3[16]; 14 | int len; 15 | /* Print out the lengths of the strings */ 16 | /* strlen returns length of string */ 17 | len = strlen(string1); 18 | printf("strlen(string1) : %d\n", len); 19 | len = strlen(string2); 20 | printf("strlen(string2) : %d\n", len); 21 | len = strlen(string3); 22 | printf("strlen(string3) : %d\n", len); 23 | 24 | 25 | /* copy string1 into string3 */ 26 | /* strcpy copies string1 into string3 */ 27 | strcpy(string3, string1); 28 | printf("strcpy( string3, string1) : %s\n", string3); 29 | len = strlen(string3); 30 | printf("strlen(string3) after copy of string1 into string3 : %d\n", len); 31 | 32 | /* strcmp compares strings & returns 0 if they are equal */ 33 | /* Compare string1 and string3 (these should be the same)*/ 34 | if (strcmp(string1, string3) == 0) 35 | printf("strings are the same\n"); 36 | 37 | 38 | /* concatenates string1 and string2 */ 39 | strcat(string1, string2); 40 | printf("strcat( string1, string2): %s\n", string1); 41 | /* total length of string1 after concatenation */ 42 | len = strlen(string1); 43 | printf("strlen(string1) after cat of string2 onto string1 : %d\n", len); 44 | printf("String as predefined quoted chars: %s\n", select); 45 | 46 | 47 | return 0; 48 | } 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | -------------------------------------------------------------------------------- /ch1sources/ch1sw.c: -------------------------------------------------------------------------------- 1 | /* ch1sw.c */ 2 | /* demonstrate switches or characters or numbers */ 3 | #define _CRT_SECURE_NO_WARNINGS 4 | 5 | #include 6 | 7 | /* Example of a switch operation */ 8 | int main() 9 | { 10 | int this_is_a_number; /* store area to hold number entered */ 11 | char this_is_a_character;/* store area to hold character entered */ 12 | 13 | printf("\nPlease enter a character a,b,c,d or e:\n "); 14 | scanf("%c", &this_is_a_character);/* read into this_is_a_character */ 15 | 16 | /* switch to the specific "case" for the character entered */ 17 | /* then print which switch case was entered */ 18 | switch (this_is_a_character) 19 | { 20 | 21 | case 'a': 22 | printf("Case1: Value is: %c", this_is_a_character); 23 | break; 24 | case 'b': 25 | printf("Case2: Value is: %c", this_is_a_character); 26 | break; 27 | case 'c': 28 | printf("Case3: Value is: %c", this_is_a_character); 29 | break; 30 | case 'd': 31 | printf("Case4: Value is: %c", this_is_a_character); 32 | break; 33 | case 'e': 34 | printf("Case5: Value is: %c", this_is_a_character); 35 | break; 36 | default: 37 | /* The character entered was not between a,b,c,d or e */ 38 | printf("Error Value is: %c", this_is_a_character); 39 | } 40 | 41 | printf("Please enter an integer between 1 and 5:\n "); 42 | scanf("%d", &this_is_a_number); 43 | 44 | /* switch to the specific "case" for the number entered */ 45 | /* then print which switch case was entered */ 46 | switch (this_is_a_number) 47 | { 48 | 49 | case 1: 50 | printf("Case1: Value is: %d", this_is_a_number); 51 | break; 52 | case 2: 53 | printf("Case2: Value is: %d", this_is_a_number); 54 | break; 55 | case 3: 56 | printf("Case3: Value is: %d", this_is_a_number); 57 | break; 58 | case 4: 59 | printf("Case4: Value is: %d", this_is_a_number); 60 | break; 61 | case 5: 62 | printf("Case5: Value is: %d", this_is_a_number); 63 | break; 64 | default: 65 | /* The number entered was not between 1 and 5 */ 66 | printf("Error Value is: %d", this_is_a_number); 67 | } 68 | 69 | 70 | 71 | return 0; 72 | } 73 | 74 | 75 | 76 | 77 | 78 | 79 | -------------------------------------------------------------------------------- /ch2sources/capm.c: -------------------------------------------------------------------------------- 1 | /* capm.c */ 2 | /* CAPM */ 3 | /* user enters points.*/ 4 | /* regression of y on x calculated */ 5 | #define _CRT_SECURE_NO_WARNINGS 6 | #include 7 | #include 8 | main() 9 | { 10 | FILE *fp; 11 | 12 | float xpoints[12],ypoints[12]; 13 | float sigmax,sigmay,sigmaxy,sigmaxsquared,xbar,ybar; 14 | float fltcnt,sxy,sxx,b,a; 15 | int i,points; 16 | fp=fopen("capm.dat","w"); 17 | 18 | printf("enter number of points (max 12 ) \n"); 19 | scanf("%d", &points); 20 | if(points>12) 21 | { 22 | printf("error - max of 12 points\n"); 23 | 24 | } 25 | else 26 | { 27 | sigmax=0; 28 | sigmay=0; 29 | sigmaxy=0; 30 | sigmaxsquared=0; 31 | 32 | 33 | /* user enters points from scatter graph */ 34 | for(i=0;i 7 | #include 8 | main() 9 | { 10 | FILE *fp; 11 | 12 | /* store areas for points in the formulae */ 13 | float xpoints[12],ypoints[12]; 14 | float sigmax,sigmay,sigmaxy,sigmaxsquared,xbar,ybar; 15 | float fltcnt,sxy,sxx,b,a; 16 | 17 | int i,points; 18 | 19 | fp=fopen("regyonx.dat","w");/* create and open the file */ 20 | 21 | printf("enter number of points (max 12 ) \n"); 22 | scanf("%d", &points); 23 | if(points>12) 24 | { 25 | printf("error - max of 12 points\n"); /* user entered more than 12 */ 26 | 27 | } 28 | else 29 | { 30 | /* Preset store areas to zero */ 31 | sigmax=0; 32 | sigmay=0; 33 | sigmaxy=0; 34 | sigmaxsquared=0; 35 | 36 | 37 | /* user enters points */ 38 | for(i=0;i 4 | #include 5 | main() 6 | { 7 | double xpoints[10], ypoints[10]; 8 | double sigmax, sigmay, sigmaxsquared, sigmaysquared, xbar, ybar, sigmaxy; 9 | double sxy, sxx, syy, sx, sy, r; 10 | int i, points; 11 | double fltcnt; 12 | FILE *fp; 13 | fp=fopen("pmccf.dat","w"); 14 | /* User enters number of points in scatter graph */ 15 | printf("enter number of points (max 10 ) \n"); 16 | scanf("%d", &points); 17 | if (points > 10) 18 | { 19 | printf("error - max of 10 points\n"); 20 | 21 | } 22 | else 23 | { 24 | sigmax = 0; 25 | sigmay = 0; 26 | sigmaxy = 0; 27 | sigmaxsquared = 0; 28 | sigmaysquared = 0; 29 | 30 | /* User enters points in scatter graph */ 31 | for (i = 0;i < points;i++) 32 | { 33 | printf("enter point (x and y separated by space) \n"); 34 | scanf("%lf %lf", &xpoints[i], &ypoints[i]); 35 | /* totals incremented by x and y points */ 36 | sigmax = sigmax + xpoints[i]; 37 | sigmay = sigmay + ypoints[i]; 38 | sigmaxy = sigmaxy + xpoints[i] * ypoints[i]; 39 | sigmaxsquared = sigmaxsquared + pow(xpoints[i], 2); 40 | sigmaysquared = sigmaysquared + pow(ypoints[i], 2); 41 | } 42 | printf("points are \n"); 43 | for (i = 0;i < points;i++) 44 | { 45 | printf(" \n"); 46 | printf("%lf %lf", xpoints[i], ypoints[i]); 47 | fprintf(fp,"%lf\t%lf\n",xpoints[i], ypoints[i]); 48 | } 49 | printf(" \n"); 50 | fltcnt = points; 51 | /* variables in PMCC formula calculated */ 52 | xbar = sigmax / fltcnt; 53 | ybar = sigmay / fltcnt; 54 | 55 | syy = (1 / fltcnt)*sigmaysquared - ybar * ybar; 56 | 57 | sxx = (1 / fltcnt)*sigmaxsquared - xbar * xbar; 58 | sx = sqrt(sxx); 59 | sy = sqrt(syy); 60 | sxy = (1 / fltcnt)*sigmaxy - xbar * ybar; 61 | 62 | /* PMCC value calculated */ 63 | r = sxy / (sx*sy); 64 | printf("r is %lf", r); 65 | } 66 | 67 | fclose(fp); 68 | } 69 | 70 | 71 | 72 | -------------------------------------------------------------------------------- /ch3sources/regboth.c: -------------------------------------------------------------------------------- 1 | /* regboth.c */ 2 | /* regression */ 3 | /* user enters points.*/ 4 | /* regression of y on x and x on y calculated */ 5 | #define _CRT_SECURE_NO_WARNINGS 6 | #include 7 | #include 8 | main() 9 | { 10 | FILE *fp; 11 | 12 | float xpoints[16]={2,2,2,2,2,18,18,18,18,18,6,10,14,6,10,14}; 13 | float ypoints[16]={2,6,10,14,18,2,6,10,14,18,2,2,2,18,18,18}; 14 | float sigmax,sigmay,sigmaxy,sigmaxsquared,sigmaysquared,xbar,ybar; 15 | float fltcnt,sxy,sxx,syy,b,a,c,d, sx, sy, r; 16 | int i,points; 17 | fp=fopen("regbotht.dat","w"); 18 | 19 | printf("enter number of points (max 16 ) \n"); 20 | scanf("%d", &points); 21 | if(points>16) 22 | { 23 | printf("error - max of 16 points\n"); 24 | 25 | } 26 | else 27 | { 28 | sigmax=0; 29 | sigmay=0; 30 | sigmaxy=0; 31 | sigmaxsquared=0; 32 | sigmaysquared=0; 33 | 34 | /* user enters points from scatter graph */ 35 | for(i=0;i 7 | #include 8 | main() 9 | { 10 | FILE *fp; 11 | 12 | double xpoints[16]; 13 | double ypoints[16]; 14 | double sigmax,sigmay,sigmaxy,sigmaxsquared,sigmaysquared,xbar,ybar; 15 | double fltcnt,sxy,sxx,syy,b,a,c,d, sx, sy, r; 16 | int i,points; 17 | fp=fopen("regbothip.dat","w"); 18 | 19 | printf("enter number of points (max 16 ) \n"); 20 | scanf("%d", &points); 21 | if(points>16) 22 | { 23 | printf("error - max of 16 points\n"); 24 | 25 | } 26 | else 27 | { 28 | sigmax=0; 29 | sigmay=0; 30 | sigmaxy=0; 31 | sigmaxsquared=0; 32 | sigmaysquared=0; 33 | 34 | /* user enters points from scatter graph */ 35 | for(i=0;i 9 | #include 10 | main() 11 | { 12 | FILE *fp; 13 | 14 | /* Preset points for x and y values */ 15 | float xpoints[16]={2.0,2.0,2.0,2.0,2.0,18.0,18.0,18.0,18.0,18.0,6.0,10.0,14.0,6.0,10.0,14.0}; 16 | float ypoints[16]={2.0,6.0,10.0,14.0,18.0,2.0,6.0,10.0,14.0,18.0,2.0,2.0,2.0,18.0,18.0,18.0}; 17 | float sigmax,sigmay,sigmaxy,sigmaxsquared,sigmaysquared,xbar,ybar; 18 | float fltcnt,sxy,sxx,syy,b,a,c,d, sx, sy, r; 19 | int i,points; 20 | 21 | /* Open the output file */ 22 | fp=fopen("regbotht2.dat","w"); 23 | 24 | points = 16; /* number of points fixed at 16 */ 25 | 26 | /* Preset storage variables to zero */ 27 | sigmax=0.0; 28 | sigmay=0.0; 29 | sigmaxy=0.0; 30 | sigmaxsquared=0.0; 31 | sigmaysquared=0.0; 32 | 33 | /* points from preset arrays */ 34 | /* Calculate sigmax,sigmay,sigmaxy,sigmaxsquared,sigmaysquared */ 35 | for(i=0;i 7 | #include 8 | #include 9 | #include 10 | 11 | double calcrand(); 12 | void avstdvar(double dayvals[]); 13 | 14 | 15 | FILE *fp; 16 | 17 | double c,c0,c1,c2,d1,d2,d3; 18 | double q,d,F,y,t; 19 | 20 | int ns,np,ss; 21 | double x1,x2,x3,x4,x5,x6,x7,x8,x9,x10; 22 | int i,j; 23 | time_t tim; 24 | 25 | 26 | 27 | int n; 28 | double average, variance, std_deviation, sum = 0, sum1 = 0; 29 | double PDRaverage,PDRvariance,PDRstd_deviation,pdrsum,nextval,lastval,drift,epsilon,exptest,nitest; 30 | double pdr[50]; 31 | 32 | main() 33 | { 34 | FILE *fp; 35 | /* Array containing day stck prices starting with yesterday and moving backwards */ 36 | double dayvals[50]={22.82,22.51,22.47,22.05,22.96,21.43,20.97,20.46,20.25,20.46,20.45,20.7,20.31,20.94,20.85,20.59,20.65,21.12,20.78}; 37 | 38 | double value,testval; 39 | int j; 40 | 41 | 42 | fp=fopen("asseta2.dat","w"); 43 | srand((unsigned) time(&tim));/* set up random number function */ 44 | 45 | for(i=0;i<50;i++) 46 | { 47 | /* clear predicted rate array */ 48 | pdr[i]=0.0; 49 | } 50 | for(i=19;i<50;i++) 51 | { 52 | /* Clear the end part of our values array for our predicted vales */ 53 | dayvals[i]=0.0; 54 | 55 | } 56 | 57 | n=19; /* number of predicted daily rates (PDR) )*/ 58 | 59 | 60 | 61 | 62 | j=0; 63 | /* write historical rates to output file */ 64 | 65 | for(i=18;i>-1;i--) 66 | { 67 | fprintf(fp,"%d\t%lf\n",j,dayvals[i]); 68 | j++; 69 | } 70 | 71 | 72 | 73 | 74 | /* Calc PDRs - if you enter 20 days there will be 19 PDRs */ 75 | 76 | for(j=0;j=0.5) 139 | q=1-y; 140 | else 141 | q=y; 142 | 143 | t=sqrt(log(1/pow(q,2))); 144 | c=c0+c1*t+c2*pow(t,2); 145 | d=1+d1*t+d2*pow(t,2)+d3*pow(t,3); 146 | 147 | F=t-(c/d); 148 | 149 | /* Use the symmetry of the Cumulative Normal Distribution Graph */ 150 | if(y < 0.5) 151 | { 152 | 153 | y=-1.0*F; 154 | 155 | } 156 | else if(y == 0.5) 157 | y=0; 158 | else 159 | 160 | y=F; 161 | 162 | 163 | printf("y = %lf\n",y); 164 | 165 | 166 | return y; 167 | 168 | } 169 | 170 | void avstdvar(double dayvals[]) 171 | { 172 | /* Average,Standard Deviation,Variance processing */ 173 | sum = 0.0; 174 | sum1 = 0.0; 175 | 176 | 177 | 178 | /* Compute the sum of all elements */ 179 | for (i = 0; i < n; i++) 180 | { 181 | 182 | sum = sum + dayvals[i]; 183 | } 184 | average = sum / (double)n; 185 | 186 | /* Compute variance and standard deviation */ 187 | for (i = 0; i < n; i++) 188 | { 189 | sum1 = sum1 + pow((dayvals[i] - average), 2); 190 | } 191 | variance = sum1 / (double)n; 192 | 193 | /* Compute PDRvariance and PDRstandard deviation */ 194 | sum1=0.0; 195 | for (i = 0; i < n-1; i++) 196 | { 197 | sum1 = sum1 + pow((pdr[i] - PDRaverage), 2); 198 | } 199 | PDRvariance = sum1 / (double)(n-1); 200 | 201 | std_deviation = sqrt(variance); 202 | PDRstd_deviation = sqrt(PDRvariance); 203 | printf("Average of all elements = %lf\n", average); 204 | printf("variance of all elements = %lf\n", variance); 205 | printf("Standard deviation = %lf\n", std_deviation); 206 | printf("PDRvariance of all elements = %lf\n", PDRvariance); 207 | printf("PDRStandard deviation = %lf\n", PDRstd_deviation); 208 | 209 | } -------------------------------------------------------------------------------- /ch4sources/assetalgorithm.c: -------------------------------------------------------------------------------- 1 | /* assetalgorithm.c */ 2 | /* Stock Price predictor simulation */ 3 | /* tests inverse cumulative */ 4 | /* normal distribution function */ 5 | /* User enters a probability value */ 6 | 7 | #define _CRT_SECURE_NO_WARNINGS 8 | #include 9 | #include 10 | #include 11 | #include 12 | 13 | double c,c0,c1,c2,d1,d2,d3; 14 | double q,d,x,y,t; 15 | 16 | int i; 17 | time_t tim; 18 | 19 | int n; 20 | 21 | double probvalue; 22 | 23 | int main() 24 | { 25 | 26 | /* set values for cumulative normal distribution formula */ 27 | c0=2.515517; 28 | c1=0.802853; 29 | c2=0.010328; 30 | d1=1.432788; 31 | d2=0.189269; 32 | d3=0.001308; 33 | 34 | 35 | /* User enters the probability used to find the x value */ 36 | printf("Please enter Probability value between 0.0 and 1.0 :\n "); 37 | scanf("%lf", &probvalue); 38 | printf("You entered %lf\n", probvalue); 39 | 40 | if(probvalue < 0.0 || probvalue > 1.0) 41 | return; /* Entered value is out of range */ 42 | 43 | y=probvalue; /* Store the entered value */ 44 | 45 | /* Use the symmetry of the Cumulative Normal Distribution Graph */ 46 | if(y>=0.5) 47 | q=1-y; 48 | else 49 | q=y; 50 | 51 | /* Calculate the values in the formula */ 52 | t=sqrt(log(1/pow(q,2))); 53 | c=c0+c1*t+c2*pow(t,2); 54 | d=1+d1*t+d2*pow(t,2)+d3*pow(t,3); 55 | 56 | x=t-(c/d); 57 | 58 | /* Use the symmetry of the Cumulative Normal Distribution Graph */ 59 | if(y < 0.5) 60 | { 61 | 62 | y=-1.0*x; 63 | 64 | } 65 | else if(y == 0.5) 66 | y=0; 67 | else 68 | y=x; 69 | 70 | /* Print the x result for the entered Probability value */ 71 | printf("probvaluevalue = %lf\n",probvalue); 72 | printf("x = %lf\n",y); 73 | 74 | return ; 75 | 76 | } 77 | 78 | -------------------------------------------------------------------------------- /ch5sources/createmarket.c: -------------------------------------------------------------------------------- 1 | /* createmarket.c */ 2 | /* creates supermarket stock file */ 3 | /* prints out the records sequentially */ 4 | /* Finds specific records and prints them */ 5 | 6 | #define _CRT_SECURE_NO_WARNINGS 7 | #include 8 | 9 | struct super { 10 | int ID; 11 | char desc[10]; 12 | int limit; 13 | int numstock; 14 | char address[30]; 15 | }; 16 | 17 | int main() 18 | { 19 | int i,numread; 20 | FILE *fp; 21 | struct super s1; 22 | struct super s2; 23 | 24 | struct super s10 = {1,"10",23,50,"1,Park St"}; 25 | struct super s11 = {2,"11",34,51,"2,Park St"}; 26 | struct super s12 = {3,"12",44,52,"3,Park St"}; 27 | struct super s13 = {4,"13",25,53,"4,Park St"}; 28 | struct super s14 = {5,"14",34,54,"5,Park St"}; 29 | struct super s15 = {6,"15",51,55,"6,Park St"}; 30 | struct super s16 = {7,"16",23,56,"7,Park St"}; 31 | struct super s17 = {8,"17",44,57,"8,Park St"}; 32 | struct super s18 = {9,"18",35,58,"9,Park St"}; 33 | struct super s19 = {10,"19",40,59,"10,Park St"}; 34 | struct super s20 = {11,"20",40,60,"11,Park St"}; 35 | struct super s21 = {12,"21",42,61,"12,Park St"}; 36 | struct super s22 = {13,"22",45,62,"13,Park St"}; 37 | struct super s23 = {14,"23",47,63,"14,Park St"}; 38 | struct super s24 = {15,"24",41,63,"15,Park St"}; 39 | 40 | struct super s28 = {16,"25",54,68,"16,Park St"}; 41 | struct super s29 = {17,"26",58,69,"17,Park St"}; 42 | 43 | 44 | /* Open the Patients file */ 45 | 46 | fp = fopen("superm.dat", "w"); 47 | 48 | /* Write details of each patient to file*/ 49 | /* From the structures defined above */ 50 | 51 | fwrite(&s10, sizeof(s1), 1, fp); 52 | fwrite(&s11, sizeof(s1), 1, fp); 53 | fwrite(&s12, sizeof(s1), 1, fp); 54 | fwrite(&s13, sizeof(s1), 1, fp); 55 | fwrite(&s14, sizeof(s1), 1, fp); 56 | fwrite(&s15, sizeof(s1), 1, fp); 57 | fwrite(&s16, sizeof(s1), 1, fp); 58 | fwrite(&s17, sizeof(s1), 1, fp); 59 | fwrite(&s18, sizeof(s1), 1, fp); 60 | fwrite(&s19, sizeof(s1), 1, fp); 61 | fwrite(&s20, sizeof(s1), 1, fp); 62 | fwrite(&s21, sizeof(s1), 1, fp); 63 | fwrite(&s22, sizeof(s1), 1, fp); 64 | fwrite(&s23, sizeof(s1), 1, fp); 65 | fwrite(&s24, sizeof(s1), 1, fp); 66 | 67 | fwrite(&s28, sizeof(s1), 1, fp); 68 | fwrite(&s29, sizeof(s1), 1, fp); 69 | 70 | /* Close the file */ 71 | 72 | fclose(fp); 73 | 74 | /* Reopen the file */ 75 | 76 | fp=fopen("superm.bin", "r"); 77 | 78 | /* Read and print out all of the records on the file */ 79 | 80 | for(i=0;i<17;i++) 81 | { 82 | 83 | numread=fread(&s2, sizeof(s2), 1, fp); 84 | 85 | if(numread == 1) 86 | { 87 | 88 | /*printf( "Number of items read = %d ", numread );*/ 89 | 90 | /*printf("\nlower limit : %d", s2.llimit); 91 | printf("\npressure : %d", s2.press); 92 | printf("\nupper limit : %d", s2.ulimit);*/ 93 | printf("\nID : %d desc : %s limit : %d numstock : %d address : %s", s2.ID,s2.desc,s2.limit,s2.numstock,s2.address); 94 | 95 | } 96 | else { 97 | /* If an error occurred on read then print out message */ 98 | 99 | if (feof(fp)) 100 | 101 | printf("Error reading patients.bin : unexpected end of file fp is %p\n",fp); 102 | 103 | else if (ferror(fp)) 104 | { 105 | perror("Error reading patients.bin"); 106 | } 107 | } 108 | 109 | 110 | 111 | 112 | 113 | } 114 | /* Close the file */ 115 | 116 | fclose(fp); 117 | 118 | 119 | 120 | 121 | 122 | } -------------------------------------------------------------------------------- /ch5sources/markettest4.c: -------------------------------------------------------------------------------- 1 | /* markettest4.c */ 2 | /* supermarket reordering simulation */ 3 | #define _CRT_SECURE_NO_WARNINGS 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | /* functions defined at the end of the program */ 10 | void reorder();/* Call a reorder function */ 11 | void updatefunc();/* Increase stock function */ 12 | void decreasefunc();/* Decrease stock function */ 13 | 14 | /* structure for each item in stock.*/ 15 | /* The "desc" part is the name of a type of cheese */ 16 | /* "limit" is the minimum level of stock */ 17 | /* after which a re-order is required */ 18 | /* "numstock is the current level of the item */ 19 | /* "address" is where to get the order */ 20 | 21 | struct super { 22 | int ID; 23 | char desc[11]; 24 | int limit; 25 | int numstock; 26 | char address[30]; 27 | }; 28 | 29 | struct super s1; 30 | struct super s2; 31 | struct super st[17]; 32 | FILE *fp; 33 | long int minusone = -1; 34 | int i; 35 | 36 | int main() 37 | { 38 | 39 | int numread; 40 | int stockitemID,updateamount; 41 | char update; 42 | 43 | /* open the supermarket file */ 44 | fp=fopen("superm.dat", "r"); 45 | 46 | /* Read and print out all of the records on the file */ 47 | printf("\nID DESCRIPTION LIMIT NUMBER IN STOCK ADDRESS "); 48 | for(i=0;i<17;i++) 49 | { 50 | numread=fread(&s2, sizeof(s2), 1, fp); 51 | 52 | if(numread == 1) 53 | { 54 | printf("\n%2d : %s : %d : %d : %s", s2.ID,s2.desc,s2.limit,s2.numstock,s2.address); /* note the 2d as we want 2 digits */ 55 | } 56 | else { 57 | /* If an error occurred on read then print out message */ 58 | 59 | if (feof(fp)) 60 | 61 | printf("Error reading superm.dat : unexpected end of file fp is %p\n",fp); 62 | 63 | else if (ferror(fp)) 64 | { 65 | perror("Error reading superm.dat"); 66 | } 67 | } 68 | 69 | 70 | } 71 | /* Close the file */ 72 | 73 | fclose(fp); 74 | 75 | /* Ask the user what they want to do with the file */ 76 | /* Increase or decrease the stock ? */ 77 | 78 | printf("\nIs this a Stock update(increase) ? (y or n) \n"); 79 | scanf("%c", &update); 80 | printf("\n answer is %c\n",update); 81 | if(update == 'y') 82 | { 83 | /* User wants to update(increase) stock level */ 84 | printf("\nenter ID \n"); 85 | scanf("%d", &stockitemID); 86 | 87 | printf("\n ID is %d",stockitemID); 88 | 89 | printf("\nenter update amount \n"); 90 | scanf("%d", &updateamount); 91 | 92 | /* Call function to update the stock */ 93 | updatefunc(stockitemID,updateamount); 94 | 95 | 96 | return; 97 | } 98 | else if(update == 'n') 99 | { 100 | /* User wants to decrease stock level */ 101 | printf("\nenter ID \n"); 102 | scanf("%d", &stockitemID); 103 | 104 | printf("\n ID is %d",stockitemID); 105 | 106 | printf("\nenter number sold \n"); 107 | scanf("%d", &updateamount); 108 | 109 | printf("\n number sold entered is %d",updateamount); 110 | 111 | /* Call function to decrease the stock */ 112 | decreasefunc(stockitemID,updateamount); 113 | fclose(fp); 114 | 115 | return; 116 | } 117 | 118 | 119 | 120 | } 121 | void reorder() 122 | { 123 | /* Function to say that you have reordered and the address */ 124 | printf("\nreorder called"); 125 | printf("\n address is is %s",s2.address); 126 | } 127 | void updatefunc(int stockitemID,int updateamount) 128 | { 129 | 130 | /* Increase stock function */ 131 | 132 | /* Function to update current level of stock */ 133 | /* After you to a read the file pointer will be */ 134 | /* pointing to the next record in the file */ 135 | /* So we move the file pointer backwards to */ 136 | /* Point to the record we have just read */ 137 | /* using the fseek command */ 138 | 139 | printf("\nupdate called"); 140 | printf("\nstockitemID is %d updateamount is %d\n",stockitemID,updateamount); 141 | fp = fopen("superm.dat", "r+"); 142 | for (i = 0;i < 17;i++) 143 | { 144 | /* Read each pressure data from file sequentially */ 145 | 146 | fread(&s2, sizeof(s2), 1, fp); 147 | 148 | if(s2.ID == stockitemID) 149 | { 150 | /* We have found the one we want to update */ 151 | s2.numstock = s2.numstock + updateamount; 152 | 153 | 154 | fseek(fp,minusone*sizeof(s2),SEEK_CUR); 155 | 156 | 157 | fwrite(&s2, sizeof(s2), 1, fp); 158 | 159 | printf("\n ID is %d",s2.ID); 160 | printf("\n limit is %d",s2.limit); 161 | printf("\n numstock is %d",s2.numstock); 162 | printf("\n address is %s",s2.address); 163 | 164 | fclose(fp); 165 | break; 166 | } 167 | } 168 | } 169 | void decreasefunc(int stockitemID,int updateamount) 170 | { 171 | 172 | /* Decrease stock function */ 173 | 174 | /* After you to a read the file pointer will be */ 175 | /* pointing to the next record in the file */ 176 | /* So we move the file pointer backwards to */ 177 | /* Point to the record we have just read */ 178 | /* using the fseek command */ 179 | 180 | /* Open supermarket file */ 181 | 182 | fp = fopen("superm.dat", "r+"); 183 | for (i = 0;i < 17;i++) 184 | { 185 | 186 | fread(&s2, sizeof(s2), 1, fp); 187 | 188 | if(s2.ID == stockitemID) 189 | { 190 | 191 | st[i].ID = s2.ID; 192 | st[i].limit = s2.limit; 193 | st[i].numstock = s2.numstock; 194 | 195 | /*printf("\n numstock is %d",st[i].numstock); 196 | printf("\n limit is %d",s2.limit); 197 | printf("\n number sold is %d",updateamount);*/ 198 | if(st[i].numstock == 0) 199 | { 200 | printf("\n Out of stock"); 201 | 202 | printf("\n numstock is %d",st[i].numstock); 203 | printf("\n limit is %d",s2.limit); 204 | printf("\n number sold is %d",updateamount); 205 | 206 | break; 207 | } 208 | 209 | 210 | if(st[i].numstock - updateamount <= 0) 211 | { 212 | /*After decrease, stock level is zero or below*/ 213 | 214 | printf("\nStock update"); 215 | st[i].numstock = 0;/* set to zero (negative is impossible) */ 216 | s2.numstock = st[i].numstock; 217 | 218 | /*printf("\n numstock is %d",st[i].numstock); 219 | printf("\n limit is %d",s2.limit); 220 | printf("\n number sold is %d",updateamount); 221 | printf("\n numstock is %d",s2.numstock); 222 | 223 | fseek(fp,minusone*sizeof(s2),SEEK_CUR); 224 | 225 | fwrite(&s2, sizeof(s2), 1, fp);*/ 226 | reorder(); 227 | 228 | } 229 | else if(st[i].numstock - updateamount <= s2.limit) 230 | { 231 | /*After decrease, stock level is below limit*/ 232 | printf("\nStock update"); 233 | s2.numstock = st[i].numstock-updateamount; 234 | 235 | /*printf("\n limit is %d",s2.limit); 236 | printf("\n number sold is %d",updateamount); 237 | printf("\n numstock is %d",s2.numstock); 238 | 239 | fseek(fp,minusone*sizeof(s2),SEEK_CUR); 240 | 241 | fwrite(&s2, sizeof(s2), 1, fp);*/ 242 | reorder(); 243 | } 244 | else 245 | { 246 | /*After decrease, stock level is above limit*/ 247 | printf("\nStock update"); 248 | st[i].numstock = st[i].numstock - updateamount; 249 | s2.numstock = st[i].numstock; 250 | /*fseek(fp,minusone*sizeof(s2),SEEK_CUR); 251 | printf("\n numstock is %d",s2.numstock); 252 | fwrite(&s2, sizeof(s2), 1, fp);*/ 253 | } 254 | printf("\n limit is %d",s2.limit); 255 | printf("\n number sold is %d",updateamount); 256 | printf("\n numstock is %d",s2.numstock); 257 | 258 | /* Move the file pointer back by one record */ 259 | fseek(fp,minusone*sizeof(s2),SEEK_CUR); 260 | 261 | fwrite(&s2, sizeof(s2), 1, fp); 262 | 263 | break; 264 | } 265 | } 266 | 267 | 268 | } 269 | -------------------------------------------------------------------------------- /ch6sources/createflightsb.c: -------------------------------------------------------------------------------- 1 | /* createflightsb.c */ 2 | /* creates file */ 3 | /* prints out the records sequentially */ 4 | 5 | 6 | #define _CRT_SECURE_NO_WARNINGS 7 | #include 8 | #include 9 | 10 | struct arrivals { 11 | char posn[3]; 12 | char flight_no[8]; 13 | char sch_arrival_time[6]; 14 | char exp_arrival_time[6]; 15 | char origin[15]; 16 | char remarks[14]; 17 | }; 18 | 19 | struct flightcount { 20 | int count; 21 | }; 22 | int main() 23 | { 24 | int i,numread; 25 | FILE *fparr; 26 | 27 | FILE *fltcnt; 28 | struct arrivals s1; 29 | 30 | struct flightcount fc={17}; 31 | struct flightcount fcr; 32 | 33 | 34 | struct arrivals s10 = {"1","AA1232","07:00","07:00","CHICAGO",""}; 35 | struct arrivals s11 = {"2","BA123","07:05","07:05","LONDON",""}; 36 | struct arrivals s12 = {"3","AA4517","07:08","07:15","BOSTON",""}; 37 | struct arrivals s13 = {"4","AF123","07:10","07:10","PARIS",""}; 38 | struct arrivals s14 = {"5","NH444","07:20","07:20","TOKYO",""}; 39 | struct arrivals s15 = {"6","DJ144","07:22","07:22","MUMBAI",""}; 40 | struct arrivals s16 = {"7","AZ2348","07:23","07:25","WASHINGTON",""}; 41 | struct arrivals s17 = {"8","VS9745","07:25","07:26","TORONTO",""}; 42 | struct arrivals s18 = {"9","DL5816","07:30","07:30","CHICAGO",""}; 43 | struct arrivals s19 = {"10","KL5393","07:33","07:33","MANCHESTER",""}; 44 | struct arrivals s20 = {"11","AZ4627","07:35","07:40","ROME",""}; 45 | struct arrivals s21 = {"12","VS4677","07:40","07:40","NEW ORLEANS",""}; 46 | struct arrivals s22 = {"13","SQ125","07:45","07:45","FRANKFURT",""}; 47 | struct arrivals s23 = {"14","EI5666","07:48","07:48","LONDON",""}; 48 | struct arrivals s24 = {"15","WS2321","07:50","07:50","DULLES",""}; 49 | struct arrivals s25 = {"16","AA197","07:55","08:00","SAN FRANCISCO",""}; 50 | struct arrivals s26 = {"17","B57321","07:58","07:48","SARASOTA",""}; 51 | 52 | 53 | /* Create the file flightcnt.dat which will contain */ 54 | /* the current number of flights in arrivals.dat. */ 55 | /* This file can then be updated when flights are */ 56 | /* removed from arrivals.dat to keep a running total */ 57 | 58 | fltcnt = fopen("flightcnt.dat","w"); 59 | fwrite(&fc, sizeof(fc), 1, fltcnt); 60 | fclose(fltcnt); 61 | 62 | fltcnt = fopen("flightcnt.dat","r"); 63 | fread(&fcr, sizeof(fcr), 1, fltcnt); 64 | printf(" Number of flights : %d", fcr.count); 65 | fclose(fltcnt); 66 | /* Open the arrivals file */ 67 | 68 | fparr = fopen("arrivals.dat", "w"); 69 | 70 | /* Write details of each flight to file*/ 71 | /* From the structures defined above */ 72 | 73 | fwrite(&s10, sizeof(s1), 1, fparr); 74 | fwrite(&s11, sizeof(s1), 1, fparr); 75 | fwrite(&s12, sizeof(s1), 1, fparr); 76 | fwrite(&s13, sizeof(s1), 1, fparr); 77 | fwrite(&s14, sizeof(s1), 1, fparr); 78 | fwrite(&s15, sizeof(s1), 1, fparr); 79 | fwrite(&s16, sizeof(s1), 1, fparr); 80 | fwrite(&s17, sizeof(s1), 1, fparr); 81 | fwrite(&s18, sizeof(s1), 1, fparr); 82 | fwrite(&s19, sizeof(s1), 1, fparr); 83 | fwrite(&s20, sizeof(s1), 1, fparr); 84 | fwrite(&s21, sizeof(s1), 1, fparr); 85 | fwrite(&s22, sizeof(s1), 1, fparr); 86 | fwrite(&s23, sizeof(s1), 1, fparr); 87 | fwrite(&s24, sizeof(s1), 1, fparr); 88 | 89 | fwrite(&s25, sizeof(s1), 1, fparr); 90 | fwrite(&s26, sizeof(s1), 1, fparr); 91 | 92 | /* Close the file */ 93 | 94 | fclose(fparr); 95 | /* Reopen the file */ 96 | 97 | fopen("arrivals.dat", "r"); 98 | 99 | /* Read and print out all of the records on the file */ 100 | printf("\n Flight :Sched: Exp: Origin Remarks"); 101 | for(i=0;i<17;i++) 102 | { 103 | 104 | numread=fread(&s1, sizeof(s1), 1, fparr); 105 | 106 | if(numread == 1) 107 | { 108 | printf("\n :%s\t%s\t%s\t%s\t%s\t%s", s1.posn,s1.flight_no,s1.sch_arrival_time,s1.exp_arrival_time,s1.origin,s1.remarks); 109 | } 110 | else { 111 | /* If an error occurred on read then print out message */ 112 | 113 | if (feof(fparr)) 114 | 115 | printf("Error reading arrivals.dat : unexpected end of file fparr is %p\n",fparr); 116 | 117 | else if (ferror(fparr)) 118 | { 119 | perror("Error reading arrivals.dat"); 120 | } 121 | } 122 | 123 | 124 | } 125 | /* Close the file */ 126 | 127 | fclose(fparr); 128 | 129 | 130 | } -------------------------------------------------------------------------------- /ch6sources/flightsg.c: -------------------------------------------------------------------------------- 1 | /* flightsg.c */ 2 | 3 | /* Airport display boards updates*/ 4 | /* arrivals only */ 5 | #define _CRT_SECURE_NO_WARNINGS 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | 12 | main() 13 | { 14 | char fltno[8]; 15 | char ch; 16 | int ret; 17 | int opt2; 18 | int eofcheck; 19 | 20 | /* Structure definition for arrivals file */ 21 | /* This contains the flight number, scheduled */ 22 | /* arrival time, expected arrival time, airport */ 23 | /* from where the flight left and remarks, which */ 24 | /* contains current information eg "landed" etc */ 25 | 26 | struct arrivals { 27 | char posn[3]; 28 | char flight_no[8]; 29 | char sch_arrival_time[6]; 30 | char exp_arrival_time[6]; 31 | char origin[15]; 32 | char remarks[14]; 33 | }; 34 | /* Structure for flightcnt.dat file */ 35 | /* which contains the current number */ 36 | /* of flights */ 37 | struct flightcount { 38 | int count; 39 | }; 40 | 41 | FILE *source, *target; 42 | FILE *fparr; 43 | FILE *fparr2; 44 | FILE *fltcnt; 45 | struct arrivals s1; 46 | 47 | 48 | struct arrivals st[17]; 49 | struct flightcount fc; 50 | 51 | char new_sch_arrival_time[6]; 52 | char new_exp_arrival_time[6]; 53 | char new_origin[15]; 54 | char new_remarks[14]; 55 | 56 | int i,opt; 57 | int lim; 58 | long int minusone = -1; 59 | char rollup; 60 | 61 | char oldname[] = "arrivals2.dat"; 62 | char newname[] = "arrivals.dat"; 63 | 64 | fltcnt = fopen("flightcnt.dat","r"); 65 | fread(&fc, sizeof(fc), 1, fltcnt); 66 | printf(" Number of flights : %d", fc.count); 67 | opt2 = fc.count; 68 | fclose(fltcnt); 69 | 70 | /* Open arrivals file */ 71 | 72 | fparr = fopen("arrivals.dat", "r"); 73 | printf("\n Flight\t:Sched:\tExp:\tOrigin:\t Remarks"); 74 | 75 | 76 | 77 | 78 | for (i = 0;i < 17;i++) 79 | { 80 | /* Read each flight data from file sequentially */ 81 | /* and display them */ 82 | if(fread(&s1, sizeof(s1), 1, fparr) == 1) 83 | { 84 | /* Print flight no, sched and expected time and flight originfor each flight */ 85 | 86 | 87 | strcpy(st[i].posn,s1.posn); 88 | strcpy(st[i].flight_no,s1.flight_no); 89 | strcpy(st[i].sch_arrival_time,s1.sch_arrival_time); 90 | strcpy(st[i].exp_arrival_time,s1.exp_arrival_time); 91 | strcpy(st[i].origin,s1.origin); 92 | strcpy(st[i].remarks,s1.remarks); 93 | 94 | /* Only print the first 12 flights on the "display board" */ 95 | if(opt2 < 12) 96 | lim = opt2; 97 | else 98 | lim = 12; 99 | 100 | if(i 11 | 12 | struct fplant { 13 | int ID; 14 | float temp; 15 | float flowrate; 16 | float hightemp; 17 | float highflow; 18 | }; 19 | 20 | int main() 21 | { 22 | int i,numread; 23 | FILE *fp; 24 | struct fplant s1; 25 | struct fplant s2; 26 | 27 | struct fplant s10 = {1,10,23,50,50}; 28 | struct fplant s11 = {2,11,34,51,50}; 29 | struct fplant s12 = {3,12,44,52,50}; 30 | struct fplant s13 = {4,13,25,53,50}; 31 | struct fplant s14 = {5,14,34,54,50}; 32 | struct fplant s15 = {6,15,51,55,50}; 33 | struct fplant s16 = {7,16,23,56,50}; 34 | struct fplant s17 = {8,17,44,57,50}; 35 | struct fplant s18 = {9,18,35,58,50}; 36 | struct fplant s19 = {10,19,40,59,50}; 37 | struct fplant s20 = {11,20,40,60,50}; 38 | struct fplant s21 = {12,21,42,61,50}; 39 | struct fplant s22 = {13,22,45,62,50}; 40 | struct fplant s23 = {14,23,47,63,50}; 41 | struct fplant s24 = {15,24,41,63,50}; 42 | 43 | struct fplant s28 = {16,28,54,68,50}; 44 | struct fplant s29 = {17,29,58,69,50}; 45 | 46 | 47 | /* Open the file */ 48 | 49 | fp = fopen("tempflow.bin", "w"); 50 | 51 | /* Write details of each ID to file*/ 52 | /* From the structures defined above */ 53 | 54 | fwrite(&s10, sizeof(s1), 1, fp); 55 | fwrite(&s11, sizeof(s1), 1, fp); 56 | fwrite(&s12, sizeof(s1), 1, fp); 57 | fwrite(&s13, sizeof(s1), 1, fp); 58 | fwrite(&s14, sizeof(s1), 1, fp); 59 | fwrite(&s15, sizeof(s1), 1, fp); 60 | fwrite(&s16, sizeof(s1), 1, fp); 61 | fwrite(&s17, sizeof(s1), 1, fp); 62 | fwrite(&s18, sizeof(s1), 1, fp); 63 | fwrite(&s19, sizeof(s1), 1, fp); 64 | fwrite(&s20, sizeof(s1), 1, fp); 65 | fwrite(&s21, sizeof(s1), 1, fp); 66 | fwrite(&s22, sizeof(s1), 1, fp); 67 | fwrite(&s23, sizeof(s1), 1, fp); 68 | fwrite(&s24, sizeof(s1), 1, fp); 69 | 70 | fwrite(&s28, sizeof(s1), 1, fp); 71 | fwrite(&s29, sizeof(s1), 1, fp); 72 | 73 | /* Close the file */ 74 | 75 | fclose(fp); 76 | 77 | /* Reopen the file */ 78 | 79 | fp=fopen("tempflow.bin", "r"); 80 | 81 | /* Read and print out all of the records on the file */ 82 | 83 | for(i=0;i<17;i++) 84 | { 85 | 86 | numread=fread(&s2, sizeof(s2), 1, fp); 87 | 88 | if(numread == 1) 89 | { 90 | 91 | printf("\nID : %d temp : %f flow rate : %f high temp : %f high flow : %f", s2.ID,s2.temp,s2.flowrate,s2.hightemp,s2.highflow); 92 | 93 | } 94 | else { 95 | /* If an error occurred on read then print out message */ 96 | 97 | if (feof(fp)) 98 | 99 | printf("Error reading tempflow.bin : unexpected end of file fp is %p\n",fp); 100 | 101 | else if (ferror(fp)) 102 | { 103 | perror("Error reading tempflow.bin"); 104 | } 105 | } 106 | 107 | 108 | } 109 | /* Close the file */ 110 | 111 | fclose(fp); 112 | 113 | 114 | 115 | 116 | 117 | } -------------------------------------------------------------------------------- /ch7sources/plantb.c: -------------------------------------------------------------------------------- 1 | /* plantb.c */ 2 | /* industrial plant simulation */ 3 | /* Finds specific records and prints them */ 4 | /* Checking power plant temperature and flow rate */ 5 | /* against acceptable levels */ 6 | #define _CRT_SECURE_NO_WARNINGS 7 | #include 8 | #include 9 | #include 10 | #include 11 | 12 | struct fplant { 13 | int ID; 14 | float temp; 15 | float flowrate; 16 | float hightemp; 17 | float highflow; 18 | }; 19 | 20 | 21 | int main() 22 | { 23 | FILE *fp; 24 | 25 | struct fplant s2; 26 | struct fplant st[17]; 27 | int i; 28 | int fpid; 29 | float fptemp,fpflow; 30 | /* Open tempflow.bin file */ 31 | 32 | fp = fopen("tempflow.bin", "r"); 33 | for (i = 0;i < 17;i++) 34 | { 35 | /* Read each pressure data from file sequentially */ 36 | fread(&s2, sizeof(s2), 1, fp); 37 | /* Print pressure data each component */ 38 | 39 | st[i].ID = s2.ID; 40 | st[i].temp = s2.temp; 41 | st[i].flowrate = s2.flowrate; 42 | st[i].hightemp = s2.hightemp; 43 | st[i].highflow = s2.highflow; 44 | 45 | printf("\nID : %2d temp : %f flow rate : %f high temp : %f high flow : %f", s2.ID,s2.temp,s2.flowrate,s2.hightemp,s2.highflow); 46 | } 47 | 48 | fclose(fp); 49 | 50 | /* User asked to enter the ID being minitored */ 51 | 52 | printf("\nenter ID \n"); 53 | scanf("%d", &fpid); 54 | 55 | printf("\n ID is %d",fpid); 56 | 57 | /* User asked to enter the Current temperature being minitored */ 58 | 59 | printf("\nenter current temperature \n"); 60 | 61 | scanf("%f", &fptemp); 62 | 63 | printf("\n current temperature is %f",fptemp); 64 | 65 | /* Current temperature checked against range of temperature */ 66 | /* An Alert is displayed if the temperature is outside the range */ 67 | 68 | for (i = 0;i < 17;i++) 69 | { 70 | 71 | if(fpid == st[i].ID) 72 | 73 | { 74 | 75 | printf("\n high temp is %f",st[i].hightemp); 76 | /*printf("\n struct upper press is %d",st[i].ulimit);*/ 77 | /*if(fppress < st[i].llimit) 78 | printf("\n ALERT! Pressure is below lower limit");*/ 79 | if(fptemp > st[i].hightemp) 80 | printf("\n ALERT! Temperature is above upper limit"); 81 | } 82 | 83 | } 84 | 85 | /* User asked to enter the Flow Rate being minitored */ 86 | 87 | printf("\nenter current flow rate \n"); 88 | scanf("%f", &fpflow); 89 | 90 | printf("\n current flow rate is %f",fpflow); 91 | 92 | /* Flow Rate checked against limits */ 93 | /* An Alert is displayed if the flow rate is outside the range */ 94 | 95 | for (i = 0;i < 17;i++) 96 | { 97 | 98 | if(fpid == st[i].ID) 99 | 100 | { 101 | 102 | printf("\n high flow rate is %f",st[i].highflow); 103 | 104 | if(fpflow > st[i].highflow) 105 | printf("\n ALERT! Flow rate is above upper limit"); 106 | } 107 | 108 | } 109 | 110 | 111 | } -------------------------------------------------------------------------------- /ch7sources/plantbam.c: -------------------------------------------------------------------------------- 1 | /* plantbam.c */ 2 | /* industrial plant simulation */ 3 | /* power plant temperature and flow rate */ 4 | /* Allows amendments to tempflow.bin file */ 5 | #define _CRT_SECURE_NO_WARNINGS 6 | #include 7 | #include 8 | #include 9 | #include 10 | 11 | struct fplant { 12 | int ID; 13 | float temp; 14 | float flowrate; 15 | float hightemp; 16 | float highflow; 17 | }; 18 | 19 | 20 | 21 | 22 | int main() 23 | { 24 | FILE *fp; 25 | 26 | struct fplant s2; 27 | struct fplant st[17]; 28 | int i; 29 | int fpid; 30 | float fnewtemp; 31 | long int minusone = -1; 32 | 33 | /* Open tempflow.bin file */ 34 | 35 | fp = fopen("tempflow.bin", "r"); 36 | for (i = 0;i < 17;i++) 37 | { 38 | /* Read each pressure data from file sequentially */ 39 | fread(&s2, sizeof(s2), 1, fp); 40 | /* Print pressure data each component */ 41 | 42 | st[i].ID = s2.ID; 43 | st[i].temp = s2.temp; 44 | st[i].flowrate = s2.flowrate; 45 | st[i].hightemp = s2.hightemp; 46 | st[i].highflow = s2.highflow; 47 | 48 | printf("\nID : %2d temp : %f flow rate : %f high temp : %f high flow : %f", s2.ID,s2.temp,s2.flowrate,s2.hightemp,s2.highflow); 49 | } 50 | 51 | fclose(fp); 52 | 53 | /* User asked to enter the ID being minitored */ 54 | 55 | fp = fopen("tempflow.bin", "r+"); 56 | printf("\nenter ID \n"); 57 | scanf("%d", &fpid); 58 | 59 | printf("\n ID is %d",fpid); 60 | 61 | 62 | 63 | for (i = 0;i < 17;i++) 64 | { 65 | fread(&s2, sizeof(s2), 1, fp); 66 | if(fpid == s2.ID) 67 | 68 | { 69 | /* User asked to enter the new temperature being minitored */ 70 | 71 | printf("\nenter new temperature \n"); 72 | scanf("%f", &fnewtemp); 73 | 74 | printf("\n new temperature is %f",fnewtemp); 75 | st[i].temp = s2.temp; 76 | s2.temp = fnewtemp; 77 | 78 | /* File updated with new temperature */ 79 | fseek(fp,minusone*sizeof(s2),SEEK_CUR); 80 | fwrite(&s2, sizeof(s2), 1, fp); 81 | 82 | printf("\nID : %d temp : %f flow rate : %f high temp : %f high flow : %f", s2.ID,s2.temp,s2.flowrate,s2.hightemp,s2.highflow); 83 | break; 84 | 85 | } 86 | 87 | } 88 | 89 | fclose(fp); 90 | 91 | 92 | 93 | 94 | } -------------------------------------------------------------------------------- /ch8sources/peke.c: -------------------------------------------------------------------------------- 1 | /* peke.c */ 2 | /* potential energy Vs kinetic energy */ 3 | /* */ 4 | /* */ 5 | #define _CRT_SECURE_NO_WARNINGS 6 | #include 7 | #include 8 | #include 9 | 10 | main() 11 | { 12 | 13 | int i; 14 | double m,g,t,h,hn,KE,PE; 15 | double u,v; 16 | 17 | 18 | FILE *fptr; 19 | 20 | 21 | 22 | fptr=fopen("peke.dat","w"); 23 | 24 | m=10.0;/* preset mass (kg) value */ 25 | g=9.8;/* preset acceleration of gravity (m/s^2) value */ 26 | h=10.0;/* preset height (m) value */ 27 | t=0.1;/* preset time division (s) value */ 28 | u=0.0;/* preset initial velocity (m/s) value */ 29 | 30 | for(i=0;i<100;i++) 31 | { 32 | v=u+g*t; /* find velocity v from initial velocity,accel. of gravity and time */ 33 | KE=0.5*m*pow(v,2);/* find kinetic energy from mass and velocity */ 34 | hn=u*t+0.5*g*pow(t,2);/* find new height after time t */ 35 | h=h-hn;/* new height after falling hn metres */ 36 | PE=m*g*h;/* find potential energy */ 37 | 38 | u=v;/* set the inital velocity for the next increment of the loop to the current velocity */ 39 | if(h<=0) 40 | break; 41 | fprintf(fptr,"%lf\t%lf\n",KE,PE); 42 | 43 | } 44 | 45 | fclose(fptr); 46 | 47 | 48 | } 49 | 50 | 51 | -------------------------------------------------------------------------------- /ch9sources/pendme.c: -------------------------------------------------------------------------------- 1 | /* pendme.c */ 2 | /* 3 | Simple Euler method 4 | */ 5 | #define _CRT_SECURE_NO_WARNINGS 6 | #include 7 | #include 8 | 9 | 10 | 11 | 12 | main() 13 | { 14 | FILE *fptr; 15 | FILE *fptr2; 16 | int i,npoints; 17 | 18 | double length,g,dt,omega[250],theta[250],time[250]; 19 | 20 | 21 | fptr=fopen("pendout.dat","w"); 22 | fptr2=fopen("pendoutb.dat","w"); 23 | length=1.0;/* preset length of pendulum (l) */ 24 | g=9.8;/* preset acceleration of gravity (m/s^2) */ 25 | npoints=250;/* Preset number of points in loop */ 26 | dt=0.04;/* preset time interval (s) */ 27 | 28 | /* Clear storage arrays to zero */ 29 | for(i=0;i 7 | #include 8 | 9 | 10 | 11 | 12 | main() 13 | { 14 | FILE *fptr; 15 | FILE *fptr2; 16 | int i,npoints; 17 | 18 | double length,g,dt,omega[250],theta[250],time[250]; 19 | 20 | 21 | fptr=fopen("pendout2.dat","w"); 22 | fptr2=fopen("pendout2b.dat","w"); 23 | length=1.0;/* preset length of pendulum (l) */ 24 | g=9.8;/* preset acceleration of gravity (m/s^2) */ 25 | npoints=250;/* Preset number of points in loop */ 26 | dt=0.04;/* preset time interval (s) */ 27 | 28 | /* Clear storage arrays to zero */ 29 | for(i=0;i