├── .gitattributes ├── 9781484250631.jpg ├── AppA ├── AppAcomp.c ├── AppAptr1.c └── AppAptr2.c ├── AppB ├── AppBC1q10.c ├── AppBC1q14.c ├── AppBC1q6.c ├── AppBC1q9.c ├── AppBC2q10.c ├── AppBC2q8.c ├── AppBC2q9.c ├── AppBC4q1.c ├── AppBC4q2.c ├── AppBC4q3.c ├── AppBC5q1.c ├── AppBC5q3.c ├── AppBC6q1.c ├── AppBC7q1.c ├── AppBC9q1.c ├── AppBC9q2.c ├── Companyex.bin └── patientex.bin ├── Ch01 ├── addtwonos.c ├── addtwonos2.c ├── arr1D.c ├── arr1D2.c ├── arr2Dtest2.c ├── arr2Dtest3.c ├── divtwonos.c ├── dloop2.c ├── forloop2.c ├── forloop3.c ├── func0.c ├── func1.c ├── funcmine.c ├── getputchar.c ├── gototest.c ├── iftest.c ├── iftest2.c ├── mathfunc.c ├── mathfunc2.c ├── multtwonos.c ├── myswitch1.c ├── myswitch5.c ├── sizeof.c ├── strings.c ├── struct0.c ├── struct5.c ├── trig.c └── trig2.c ├── Ch02 ├── quad1.c ├── quad3.c ├── trialimp1.c ├── trialimp2.c └── trialimp3.c ├── Ch03 ├── simpsons.c ├── trapezium1.c ├── trapezium2.c ├── trapezium3.c └── trapezium4.c ├── Ch04 ├── montecarlo.c ├── montecarlo2.c ├── montecarlocircle.c ├── montecarlocylinder.c └── montecarlosphere.c ├── Ch05 ├── matrixadd2.c ├── matrixinv3x3.c └── matrixmult3.c ├── Ch06 ├── pmcc2b.c └── regyonx2.c ├── Ch07 ├── buffon3.c ├── radioact5.c └── randwalk4C.c ├── Ch08 ├── augmat17A.c ├── augmat18.c ├── augmat18C.c ├── augmat19C.c ├── augmat25.c └── augmat2A.c ├── Ch09 ├── filereadCh.c ├── filereadE.c ├── fileseek6ra.c ├── filewrite.c ├── filewriteE.c ├── filewriteex3.c ├── filewritepatients.c ├── radioact4a.c └── randwalk6.c ├── Ch10 ├── eulermeCh.c ├── eulermeCh2.c ├── rkch1.c ├── rkch2.c └── runge2me5a.c ├── Contributing.md ├── LICENSE.txt ├── README.md └── errata.md /.gitattributes: -------------------------------------------------------------------------------- 1 | # Auto detect text files and perform LF normalization 2 | * text=auto 3 | -------------------------------------------------------------------------------- /9781484250631.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Apress/numerical-c/e0ff82d28d2e837846b27255ccd745c00f51bd5b/9781484250631.jpg -------------------------------------------------------------------------------- /AppA/AppAcomp.c: -------------------------------------------------------------------------------- 1 | #include 2 | main() 3 | { 4 | float f,f1,f2; 5 | double d,d1,d2; 6 | int i,i1,i2; 7 | /* We want to divide 1623875 by 57 in double format, float format and integer format */ 8 | 9 | f1=1623875; 10 | f2=57; 11 | 12 | d1=1623875; 13 | d2=57; 14 | 15 | i1=1623875; 16 | i2=57; 17 | 18 | d=d1/d2; 19 | f=f1/f2; 20 | i=i1/i2; 21 | 22 | printf("d is %lf, f is %f, i is %d\n",d,f,i); 23 | 24 | /* Answer to this is d = 28489.035088 f = 28489.035156 i = 28489 25 | Calculator anwer is 28489.035087719289245614 (recurring) 26 | */ 27 | 28 | 29 | } 30 | -------------------------------------------------------------------------------- /AppA/AppAptr1.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | int ourvariable; 6 | char achar; 7 | char anarray[10]; 8 | 9 | printf("address of ourvariable is %xp\n",&ourvariable); 10 | printf("address of achar is %xp\n",&achar); 11 | printf("address of anarray is %xp\n",&anarray); 12 | 13 | return(0); 14 | } 15 | -------------------------------------------------------------------------------- /AppA/AppAptr2.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | int ourvariable = 38; 6 | char achar = 'M'; 7 | char anarray[10] = "HELLO"; 8 | 9 | int *ourvariablep; 10 | char *acharp; 11 | char *anarrayp; 12 | 13 | ourvariablep = &ourvariable; 14 | acharp = &achar; 15 | anarrayp = anarray; 16 | 17 | 18 | 19 | printf("address of ourvariable is %xp\n",&ourvariable); 20 | printf("value in ourvariable is %d\n",ourvariable); 21 | printf("address in ourvariablep is %xp\n",ourvariablep); 22 | 23 | printf("address of achar is %xp\n",&achar); 24 | printf("value in achar is %c\n",achar); 25 | printf("address in acharp is %xp\n",acharp); 26 | 27 | printf("address of anarray is %xp\n",&anarray); 28 | printf("value in anarray is %s\n",anarray); 29 | printf("address in anarrayp is %xp\n", anarrayp); 30 | 31 | return(0); 32 | } 33 | -------------------------------------------------------------------------------- /AppB/AppBC1q10.c: -------------------------------------------------------------------------------- 1 | /* Function which returns an answer */ 2 | /* finds the pupil in one year of the school with the highest marks */ 3 | 4 | #define _CRT_SECURE_NO_WARNINGS 5 | #include 6 | double getmarks(double pupils[]); 7 | 8 | int main() 9 | { 10 | double pupil; 11 | /* Array with marks for class is preset in the main part of the program */ 12 | double marks[] = {1.2,2.3,3.4,4.5,5.6,6.7,7.8,8.9,9.0}; 13 | /* Call function getmarks. The function returns the average marks which is then stored in pupil */ 14 | pupil = getmarks(marks); 15 | printf("Avarage mark is = %lf", pupil); 16 | return 0; 17 | } 18 | 19 | double getmarks(double pupils[]) 20 | { 21 | int i; 22 | double average, total; 23 | total = 0; 24 | /* Go through all the pupils in turn and add their mark */ 25 | for (i = 0; i < 9; ++i) 26 | { 27 | total = total + pupils[i]; 28 | 29 | } 30 | average = total/9; 31 | return average; /* returns the value in average to where the function was called */ 32 | } 33 | -------------------------------------------------------------------------------- /AppB/AppBC1q14.c: -------------------------------------------------------------------------------- 1 | /* Structure example program (extended structure)*/ 2 | #define _CRT_SECURE_NO_WARNINGS 3 | #include 4 | 5 | /* define the structure */ 6 | struct Student { 7 | int id; 8 | char name[16]; 9 | float percent; 10 | }; 11 | 12 | int main() { 13 | int i; 14 | /* define 5 data locations of type "student" */ 15 | 16 | struct Student year9[5]; 17 | 18 | for(i=0; i<5; i++) 19 | { 20 | /* Assign values to the structure */ 21 | printf("enter student ID\n"); 22 | scanf("%d",&year9[i].id); 23 | printf("enter student name\n"); 24 | scanf("%s",year9[i].name); 25 | printf("enter student percent\n"); 26 | scanf("%f",&year9[i].percent); 27 | 28 | 29 | } 30 | for(i=0; i<5; i++) 31 | { 32 | /* Print out structure s1 */ 33 | 34 | printf("\nid : %d", year9[i].id); 35 | printf("\nName : %s", year9[i].name); 36 | printf("\nPercent : %f", year9[i].percent); 37 | 38 | 39 | } 40 | 41 | 42 | 43 | return (0); 44 | } 45 | -------------------------------------------------------------------------------- /AppB/AppBC1q6.c: -------------------------------------------------------------------------------- 1 | #define _CRT_SECURE_NO_WARNINGS 2 | #include 3 | /* demonstrate a forloop (setting the forloop limit)*/ 4 | main() 5 | 6 | { 7 | 8 | float this_is_a_number , total; 9 | int i,forlimit; 10 | 11 | total = 0; 12 | printf( "Please enter forloop limit:\n " ); 13 | scanf( "%d", &forlimit );/* entered limit stored in forlimit */ 14 | for(i=0;i 4 | 5 | /* example of a 2D array test for 2 arrays*/ 6 | int main() 7 | { 8 | int arr1[8][8]; 9 | int arr2[8][8]; 10 | 11 | int i,j,k,l; 12 | 13 | printf("enter number of rows and columns of first array(max 8 rows max 8 columns) \n"); 14 | scanf("%d %d", &k, &l); 15 | if(k>8 || l>8) 16 | { 17 | printf("error - max of 8 for rows or columns\n"); 18 | 19 | } 20 | 21 | else 22 | { 23 | printf("enter array\n"); 24 | for(i=0;i8 || l>8) 52 | { 53 | printf("error - max of 8 for rows or columns\n"); 54 | 55 | } 56 | 57 | else 58 | { 59 | printf("enter array\n"); 60 | for(i=0;i 4 | #include 5 | main() 6 | { 7 | float lower,upper; 8 | 9 | int i; 10 | double testhigh,testlow,testvalue,middle; 11 | 12 | int iterations; 13 | 14 | printf("enter lower limit");/* the lower x value for your integration */ 15 | scanf("%f",&lower); 16 | printf("enter upper limit");/* the upper x value for your integration */ 17 | scanf("%f",&upper); 18 | printf("enter number of iterations"); 19 | scanf("%d",&iterations); 20 | 21 | testlow=lower; 22 | testhigh=upper; 23 | 24 | for(i=0;i 0) 38 | { 39 | testhigh=middle; 40 | } 41 | else 42 | { 43 | testlow=middle; 44 | } 45 | 46 | } 47 | printf("x is %f",middle); 48 | 49 | } 50 | -------------------------------------------------------------------------------- /AppB/AppBC2q8.c: -------------------------------------------------------------------------------- 1 | #define _CRT_SECURE_NO_WARNINGS 2 | #include 3 | #include 4 | main() 5 | { 6 | 7 | float lower,upper; 8 | int i; 9 | double testhigh,testlow,testvalue,middle; 10 | int iterations; 11 | 12 | printf("enter lower limit");/* the lower x value for your integration */ 13 | scanf("%f",&lower); 14 | printf("enter upper limit");/* the upper x value for your integration */ 15 | scanf("%f",&upper); 16 | printf("enter number of iterations"); 17 | scanf("%d",&iterations); 18 | 19 | testlow=lower; 20 | testhigh=upper; 21 | 22 | for(i=0;i 0) 36 | { 37 | testhigh=middle; 38 | } 39 | else 40 | { 41 | testlow=middle; 42 | } 43 | 44 | } 45 | printf("x is %f",middle); 46 | } 47 | -------------------------------------------------------------------------------- /AppB/AppBC2q9.c: -------------------------------------------------------------------------------- 1 | /*trapezium - trial and improvement using inverse functions */ 2 | #define _CRT_SECURE_NO_WARNINGS 3 | #include 4 | #include 5 | main() 6 | { 7 | float lower,upper; 8 | 9 | int i; 10 | double testhigh,testlow,testvalue,middle; 11 | 12 | int iterations; 13 | 14 | printf("enter lower limit");/* the lower x value for your integration */ 15 | scanf("%f",&lower); 16 | printf("enter upper limit");/* the upper x value for your integration */ 17 | scanf("%f",&upper); 18 | printf("enter number of iterations"); 19 | scanf("%d",&iterations); 20 | 21 | testlow=lower; 22 | testhigh=upper; 23 | 24 | for(i=0;i 0) 37 | { 38 | testhigh=middle; 39 | } 40 | else 41 | { 42 | testlow=middle; 43 | } 44 | 45 | } 46 | printf("x is %f",middle); 47 | 48 | 49 | } 50 | -------------------------------------------------------------------------------- /AppB/AppBC4q1.c: -------------------------------------------------------------------------------- 1 | /* Montecarlo sphere (whole sphere in 1st quadrant)*/ 2 | /* Calculation of volume using monte carlo */ 3 | /* by counting relative volumes */ 4 | /* integrates (x-2)^2 + (y-2)^2 + (z-2)^2 = 2^2 to your specified limits */ 5 | #define _CRT_SECURE_NO_WARNINGS 6 | #include 7 | #include 8 | #include 9 | main() 10 | { 11 | 12 | double x, y, z; 13 | double zupper, zlower, yupper, ylower, xupper, xlower; 14 | double montevol, volume; 15 | double totalexpvol, totalvol; 16 | int j; 17 | /* unsigned int iterations;*/ 18 | long int iterations; 19 | 20 | 21 | 22 | printf("enter lower x limit\n"); 23 | scanf("%lf", &xlower); 24 | printf("enter upper x limit\n"); 25 | scanf("%lf", &xupper); 26 | printf("xlower %lf xupper %lf\n", xlower, xupper); 27 | 28 | 29 | printf("enter lower y limit\n"); 30 | scanf("%lf", &ylower); 31 | printf("enter upper y limit\n"); 32 | scanf("%lf", &yupper); 33 | printf("ylower %lf yupper %lf\n", ylower, yupper); 34 | 35 | 36 | printf("enter lower z limit\n"); 37 | scanf("%lf", &zlower); 38 | printf("enter upper z limit\n"); 39 | scanf("%lf", &zupper); 40 | printf("zlower %lf zupper %lf\n", zlower, zupper); 41 | 42 | 43 | volume = (xupper - xlower)*(yupper - ylower)*(zupper - zlower); 44 | printf("volume is %lf\n", volume); 45 | printf("enter iterations up to 1000000\n"); 46 | scanf("%d", &iterations); 47 | 48 | totalvol = 0; 49 | totalexpvol = 0; 50 | 51 | 52 | for (j = 1;j < iterations;j++) 53 | { 54 | 55 | /* find random numbers for x,y ansd z */ 56 | x = rand() % 1000; 57 | y = rand() % 1000; 58 | z = rand() % 1000; 59 | y = y / 1000; 60 | x = x / 1000; 61 | z = z / 1000; 62 | 63 | /* x,y and z will have numbers between 0 and 1 */ 64 | /* so multiply by the user's entered ranges for x,y and z */ 65 | x = xlower + (xupper - xlower)*x; 66 | y = ylower + (yupper - ylower)*y; 67 | z = zlower + (zupper - zlower)*z; 68 | 69 | 70 | 71 | if (x >= xlower && y >= ylower && z >= zlower) 72 | { 73 | totalvol = totalvol + 1; /* This contains the total number of entries */ 74 | 75 | if ((pow((y - 2), 2) + pow((x - 2), 2)) + pow((z - 2), 2) < 4) 76 | { 77 | 78 | totalexpvol = totalexpvol + 1;/* This contains number of entries within desired vol */ 79 | 80 | } 81 | } 82 | 83 | } 84 | if (totalvol != 0) 85 | { 86 | montevol = volume * (totalexpvol / totalvol);/* Monte Carlo volume os the fraction of the cube volume */ 87 | } 88 | printf("monte carlo volume is %lf\n", montevol); 89 | } 90 | -------------------------------------------------------------------------------- /AppB/AppBC4q2.c: -------------------------------------------------------------------------------- 1 | /* Montecarlo cone*/ 2 | /* Calculation of volume using monte carlo */ 3 | /* by counting relative volumes */ 4 | /* integrates x^2 + y^2 * z to your specified limits */ 5 | #define _CRT_SECURE_NO_WARNINGS 6 | #include 7 | #include 8 | #include 9 | main() 10 | { 11 | 12 | double x, y, z; 13 | double zupper, zlower, yupper, ylower, xupper, xlower; 14 | double montevol, volume; 15 | double totalexpvol, totalvol, tantheta, radius; 16 | int j; 17 | int iterations; 18 | 19 | 20 | 21 | printf("enter lower x limit\n"); 22 | scanf("%lf", &xlower); 23 | printf("enter upper x limit\n"); 24 | scanf("%lf", &xupper); 25 | printf("xlower %lf xupper %lf\n", xlower, xupper); 26 | 27 | 28 | printf("enter lower y limit\n"); 29 | scanf("%lf", &ylower); 30 | printf("enter upper y limit\n"); 31 | scanf("%lf", &yupper); 32 | printf("ylower %lf yupper %lf\n", ylower, yupper); 33 | 34 | 35 | printf("enter lower z limit\n"); 36 | scanf("%lf", &zlower); 37 | printf("enter upper z limit\n"); 38 | scanf("%lf", &zupper); 39 | printf("zlower %lf zupper %lf\n", zlower, zupper); 40 | 41 | 42 | volume = (xupper - xlower)*(yupper - ylower)*(zupper - zlower);/* volume of cuboid enclosing the cone */ 43 | printf("volume is %lf\n", volume); 44 | 45 | printf("enter iterations up to 1000000\n"); 46 | scanf("%d", &iterations); 47 | 48 | totalvol = 0; 49 | totalexpvol = 0; 50 | tantheta = (zupper - zlower) / (xupper - xlower);/* Tangent of the angle the slant edge makes with the base */ 51 | radius = sqrt(pow(xupper, 2) + pow(yupper, 2)); 52 | radius = 2; 53 | 54 | for (j = 1;j < iterations;j++) 55 | { 56 | 57 | /* find random numbers for x,y ansd z */ 58 | x = rand() % 1000; 59 | y = rand() % 1000; 60 | z = rand() % 1000; 61 | y = y / 1000; 62 | x = x / 1000; 63 | z = z / 1000; 64 | 65 | /* x,y and z will have numbers between 0 and 1 */ 66 | /* so multiply by the user's entered ranges for x,y and z */ 67 | x = xlower + (xupper - xlower)*x; 68 | y = ylower + (yupper - ylower)*y; 69 | z = zlower + (zupper - zlower)*z; 70 | 71 | 72 | 73 | if (x >= xlower && z >= zlower && y >= ylower) 74 | { 75 | totalvol = totalvol + 1; /* This contains the total number of entries */ 76 | /* x and y coordinates have to be within circular base */ 77 | /* z coordinate has to be below the slanted edge which */ 78 | /* is vertically above the (x,y) point */ 79 | 80 | if ((pow(y, 2) + pow(x, 2) < 4) && (z < tantheta*(radius - sqrt(pow(x, 2) + pow(y, 2))))) 81 | 82 | { 83 | 84 | totalexpvol = totalexpvol + 1;/* This contains number of entries within desired vol */ 85 | 86 | } 87 | } 88 | 89 | 90 | } 91 | if (totalvol != 0) 92 | { 93 | montevol = volume * (totalexpvol / totalvol);/* Monte Carlo volume os the fraction of the cube volume */ 94 | } 95 | printf("monte carlo volume is %lf\n", montevol); 96 | 97 | 98 | } 99 | -------------------------------------------------------------------------------- /AppB/AppBC4q3.c: -------------------------------------------------------------------------------- 1 | /* Montecarlo 4-D sphere*/ 2 | /* Calculation of volume using monte carlo */ 3 | /* by counting relative volumes */ 4 | /* integrates x^2 + y^2 + z^2 + p^2= 2^2 to your specified limits */ 5 | /* NB 4D graphs have 16 "quadrants"(8 for 3D, 4 for 2D) */ 6 | #define _CRT_SECURE_NO_WARNINGS 7 | #include 8 | #include 9 | #include 10 | main() 11 | { 12 | 13 | double x, y, z, p; 14 | double zupper, zlower, yupper, ylower, xupper, xlower, pupper, plower; 15 | double montevol, volume; 16 | double totalexpvol, totalvol; 17 | int j; 18 | int iterations; 19 | 20 | 21 | 22 | printf("enter lower x limit\n"); 23 | scanf("%lf", &xlower); 24 | 25 | printf("enter upper x limit\n"); 26 | scanf("%lf", &xupper); 27 | printf("xlower %lf xupper %lf\n", xlower, xupper); 28 | 29 | 30 | printf("enter lower y limit\n"); 31 | scanf("%lf", &ylower); 32 | printf("enter upper y limit\n"); 33 | scanf("%lf", &yupper); 34 | printf("ylower %lf yupper %lf\n", ylower, yupper); 35 | 36 | 37 | printf("enter lower z limit\n"); 38 | scanf("%lf", &zlower); 39 | printf("enter upper z limit\n"); 40 | scanf("%lf", &zupper); 41 | printf("zlower %lf zupper %lf\n", zlower, zupper); 42 | 43 | printf("enter lower p limit\n"); 44 | scanf("%lf", &plower); 45 | printf("enter upper p limit\n"); 46 | 47 | scanf("%lf", &pupper); 48 | printf("plower %lf pupper %lf\n", plower, pupper); 49 | 50 | 51 | 52 | 53 | volume = (xupper - xlower)*(yupper - ylower)*(zupper - zlower)*(pupper - plower); 54 | printf("volume is %lf\n", volume); 55 | 56 | printf("enter iterations up to 1000000\n"); 57 | scanf("%d", &iterations); 58 | 59 | totalvol = 0; 60 | totalexpvol = 0; 61 | 62 | 63 | for (j = 1;j < iterations;j++) 64 | { 65 | 66 | /* find random numbers for x,y ansd z */ 67 | x = rand() % 1000; 68 | y = rand() % 1000; 69 | z = rand() % 1000; 70 | p = rand() % 1000; 71 | y = y / 1000; 72 | x = x / 1000; 73 | z = z / 1000; 74 | p = p / 1000; 75 | /* x,y and z will have numbers between 0 and 1 */ 76 | /* so multiply by the user's entered ranges for x,y and z */ 77 | x = xlower + (xupper - xlower)*x; 78 | y = ylower + (yupper - ylower)*y; 79 | z = zlower + (zupper - zlower)*z; 80 | p = plower + (pupper - plower)*p; 81 | 82 | 83 | if (x >= xlower && z >= zlower && y >= ylower && p >= plower) 84 | { 85 | totalvol = totalvol + 1; /* This contains the total number of entries */ 86 | 87 | 88 | if ((pow(y, 2) + pow(x, 2) + pow(z, 2) + pow(p, 2)) < 4) 89 | { 90 | 91 | totalexpvol = totalexpvol + 1;/* This contains number of entries within desired vol */ 92 | 93 | } 94 | } 95 | 96 | 97 | } 98 | if (totalvol != 0) 99 | { 100 | montevol = volume * (totalexpvol / totalvol);/* Monte Carlo volume os the fraction of the cube volume */ 101 | } 102 | printf("monte carlo volume is %lf\n", montevol); 103 | 104 | 105 | } 106 | -------------------------------------------------------------------------------- /AppB/AppBC5q1.c: -------------------------------------------------------------------------------- 1 | /* Matrix program */ 2 | /* Add two floating point matrices */ 3 | #define _CRT_SECURE_NO_WARNINGS 4 | 5 | #include 6 | 7 | 8 | main() 9 | 10 | { 11 | float matarr1[8][8];/* First matrix store (rowxcolumn)*/ 12 | float matarr2[8][8];/* Second matrix store (rowxcolumn)*/ 13 | float matsum[8][8];/* Sum of matrices store (rowxcolumn)*/ 14 | int i,j,k,l; 15 | 16 | printf("enter order of the two matrices (max 8 rows max 8 columns) \n"); 17 | scanf("%d %d", &k, &l); 18 | if(k>8 || l>8) 19 | { 20 | printf("error - max of 8 for rows or columns\n"); 21 | 22 | } 23 | 24 | else 25 | { 26 | printf("enter first matrix\n"); 27 | for(i=0;i 5 | 6 | 7 | int main() 8 | 9 | { 10 | float matarr1[8][8];/* First matrix store (rowxcolumn)*/ 11 | float matarr2[8][8];/* second matrix store (rowxcolumn)*/ 12 | float matmult[8][8];/* matrix answer (rowxcolumn)*/ 13 | int i,j,k; 14 | int r1,c1,r2,c2; 15 | int error; 16 | 17 | error=0; 18 | 19 | printf("enter order of the first matrix (max 8 rows max 8 columns) \n"); 20 | scanf("%d %d", &r1, &c1); 21 | if(r1>8 || c1>8) 22 | { 23 | printf("error - max of 8 for rows or columns\n"); 24 | error=1; 25 | 26 | } 27 | printf("enter order of the second matrix (max 8 rows max 8 columns) \n"); 28 | scanf("%d %d", &r2, &c2); 29 | if(r2>8 || c2>8) 30 | { 31 | printf("error - max of 8 for rows or columns\n"); 32 | error=1; 33 | 34 | } 35 | if(error == 0) 36 | 37 | { 38 | for(i=0;i 6 | #include 7 | main() 8 | { 9 | 10 | 11 | float xpoints[10], ypoints[10]; 12 | float sigmax, sigmay, sigmaxy, sigmaysquared, xbar, ybar; 13 | float fltcnt, sxy, syy, c, d; 14 | int i, points; 15 | 16 | /* User asked for number of points on scatter graph */ 17 | printf("enter number of points (max 10 ) \n"); 18 | scanf("%d", &points); 19 | if (points > 10) 20 | { 21 | printf("error - max of 10 points\n"); 22 | 23 | } 24 | else 25 | { 26 | sigmax = 0; 27 | sigmay = 0; 28 | sigmaxy = 0; 29 | 30 | sigmaysquared = 0; 31 | 32 | /* User enters points */ 33 | for (i = 0;i < points;i++) 34 | { 35 | printf("enter point (x and y separated by space) \n"); 36 | scanf("%f %f", &xpoints[i], &ypoints[i]); 37 | sigmax = sigmax + xpoints[i]; 38 | sigmay = sigmay + ypoints[i]; 39 | sigmaxy = sigmaxy + xpoints[i] * ypoints[i]; 40 | 41 | sigmaysquared = sigmaysquared + (float)pow(ypoints[i], 2); 42 | } 43 | printf("points are \n"); 44 | for (i = 0;i < points;i++) 45 | { 46 | printf(" \n"); 47 | printf("%f %f", xpoints[i], ypoints[i]); 48 | 49 | } 50 | printf(" \n"); 51 | fltcnt = (float)points; 52 | 53 | /* regression variables calculated */ 54 | xbar = sigmax / fltcnt; 55 | ybar = sigmay / fltcnt; 56 | sxy = (1 / fltcnt)*sigmaxy - xbar * ybar; 57 | 58 | syy = (1 / fltcnt)*sigmaysquared - ybar * ybar; 59 | 60 | d = sxy / syy; 61 | c = xbar - d * ybar; 62 | 63 | 64 | /* Regression line */ 65 | printf("Equation of regression line x on y is\n "); 66 | printf(" x=%f + %fy", c, d); 67 | } 68 | 69 | } 70 | 71 | -------------------------------------------------------------------------------- /AppB/AppBC7q1.c: -------------------------------------------------------------------------------- 1 | /* simple random walk simulation in 1 dimension */ 2 | #define _CRT_SECURE_NO_WARNINGS 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | 9 | FILE *output; 10 | time_t t; 11 | 12 | main() 13 | { 14 | int i; 15 | double xrand; 16 | 17 | double x,randwalkarr[20001]; 18 | 19 | 20 | output= fopen ("randwalk6.dat", "w"); /* external file name */ 21 | 22 | for (i=0; i<=20000; i++) 23 | randwalkarr [i]=0.0; /* clear array */ 24 | 25 | 26 | srand((unsigned) time(&t)); /* set the number generator */ 27 | 28 | 29 | 30 | x=0.0; 31 | 32 | 33 | for (i=1;i<=20000; i++) 34 | { 35 | /* generate x random number */ 36 | xrand=rand()%1000; 37 | xrand=xrand/1000; 38 | if(xrand<0.5) 39 | x=x+1.0; 40 | else 41 | x=x-1.0; 42 | 43 | 44 | randwalkarr[i] = sqrt(x*x);/* store randwalkarr to total */ 45 | 46 | 47 | } 48 | /* Write values to file */ 49 | for (i=0; i<=200; i++) 50 | { 51 | 52 | fprintf(output,"%d %lf\n", i, randwalkarr[i*100]); 53 | } 54 | 55 | fclose (output); 56 | } 57 | 58 | -------------------------------------------------------------------------------- /AppB/AppBC9q1.c: -------------------------------------------------------------------------------- 1 | /* filereadexr */ 2 | /* reads from file */ 3 | /* reads and prints sequentially */ 4 | /* reads and prints specific records */ 5 | /* Does not use seek */ 6 | #define _CRT_SECURE_NO_WARNINGS 7 | #include 8 | 9 | struct Patient { 10 | int PatientID; 11 | char name[13]; 12 | int BloodPressure; 13 | char allergies; 14 | char leukaemia; 15 | char anaemia; 16 | char asthma; 17 | char epilepsy; 18 | char famepil; 19 | }; 20 | 21 | int main() 22 | { 23 | FILE *fp; 24 | 25 | 26 | struct Patient s2; 27 | 28 | 29 | int numread, i; 30 | double casthepi; 31 | double percent; 32 | 33 | /* Open patients file */ 34 | 35 | fp = fopen("patientex.bin", "r"); 36 | if(!fp) 37 | { 38 | printf("patientex.bin file unavailable"); 39 | return(0); 40 | } 41 | for (i = 0;i < 17;i++) 42 | { 43 | /* Read each patient data from file sequentially */ 44 | fread(&s2, sizeof(s2), 1, fp); 45 | /* Print patient ID, name and Blood Pressure for each patient */ 46 | 47 | printf("\nPatientID : %d", s2.PatientID); 48 | printf("\n Name : %s", s2.name); 49 | printf("\nBloodPressure : %d", s2.BloodPressure); 50 | printf("\nAllergies %c leukaemia %c anaemia %c", s2.allergies, s2.leukaemia, s2.anaemia); 51 | printf("\nAsthma %c epilepsy %c famely epilepsy %c", s2.asthma, s2.epilepsy, s2.famepil); 52 | 53 | 54 | } 55 | 56 | fclose(fp); 57 | 58 | 59 | /* Re-open the patients file */ 60 | 61 | fp = fopen("patientex.bin", "r"); 62 | for (i = 0;i < 17;i++) 63 | { 64 | /* Search the file for patient with ID of 23 */ 65 | 66 | fread(&s2, sizeof(s2), 1, fp); 67 | if (s2.PatientID == 23) 68 | { 69 | /* Found the patient. Print their name */ 70 | printf("\nName : %s", s2.name); 71 | break; 72 | } 73 | } 74 | /* Go back to the beginning of the file */ 75 | 76 | 77 | rewind(fp); 78 | /* Find all patients with Blood Pressure reading above 63 */ 79 | 80 | for (i = 0;i < 17;i++) 81 | { 82 | fread(&s2, sizeof(s2), 1, fp); 83 | if (s2.BloodPressure > 63) 84 | { 85 | /* Print out name of each patient with Blood pressure above 63 */ 86 | printf("\nName : %s", s2.name); 87 | 88 | } 89 | } 90 | /* Go back to the beginning of the file */ 91 | rewind(fp); 92 | 93 | /* Read and print out the first 3 patients in the file */ 94 | 95 | numread = fread(&s2, sizeof(s2), 1, fp); 96 | if (numread == 1) 97 | { 98 | printf("\nPatientID : %d", s2.PatientID); 99 | printf("\nName : %s", s2.name); 100 | printf("\nBloodPressure : %d", s2.BloodPressure); 101 | 102 | } 103 | numread = fread(&s2, sizeof(s2), 1, fp); 104 | if (numread == 1) 105 | { 106 | 107 | printf("\nPatientID : %d", s2.PatientID); 108 | printf("\nName : %s", s2.name); 109 | printf("\nBloodPressure : %d", s2.BloodPressure); 110 | } 111 | numread = fread(&s2, sizeof(s2), 1, fp); 112 | if (numread == 1) 113 | { 114 | printf("\nPatientID : %d", s2.PatientID); 115 | printf("\nName : %s", s2.name); 116 | printf("\nBloodPressure : %d", s2.BloodPressure); 117 | } 118 | /* Close the file */ 119 | 120 | fclose(fp); 121 | /* Re-open the patients file */ 122 | casthepi = 0; 123 | fp = fopen("patientex.bin", "r"); 124 | for (i = 0;i < 17;i++) 125 | { 126 | /* Search the file for link between asthma and epilepsy */ 127 | 128 | fread(&s2, sizeof(s2), 1, fp); 129 | if (s2.epilepsy == 'y' && s2.asthma == 'y') 130 | { 131 | casthepi = casthepi + 1.0; 132 | /* Found the patient. Print their name */ 133 | printf("\nLink between asthma and epilepsy"); 134 | printf("\nName : %s", s2.name); 135 | 136 | } 137 | } 138 | percent = (casthepi / 17.0)*100.0; 139 | printf("\npercent asthma & epilepsy : %f", percent); 140 | fclose(fp); 141 | 142 | } 143 | -------------------------------------------------------------------------------- /AppB/AppBC9q2.c: -------------------------------------------------------------------------------- 1 | /* filereadex3r */ 2 | /* reads from Company file */ 3 | /* reads and prints sequentially */ 4 | /* reads and prints specific records */ 5 | /* does not use seek */ 6 | #define _CRT_SECURE_NO_WARNINGS 7 | #include 8 | 9 | 10 | struct Company { 11 | int CompanyID; 12 | char companyname[13]; 13 | float salesprofitpct;/* profit as a % of sales */ 14 | float totalctrypop;/* total populations countries for sales (in millions) */ 15 | float advertpct;/* Advertising as a % of sales */ 16 | float salprofpct;/* Total salaries as a % of profit */ 17 | float mwpct;/* Women as a % of total workers */ 18 | float alienwpct;/* Foreign workers as a % of total */ 19 | 20 | }; 21 | int main() 22 | { 23 | FILE *fp; 24 | 25 | struct Company s2; 26 | 27 | 28 | int numread, i; 29 | double count; /* count of women to men >40 salespercent >40 */ 30 | double percent;/* percent of women to men >40 salespercent >40 */ 31 | 32 | /* Open patients file */ 33 | 34 | fp = fopen("Companyex.bin", "r"); 35 | if(!fp) 36 | { 37 | printf("Companyex.bin file not available"); 38 | return(0); 39 | } 40 | for (i = 0;i < 17;i++) 41 | { 42 | /* Read and print each Company data from file sequentially */ 43 | 44 | 45 | fread(&s2, sizeof(s2), 1, fp); 46 | /* Print Company ID, name etc */ 47 | printf("\nCompanyID : %d", s2.CompanyID); 48 | printf("\ncompanyname : %s", s2.companyname); 49 | printf("\nprofit as a percentage of sales : %f", s2.salesprofitpct); 50 | printf("\ntotal populations countries for sales (in millions) %f ", s2.totalctrypop); 51 | printf("\nAdvertising as a percentage of sales %f ", s2.advertpct); 52 | printf("\nTotal salaries as a percentage of profit %f ", s2.salprofpct); 53 | printf("\nWomen as a percentage of total workers %f ", s2.mwpct); 54 | printf("\nForeign workers as a percentage of total %f ", s2.alienwpct); 55 | 56 | 57 | 58 | 59 | } 60 | 61 | fclose(fp); 62 | 63 | 64 | /* Re-open the Company file */ 65 | 66 | fp = fopen("Companyex.bin", "r"); 67 | for (i = 0;i < 17;i++) 68 | { 69 | /* Search the file for Company with ID of 23 */ 70 | 71 | fread(&s2, sizeof(s2), 1, fp); 72 | if (s2.CompanyID == 23) 73 | { 74 | /* Found the company. Print their name */ 75 | printf("\nCompany with ID of 23 "); 76 | printf("\nCompany Name : %s", s2.companyname); 77 | break; 78 | } 79 | } 80 | /* Go back to the beginning of the file */ 81 | 82 | 83 | rewind(fp); 84 | /* Find all Companys with women to men percent > 50 */ 85 | 86 | for (i = 0;i < 17;i++) 87 | { 88 | fread(&s2, sizeof(s2), 1, fp); 89 | if (s2.mwpct > 50) 90 | { 91 | /* Print out name of each company with women to men percent > 50*/ 92 | printf("\nwomen to men >50pc"); 93 | printf("\nCompany Name : %s", s2.companyname); 94 | 95 | } 96 | } 97 | /* Go back to the beginning of the file */ 98 | 99 | 100 | rewind(fp); 101 | 102 | /* Read and print out the first 3 Companys in the file */ 103 | 104 | numread = fread(&s2, sizeof(s2), 1, fp); 105 | printf("\nFirst 3 companies on file"); 106 | if (numread == 1) 107 | { 108 | printf("\nCompanyID : %d", s2.CompanyID); 109 | printf("\nCompany Name : %s", s2.companyname); 110 | 111 | } 112 | numread = fread(&s2, sizeof(s2), 1, fp); 113 | if (numread == 1) 114 | { 115 | 116 | printf("\nCompanyID : %d", s2.CompanyID); 117 | printf("\nCompany Name : %s", s2.companyname); 118 | 119 | } 120 | numread = fread(&s2, sizeof(s2), 1, fp); 121 | if (numread == 1) 122 | { 123 | 124 | printf("\nCompanyID : %d", s2.CompanyID); 125 | printf("\nCompany Name : %s", s2.companyname); 126 | 127 | } 128 | /* Close the file */ 129 | 130 | fclose(fp); 131 | /* Re-open the patients file */ 132 | count = 0;/* set count of percent women to men >40 salespercent >40*/ 133 | fp = fopen("Companyex.bin", "r"); 134 | for (i = 0;i < 17;i++) 135 | { 136 | /* Search count of percent women to men >40 salespercent >40 */ 137 | 138 | fread(&s2, sizeof(s2), 1, fp); 139 | if (s2.mwpct > 40.0 && s2.salesprofitpct > 40.0) 140 | { 141 | count = count + 1.0; /* Add 1 to overall count */ 142 | 143 | /* Found the company. Print their name */ 144 | 145 | printf("\nLink between women to men >40pc and salespc > 40 "); 146 | printf("\nName : %s", s2.companyname); 147 | 148 | } 149 | } 150 | /* Calculated and print percentage of women to men over 40 and salespercent over 40 */ 151 | percent = (count / 17.0)*100.0; 152 | printf("\npercent women to men >40 salespercent >40 : %f", percent); 153 | 154 | fclose(fp); 155 | } 156 | -------------------------------------------------------------------------------- /AppB/Companyex.bin: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Apress/numerical-c/e0ff82d28d2e837846b27255ccd745c00f51bd5b/AppB/Companyex.bin -------------------------------------------------------------------------------- /AppB/patientex.bin: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Apress/numerical-c/e0ff82d28d2e837846b27255ccd745c00f51bd5b/AppB/patientex.bin -------------------------------------------------------------------------------- /Ch01/addtwonos.c: -------------------------------------------------------------------------------- 1 | #define _CRT_SECURE_NO_WARNINGS 2 | #include 3 | 4 | 5 | /* Read in two integers and add them */ 6 | 7 | int main() 8 | { 9 | int this_is_a_number1 , this_is_a_number2 , total; 10 | 11 | printf( "Please enter an integer number: \n" ); 12 | scanf( "%d", &this_is_a_number1 ); 13 | printf( "You entered %d\n", this_is_a_number1 ); 14 | 15 | printf( "Please enter another number: \n" ); 16 | scanf( "%d", &this_is_a_number2 ); 17 | printf( "You entered %d\n", this_is_a_number2 ); 18 | 19 | total = this_is_a_number1 + this_is_a_number2; 20 | printf( "total is %d\n", total ); 21 | 22 | return 0; 23 | } 24 | -------------------------------------------------------------------------------- /Ch01/addtwonos2.c: -------------------------------------------------------------------------------- 1 | #define _CRT_SECURE_NO_WARNINGS 2 | #include 3 | 4 | /* Add two floating point numbers */ 5 | 6 | int main() 7 | { 8 | float this_is_a_number1 , this_is_a_number2 , total; 9 | 10 | printf( "Please enter a number:\n " ); 11 | scanf( "%f", &this_is_a_number1 ); 12 | printf( "You entered %f\n", this_is_a_number1 ); 13 | 14 | printf( "Please enter another number: \n" ); 15 | scanf( "%f", &this_is_a_number2 ); 16 | printf( "You entered %f\n", this_is_a_number2 ); 17 | 18 | total = this_is_a_number1 + this_is_a_number2; 19 | printf( "total is %f\n", total ); 20 | 21 | return 0; 22 | } 23 | -------------------------------------------------------------------------------- /Ch01/arr1D.c: -------------------------------------------------------------------------------- 1 | #include 2 | /* program to show array use */ 3 | 4 | int main() 5 | 6 | { 7 | int arr1[8]; 8 | int i; 9 | 10 | 11 | 12 | printf("enter 8 integer numbers\n"); 13 | 14 | for(i=0;i<8;i++) 15 | { 16 | scanf("%d",&arr1[i]); 17 | } 18 | printf("Your 8 numbers are \n"); 19 | 20 | for(i=0;i<8;i++) 21 | { 22 | printf("%d ",arr1[i]); 23 | } 24 | printf("\n"); 25 | 26 | 27 | 28 | 29 | } 30 | -------------------------------------------------------------------------------- /Ch01/arr1D2.c: -------------------------------------------------------------------------------- 1 | #define _CRT_SECURE_NO_WARNINGS 2 | #include 3 | /* program to show character array use */ 4 | 5 | int main() 6 | 7 | { 8 | char arr2[10]; 9 | int i; 10 | 11 | 12 | 13 | printf("enter 10 characters \n"); 14 | 15 | for(i=0;i<10;i++) 16 | { 17 | scanf("%c",&arr2[i]); 18 | } 19 | printf("Your 10 characters are \n"); 20 | 21 | for(i=0;i<10;i++) 22 | { 23 | printf("%c ",arr2[i]); 24 | } 25 | printf("\n"); 26 | 27 | 28 | 29 | 30 | } 31 | -------------------------------------------------------------------------------- /Ch01/arr2Dtest2.c: -------------------------------------------------------------------------------- 1 | #define _CRT_SECURE_NO_WARNINGS 2 | #include 3 | 4 | /* example of a 2D array test for 2 arrays*/ 5 | int main() 6 | 7 | { 8 | int arr1[8][8]; 9 | int arr2[8][8]; 10 | 11 | 12 | int i,j,k,l; 13 | 14 | printf("enter number of rows and columns of first array(max 8 rows max 8 columns) \n"); 15 | scanf("%d %d", &k, &l); 16 | if(k>8 || l>8) 17 | { 18 | printf("error - max of 8 for rows or columns\n"); 19 | 20 | } 21 | 22 | else 23 | { 24 | printf("enter array\n"); 25 | for(i=0;i8 || l>8) 53 | { 54 | printf("error - max of 8 for rows or columns\n"); 55 | 56 | } 57 | 58 | else 59 | { 60 | printf("enter array\n"); 61 | for(i=0;i 3 | 4 | /* example of a 2D array test*/ 5 | int main() 6 | 7 | { 8 | int arr1[7][8]; 9 | 10 | 11 | int i,j,k,l; 12 | 13 | printf("enter number of rows and columns (max 7 rows max 8 columns) \n"); 14 | scanf("%d %d", &k, &l); 15 | if(k>7 || l>8) 16 | { 17 | printf("error - max of 8 for rows or columns\n"); 18 | 19 | } 20 | 21 | else 22 | { 23 | printf("enter array\n"); 24 | for(i=0;i 3 | 4 | 5 | /* divide two floating point numbers */ 6 | 7 | int main() 8 | { 9 | float this_is_a_number1 , this_is_a_number2 , total; 10 | 11 | printf( "Please enter a number: \n" ); 12 | scanf( "%f", &this_is_a_number1 ); 13 | printf( "You entered %f\n", this_is_a_number1 ); 14 | 15 | printf( "Please enter another number:\n " ); 16 | scanf( "%f", &this_is_a_number2 ); 17 | printf( "You entered %f\n", this_is_a_number2 ); 18 | 19 | total = this_is_a_number1 / this_is_a_number2; 20 | printf( "quotient is %f\n", total ); 21 | 22 | return 0; 23 | } 24 | -------------------------------------------------------------------------------- /Ch01/dloop2.c: -------------------------------------------------------------------------------- 1 | #define _CRT_SECURE_NO_WARNINGS 2 | #include 3 | /* demonstrate a do loop */ 4 | main() 5 | 6 | 7 | { 8 | 9 | float this_is_a_number , total; 10 | int i; 11 | 12 | total = 0; 13 | i=0; 14 | do{ 15 | 16 | 17 | printf( "Please enter a number:\n " ); 18 | scanf( "%f", &this_is_a_number ); 19 | total = total + this_is_a_number; 20 | i++; 21 | 22 | }while( i < 10); 23 | printf("Total Sum is = %f\n",total); 24 | 25 | 26 | } -------------------------------------------------------------------------------- /Ch01/forloop2.c: -------------------------------------------------------------------------------- 1 | #define _CRT_SECURE_NO_WARNINGS 2 | #include 3 | /* demonstrate a forloop */ 4 | main() 5 | 6 | 7 | { 8 | 9 | float this_is_a_number , total; 10 | int i; 11 | 12 | total = 0; 13 | 14 | /* forloop goes round 10 times */ 15 | 16 | for(i=0;i<10;i++) 17 | { 18 | 19 | printf( "Please enter a number:\n " ); 20 | scanf( "%f", &this_is_a_number ); 21 | total = total + this_is_a_number; 22 | 23 | } 24 | printf("Total Sum is = %f\n",total); 25 | 26 | 27 | } -------------------------------------------------------------------------------- /Ch01/forloop3.c: -------------------------------------------------------------------------------- 1 | #define _CRT_SECURE_NO_WARNINGS 2 | #include 3 | /* demonstrate a nested forloop */ 4 | main() 5 | 6 | 7 | { 8 | 9 | float this_is_a_number , total; 10 | int i,j; 11 | 12 | total = 0; 13 | for(i=0;i<10;i++) 14 | { 15 | for(j=0;j<2;j++) 16 | { 17 | 18 | printf( "Please enter a number:\n " ); 19 | scanf( "%f", &this_is_a_number ); 20 | total = total + this_is_a_number; 21 | } 22 | 23 | } 24 | printf("Total Sum is = %f\n",total); 25 | 26 | 27 | } -------------------------------------------------------------------------------- /Ch01/func0.c: -------------------------------------------------------------------------------- 1 | #define _CRT_SECURE_NO_WARNINGS 2 | #include 3 | /* This code demonstrates what a function does */ 4 | /* The function here compares two numbers and says which is bigger */ 5 | /* The user enters three numbers and gets told which is bigger than which !*/ 6 | void myfunction(int a,int b); /* decaration of your function and its parameters */ 7 | int first , second, third; 8 | 9 | main() 10 | { 11 | printf( "Please enter first integer number: " ); 12 | scanf( "%d", &first ); 13 | printf( "Please enter second integer number: " ); 14 | scanf( "%d", &second ); 15 | printf( "Please enter third integer number: " ); 16 | scanf( "%d", &third ); 17 | 18 | myfunction(first , second); 19 | myfunction(first , third); 20 | myfunction(second , third); 21 | } 22 | void myfunction(int a,int b) 23 | /* the function is outside the main{} part of the program */ 24 | /* The function just compares the two parameters, a and b, and says which is greater*/ 25 | { 26 | 27 | if(a>b) 28 | printf("%d is greater than %d\n", a,b); 29 | else if (a 3 | 4 | /* example of a function*/ 5 | void printarow(int row,int cols, int arr[8][8]); 6 | int main() 7 | 8 | { 9 | int arr1[8][8]; 10 | 11 | 12 | int i,j,rows,cols; 13 | 14 | 15 | printf("enter number of rows and columns (max 8 rows max 8 columns) \n"); 16 | scanf("%d %d", &rows, &cols); 17 | if(rows>8 || cols>8) 18 | { 19 | printf("error - max of 8 for rows or columns\n"); 20 | 21 | } 22 | 23 | else 24 | { 25 | printf("enter array\n"); 26 | for(i=0;i 7 | float getmarks(float pupils[]); 8 | 9 | int main() 10 | { 11 | float pupil; 12 | /* Array with marks for class is preset in the main part of the program */ 13 | float marks[] = {10.6, 23.7, 67.9, 93.0, 64.2, 33.8 ,57.5 ,82.2 ,50.7 ,45.7}; 14 | /* Call function getmarks. The function returns the max marks which is then stored in pupil */ 15 | pupil = getmarks(marks); 16 | printf("Max mark is = %f", pupil); 17 | return 0; 18 | } 19 | 20 | float getmarks(float pupils[]) 21 | { 22 | int i; 23 | float highest; 24 | highest = 0; 25 | /* Go through all the pupils in turn and store the highest mark */ 26 | for (i = 0; i < 6; ++i) 27 | { 28 | if(highest < pupils[i]) 29 | highest=pupils[i]; 30 | 31 | } 32 | return highest; /* returns the value in highest to where the function was called */ 33 | } 34 | -------------------------------------------------------------------------------- /Ch01/getputchar.c: -------------------------------------------------------------------------------- 1 | #define _CRT_SECURE_NO_WARNINGS 2 | #include 3 | /* read and display a character */ 4 | int main () { 5 | char c; 6 | 7 | printf("Enter character: "); 8 | c = getchar(); /* read the character in */ 9 | 10 | printf("Character entered: "); 11 | putchar(c); /* write the character */ 12 | 13 | return(0); 14 | } 15 | -------------------------------------------------------------------------------- /Ch01/gototest.c: -------------------------------------------------------------------------------- 1 | #include /* Demonstrate a goto statement */ 2 | int main() 3 | { 4 | int i,testvalue; 5 | 6 | testvalue = 2; 7 | 8 | for (i=0; i<10; i++) 9 | 10 | { 11 | if(testvalue == 2) 12 | goto error; 13 | } 14 | printf("Normal Exit from forloop\n"); 15 | 16 | error: 17 | printf("testvalue is %d\n", testvalue); 18 | } 19 | -------------------------------------------------------------------------------- /Ch01/iftest.c: -------------------------------------------------------------------------------- 1 | #define _CRT_SECURE_NO_WARNINGS 2 | #include 3 | 4 | /* Example of an if operation */ 5 | int main() 6 | { 7 | int this_is_a_number; 8 | 9 | printf( "Please enter an integer between 1 and 10:\n " ); 10 | scanf( "%d", &this_is_a_number ); 11 | 12 | if (this_is_a_number <6) 13 | printf( "This number is less than 6;\n " ); 14 | 15 | 16 | printf( "Please enter an integer between 10 and 20:\n " ); 17 | scanf( "%d", &this_is_a_number ); 18 | 19 | if (this_is_a_number <16) 20 | printf( "This number is less than 16\n " ); 21 | else 22 | printf( "This number is greater than 15\n " ); 23 | 24 | return 0; 25 | } -------------------------------------------------------------------------------- /Ch01/iftest2.c: -------------------------------------------------------------------------------- 1 | #define _CRT_SECURE_NO_WARNINGS 2 | #include 3 | /* Example of an if then else if operation */ 4 | int main() 5 | { 6 | int this_is_a_number; 7 | 8 | printf( "Please enter an integer between 1 and 10:\n " ); 9 | scanf( "%d", &this_is_a_number ); 10 | 11 | if (this_is_a_number <6) 12 | printf( "This number is less than 6;\n " ); 13 | 14 | 15 | printf( "Please enter an integer between 10 and 20:\n " ); 16 | scanf( "%d", &this_is_a_number ); 17 | 18 | if (this_is_a_number <16) 19 | { 20 | printf( "This number is less than 16\n " ); 21 | } 22 | else if (this_is_a_number == 20) 23 | { 24 | printf( "This number is 20\n " ); 25 | } 26 | else 27 | { 28 | printf( "This number is greater than 15\n " ); 29 | } 30 | 31 | return 0; 32 | } -------------------------------------------------------------------------------- /Ch01/mathfunc.c: -------------------------------------------------------------------------------- 1 | #define _CRT_SECURE_NO_WARNINGS 2 | #include 3 | /* showing use of exp, log and log10 functions */ 4 | int main() 5 | { 6 | 7 | float answer, expno,natlog,lb10; 8 | 9 | /* find exponent of entered number */ 10 | 11 | printf( "exponental function:\n " ); 12 | printf( "Please enter number:\n " ); 13 | scanf( "%f", &expno ); 14 | printf( "You entered %f\n", expno ); 15 | 16 | answer = exp(expno); 17 | printf( "exponent of %f is %f\n",expno, answer ); 18 | 19 | /* find natural logarithm of entered number */ 20 | 21 | printf( "natural logarithm function:\n " ); 22 | printf( "Please enter number:\n " ); 23 | scanf( "%f", &natlog ); 24 | printf( "You entered %f\n", natlog ); 25 | answer = log(natlog); 26 | printf( "natural logarithm of %f is %f\n",natlog, answer ); 27 | 28 | 29 | /* find log to base 10 of entered number */ 30 | 31 | printf( "log to base 10 function:\n " ); 32 | printf( "Please enter number:\n " ); 33 | scanf( "%f", &lb10 ); 34 | printf( "You entered %f\n", lb10 ); 35 | answer = log10(lb10); 36 | 37 | printf( "log to base 10 of %f iis %f\n",lb10, answer ); 38 | } -------------------------------------------------------------------------------- /Ch01/mathfunc2.c: -------------------------------------------------------------------------------- 1 | #define _CRT_SECURE_NO_WARNINGS 2 | #include 3 | /* showing use of pow, sqrt and fabs functions */ 4 | int main() 5 | { 6 | 7 | float answer, pownum,power,sqroot,fabsno; 8 | 9 | /* find x raised to power y number */ 10 | 11 | printf( "power:\n " ); 12 | printf( "Please enter number:\n " ); 13 | scanf( "%f", &pownum ); 14 | printf( "You entered %f\n", pownum ); 15 | printf( "Please enter power:\n " ); 16 | scanf( "%f", &power ); 17 | printf( "You entered %f\n", power ); 18 | 19 | answer = pow(pownum,power); 20 | printf( "%f raised to power %f is %f\n",pownum,power, answer ); 21 | 22 | /* find square root of number */ 23 | 24 | printf( "square root:\n " ); 25 | printf( "Please enter number:\n " ); 26 | scanf( "%f", &sqroot ); 27 | printf( "You entered %f\n", sqroot ); 28 | 29 | answer = sqrt(sqroot); 30 | printf( "The square root of %f is %f\n",sqroot, answer ); 31 | 32 | /* find absolute value of number */ 33 | 34 | printf( "absolute value:\n " ); 35 | printf( "Please enter number:\n " ); 36 | scanf( "%f", &fabsno ); 37 | printf( "You entered %f\n", fabsno ); 38 | 39 | answer = fabs(fabsno); 40 | printf( "The absolute value of %f is %f\n",fabsno, answer ); 41 | 42 | 43 | 44 | 45 | } -------------------------------------------------------------------------------- /Ch01/multtwonos.c: -------------------------------------------------------------------------------- 1 | #define _CRT_SECURE_NO_WARNINGS 2 | #include 3 | 4 | 5 | /* multiply two floating point numbers */ 6 | 7 | int main() 8 | { 9 | float this_is_a_number1 , this_is_a_number2 , total; 10 | 11 | printf( "Please enter a number:\n " ); 12 | scanf( "%f", &this_is_a_number1 ); 13 | printf( "You entered %f\n", this_is_a_number1 ); 14 | 15 | printf( "Please enter another number: \n" ); 16 | scanf( "%f", &this_is_a_number2 ); 17 | printf( "You entered %f\n", this_is_a_number2 ); 18 | 19 | total = this_is_a_number1 * this_is_a_number2; 20 | printf( "product is %f\n", total ); 21 | 22 | return 0; 23 | } 24 | -------------------------------------------------------------------------------- /Ch01/myswitch1.c: -------------------------------------------------------------------------------- 1 | #define _CRT_SECURE_NO_WARNINGS 2 | #include 3 | /* Example of a switch operation */ 4 | int main() 5 | { 6 | int this_is_a_number; 7 | 8 | printf( "Please enter an integer between 1 and 5:\n " ); 9 | scanf( "%d", &this_is_a_number ); 10 | 11 | switch(this_is_a_number) 12 | { 13 | 14 | case 1: 15 | printf("Case1: Value is: %d", this_is_a_number); 16 | break; 17 | case 2: 18 | printf("Case2: Value is: %d", this_is_a_number); 19 | break; 20 | case 3: 21 | printf("Case3: Value is: %d", this_is_a_number); 22 | break; 23 | case 4: 24 | printf("Case4: Value is: %d", this_is_a_number); 25 | break; 26 | case 5: 27 | printf("Case5: Value is: %d", this_is_a_number); 28 | break; 29 | default: 30 | printf("Error Value is: %d", this_is_a_number); /* The number entered was not between 1 and 5 */ 31 | } 32 | return 0; 33 | } -------------------------------------------------------------------------------- /Ch01/myswitch5.c: -------------------------------------------------------------------------------- 1 | #define _CRT_SECURE_NO_WARNINGS 2 | #include 3 | /* Example of using a switch on characters */ 4 | int main() 5 | { 6 | 7 | char this_is_a_character; 8 | 9 | printf( "Please enter character a,b,c,d or e:\n " ); 10 | scanf( "%c", &this_is_a_character ); 11 | 12 | 13 | switch (this_is_a_character) 14 | { 15 | case 'a': 16 | printf("a entered"); 17 | break; 18 | case 'b': 19 | printf("b entered"); 20 | break; 21 | case 'c': 22 | printf("c entered"); 23 | break; 24 | case 'd': 25 | printf("d entered"); 26 | break; 27 | case 'e': 28 | printf("e entered"); 29 | break; 30 | default: 31 | printf("Default "); 32 | } 33 | return 0; 34 | } -------------------------------------------------------------------------------- /Ch01/sizeof.c: -------------------------------------------------------------------------------- 1 | /* Program to illustrate the use of the sizeof command */ 2 | #define _CRT_SECURE_NO_WARNINGS 3 | #include 4 | #include < limits.h > 5 | #include < math.h > 6 | 7 | int main(){ 8 | 9 | int sizeofint; 10 | unsigned int sizeofunsint; 11 | float sizeoffloat; 12 | double sizeofdouble; 13 | 14 | printf("storage size for int : %d \n", sizeof(sizeofint)); 15 | printf("storage size for uns int : %d \n", sizeof(sizeofunsint)); 16 | printf("storage size for float : %d \n", sizeof(sizeoffloat)); 17 | printf("storage size for double float: %d \n", sizeof(sizeofdouble)); 18 | 19 | return(0); 20 | 21 | 22 | } -------------------------------------------------------------------------------- /Ch01/strings.c: -------------------------------------------------------------------------------- 1 | #define _CRT_SECURE_NO_WARNINGS 2 | #include 3 | #include 4 | /* Program to demonstrate string operations strlen, strcpy, strcat, strcmp */ 5 | 6 | int main () { 7 | char borrow[7] = {'b', 'o', 'r', 'r', 'o', 'w','\0'}; 8 | char string1[32] = "This is string1"; 9 | char string2[16] = "This is string2"; 10 | char string3[16]; 11 | int len ; 12 | /* Print out the lengths of the strings */ 13 | 14 | len = strlen(string1); 15 | printf("strlen(string1) : %d\n", len ); 16 | len = strlen(string2); 17 | printf("strlen(string2) : %d\n", len ); 18 | len = strlen(string3); 19 | printf("strlen(string3) : %d\n", len ); 20 | 21 | 22 | /* copy string1 into string3 */ 23 | strcpy(string3, string1); 24 | printf("strcpy( string3, string1) : %s\n", string3 ); 25 | len = strlen(string3); 26 | printf("strlen(string3) after copy of string1 into string3 : %d\n", len ); 27 | /* Compare string1 and string3 (these should be the same)*/ 28 | if(strcmp(string1,string3) == 0) 29 | printf("strings are the same\n"); 30 | 31 | 32 | /* concatenates string1 and string2 */ 33 | strcat( string1, string2); 34 | printf("strcat( string1, string2): %s\n", string1 ); 35 | /* total length of string1 after concatenation */ 36 | len = strlen(string1); 37 | printf("strlen(string1) after cat of string2 onto string1 : %d\n", len ); 38 | 39 | 40 | 41 | 42 | printf("String as predefined quoted chars: %s\n", borrow ); 43 | 44 | 45 | return 0; 46 | } 47 | 48 | -------------------------------------------------------------------------------- /Ch01/struct0.c: -------------------------------------------------------------------------------- 1 | /* Structure example program */ 2 | #define _CRT_SECURE_NO_WARNINGS 3 | #include 4 | 5 | /* define the structure */ 6 | struct Student { 7 | int id; 8 | char name[16]; 9 | float percent; 10 | }; 11 | 12 | int main() { 13 | /* define two data locations of type "student" */ 14 | struct Student s1, s2; 15 | 16 | /* Assign values to the s1 structure */ 17 | 18 | s1.id = 56; 19 | strcpy( s1.name, "Rob Smith"); 20 | s1.percent = 67.400000; 21 | 22 | /* Print out structure s1 */ 23 | 24 | printf("\nid : %d", s1.id); 25 | printf("\nName : %s", s1.name); 26 | printf("\nPercent : %f", s1.percent); 27 | 28 | /* Assign values to the s2 structure */ 29 | 30 | s2.id = 73; 31 | strcpy( s2.name, "Mary Gallagher"); 32 | s2.percent = 93.800000; 33 | 34 | /* Print out structure s1 */ 35 | 36 | printf("\nid : %d", s2.id); 37 | printf("\nName : %s", s2.name); 38 | printf("\nPercent : %f", s2.percent); 39 | 40 | 41 | return (0); 42 | } 43 | -------------------------------------------------------------------------------- /Ch01/struct5.c: -------------------------------------------------------------------------------- 1 | /* Structure example program (extended structure)*/ 2 | #define _CRT_SECURE_NO_WARNINGS 3 | #include 4 | 5 | /* define the structure */ 6 | struct Student { 7 | int id; 8 | char name[16]; 9 | float percent; 10 | }; 11 | 12 | int main() { 13 | /* define 5 data locations of type "student" */ 14 | 15 | struct Student year9[5]; 16 | 17 | /* Assign values to the s1 structure */ 18 | 19 | year9[0].id = 56; 20 | strcpy( year9[0].name, "Rob Smith"); 21 | year9[0].percent = 67.400000; 22 | 23 | /* Print out structure s1 */ 24 | 25 | printf("\nid : %d", year9[0].id); 26 | printf("\nName : %s", year9[0].name); 27 | printf("\nPercent : %f", year9[0].percent); 28 | 29 | /* Assign values to the s2 structure */ 30 | 31 | year9[1].id = 73; 32 | strcpy(year9[1].name, "Mary Gallagher"); 33 | year9[1].percent = 93.800000; 34 | 35 | /* Print out structure s1 */ 36 | 37 | printf("\nid : %d", year9[1].id); 38 | printf("\nName : %s", year9[1].name); 39 | printf("\nPercent : %f", year9[1].percent); 40 | 41 | 42 | return (0); 43 | } 44 | -------------------------------------------------------------------------------- /Ch01/trig.c: -------------------------------------------------------------------------------- 1 | #define _CRT_SECURE_NO_WARNINGS 2 | #include 3 | #include 4 | /* Illustration of the common trigonometric functions */ 5 | 6 | int main() 7 | { 8 | #define PI 3.14159265 9 | 10 | float angle , radianno , answer; 11 | 12 | /* The cosine function */ 13 | 14 | printf( "cosine function:\n " ); 15 | printf( "Please enter angle in degrees:\n " ); 16 | scanf( "%f", &angle ); 17 | printf( "You entered %f\n", angle ); 18 | radianno = angle*(2*PI/360); 19 | answer = cos(radianno); 20 | printf( "cos of %f is %f\n",angle, answer ); 21 | 22 | /* The sine function */ 23 | 24 | printf( "sine function:\n " ); 25 | printf( "Please enter angle in degrees:\n " ); 26 | scanf( "%f", &angle ); 27 | printf( "You entered %f\n", angle ); 28 | radianno = angle*(2*PI/360); 29 | answer = sin(radianno); 30 | printf( "sin of %f is %f\n",angle, answer ); 31 | 32 | /* The tangent function */ 33 | 34 | printf( "tangent function:\n " ); 35 | printf( "Please enter angle in degrees:\n " ); 36 | scanf( "%f", &angle ); 37 | printf( "You entered %f\n", angle ); 38 | radianno = angle*(2*PI/360); 39 | answer = tan(radianno); 40 | printf( "tan of %f is %f\n",angle, answer ); 41 | 42 | return 0; 43 | 44 | 45 | 46 | 47 | 48 | 49 | } 50 | -------------------------------------------------------------------------------- /Ch01/trig2.c: -------------------------------------------------------------------------------- 1 | #define _CRT_SECURE_NO_WARNINGS 2 | #include 3 | 4 | int main() 5 | { 6 | #define PI 3.14159265 7 | 8 | float angle , radianno , answer, arccos, arcsin, arctan; 9 | 10 | /* the arccos function */ 11 | 12 | printf( "arccos function:\n " ); 13 | printf( "Please enter arccos:\n " ); 14 | scanf( "%f", &arccos ); 15 | printf( "You entered %f\n", arccos ); 16 | radianno = acos(arccos); 17 | answer = radianno*(360/(2*PI)); 18 | printf( "arccos of %f in degrees is %f\n",arccos, answer ); 19 | 20 | /* the arcsin function */ 21 | 22 | printf( "arcsin function:\n " ); 23 | printf( "Please enter arcsin:\n " ); 24 | scanf( "%f", &arcsin ); 25 | printf( "You entered %f\n", arcsin ); 26 | radianno = asin(arcsin); 27 | answer = radianno*(360/(2*PI)); 28 | printf( "arcsin of %f in degrees is %f\n",arcsin, answer ); 29 | 30 | /* the arctan function */ 31 | 32 | printf( "arctan function:\n " ); 33 | printf( "Please enter arctan:\n " ); 34 | scanf( "%f", &arctan ); 35 | printf( "You entered %f\n", arctan ); 36 | radianno = atan(arctan); 37 | answer = radianno*(360/(2*PI)); 38 | printf( "arctan of %f in degrees is %f\n",arctan, answer ); 39 | 40 | 41 | 42 | return 0; 43 | 44 | 45 | 46 | 47 | 48 | 49 | } -------------------------------------------------------------------------------- /Ch02/quad1.c: -------------------------------------------------------------------------------- 1 | /*quad1 - quadratic solver with complex numbers*/ 2 | #define _CRT_SECURE_NO_WARNINGS 3 | #include 4 | #include 5 | main() 6 | { 7 | 8 | double a, b, c, xra, xia, xrb, xib, xa, xb; 9 | 10 | /* prompt and read in coefficients of x^2,x and constant */ 11 | printf("enter A value"); 12 | scanf("%lf", &a); 13 | printf("enter B value"); 14 | scanf("%lf", &b); 15 | printf("enter C value"); 16 | scanf("%lf", &c); 17 | if (pow(b, 2) < 4 * a*c) /* test for complex root */ 18 | { 19 | /* complex root */ 20 | /* switch b^2 and 4ac to find the positive root then add i to the answer*/ 21 | printf("complex root\n"); 22 | xra = -b / (2 * a); 23 | xia = sqrt((4 * a*c) - (pow(b, 2))) / (2 * a); 24 | xrb = -b / (2 * a); 25 | xib = -sqrt((4 * a*c) - (pow(b, 2))) / (2 * a); 26 | printf("Roots are %lf +%lfi and %lf - %lfi", xra, xia, xra, xia); 27 | } 28 | else 29 | { 30 | /* real root */ 31 | xa = (-b + sqrt(pow(b, 2) - (4 * a*c))) / (2 * a); 32 | xb = (-b - sqrt(pow(b, 2) - (4 * a*c))) / (2 * a); 33 | printf("Roots are %lf and %lf", xa, xb); 34 | } 35 | 36 | } 37 | -------------------------------------------------------------------------------- /Ch02/quad3.c: -------------------------------------------------------------------------------- 1 | /*quad3 - first attempt at quadratic solver*/ 2 | #define _CRT_SECURE_NO_WARNINGS 3 | #include 4 | #include 5 | main() 6 | { 7 | double a, b, c,xa, xb; 8 | 9 | /* prompt and read in coefficients of x^2,x and constant */ 10 | printf("enter a value"); 11 | scanf("%lf", &a); 12 | printf("enter b value"); 13 | scanf("%lf", &b); 14 | printf("enter c value"); 15 | scanf("%lf", &c); 16 | if (pow(b, 2) < 4 * a*c) /* test for real root */ 17 | { 18 | /* not real root */ 19 | printf("Not a real root"); 20 | } 21 | else 22 | { 23 | /* real root */ 24 | xa = (-b + sqrt(pow(b, 2) - (4 * a*c))) / (2 * a); 25 | xb = (-b - sqrt(pow(b, 2) - (4 * a*c))) / (2 * a); 26 | printf("Roots are %lf and %lf", xa, xb); 27 | } 28 | 29 | } 30 | -------------------------------------------------------------------------------- /Ch02/trialimp1.c: -------------------------------------------------------------------------------- 1 | /* trialimp */ 2 | /* program uses x^2 -8x - 13 =0*/ 3 | /* equation is solved by trial & improvement */ 4 | #define _CRT_SECURE_NO_WARNINGS 5 | #include 6 | #include 7 | main() 8 | { 9 | 10 | 11 | float p2, p1, lower, upper; 12 | 13 | double testhigh, testlow, testvalue, middle; 14 | float constval; 15 | int i, iterations; 16 | 17 | 18 | 19 | /* Your curve should cross the x-axis */ 20 | /* Here, your lower value is a value of x where your curve is below the x-axis */ 21 | /* Your upper value is a value of x where your curve is above the x-axis */ 22 | /* Both values should be close to where your curve crosses the x-axis */ 23 | 24 | printf("enter initial lower value"); 25 | scanf("%f", &lower); 26 | printf("enter initial upper value"); 27 | scanf("%f", &upper); 28 | printf("enter number of iterations"); 29 | scanf("%d", &iterations); 30 | 31 | /* Preset constant values */ 32 | p2 = 1;/* coefficient of power of x squared */ 33 | p1 = -8;/* coefficient of x */ 34 | constval = -13; /* numeric constant*/ 35 | 36 | testlow = lower; 37 | testhigh = upper; 38 | printf("Equation is:-%f x**2 %f x %f=0\n", p2, p1, constval); 39 | /*printf("%f x**2 %f x= %f\n", p2,p1,constval);*/ 40 | for (i = 0;i < iterations;i++) 41 | { 42 | 43 | middle = (testhigh + testlow) / 2; 44 | testvalue = pow(middle, 2) - 8 * middle - 13; 45 | 46 | if (testvalue == 0) 47 | { 48 | printf("x is %f", middle); 49 | return(0); 50 | } 51 | if (testvalue > 0) 52 | { 53 | testhigh = middle;/* replace upper value with this one */ 54 | } 55 | else 56 | { 57 | testlow = middle; ;/* replace lower value with this one */ 58 | 59 | } 60 | 61 | } 62 | printf("x is %f", middle); 63 | 64 | } 65 | 66 | -------------------------------------------------------------------------------- /Ch02/trialimp2.c: -------------------------------------------------------------------------------- 1 | /* trialimp */ 2 | /* program uses x^3 +2x^2 - x =0*/ 3 | /* equation is solved by trial & improvement */ 4 | #define _CRT_SECURE_NO_WARNINGS 5 | #include 6 | #include 7 | main() 8 | { 9 | 10 | 11 | float p3, p2, p1, lower, upper; 12 | 13 | double testhigh, testlow, testvalue, middle; 14 | float constval; 15 | int i, iterations; 16 | 17 | 18 | 19 | /* Your curve should cross the x-axis */ 20 | /* Here, your lower value is a value of x where your curve is below the x-axis */ 21 | /* Your upper value is a value of x where your curve is above the x-axis */ 22 | /* Both values should be close to where your curve crosses the x-axis */ 23 | 24 | printf("enter initial lower value"); 25 | scanf("%f", &lower); 26 | printf("enter initial upper value"); 27 | scanf("%f", &upper); 28 | printf("enter number of iterations"); 29 | scanf("%d", &iterations); 30 | 31 | /* Preset constant values */ 32 | p3 = 1;/* coefficient of power of x power 3 */ 33 | p2 = 2;/* coefficient of power of x squared */ 34 | p1 = -1;/* coefficient of x */ 35 | constval = 0; /* numeric constant*/ 36 | 37 | testlow = lower; 38 | testhigh = upper; 39 | /*printf("Equation is:-%f x**2 %f x %f=0\n", p3,p2,constval);*/ 40 | 41 | printf("Equation is:%f x**3 %f x**2 %f x= 0\n", p3, p2, p1); 42 | for (i = 0;i < iterations;i++) 43 | { 44 | 45 | 46 | middle = (testhigh + testlow) / 2; 47 | testvalue = pow(middle, 3) + 2 * pow(middle, 2) - middle; 48 | 49 | if (testvalue == 0) 50 | { 51 | printf("x is %f", middle); 52 | /*exit(1);*/ 53 | return(0);/* test */ 54 | } 55 | if (testvalue > 0) 56 | { 57 | testhigh = middle; ;/* replace upper value with this one */ 58 | 59 | } 60 | else 61 | { 62 | testlow = middle; ;/* replace lower value with this one */ 63 | 64 | } 65 | 66 | } 67 | printf("x is %f", middle); 68 | } 69 | -------------------------------------------------------------------------------- /Ch02/trialimp3.c: -------------------------------------------------------------------------------- 1 | /* trialimp */ 2 | /* user enters an equation.*/ 3 | /* equation is solved by trial & improvement */ 4 | #define _CRT_SECURE_NO_WARNINGS 5 | #include 6 | #include 7 | main() 8 | { 9 | 10 | 11 | float p6, p5, p4, p3, p2, p1, lower, upper; 12 | 13 | double testhigh, testlow, testvalue, middle; 14 | float constval; 15 | int i, iterations, highpower; 16 | 17 | /* Preset constant values to zero */ 18 | 19 | p6 = 0.0; 20 | p5 = 0.0; 21 | p4 = 0.0; 22 | p3 = 0.0; 23 | p2 = 0.0; 24 | p1 = 0.0; 25 | constval = 0.0; 26 | 27 | /* Enter the highest power of x in your equation */ 28 | /* (so that you don't have to enter values if you don't have higher powers) */ 29 | 30 | printf("enter highest power of x (max 6)"); 31 | scanf("%d", &highpower); 32 | 33 | /* Enter the coefficient for each of your powers */ 34 | 35 | switch (highpower) { 36 | case 6: 37 | printf("enter coefficient of x power 6(-9 to 9)"); 38 | scanf("%f", &p6); 39 | 40 | case 5: 41 | printf("enter coefficient of x power 5(-9 to 9)"); 42 | scanf("%f", &p5); 43 | case 4: 44 | printf("enter coefficient of x power 4(-90 to 9)"); 45 | scanf("%f", &p4); 46 | case 3: 47 | printf("enter coefficient of x power 3(-9 to 9)"); 48 | scanf("%f", &p3); 49 | case 2: 50 | printf("enter coefficient of x power 2(-9 to +9)"); 51 | scanf("%f", &p2); 52 | case 1: 53 | printf("enter coefficient of x (-9 to +9)"); 54 | scanf("%f", &p1); 55 | case 0: 56 | printf("enter numeric constant (-999 to +999)"); 57 | scanf("%f", &constval); 58 | default: 59 | printf("default"); 60 | 61 | } 62 | /* Display the equation you have entered */ 63 | 64 | printf("Equation is:- "); 65 | printf("%f x**6+%f x**5+%f x**4+%f x**3 +%f x**2 %f x= %f\n", p6, p5, p4, p3, p2, p1, constval); 66 | 67 | /* Your curve should cross the x-axis */ 68 | /* Here, your lower value is a value of x where your curve is below the x-axis */ 69 | /* Your upper value is a value of x where your curve is above the x-axis */ 70 | /* Both values should be close to where your curve crosses the x-axis */ 71 | 72 | printf("enter initial lower value"); 73 | scanf("%f", &lower); 74 | printf("enter initial upper value"); 75 | scanf("%f", &upper); 76 | printf("enter number of iterations"); 77 | scanf("%d", &iterations); 78 | 79 | 80 | testlow = lower; 81 | testhigh = upper; 82 | 83 | for (i = 0;i < iterations;i++) 84 | { 85 | 86 | 87 | middle = (testhigh + testlow) / 2; 88 | testvalue = p6 * pow(middle, 6) + p5 * pow(middle, 5) + p4 * pow(middle, 4) + p3 * pow(middle, 3) + p2 * pow(middle, 2) + p1 * middle + constval; 89 | 90 | if (testvalue == 0) 91 | { 92 | printf("x is %f", middle); 93 | return(0); 94 | } 95 | if (testvalue > 0) 96 | { 97 | testhigh = middle; 98 | } 99 | else 100 | { 101 | testlow = middle; 102 | } 103 | 104 | } 105 | printf("x is %f", middle); 106 | 107 | } 108 | -------------------------------------------------------------------------------- /Ch03/simpsons.c: -------------------------------------------------------------------------------- 1 | /*simpsons - to integrate sqrt(4+x^2)*/ 2 | #define _CRT_SECURE_NO_WARNINGS 3 | #include 4 | #include 5 | main() 6 | { 7 | 8 | double lower, upper; 9 | 10 | double stripwidth, xposn; 11 | double mideven, midodd, width, area; 12 | double yarr[10002]; 13 | int i, strips; 14 | 15 | /* prompt and read in limits and number of strips */ 16 | 17 | 18 | printf("enter lower limit"); 19 | scanf("%lf", &lower); 20 | printf("enter upper limit"); 21 | scanf("%lf", &upper); 22 | printf("enter number of strips"); 23 | scanf("%d", &strips); 24 | 25 | if (strips > 10000) 26 | { 27 | printf("Number of strips exceeds 10000"); 28 | return(0); 29 | } 30 | 31 | width = upper - lower; 32 | stripwidth = width / strips; 33 | yarr[0] = sqrt(4 + pow(lower, 2)); /* First ordinate */ 34 | yarr[strips] = sqrt(4 + pow(upper, 2)); /* Last ordinate */ 35 | 36 | mideven = 0; 37 | midodd = 0; 38 | 39 | /* Process odd-numbered strips */ 40 | for (i = 1;i < strips;i++) 41 | { 42 | 43 | xposn = lower + (i*stripwidth); 44 | yarr[i] = sqrt(4 + pow(xposn, 2)); 45 | midodd = midodd + yarr[i]; 46 | i++; 47 | 48 | } 49 | /* Process even-numbered strips */ 50 | for (i = 2;i < strips;i++) 51 | { 52 | 53 | xposn = lower + (i*stripwidth); 54 | yarr[i] = sqrt(4 + pow(xposn, 2)); 55 | mideven = mideven + yarr[i]; 56 | i++; 57 | 58 | } 59 | 60 | /* Process Simpson's formula */ 61 | area = (0.3333)*stripwidth*((yarr[0] + yarr[strips]) + 4 * midodd + 2 * mideven); 62 | 63 | printf("Area is %lf\n", area); 64 | 65 | } 66 | 67 | -------------------------------------------------------------------------------- /Ch03/trapezium1.c: -------------------------------------------------------------------------------- 1 | /*trapezium - first attempt at trapezium method*/ 2 | #define _CRT_SECURE_NO_WARNINGS 3 | #include 4 | #include 5 | main() 6 | { 7 | 8 | double p6, p5, p4, p3, p2, p1, p0, lower, upper; 9 | 10 | double stripwidth, xposn; 11 | double middlevalues, zero, width, area; 12 | double yarr[10002]; /* Array to store lengths of sides of each trapezium */ 13 | int i, strips, highpower; 14 | 15 | /* Preset constant values to zzero */ 16 | 17 | p6 = 0.0; 18 | p5 = 0.0; 19 | p4 = 0.0; 20 | p3 = 0.0; 21 | p2 = 0.0; 22 | p1 = 0.0; 23 | p0 = 0.0; 24 | /* Enter the highest power of x in your equation */ 25 | /* (so that you don't have to enter values if you don't have higher powers) */ 26 | 27 | printf("enter highest power of x (max 6)"); 28 | scanf("%d", &highpower); 29 | 30 | /* Enter the coefficient for each of your powers */ 31 | 32 | switch (highpower) { 33 | case 6: 34 | printf("enter coefficient of x power 6(-9 to 9)"); 35 | scanf("%lf", &p6); 36 | case 5: 37 | printf("enter coefficient of x power 5(-9 to 9)"); 38 | scanf("%lf", &p5); 39 | case 4: 40 | printf("enter coefficient of x power 4(-90 to 9)"); 41 | scanf("%lf", &p4); 42 | case 3: 43 | printf("enter coefficient of x power 3(-9 to 9)"); 44 | scanf("%lf", &p3); 45 | case 2: 46 | printf("enter coefficient of x power 2(-9 to +9)"); 47 | scanf("%lf", &p2); 48 | case 1: 49 | printf("enter coefficient of x (-9 to +9)"); 50 | scanf("%lf", &p1); 51 | case 0: 52 | printf("enter numeric constant (-999 to +999)"); 53 | scanf("%lf", &p0); 54 | default: 55 | printf("default"); 56 | 57 | } 58 | 59 | /* Display the equation you have entered */ 60 | 61 | printf("Equation is:- "); 62 | printf("%lf x**6+%lf x**5+%lf x**4+%lf x**3 +%lf x**2 %lf x= %lf\n", p6, p5, p4, p3, p2, p1, p0); 63 | 64 | 65 | 66 | printf("enter lower limit");/* the lower x value for your integration */ 67 | scanf("%lf", &lower); 68 | printf("enter upper limit");/* the upper x value for your integration */ 69 | scanf("%lf", &upper); 70 | printf("enter number of strips (max 1000)");/* how many trapezia are you splitting your area into */ 71 | scanf("%d", &strips); 72 | if (strips > 10000) 73 | { 74 | printf("Number of strips exceeds 10000"); 75 | return(0); 76 | } 77 | zero = 0; 78 | width = upper - lower;/* overall x-distance between limits of our integration */ 79 | stripwidth = width / strips; /* stripwidth is perpendicular height of each trapezium */ 80 | /* yarr[1] contains the First side from our trapezium area formula */ 81 | yarr[1] = p6 * pow(lower, 6) + p5 * pow(lower, 5) + p4 * pow(lower, 4) + p3 * pow(lower, 3) + p2 * pow(lower, 2) + (p1*lower) + p0; 82 | /* yarr[strips+1] contains the Last side from our trapezium area formula */ 83 | yarr[strips + 1] = p6 * pow(upper, 6) + p5 * pow(upper, 5) + p4 * pow(upper, 4) + (p3*pow(upper, 3)) + (p2*pow(upper, 2)) + (p1*upper) + p0; 84 | middlevalues = zero; 85 | 86 | /* forloop loops round yarr to add all the values of the sides of the trapezia in the formula */ 87 | for (i = 1;i < strips;i++) 88 | { 89 | 90 | xposn = lower + (i*stripwidth); 91 | yarr[i + 1] = (p6*pow(xposn, 6) + p5 * pow(xposn, 5) + p4 * pow(xposn, 4) + p3 * pow(xposn, 3)) + (p2*pow(xposn, 2)) + (p1*xposn) + p0; 92 | middlevalues = middlevalues + yarr[i + 1]; 93 | } 94 | /* Now collect the first side, the last side and 2x the middle sides and multiply by 0.5x the stripwidth (as in the formula) */ 95 | 96 | area = 0.5*stripwidth*(yarr[1] + 2 * middlevalues + yarr[strips + 1]); 97 | /* Now we have the area */ 98 | 99 | printf("Area is %lf\n", area); 100 | 101 | } 102 | -------------------------------------------------------------------------------- /Ch03/trapezium2.c: -------------------------------------------------------------------------------- 1 | /*trapezium - trapezium method using inverse functions */ 2 | #define _CRT_SECURE_NO_WARNINGS 3 | #include 4 | #include 5 | main() 6 | { 7 | 8 | double p6, p5, p4, p3, p2, p1, p0, lower, upper; 9 | 10 | double stripwidth, xposn; 11 | double middlevalues, zero, width, area; 12 | double yarr[10002]; /* Array to store lengths of sides of each trapezium */ 13 | int i, strips, highpower; 14 | 15 | 16 | /* Preset constant values to zzero */ 17 | 18 | p6 = 0.0; 19 | p5 = 0.0; 20 | p4 = 0.0; 21 | p3 = 0.0; 22 | p2 = 0.0; 23 | p1 = 0.0; 24 | p0 = 0.0; 25 | 26 | 27 | /* Enter the highest inverse power of x in your equation */ 28 | /* (so that you don't have to enter values if you don't have higher powers) */ 29 | 30 | printf("enter highest inverse power of x (max 6)"); 31 | scanf("%d", &highpower); 32 | 33 | /* Enter the coefficient for each of your powers */ 34 | 35 | switch (highpower) { 36 | 37 | case 6: 38 | printf("enter coefficient of inverse x power 6(-9 to 9)"); 39 | scanf("%lf", &p6); 40 | case 5: 41 | printf("enter coefficient of inverse x power 5(-9 to 9)"); 42 | scanf("%lf", &p5); 43 | case 4: 44 | printf("enter coefficient of inverse x power 4(-90 to 9)"); 45 | scanf("%lf", &p4); 46 | case 3: 47 | printf("enter coefficient of inverse x power 3(-9 to 9)"); 48 | scanf("%lf", &p3); 49 | case 2: 50 | printf("enter coefficient of inverse x power 2(-9 to +9)"); 51 | scanf("%lf", &p2); 52 | case 1: 53 | printf("enter coefficient of inverse x (-9 to +9)"); 54 | scanf("%lf", &p1); 55 | case 0: 56 | printf("enter numeric constant (-999 to +999)"); 57 | scanf("%lf", &p0); 58 | default: 59 | printf("default"); 60 | 61 | } 62 | 63 | /* Display the equation you have entered */ 64 | 65 | printf("Equation is:- "); 66 | printf("%lf x**-6+%lf x**-5+%lf x**-4+%lf x**-3 +%lf x**-2 %lf x**-1= %lf\n", p6, p5, p4, p3, p2, p1, p0); 67 | 68 | 69 | 70 | printf("enter lower limit");/* the lower x value for your integration */ 71 | scanf("%lf", &lower); 72 | printf("enter upper limit");/* the upper x value for your integration */ 73 | scanf("%lf", &upper); 74 | printf("enter number of strips (max 1000)");/* how many trapezia are you splitting your area into */ 75 | scanf("%d", &strips); 76 | if (strips > 10000) 77 | { 78 | printf("Number of strips exceeds 10000"); 79 | return(0); 80 | } 81 | zero = 0; 82 | width = upper - lower;/* overall x-distance between limits of our integration */ 83 | stripwidth = width / strips; /* stripwidth is perpendicular height of each trapezium */ 84 | /* yarr[1] contains the First side from our trapezium area formula */ 85 | yarr[1] = p6 * pow(lower, -6) + p5 * pow(lower, -5) + p4 * pow(lower, -4) + p3 * pow(lower, -3) + p2 * pow(lower, -2) + p1 * pow(lower, -1) + p0; 86 | /* yarr[strips+1] contains the Last side from our trapezium area formula */ 87 | yarr[strips + 1] = p6 * pow(upper, -6) + p5 * pow(upper, -5) + p4 * pow(upper, -4) + (p3*pow(upper, -3)) + (p2*pow(upper, -2)) + p1 * pow(upper, -1) + p0; 88 | middlevalues = zero; 89 | 90 | /* forloop loops round yarr to add all the values of the sides of the trapezia in the formula */ 91 | for (i = 1;i < strips;i++) 92 | { 93 | 94 | xposn = lower + (i*stripwidth); 95 | yarr[i + 1] = p6 * pow(xposn, -6) + p5 * pow(xposn, -5) + p4 * pow(xposn, -4) + (p3*pow(xposn, -3)) + (p2*pow(xposn, -2)) + p1 * pow(xposn, -1) + p0; 96 | middlevalues = middlevalues + yarr[i + 1]; 97 | } 98 | 99 | area = 0.5*stripwidth*(yarr[1] + 2 * middlevalues + yarr[strips + 1]); 100 | 101 | printf("Area is %lf\n", area); 102 | 103 | } 104 | -------------------------------------------------------------------------------- /Ch03/trapezium3.c: -------------------------------------------------------------------------------- 1 | /*trapezium - trapezium method using a mix of direct and inverse functions */ 2 | #define _CRT_SECURE_NO_WARNINGS 3 | #include 4 | #include 5 | main() 6 | { 7 | double p6, p5, p4, p3, p2, p1, p0, lower, upper; 8 | double ip6, ip5, ip4, ip3, ip2, ip1, ip0; 9 | double stripwidth, xposn; 10 | double middlevalues, zero, width, area; 11 | double yarr[10002]; /* Array to store lengths of sides of each trapezium */ 12 | int i, strips, highpower; 13 | 14 | /* Preset constant values to zzero */ 15 | 16 | p6 = 0.0; 17 | p5 = 0.0; 18 | p4 = 0.0; 19 | p3 = 0.0; 20 | p2 = 0.0; 21 | p1 = 0.0; 22 | p0 = 0.0; 23 | ip6 = 0.0; 24 | ip5 = 0.0; 25 | ip4 = 0.0; 26 | ip3 = 0.0; 27 | ip2 = 0.0; 28 | ip1 = 0.0; 29 | ip0 = 0.0; 30 | 31 | 32 | /* Enter the highest inverse power of x in your equation */ 33 | /* (so that you don't have to enter values if you don't have higher powers) */ 34 | 35 | printf("enter highest inverse power of x (max 4)"); 36 | scanf("%d", &highpower); 37 | 38 | /* Enter the coefficient for each of your powers */ 39 | 40 | switch (highpower) { 41 | 42 | case 4: 43 | printf("enter coefficient of inverse x power 4(-90 to 9)"); 44 | scanf("%lf", &ip4); 45 | case 3: 46 | printf("enter coefficient of inverse x power 3(-9 to 9)"); 47 | scanf("%lf", &ip3); 48 | case 2: 49 | printf("enter coefficient of inverse x power 2(-9 to +9)"); 50 | scanf("%lf", &ip2); 51 | case 1: 52 | printf("enter coefficient of inverse x (-9 to +9)"); 53 | scanf("%lf", &ip1); 54 | case 0: 55 | printf("enter numeric constant (-999 to +999)"); 56 | scanf("%lf", &ip0); 57 | default: 58 | printf("default"); 59 | 60 | } 61 | /* Enter the highest power of x in your equation */ 62 | /* (so that you don't have to enter values if you don't have higher powers) */ 63 | 64 | printf("enter highest power of x (max 4)"); 65 | scanf("%d", &highpower); 66 | 67 | /* Enter the coefficient for each of your powers */ 68 | 69 | switch (highpower) { 70 | 71 | case 4: 72 | printf("enter coefficient of x power 4(-90 to 9)"); 73 | scanf("%lf", &p4); 74 | case 3: 75 | printf("enter coefficient of x power 3(-9 to 9)"); 76 | scanf("%lf", &p3); 77 | case 2: 78 | printf("enter coefficient of x power 2(-9 to +9)"); 79 | scanf("%lf", &p2); 80 | case 1: 81 | printf("enter coefficient of x (-9 to +9)"); 82 | scanf("%lf", &p1); 83 | case 0: 84 | printf("enter numeric constant (-999 to +999)"); 85 | scanf("%lf", &p0); 86 | default: 87 | printf("default"); 88 | 89 | } 90 | 91 | /* Display the equation you have entered */ 92 | 93 | printf("Equation is:- "); 94 | printf("%lf x**-4+%lf x**-3 +%lf x**-2 %lf x**-1= %lf\n", p4, p3, p2, p1, p0); 95 | printf("%lf x**-4+%lf x**-3 +%lf x**-2 %lf x**-1= %lf\n", ip4, ip3, ip2, ip1, ip0); 96 | 97 | printf("enter lower limit");/* the lower x value for your integration */ 98 | scanf("%lf", &lower); 99 | printf("enter upper limit");/* the upper x value for your integration */ 100 | scanf("%lf", &upper); 101 | printf("enter number of strips (max 1000)");/* how many trapezia are you splitting your area into */ 102 | scanf("%d", &strips); 103 | if (strips > 10000) 104 | { 105 | printf("Number of strips exceeds 10000"); 106 | return(0); 107 | } 108 | zero = 0; 109 | width = upper - lower;/* overall x-distance between limits of our integration */ 110 | stripwidth = width / strips; /* stripwidth is perpendicular height of each trapezium */ 111 | /* yarr[1] contains the First side from our trapezium area formula */ 112 | yarr[1] = p4 * pow(lower, 4) + p3 * pow(lower, 3) + p2 * pow(lower, 2) + p1 * pow(lower, 1) + p0 + ip4 * pow(lower, -4) + ip3 * pow(lower, -3) + ip2 * pow(lower, -2) + ip1 * pow(lower, -1); 113 | /* yarr[strips+1] contains the Last side from our trapezium area formula */ 114 | yarr[strips + 1] = p4 * pow(upper, 4) + (p3*pow(upper, 3)) + (p2*pow(upper, 2)) + p1 * pow(upper, 1) + p0 + p4 * pow(upper, -4) + (p3*pow(upper, -3)) + (p2*pow(upper, -2)) + p1 * pow(upper, -1); 115 | middlevalues = zero; 116 | 117 | /* forloop loops round yarr to add all the values of the sides of the trapezia in the formula */ 118 | for (i = 1;i < strips;i++) 119 | { 120 | xposn = lower + (i*stripwidth); 121 | yarr[i + 1] = p4 * pow(xposn, 4) + (p3*pow(xposn, 3)) + (p2*pow(xposn, 2)) + p1 * pow(xposn, 1) + p0 + ip4 * pow(xposn, -4) + (ip3*pow(xposn, -3)) + (ip2*pow(xposn, -2)) + ip1 * pow(xposn, -1); 122 | middlevalues = middlevalues + yarr[i + 1]; 123 | } 124 | 125 | area = 0.5*stripwidth*(yarr[1] + 2 * middlevalues + yarr[strips + 1]); 126 | printf("Area is %lf\n", area); 127 | } 128 | T -------------------------------------------------------------------------------- /Ch03/trapezium4.c: -------------------------------------------------------------------------------- 1 | /*trapezium - trapezium method showing problem with negative areas */ 2 | #define _CRT_SECURE_NO_WARNINGS 3 | #include 4 | #include 5 | main() 6 | { 7 | 8 | double lowerx, upperx; 9 | double PI; 10 | 11 | double stripwidth, xposn; 12 | double middlevalues, zero, width, area; 13 | double yarr[10002]; 14 | int i, strips; 15 | 16 | double lowangle, uppangle, lowradianno, uppradianno; 17 | 18 | /* Preset constant values */ 19 | 20 | PI = 3.141592654; 21 | zero = 0; 22 | printf("Please enter lower angle in degrees:\n "); 23 | scanf("%lf", &lowangle); 24 | printf("You entered %f\n", lowangle); 25 | 26 | lowradianno = lowangle * (2 * PI / 360); 27 | 28 | printf("Please enter upper angle in degrees:\n "); 29 | scanf("%lf", &uppangle); 30 | printf("You entered %lf\n", uppangle); 31 | uppradianno = uppangle * (2 * PI / 360); 32 | 33 | printf("enter number of strips"); 34 | scanf("%d", &strips); 35 | 36 | if (strips > 10000) 37 | { 38 | printf("Number of strips exceeds 10000"); 39 | return(0); 40 | } 41 | width = uppradianno - lowradianno; 42 | stripwidth = width / strips; 43 | 44 | lowerx = sin(lowradianno); 45 | yarr[1] = lowerx; 46 | upperx = sin(uppradianno); 47 | yarr[strips + 1] = upperx; 48 | 49 | middlevalues = zero; 50 | 51 | for (i = 1;i < strips;i++) 52 | { 53 | xposn = sin(lowradianno + (i*stripwidth)); 54 | 55 | yarr[i + 1] = xposn; 56 | 57 | middlevalues = middlevalues + yarr[i + 1]; 58 | } 59 | 60 | area = 0.5*stripwidth*(yarr[1] + 2 * middlevalues + yarr[strips + 1]); 61 | 62 | printf("Area is %lf\n", area); 63 | 64 | } 65 | -------------------------------------------------------------------------------- /Ch04/montecarlo.c: -------------------------------------------------------------------------------- 1 | /* Montecarlo */ 2 | /* integration using monte carlo */ 3 | /* by counting relative areas */ 4 | /* integrates y=x^2 to your specified limits */ 5 | #define _CRT_SECURE_NO_WARNINGS 6 | #include 7 | #include 8 | #include 9 | main() 10 | { 11 | 12 | double x, y; 13 | double yupper, ylower, xupper, xlower; 14 | double montearea, area; 15 | double totalexparea, totalarea; 16 | int j; 17 | int iterations; 18 | 19 | 20 | printf("enter lower limit\n"); 21 | scanf("%lf", &xlower); 22 | printf("enter upper limit\n"); 23 | scanf("%lf", &xupper); 24 | printf("xlower %lf xupper %lf\n", xlower, xupper); 25 | yupper = pow(xupper, 2); 26 | ylower = pow(xlower, 2); 27 | printf("ylower %lf yupper %lf\n", ylower, yupper); 28 | area = yupper * (xupper - xlower); 29 | printf("outer area is %lf\n", area); 30 | printf("enter iterations \n"); 31 | scanf("%d", &iterations); 32 | 33 | totalarea = 0; 34 | totalexparea = 0; 35 | 36 | for (j = 1;j < iterations;j++) 37 | { 38 | 39 | 40 | x = rand() % 1000;/* generate random number for x up to 1000 */ 41 | y = rand() % 1000;/* generate random number for y up to 1000 */ 42 | y = y / 1000;/* Divide by 1000 so our number is between 0 and 1 */ 43 | x = x / 1000;/* Divide by 1000 so our number is between 0 and 1 */ 44 | 45 | x = xlower + (xupper - xlower)*x;/* Adjust x value to be between required limits */ 46 | y = yupper * y;/* Adjust y value to be between required limits */ 47 | 48 | if (x >= xlower) 49 | { 50 | totalarea = totalarea + 1;/* add 1 to count of points within whole area */ 51 | 52 | /* test if this y value is below the curve */ 53 | if (y <= pow(x, 2)) 54 | { 55 | 56 | totalexparea = totalexparea + 1;/* add 1 to count of points below the curve */ 57 | 58 | } 59 | } 60 | 61 | 62 | } 63 | if (totalarea != 0) 64 | { 65 | montearea = area * (totalexparea / totalarea);/* calculate the area below the curve */ 66 | } 67 | printf("monte area is %lf\n", montearea); 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | } -------------------------------------------------------------------------------- /Ch04/montecarlo2.c: -------------------------------------------------------------------------------- 1 | /* Montecarlo2 */ 2 | /* integration using monte carlo */ 3 | /* by counting relative areas */ 4 | /* integrates x^2 + y^2 = 4 in the first quadrant */ 5 | #define _CRT_SECURE_NO_WARNINGS 6 | #include 7 | #include 8 | #include 9 | main() 10 | { 11 | 12 | double x, y; 13 | double yupper, ylower, xupper, xlower; 14 | double montearea, area; 15 | double totalexparea, totalarea; 16 | int j; 17 | int iterations; 18 | 19 | 20 | printf("enter lower limit\n"); 21 | scanf("%lf", &xlower); 22 | printf("enter upper limit\n"); 23 | scanf("%lf", &xupper); 24 | printf("xlower %lf xupper %lf\n", xlower, xupper); 25 | yupper = 2;/* fixed at 2 -see graph of function */ 26 | ylower = pow(xlower, 2); 27 | printf("ylower %lf yupper %lf\n", ylower, yupper); 28 | area = yupper * (xupper - xlower); 29 | printf("area is %lf\n", area); 30 | printf("enter iterations up to 1000000\n"); 31 | scanf("%d", &iterations); 32 | 33 | totalarea = 0; 34 | totalexparea = 0; 35 | 36 | for (j = 1;j < iterations;j++) 37 | { 38 | 39 | 40 | x = rand() % 1000;/* generate random number for x up to 1000 */ 41 | y = rand() % 1000;/* generate random number for y up to 1000 */ 42 | y = y / 1000;/* Divide by 1000 so our number is between 0 and 1 */ 43 | x = x / 1000;/* Divide by 1000 so our number is between 0 and 1 */ 44 | x = xlower + (xupper - xlower)*x;/* Adjust x value to be between required limits */ 45 | y = yupper * y;/* Adjust y value to be between required limits */ 46 | 47 | 48 | if (x >= xlower) 49 | { 50 | totalarea = totalarea + 1;/* add 1 to count of points within whole area */ 51 | /* test if these coordinates are within the curve */ 52 | 53 | if (pow(x, 2) + pow(y, 2) < 4) 54 | { 55 | 56 | totalexparea = totalexparea + 1;/* add 1 to count of points below the curve */ 57 | 58 | } 59 | } 60 | 61 | 62 | } 63 | if (totalarea != 0) 64 | { 65 | montearea = area * (totalexparea / totalarea);/* calculate the area below the curve */ 66 | } 67 | printf("monte area is %lf\n", montearea); 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | } -------------------------------------------------------------------------------- /Ch04/montecarlocircle.c: -------------------------------------------------------------------------------- 1 | /* Montecarlo circle (whole circle in 1st quadrant)*/ 2 | /* Calculation of volume using monte carlo */ 3 | /* by counting relative volumes */ 4 | /* integrates (x-2)^2 + (y-2)^2 = 2^2 to your specified limits (radius fixed at 2 - centre at (2,2)) */ 5 | #define _CRT_SECURE_NO_WARNINGS 6 | #include 7 | #include 8 | #include 9 | main() 10 | { 11 | 12 | double x, y; 13 | double yupper, ylower, xupper, xlower; 14 | double montearea, area; 15 | double totalexparea, totalarea; 16 | int j; 17 | int iterations; 18 | 19 | 20 | 21 | printf("enter lower x limit\n"); 22 | scanf("%lf", &xlower); 23 | printf("enter upper x limit\n"); 24 | scanf("%lf", &xupper); 25 | printf("xlower %lf xupper %lf\n", xlower, xupper); 26 | 27 | 28 | printf("enter lower y limit\n"); 29 | scanf("%lf", &ylower); 30 | printf("enter upper y limit\n"); 31 | scanf("%lf", &yupper); 32 | printf("ylower %lf yupper %lf\n", ylower, yupper); 33 | 34 | 35 | 36 | area = (xupper - xlower)*(yupper - ylower); 37 | printf("overall area is %lf\n", area); 38 | 39 | printf("enter iterations \n"); 40 | scanf("%d", &iterations); 41 | 42 | totalarea = 0; 43 | totalexparea = 0; 44 | 45 | 46 | for (j = 1;j < iterations;j++) 47 | { 48 | 49 | /* find random numbers for x,y ansd z */ 50 | x = rand() % 1000; 51 | y = rand() % 1000; 52 | 53 | y = y / 1000; 54 | x = x / 1000; 55 | 56 | 57 | /* x,y will have numbers between 0 and 1 */ 58 | /* so multiply by the user's entered ranges for x,y */ 59 | x = xlower + (xupper - xlower)*x; 60 | y = ylower + (yupper - ylower)*y; 61 | 62 | 63 | 64 | 65 | if (x >= xlower && y >= ylower) 66 | { 67 | totalarea = totalarea + 1; /* This contains the total number of entries */ 68 | 69 | 70 | if ((pow((y - 2), 2) + pow((x - 2), 2)) < 4) 71 | { 72 | 73 | totalexparea = totalexparea + 1;/* This contains number of entries within desired area */ 74 | 75 | } 76 | } 77 | 78 | 79 | } 80 | if (totalarea != 0) 81 | { 82 | montearea = area * (totalexparea / totalarea);/* Monte Carlo area os the fraction of the outer area */ 83 | } 84 | printf("monte carlo area is %lf\n", montearea); 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | } -------------------------------------------------------------------------------- /Ch04/montecarlocylinder.c: -------------------------------------------------------------------------------- 1 | /* Calculation of volume using monte carlo */ 2 | /* by counting relative volumes */ 3 | /* integrates x^2 + y^2 * z to your specified limits */ 4 | #define _CRT_SECURE_NO_WARNINGS 5 | #include 6 | #include 7 | #include 8 | main() 9 | /* Montecarlo cylinder*/ 10 | { 11 | 12 | double x, y, z; 13 | double zupper, zlower, yupper, ylower, xupper, xlower; 14 | double montevol, volume; 15 | double totalexpvol, totalvol; 16 | int j; 17 | int iterations; 18 | 19 | 20 | 21 | printf("enter lower x limit\n"); 22 | scanf("%lf", &xlower); 23 | printf("enter upper x limit\n"); 24 | scanf("%lf", &xupper); 25 | printf("xlower %lf xupper %lf\n", xlower, xupper); 26 | 27 | 28 | printf("enter lower y limit\n"); 29 | scanf("%lf", &ylower); 30 | printf("enter upper y limit\n"); 31 | scanf("%lf", &yupper); 32 | printf("ylower %lf yupper %lf\n", ylower, yupper); 33 | 34 | 35 | printf("enter lower z limit\n"); 36 | scanf("%lf", &zlower); 37 | printf("enter upper z limit\n"); 38 | scanf("%lf", &zupper); 39 | printf("zlower %lf zupper %lf\n", zlower, zupper); 40 | 41 | 42 | volume = (xupper - xlower)*(yupper - ylower)*(zupper - zlower); 43 | printf("volume is %lf\n", volume); 44 | 45 | printf("enter iterations \n"); 46 | scanf("%d", &iterations); 47 | 48 | totalvol = 0; 49 | totalexpvol = 0; 50 | 51 | 52 | for (j = 1;j < iterations;j++) 53 | { 54 | 55 | /* find random numbers for x,y ansd z */ 56 | x = rand() % 1000; 57 | y = rand() % 1000; 58 | z = rand() % 1000; 59 | y = y / 1000; 60 | x = x / 1000; 61 | z = z / 1000; 62 | 63 | /* x,y and z will have numbers between 0 and 1 */ 64 | /* so multiply by the user's entered ranges for x,y and z */ 65 | x = xlower + (xupper - xlower)*x; 66 | y = ylower + (yupper - ylower)*y; 67 | z = zlower + (zupper - zlower)*z; 68 | 69 | 70 | 71 | if (x >= xlower && z >= zlower && y >= ylower) 72 | { 73 | totalvol = totalvol + 1; /* This contains the total number of entries */ 74 | 75 | 76 | if ((pow(y, 2) + pow(x, 2)) < 4) 77 | { 78 | 79 | totalexpvol = totalexpvol + 1;/* This contains number of entries within desired vol */ 80 | 81 | } 82 | } 83 | 84 | 85 | } 86 | if (totalvol != 0) 87 | { 88 | montevol = volume * (totalexpvol / totalvol);/* Monte Carlo volume os the fraction of the cube volume */ 89 | } 90 | printf("monte carlo volume is %lf\n", montevol); 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | } -------------------------------------------------------------------------------- /Ch04/montecarlosphere.c: -------------------------------------------------------------------------------- 1 | /* Montecarlo sphere*/ 2 | /* Calculation of volume using monte carlo */ 3 | /* by counting relative volumes */ 4 | /* integrates x^2 + y^2 + z^2 = 2^2 to your specified limits (radius fixed at 2 ) */ 5 | #define _CRT_SECURE_NO_WARNINGS 6 | #include 7 | #include 8 | #include 9 | main() 10 | { 11 | 12 | double x, y, z; 13 | double zupper, zlower, yupper, ylower, xupper, xlower; 14 | double montevol,/* vol,*/ volume; 15 | double totalexpvol, totalvol; 16 | int j; 17 | int iterations; 18 | 19 | 20 | 21 | printf("enter lower x limit\n"); 22 | scanf("%lf", &xlower); 23 | printf("enter upper x limit\n"); 24 | scanf("%lf", &xupper); 25 | printf("xlower %lf xupper %lf\n", xlower, xupper); 26 | 27 | 28 | printf("enter lower y limit\n"); 29 | scanf("%lf", &ylower); 30 | printf("enter upper y limit\n"); 31 | scanf("%lf", &yupper); 32 | printf("ylower %lf yupper %lf\n", ylower, yupper); 33 | 34 | 35 | printf("enter lower z limit\n"); 36 | scanf("%lf", &zlower); 37 | printf("enter upper z limit\n"); 38 | scanf("%lf", &zupper); 39 | printf("zlower %lf zupper %lf\n", zlower, zupper); 40 | 41 | 42 | volume = (xupper - xlower)*(yupper - ylower)*(zupper - zlower); 43 | printf("volume is %lf\n", volume); 44 | 45 | printf("enter iterations up to 1000000\n"); 46 | scanf("%d", &iterations); 47 | 48 | totalvol = 0; 49 | totalexpvol = 0; 50 | 51 | 52 | for (j = 1;j < iterations;j++) 53 | { 54 | 55 | /* find random numbers for x,y ansd z */ 56 | x = rand() % 1000; 57 | y = rand() % 1000; 58 | z = rand() % 1000; 59 | y = y / 1000; 60 | x = x / 1000; 61 | z = z / 1000; 62 | 63 | /* x,y and z will have numbers between 0 and 1 */ 64 | /* so multiply by the user's entered ranges for x,y and z */ 65 | x = xlower + (xupper - xlower)*x; 66 | y = ylower + (yupper - ylower)*y; 67 | z = zlower + (zupper - zlower)*z; 68 | 69 | 70 | 71 | if (x >= xlower && z >= zlower && y >= ylower) 72 | { 73 | totalvol = totalvol + 1; /* This contains the total number of entries */ 74 | 75 | 76 | if ((pow(y, 2) + pow(x, 2) + pow(z, 2)) < 4) 77 | { 78 | 79 | totalexpvol = totalexpvol + 1;/* This contains number of entries within desired vol */ 80 | 81 | } 82 | } 83 | 84 | 85 | } 86 | if (totalvol != 0) 87 | { 88 | montevol = volume * (totalexpvol / totalvol);/* Monte Carlo volume os the fraction of the cube volume */ 89 | } 90 | printf("monte carlo volume is %lf\n", montevol); 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | } -------------------------------------------------------------------------------- /Ch05/matrixadd2.c: -------------------------------------------------------------------------------- 1 | /* Matrix program */ 2 | /* Add two matrices */ 3 | #define _CRT_SECURE_NO_WARNINGS 4 | #include 5 | 6 | 7 | main() 8 | 9 | { 10 | #define MAXROW 8 11 | #define MAXCOL 8 12 | int matarr1[MAXROW][MAXCOL];/* First matrix store (rowxcolumn)*/ 13 | int matarr2[MAXROW][MAXCOL];/* Second matrix store (rowxcolumn)*/ 14 | int matsum[MAXROW][MAXCOL];/* Sum of matrices store (rowxcolumn)*/ 15 | int i,j,numrows,numcols; 16 | 17 | printf("enter order of the two matrices (max 8 rows max 8 columns) \n"); 18 | scanf("%d %d", &numrows, &numcols); 19 | 20 | /* Check if user is trying to enter too many rows or columns */ 21 | 22 | if(numrows>MAXROW || numcols>MAXCOL) 23 | { 24 | printf("error - max of 8 for rows or columns\n"); 25 | 26 | } 27 | 28 | else 29 | { 30 | 31 | /* Read in first matrix */ 32 | 33 | printf("enter first matrix\n"); 34 | for(i=0;i 5 | 6 | 7 | int main() 8 | 9 | { 10 | 11 | double matarr1[3][3];/* matrix store (rowxcolumn)*/ 12 | double mattrans[3][3];/* adjugate matrix store (rowxcolumn)*/ 13 | double matinv[3][3];/* matrix answer (rowxcolumn)*/ 14 | 15 | /* array to hold the positions of the minors for each of the 9 points in the matrix */ 16 | 17 | int posarr[78]={1,1,2,2,1,2,2,1, /* row 0 col 0 */ 18 | 1,0,2,2,1,2,2,0, /* row 0 col 1 */ 19 | 1,0,2,1,1,1,2,0, /* row 0 col 2 */ 20 | 0,1,2,2,0,2,2,1, /* row 1 col 0 */ 21 | 0,0,2,2,0,2,2,0, /* row 1 col 1 */ 22 | 0,0,2,1,0,1,2,0, /* row 1 col 2 */ 23 | 0,1,1,2,0,2,1,1, /* row 2 col 0 */ 24 | 0,0,1,2,0,2,1,0, /* row 2 col 1 */ 25 | 0,0,1,1,0,1,1,0}; /* row 2 col 2 */ 26 | 27 | double det[9];/* array to contain matrix of minors row1col1,row1col2,row1col3,row2col1 etc */ 28 | double detant; /* The determinant (any row or col of the original matrix X corresponding one in cofactors) */ 29 | 30 | int i,j,x; 31 | int r1,c1; 32 | 33 | 34 | 35 | r1=3; 36 | c1=3; 37 | 38 | 39 | 40 | printf("enter matrix\n"); 41 | for(i=0;i 5 | 6 | 7 | int main() 8 | 9 | { 10 | #define MAXROW 8 11 | #define MAXCOL 8 12 | int matarr1[MAXROW][MAXCOL];/* First matrix store (rowxcolumn)*/ 13 | int matarr2[MAXROW][MAXCOL];/* Second matrix store (rowxcolumn)*/ 14 | int matmult[MAXROW][MAXCOL];/* matrix answer (rowxcolumn)*/ 15 | 16 | int i,j,k; 17 | int r1,c1,r2,c2;/* row and col for 1st and 2nd matrices */ 18 | int error; 19 | 20 | error=0; 21 | 22 | printf("enter order of the first matrix (max 8 rows max 8 columns) \n"); 23 | scanf("%d %d", &r1, &c1); 24 | 25 | /* Check if user is trying to enter too many rows or columns */ 26 | /* or the number of columns in their 1st matrix is not the same as */ 27 | /* the number of rows in their 2nd matrix */ 28 | 29 | if(r1>MAXROW || c1>MAXCOL) 30 | { 31 | printf("error - max for rows or columns exceeded\n"); 32 | error=1; 33 | 34 | } 35 | if(error == 0) 36 | { 37 | printf("enter order of the second matrix (max %d rows max %d columns) \n",MAXROW,MAXCOL); 38 | scanf("%d %d", &r2, &c2); 39 | if(r2>MAXROW || c2>MAXCOL) 40 | { 41 | printf("error - max for rows or columns exceeded\n"); 42 | error=1; 43 | 44 | } 45 | else 46 | if(c1 != r2) 47 | { 48 | printf("error - number of columns in 1st matrix must equal number of rows in 2nd\n"); 49 | error=1; 50 | 51 | } 52 | 53 | 54 | } 55 | if(error == 0) 56 | 57 | { 58 | 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 | 13 | 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 | 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 | 68 | } 69 | -------------------------------------------------------------------------------- /Ch06/regyonx2.c: -------------------------------------------------------------------------------- 1 | /* regression */ 2 | /* user enters points.*/ 3 | /* regression of y on x calculated */ 4 | #define _CRT_SECURE_NO_WARNINGS 5 | #include 6 | #include 7 | main() 8 | { 9 | 10 | 11 | float xpoints[10],ypoints[10]; 12 | float sigmax,sigmay,sigmaxy,sigmaxsquared,xbar,ybar; 13 | float fltcnt,sxy,sxx,b,a; 14 | int i,points; 15 | 16 | 17 | printf("enter number of points (max 10 ) \n"); 18 | scanf("%d", &points); 19 | if(points>10) 20 | { 21 | printf("error - max of 10 points\n"); 22 | 23 | } 24 | else 25 | { 26 | sigmax=0; 27 | sigmay=0; 28 | sigmaxy=0; 29 | sigmaxsquared=0; 30 | 31 | 32 | /* user enters points from scatter graph */ 33 | for(i=0;i 3 | #include 4 | #include 5 | #include 6 | 7 | 8 | #define PI 3.141592654 9 | 10 | 11 | main() 12 | { 13 | 14 | time_t t; 15 | 16 | 17 | 18 | int i, throws, count; 19 | double randno,anglerand; 20 | 21 | 22 | srand((unsigned) time(&t));/* set the random number seed */ 23 | printf("Enter number of throws "); 24 | scanf("%d",&throws); 25 | 26 | count=0; 27 | 28 | for(i=1; i<=throws; i++) 29 | { 30 | 31 | randno=rand()%1000; 32 | randno=randno/1000;/* randno is the random number */ 33 | anglerand=rand()%1000; 34 | anglerand=anglerand/1000; 35 | 36 | anglerand=0.5*PI*anglerand; /* anglerand is the angle random number*/ 37 | 38 | 39 | if(randno <= sin(anglerand)) 40 | count=count+1; /* Add to count */ 41 | 42 | 43 | } 44 | 45 | printf("PI is %lf \n",2*(double)i/(double)count); 46 | } 47 | -------------------------------------------------------------------------------- /Ch07/radioact5.c: -------------------------------------------------------------------------------- 1 | /* radioactive decay simulation */ 2 | #define _CRT_SECURE_NO_WARNINGS 3 | #include 4 | #include 5 | #include 6 | #include 7 | main() 8 | { 9 | 10 | int j,timelimit,nuc; 11 | 12 | double randnumber,timeinc,lambda,timecount,probunittime; 13 | FILE *fptr; 14 | time_t t; 15 | srand((unsigned) time(&t)); /* random number generator seed */ 16 | fptr=fopen("radioact.dat","w"); 17 | 18 | 19 | /* Ask user to input specific data */ 20 | /* initial number of nuclei, the value of lambda, time for experiment */ 21 | 22 | printf("Enter initial number of nuclei : "); 23 | scanf("%d",&nuc); 24 | 25 | printf("Enter lambda : "); 26 | scanf("%lf",&lambda); 27 | 28 | printf("Enter time : "); 29 | scanf("%d",&timelimit); 30 | 31 | /* time increment of loop */ 32 | 33 | timeinc=0.001/lambda; 34 | 35 | printf("Time increment :%lf",timeinc); 36 | 37 | /* (delta t * lambda) */ 38 | 39 | probunittime=0.001*lambda; 40 | timecount=0; 41 | 42 | /* Monte Carlo loop */ 43 | while(timecount<=timelimit) 44 | { 45 | 46 | 47 | fprintf(fptr,"%lf %d\n",timecount,nuc); 48 | 49 | timecount=timecount+timeinc; 50 | 51 | for(j=0;j<=nuc;j++) 52 | { 53 | randnumber=rand()%1000; 54 | randnumber=randnumber/1000; 55 | 56 | /* Monte Carlo method checks random number less than (delta t * lambda) */ 57 | 58 | if(randnumber<=probunittime) 59 | nuc=nuc-1;/* If less, then prob. that nucleus has decayed */ 60 | 61 | if(nuc<=0) 62 | goto nuclimitreached; 63 | 64 | 65 | } 66 | } 67 | nuclimitreached: fclose(fptr); /* nuclei limit or time limit reached */ 68 | 69 | } 70 | -------------------------------------------------------------------------------- /Ch07/randwalk4C.c: -------------------------------------------------------------------------------- 1 | /* simple random walk simulation */ 2 | #define _CRT_SECURE_NO_WARNINGS 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | 9 | 10 | FILE *output; 11 | time_t t; 12 | 13 | main() 14 | { 15 | int i; 16 | float xrand,yrand; 17 | double x, y, rms[10000]; 18 | 19 | 20 | 21 | output= fopen ("randwalk4.dat", "w"); /* external file name */ 22 | 23 | for (i=0; i<=10000; i++) 24 | rms[i]=0.0; /* clear array */ 25 | 26 | 27 | srand((unsigned) time(&t)); /* set the number generator */ 28 | 29 | 30 | 31 | x=0.0; y=0.0; 32 | 33 | for (i=1;i<=10000; i++) 34 | { 35 | /* generate x random number */ 36 | xrand=rand()%1000; 37 | xrand=xrand/1000; 38 | if(xrand<0.5) 39 | x=x+1.0; 40 | else 41 | x=x-1.0; 42 | 43 | /* generate y random number */ 44 | yrand=rand()%1000; 45 | yrand=yrand/1000; 46 | if(yrand<0.5) 47 | y=y+1.0; 48 | else 49 | y=y-1.0; 50 | 51 | 52 | rms[i] = sqrt(x*x+y*y);/* store rms to total */ 53 | 54 | 55 | 56 | } 57 | /* Write values to file */ 58 | for (i=0; i<=100; i++) 59 | { 60 | 61 | fprintf(output,"%d %lf\n", i, rms[i*100]); 62 | } 63 | 64 | fclose (output); 65 | } 66 | -------------------------------------------------------------------------------- /Ch08/augmat17A.c: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Apress/numerical-c/e0ff82d28d2e837846b27255ccd745c00f51bd5b/Ch08/augmat17A.c -------------------------------------------------------------------------------- /Ch08/augmat18.c: -------------------------------------------------------------------------------- 1 | /* augmat18 */ 2 | /* augmented matrix for 3,4 or 5 equations */ 3 | /* calls functions for row division and row subtraction */ 4 | #define _CRT_SECURE_NO_WARNINGS 5 | #include 6 | #include 7 | 8 | void funcdivide(int first,int second,int count); 9 | void funcsubtract(int first,int second,int count); 10 | double matrix[12][13]; 11 | double divisor; 12 | int i,row,col; 13 | 14 | main() 15 | { 16 | 17 | double value,x,y,z,a,b,c,d,e; 18 | 19 | int i,j,k,n,count; 20 | 21 | /* Enter data for augmented matrix - one element at a time*/ 22 | 23 | printf("enter row/column number (square matrix 3x3 4x4 5x5 only)"); 24 | scanf("%d",&n); 25 | 26 | printf("square matrix is %d",n); 27 | 28 | row=n; 29 | col=n+1; 30 | 31 | for(j=0;j 7 | #include 8 | 9 | void funcdivide(int first,int second,int count); 10 | void funcsubtract(int first,int second,int count); 11 | double matrix[12][13]; 12 | double divisor; 13 | 14 | int i,row,col; 15 | 16 | 17 | main() 18 | { 19 | 20 | double value,x,y,z,a,b,c,d,e; 21 | 22 | double solution[12]; 23 | 24 | int i,j,k,m,n,count; 25 | 26 | /* Enter data for augmented matrix - one element at a time*/ 27 | 28 | printf("enter row/column number (square matrix 3x3 4x4 5x5 only)"); 29 | scanf("%d",&n); 30 | 31 | printf("square matrix is %d",n); 32 | 33 | row=n; 34 | col=n+1; 35 | 36 | for(j=0;j 8 | #include 9 | #include 10 | 11 | void funcdivide(int first,int second,int count); 12 | void funcsubtract(int first,int second,int count); 13 | double matrix[12][13]; 14 | double divisor; 15 | int i,row,col,rw,cl; 16 | 17 | main() 18 | { 19 | 20 | 21 | double solution[12]; 22 | 23 | int i,j,k,n,count; 24 | 25 | 26 | 27 | double inmat[12][13]={ 28 | {2.6,3.1,7.4,0.6,9.3,4.9,3.4,8.7,0.2,3.6,7.7,3.9,394.125}, 29 | {4.9,9.3,0.6,7.4,3.1,2.6,0.3,6.3,5.1,4.9,9.1,0.6,360.703}, 30 | {8.3,8.8,5.2,2.7,0.8,1.3,8.5,7.6,6.2,4.1,0.4,1.2,324.618}, 31 | {1.3,0.4,2.3,5.8,8.1,6.3,6.3,5.1,9.2,6.6,1.3,2.3,414.999}, 32 | {9.7,6.8,3.9,0.4,6.7,4.1,7.1,6.3,5.5,4.1,1.7,3.1,366.599}, 33 | {7.3,5.8,6.1,2.7,9.2,1.8,4.2,5.2,7.1,3.7,2.9,2.6,364.771}, 34 | {9.2,7.3,9.3,2.4,3.6,1.2,2.6,3.7,6.2,2.7,3.1,0.2,273.245}, 35 | {8.6,8.4,8.7,6.8,3.9,4.3,3.8,4.6,5.3,6.5,2.6,0.5,359.206}, 36 | {9.4,9.3,7.1,7.3,2.4,3.1,9.2,8.3,7.1,6.7,4.1,1.3,463.157}, 37 | {7.2,6.8,7.6,3.5,3.1,2.5,3.9,7.6,8.3,8.5,5.2,2.1,445.064}, 38 | {6.9,9.9,8.4,7.7,4.1,3.8,8.2,9.7,6.5,7.7,1.3,1.8,465.479}, 39 | {5.9,8.1,6.8,4.6,1.6,2.2,7.9,6.4,8.5,5.9,0.6,1.6,377.258} 40 | }; 41 | 42 | /* Use preset array */ 43 | 44 | n=12;/* set matrix to 12x12 */ 45 | 46 | 47 | /* Copy preset array to output array */ 48 | 49 | for(i=0;i<12;i++) 50 | { 51 | for(j=0;j<13;j++) 52 | { 53 | matrix[i][j]=inmat[i][j]; 54 | } 55 | 56 | } 57 | 58 | 59 | 60 | /* Print preset matrix */ 61 | 62 | printf("augmented matrix is\n"); 63 | for(j=0;j 9 | #include 10 | #include 11 | 12 | struct record 13 | { 14 | double matrix[12][13]; 15 | }; 16 | 17 | void funcdivide(int first,int second,int count); 18 | void funcsubtract(int first,int second,int count); 19 | double matrix[12][13]; 20 | double divisor; 21 | int i,row,col,rw,cl; 22 | 23 | main() 24 | { 25 | 26 | double value,x,y,z,a,b,c,d,e; 27 | double solution[12]; 28 | double x1,x2,x3; 29 | int j,k,n,count; 30 | int pr; 31 | 32 | 33 | int counter; 34 | 35 | struct record data_record; 36 | 37 | 38 | 39 | 40 | 41 | double inmat[12][13]={ 42 | {2.6,3.1,7.4,0.6,9.3,4.9,3.4,8.7,0.2,3.6,7.7,3.9,394.125}, 43 | {4.9,9.3,0.6,7.4,3.1,2.6,0.3,6.3,5.1,4.9,9.1,0.6,360.703}, 44 | {8.3,8.8,5.2,2.7,0.8,1.3,8.5,7.6,6.2,4.1,0.4,1.2,324.618}, 45 | {1.3,0.4,2.3,5.8,8.1,6.3,6.3,5.1,9.2,6.6,1.3,2.3,414.999}, 46 | {9.7,6.8,3.9,0.4,6.7,4.1,7.1,6.3,5.5,4.1,1.7,3.1,366.599}, 47 | {7.3,5.8,6.1,2.7,9.2,1.8,4.2,5.2,7.1,3.7,2.9,2.6,364.771}, 48 | {9.2,7.3,9.3,2.4,3.6,1.2,2.6,3.7,6.2,2.7,3.1,0.2,273.245}, 49 | {8.6,8.4,8.7,6.8,3.9,4.3,3.8,4.6,5.3,6.5,2.6,0.5,359.206}, 50 | {9.4,9.3,7.1,7.3,2.4,3.1,9.2,8.3,7.1,6.7,4.1,1.3,463.157}, 51 | {7.2,6.8,7.6,3.5,3.1,2.5,3.9,7.6,8.3,8.5,5.2,2.1,445.064}, 52 | {6.9,9.9,8.4,7.7,4.1,3.8,8.2,9.7,6.5,7.7,1.3,1.8,465.479}, 53 | {5.9,8.1,6.8,4.6,1.6,2.2,7.9,6.4,8.5,5.9,0.6,1.6,377.258} 54 | }; 55 | 56 | printf("enter 1 or 2 (1=use preset matrix, 2=enter matrix manually))"); 57 | scanf("%d",&pr); 58 | if(pr == 2) 59 | { 60 | /* Enter data manually */ 61 | 62 | 63 | printf("enter row/column number (square matrix only up to 12 rows)"); 64 | scanf("%d",&n); 65 | 66 | printf("square matrix is %d",n); 67 | 68 | row=n; 69 | col=n+1; 70 | for(j=0;j 6 | #include 7 | main() 8 | { 9 | 10 | float /*value,*/ x, y, z; 11 | 12 | /* Augmented matrix to be input is preset */ 13 | float matrix[3][4] = { 14 | {2,1,-1,1}, 15 | {2,-3,1,-1}, 16 | {4,-1,4,14} 17 | }; 18 | float divisor; 19 | int i, j; 20 | 21 | 22 | 23 | /* Read in the three rows (one element at a time) */ 24 | 25 | /*for (j = 0;j < 3;j++) 26 | { 27 | printf("row %d ", j); 28 | for (i = 0;i < 4;i++) 29 | { 30 | printf("enter x\n"); 31 | scanf("%f", &value); 32 | matrix[j][i] = value; 33 | } 34 | } 35 | */ 36 | 37 | /* Print out the preset augmented matrix */ 38 | 39 | printf("augmented matrix is\n"); 40 | for (j = 0;j < 3;j++) 41 | { 42 | 43 | for (i = 0;i < 4;i++) 44 | { 45 | printf("matrix[%d][%d] = %f\n", j, i, matrix[j][i]); 46 | 47 | } 48 | } 49 | 50 | /* Nine stages on the rows of the augmented matrix for our 3 equation problem */ 51 | 52 | /* stage 1 divide first row by (row 0 col 0 ) */ 53 | divisor = matrix[0][0]; 54 | for (i = 0;i < 4;i++) 55 | { 56 | matrix[0][i] = (matrix[0][i]) / divisor; 57 | } 58 | printf("augmented matrix after division of first row is\n"); 59 | for (j = 0;j < 3;j++) 60 | { 61 | printf("%f %f %f %f\n", matrix[j][0], matrix[j][1], matrix[j][2], matrix[j][3]); 62 | } 63 | /* stage 2 divide second row by (row 1 col 0 )*/ 64 | divisor = matrix[1][0]; 65 | for (i = 0;i < 4;i++) 66 | { 67 | matrix[1][i] = (matrix[1][i]) / divisor; 68 | } 69 | printf("augmented matrix after division of second row is\n"); 70 | for (j = 0;j < 3;j++) 71 | { 72 | printf("%f %f %f %f\n", matrix[j][0], matrix[j][1], matrix[j][2], matrix[j][3]); 73 | } 74 | /* stage 3 divide third row by (row 2 col 0 )*/ 75 | divisor = matrix[2][0]; 76 | for (i = 0;i < 4;i++) 77 | { 78 | matrix[2][i] = (matrix[2][i]) / divisor; 79 | } 80 | printf("augmented matrix after division of third row is\n"); 81 | for (j = 0;j < 3;j++) 82 | { 83 | printf("%f %f %f %f\n", matrix[j][0], matrix[j][1], matrix[j][2], matrix[j][3]); 84 | } 85 | 86 | /* stage 4 subtract first row from second row */ 87 | divisor = matrix[1][0]; 88 | for (i = 0;i < 4;i++) 89 | { 90 | matrix[1][i] = (matrix[1][i]) - matrix[0][i]; 91 | } 92 | printf("augmented matrix after subtraction of first row from second row is\n"); 93 | for (j = 0;j < 3;j++) 94 | { 95 | printf("%f %f %f %f\n", matrix[j][0], matrix[j][1], matrix[j][2], matrix[j][3]); 96 | } 97 | 98 | /* stage 5 subtract first row from third row*/ 99 | divisor = matrix[2][0]; 100 | for (i = 0;i < 4;i++) 101 | { 102 | matrix[2][i] = (matrix[2][i]) - matrix[0][i]; 103 | } 104 | printf("augmented matrix after subtraction of first row from third row is\n"); 105 | for (j = 0;j < 3;j++) 106 | { 107 | printf("%f %f %f %f\n", matrix[j][0], matrix[j][1], matrix[j][2], matrix[j][3]); 108 | } 109 | /* stage 6 divide second row by (row 1 col 1 )*/ 110 | divisor = matrix[1][1]; 111 | for (i = 0;i < 4;i++) 112 | { 113 | matrix[1][i] = (matrix[1][i]) / divisor; 114 | } 115 | printf("augmented matrix after division of second row is\n"); 116 | for (j = 0;j < 3;j++) 117 | { 118 | printf("%f %f %f %f\n", matrix[j][0], matrix[j][1], matrix[j][2], matrix[j][3]); 119 | } 120 | /* stage 7 divide third row by (row 2 col 1) */ 121 | divisor = matrix[2][1]; 122 | for (i = 0;i < 4;i++) 123 | { 124 | matrix[2][i] = (matrix[2][i]) / divisor; 125 | } 126 | printf("augmented matrix after division of third row is\n"); 127 | for (j = 0;j < 3;j++) 128 | { 129 | printf("%f %f %f %f\n", matrix[j][0], matrix[j][1], matrix[j][2], matrix[j][3]); 130 | } 131 | /* stage 8 subtract second row from third row*/ 132 | divisor = matrix[2][0]; 133 | for (i = 0;i < 4;i++) 134 | { 135 | matrix[2][i] = (matrix[2][i]) - matrix[1][i]; 136 | } 137 | printf("augmented matrix after subtraction of second row from third row is\n"); 138 | for (j = 0;j < 3;j++) 139 | { 140 | printf("%f %f %f %f\n", matrix[j][0], matrix[j][1], matrix[j][2], matrix[j][3]); 141 | } 142 | /* stage 9 divide third row by (row 2 col 2 )*/ 143 | divisor = matrix[2][2]; 144 | for (i = 0;i < 4;i++) 145 | { 146 | matrix[2][i] = (matrix[2][i]) / divisor; 147 | } 148 | printf("augmented matrix after division of third row is\n"); 149 | for (j = 0;j < 3;j++) 150 | { 151 | printf("%f %f %f %f\n", matrix[j][0], matrix[j][1], matrix[j][2], matrix[j][3]); 152 | } 153 | 154 | /* Print out x,y and z solutions */ 155 | 156 | z = matrix[2][3]; 157 | y = matrix[1][3] - z * matrix[1][2]; 158 | x = matrix[0][3] - y * matrix[0][1] - z * matrix[0][2]; 159 | 160 | printf("x = %f y= %f z = %f", x, y, z); 161 | 162 | 163 | 164 | } -------------------------------------------------------------------------------- /Ch09/filereadCh.c: -------------------------------------------------------------------------------- 1 | /* fileread */ 2 | /* reads from file */ 3 | /* reads and prints sequentially */ 4 | /* reads and prints specific records */ 5 | 6 | #define _CRT_SECURE_NO_WARNINGS 7 | #include 8 | 9 | struct Patient { 10 | int PatientID; 11 | char name[13]; 12 | int BloodPressure; 13 | }; 14 | 15 | int main() 16 | { 17 | FILE *fp; 18 | 19 | struct Patient s2; 20 | 21 | 22 | int numread, i; 23 | /* Open patients file */ 24 | 25 | fp = fopen("patients.bin", "r"); 26 | for (i = 0;i < 17;i++) 27 | { 28 | /* Read each patient data from file sequentially */ 29 | fread(&s2, sizeof(s2), 1, fp); 30 | /* Print patient ID, name and Blood Pressure for each patient */ 31 | 32 | printf("\nPatientID : %d", s2.PatientID); 33 | printf("\n Name : %s", s2.name); 34 | printf("\nBloodPressure : %d", s2.BloodPressure); 35 | 36 | 37 | } 38 | 39 | fclose(fp); 40 | 41 | 42 | /* Re-open the patients file */ 43 | fp = fopen("patients.bin", "r"); 44 | for (i = 0;i < 17;i++) 45 | { 46 | /* Search the file for patient with ID of 23 */ 47 | 48 | fread(&s2, sizeof(s2), 1, fp); 49 | if (s2.PatientID == 23) 50 | { 51 | /* Found the patient. Print their name */ 52 | printf("\nName : %s", s2.name); 53 | break; 54 | } 55 | } 56 | /* Go back to the beginning of the file */ 57 | 58 | fseek(fp, sizeof(s2), SEEK_END); 59 | rewind(fp); 60 | /* Find all patients with Blood Pressure reading above 63 */ 61 | 62 | for (i = 0;i < 17;i++) 63 | { 64 | fread(&s2, sizeof(s2), 1, fp); 65 | if (s2.BloodPressure > 63) 66 | { 67 | /* Print out name of each patient with Blood pressure above 63 */ 68 | printf("\nName : %s", s2.name); 69 | 70 | } 71 | } 72 | /* Go back to the beginning of the file */ 73 | 74 | 75 | rewind(fp); 76 | 77 | /* Read and print out the first 3 patients in the file */ 78 | 79 | numread = fread(&s2, sizeof(s2), 1, fp); 80 | if (numread == 1) 81 | { 82 | printf("\nPatientID : %d", s2.PatientID); 83 | printf("\nName : %s", s2.name); 84 | printf("\nBloodPressure : %d", s2.BloodPressure); 85 | } 86 | numread = fread(&s2, sizeof(s2), 1, fp); 87 | if (numread == 1) 88 | { 89 | 90 | printf("\nPatientID : %d", s2.PatientID); 91 | printf("\nName : %s", s2.name); 92 | printf("\nBloodPressure : %d", s2.BloodPressure); 93 | } 94 | numread = fread(&s2, sizeof(s2), 1, fp); 95 | if (numread == 1) 96 | { 97 | printf("\nPatientID : %d", s2.PatientID); 98 | printf("\nName : %s", s2.name); 99 | printf("\nBloodPressure : %d", s2.BloodPressure); 100 | } 101 | /* Close the file */ 102 | 103 | fclose(fp); 104 | 105 | } 106 | -------------------------------------------------------------------------------- /Ch09/filereadE.c: -------------------------------------------------------------------------------- 1 | #define _CRT_SECURE_NO_WARNINGS 2 | #include 3 | #include 4 | 5 | 6 | 7 | struct record 8 | { 9 | double matrix[12][13]; 10 | }; 11 | 12 | int main() 13 | { 14 | int counter,i; 15 | FILE *ptr; 16 | struct record data_record; 17 | size_t r1; 18 | 19 | /* Open input file (read/binary) */ 20 | 21 | ptr=fopen("testaug.bin","rb"); 22 | if (!ptr) 23 | { 24 | printf("Can not open file"); 25 | return 1; 26 | } 27 | 28 | /* Read input matrix from input file */ 29 | 30 | r1=fread(data_record.matrix,sizeof(data_record.matrix),1,ptr); 31 | printf("read %d elements \n", r1); 32 | printf("size of struct record is %d \n", sizeof(struct record)); 33 | 34 | /* Print matrix read from file */ 35 | 36 | for ( counter=0; counter < 12; counter++) 37 | { 38 | for ( i=0; i < 13; i++) 39 | { 40 | 41 | printf("matrix[%d][%d] = %lf \n",counter,i,data_record.matrix[counter][i]); 42 | } 43 | 44 | 45 | } 46 | fclose(ptr); 47 | return 0; 48 | } -------------------------------------------------------------------------------- /Ch09/fileseek6ra.c: -------------------------------------------------------------------------------- 1 | /* fileseek6r */ 2 | /* reads from file */ 3 | /* reads and prints sequentially */ 4 | /* reads and prints specific records */ 5 | /* Only does seek when finding a record (not going back to start) */ 6 | #define _CRT_SECURE_NO_WARNINGS 7 | #include 8 | 9 | struct Patient { 10 | int PatientID; 11 | char name[13]; 12 | int BloodPressure; 13 | }; 14 | 15 | int main() 16 | { 17 | FILE *fp; 18 | 19 | /*FILE *fpout;*/ 20 | struct Patient s2; 21 | struct Patient s1 = { 68,"Warne ",95 }; 22 | 23 | int numread, i; 24 | int posn; 25 | long int minusone = -1; 26 | /* Open patients file */ 27 | 28 | fp = fopen("patients.bin", "r+"); 29 | for (i = 0;i < 17;i++) 30 | { 31 | /* Read each patient data from file sequentially */ 32 | fread(&s2, sizeof(s2), 1, fp); 33 | /* Print patient ID, name and Blood Pressure for each patient */ 34 | 35 | printf("\nPatientID : %d", s2.PatientID); 36 | printf("\n Name : %s", s2.name); 37 | printf("\nBloodPressure : %d", s2.BloodPressure); 38 | posn = ftell(fp);/* Find current file position */ 39 | printf("\n file posn is : %d", posn);/* Print current file position */ 40 | } 41 | 42 | fclose(fp); 43 | 44 | /* Re-open the patients file */ 45 | 46 | fp = fopen("patients.bin", "r+"); 47 | for (i = 0;i < 17;i++) 48 | { 49 | /* Search the file for patient with ID of 23 */ 50 | 51 | fread(&s2, sizeof(s2), 1, fp); 52 | if (s2.PatientID == 23) 53 | { 54 | /* Found the patient. Print their name */ 55 | printf("\nName : %s", s2.name); 56 | break; 57 | } 58 | } 59 | /* Go back to the beginning of the file */ 60 | 61 | 62 | rewind(fp); 63 | /* Find all patients with Blood Pressure reading above 63 */ 64 | for (i = 0;i < 17;i++) 65 | { 66 | fread(&s2, sizeof(s2), 1, fp); 67 | if (s2.BloodPressure > 63) 68 | { 69 | /* Print out name of each patient with Blood pressure above 63 */ 70 | printf("\nName : %s", s2.name); 71 | 72 | } 73 | } 74 | /* Go back to the beginning of the file */ 75 | 76 | 77 | rewind(fp); 78 | 79 | /* Read and print out the first 3 patients in the file */ 80 | 81 | numread = fread(&s2, sizeof(s2), 1, fp); 82 | if (numread == 1) 83 | { 84 | printf("\nPatientID : %d", s2.PatientID); 85 | printf("\nName : %s", s2.name); 86 | printf("\nBloodPressure : %d", s2.BloodPressure); 87 | 88 | } 89 | numread = fread(&s2, sizeof(s2), 1, fp); 90 | if (numread == 1) 91 | { 92 | 93 | printf("\nPatientID : %d", s2.PatientID); 94 | printf("\nName : %s", s2.name); 95 | printf("\nBloodPressure : %d", s2.BloodPressure); 96 | } 97 | numread = fread(&s2, sizeof(s2), 1, fp); 98 | if (numread == 1) 99 | { 100 | 101 | printf("\nPatientID : %d", s2.PatientID); 102 | printf("\nName : %s", s2.name); 103 | printf("\nBloodPressure : %d", s2.BloodPressure); 104 | } 105 | 106 | /* Demonstrate use of fseek to move current position in the file */ 107 | /* Then overwrite the current structure with a new one */ 108 | 109 | 110 | posn = ftell(fp);/* Find current file position */ 111 | printf("\n file posn is : %d", posn);/* Print current file position */ 112 | 113 | /* File pointer is now pointing to the 4th (the next) record in the file */ 114 | 115 | fseek(fp, minusone*sizeof(s2), SEEK_CUR);/* set it back to point at the third */ 116 | 117 | posn = ftell(fp);/* Find current file position */ 118 | printf("\n file posn is : %d", posn);/* Print current file position */ 119 | 120 | fwrite(&s1, sizeof(s1), 1, fp);/* overwrites what was in that position (3rd) */ 121 | fclose(fp); 122 | fp = fopen("patients.bin", "r"); 123 | for (i = 0;i < 18;i++) 124 | { 125 | /* Read each patient data from file sequentially */ 126 | fread(&s2, sizeof(s2), 1, fp); 127 | /* Print patient ID, name and Blood Pressure for each patient */ 128 | 129 | printf("\nPatientID : %d", s2.PatientID); 130 | printf("\n Name : %s", s2.name); 131 | printf("\nBloodPressure : %d", s2.BloodPressure); 132 | 133 | 134 | } 135 | 136 | 137 | /* Close the file */ 138 | 139 | fclose(fp); 140 | 141 | } 142 | -------------------------------------------------------------------------------- /Ch09/filewrite.c: -------------------------------------------------------------------------------- 1 | /* filewrite */ 2 | /* reads from 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 Patient { 10 | int PatientID; 11 | char name[13]; 12 | int BloodPressure; 13 | }; 14 | 15 | int main() 16 | { 17 | int i,numread; 18 | FILE *fp; 19 | struct Patient s1; 20 | struct Patient s2; 21 | 22 | struct Patient s10 = {10,"Brown ",50}; 23 | struct Patient s11 = {11,"Jones ",51}; 24 | struct Patient s12 = {12,"White ",52}; 25 | struct Patient s13 = {13,"Green ",53}; 26 | struct Patient s14 = {14,"Smith ",54}; 27 | struct Patient s15 = {15,"Black ",55}; 28 | struct Patient s16 = {16,"Allen ",56}; 29 | struct Patient s17 = {17,"Stone ",57}; 30 | struct Patient s18 = {18,"Evans ",58}; 31 | struct Patient s19 = {19,"Royle ",59}; 32 | struct Patient s20 = {20,"Stone ",60}; 33 | struct Patient s21 = {21,"Weeks ",61}; 34 | struct Patient s22 = {22,"Owens ",62}; 35 | struct Patient s23 = {23,"Power ",63}; 36 | struct Patient s24 = {24,"Bloom ",63}; 37 | 38 | struct Patient s28 = {28,"Haver ",68}; 39 | struct Patient s29 = {29,"James ",69}; 40 | 41 | 42 | /* Open the Patients file */ 43 | 44 | fp = fopen("patients.bin", "w"); 45 | 46 | /* Write details of each patient to file*/ 47 | /* From the structures defined above */ 48 | 49 | fwrite(&s10, sizeof(s1), 1, fp); 50 | fwrite(&s11, sizeof(s1), 1, fp); 51 | fwrite(&s12, sizeof(s1), 1, fp); 52 | fwrite(&s13, sizeof(s1), 1, fp); 53 | fwrite(&s14, sizeof(s1), 1, fp); 54 | fwrite(&s15, sizeof(s1), 1, fp); 55 | fwrite(&s16, sizeof(s1), 1, fp); 56 | fwrite(&s17, sizeof(s1), 1, fp); 57 | fwrite(&s18, sizeof(s1), 1, fp); 58 | fwrite(&s19, sizeof(s1), 1, fp); 59 | fwrite(&s20, sizeof(s1), 1, fp); 60 | fwrite(&s21, sizeof(s1), 1, fp); 61 | fwrite(&s22, sizeof(s1), 1, fp); 62 | fwrite(&s23, sizeof(s1), 1, fp); 63 | fwrite(&s24, sizeof(s1), 1, fp); 64 | 65 | fwrite(&s28, sizeof(s1), 1, fp); 66 | fwrite(&s29, sizeof(s1), 1, fp); 67 | 68 | /* Close the file */ 69 | 70 | fclose(fp); 71 | 72 | /* Reopen the file */ 73 | 74 | fopen("patients.bin", "r"); 75 | 76 | /* Read and print out all of the records on the file */ 77 | 78 | for(i=0;i<17;i++) 79 | { 80 | 81 | numread=fread(&s2, sizeof(s2), 1, fp); 82 | 83 | if(numread == 1) 84 | { 85 | 86 | /*printf( "Number of items read = %d ", numread );*/ 87 | 88 | printf("\nPatientID : %d", s2.PatientID); 89 | printf("\nName : %s", s2.name); 90 | printf("\nBloodPressure : %d", s2.BloodPressure); 91 | } 92 | else { 93 | /* If an error occurred on read then print out message */ 94 | 95 | if (feof(fp)) 96 | 97 | printf("Error reading patients.bin : unexpected end of file fp is %p\n",fp); 98 | 99 | else if (ferror(fp)) 100 | { 101 | perror("Error reading patients.bin"); 102 | } 103 | } 104 | 105 | 106 | 107 | 108 | 109 | } 110 | /* Close the file */ 111 | 112 | fclose(fp); 113 | 114 | 115 | 116 | 117 | 118 | } -------------------------------------------------------------------------------- /Ch09/filewriteE.c: -------------------------------------------------------------------------------- 1 | #define _CRT_SECURE_NO_WARNINGS 2 | #include 3 | #include 4 | 5 | 6 | 7 | 8 | int main() 9 | { 10 | 11 | struct record 12 | { 13 | double matrix[12][13]; 14 | }; 15 | int counter,i,j; 16 | FILE *ptr; 17 | struct record data_record; 18 | size_t r1; 19 | 20 | double inmat[12][13]={ 21 | {2.6,3.1,7.4,0.6,9.3,4.9,3.4,8.7,0.2,3.6,7.7,3.9,394.125}, 22 | {4.9,9.3,0.6,7.4,3.1,2.6,0.3,6.3,5.1,4.9,9.1,0.6,360.703}, 23 | {8.3,8.8,5.2,2.7,0.8,1.3,8.5,7.6,6.2,4.1,0.4,1.2,324.618}, 24 | {1.3,0.4,2.3,5.8,8.1,6.3,6.3,5.1,9.2,6.6,1.3,2.3,414.999}, 25 | {9.7,6.8,3.9,0.4,6.7,4.1,7.1,6.3,5.5,4.1,1.7,3.1,366.599}, 26 | {7.3,5.8,6.1,2.7,9.2,1.8,4.2,5.2,7.1,3.7,2.9,2.6,364.771}, 27 | {9.2,7.3,9.3,2.4,3.6,1.2,2.6,3.7,6.2,2.7,3.1,0.2,273.245}, 28 | {8.6,8.4,8.7,6.8,3.9,4.3,3.8,4.6,5.3,6.5,2.6,0.5,359.206}, 29 | {9.4,9.3,7.1,7.3,2.4,3.1,9.2,8.3,7.1,6.7,4.1,1.3,463.157}, 30 | {7.2,6.8,7.6,3.5,3.1,2.5,3.9,7.6,8.3,8.5,5.2,2.1,445.064}, 31 | {6.9,9.9,8.4,7.7,4.1,3.8,8.2,9.7,6.5,7.7,1.3,1.8,465.479}, 32 | {5.9,8.1,6.8,4.6,1.6,2.2,7.9,6.4,8.5,5.9,0.6,1.6,377.258} 33 | }; 34 | 35 | /* Copy preset array to output array */ 36 | 37 | for(i=0;i<12;i++) 38 | { 39 | for(j=0;j<13;j++) 40 | { 41 | data_record.matrix[i][j]=inmat[i][j]; 42 | } 43 | 44 | } 45 | 46 | /* Open output file (write/binary) */ 47 | 48 | ptr=fopen("testaug.bin","wb"); 49 | if (!ptr) 50 | { 51 | printf("Can not open file"); 52 | return 1; 53 | } 54 | 55 | /* Write output matrix to output file */ 56 | 57 | r1 = fwrite(data_record.matrix, sizeof(data_record.matrix), 1, ptr); 58 | printf("wrote %d elements \n", r1); 59 | printf("size of data_record.matrix is %d \n", sizeof(data_record.matrix)); 60 | 61 | 62 | /* Print matrix written to file */ 63 | 64 | for(i=0;i<12;i++) 65 | { 66 | for(j=0;j<13;j++) 67 | { 68 | data_record.matrix[i][j]=inmat[i][j]; 69 | printf("data_record.matrix[%d][%d] = %lf \n",i,j,data_record.matrix[i][j]); 70 | } 71 | 72 | } 73 | 74 | 75 | fclose(ptr); 76 | return 0; 77 | } 78 | 79 | -------------------------------------------------------------------------------- /Ch09/filewriteex3.c: -------------------------------------------------------------------------------- 1 | /* filewriteex3 */ 2 | /* Creates Company file */ 3 | /* prints out the records sequentially */ 4 | 5 | #define _CRT_SECURE_NO_WARNINGS 6 | #include 7 | 8 | struct Company { 9 | int CompanyID; 10 | char companyname[13]; 11 | float salesprofitpct;/* profit as a % of sales */ 12 | float totalctrypop;/* total populations countries for sales (in millions) */ 13 | float advertpct;/* Advertising as a % of sales */ 14 | float salprofpct;/* Total salaries as a % of profit */ 15 | float mwpct;/* Women as a % of total workers */ 16 | float alienwpct;/* Foreign workers as a % of total */ 17 | 18 | }; 19 | 20 | int main() 21 | { 22 | int i,numread; 23 | FILE *fp; 24 | struct Company s1; 25 | struct Company s2; 26 | 27 | /* Preset a structure for each company to be written to the output file */ 28 | 29 | struct Company s10 = {10,"Brown Co ",20.2,402,0.3,45.5,43.2,2.7}; 30 | struct Company s11 = {11,"CompuFix ",1.3,354,2.6,60.3,27.5,1.6}; 31 | struct Company s12 = {12,"Wall's ",12.6,766,5.8,14.7,54.6,3.8}; 32 | struct Company s13 = {13,"Goldman Inc ",29.5,876,12.6,21.6,43.9,9.3}; 33 | struct Company s14 = {14,"Stocks LLC ",0.7,1252,8.2,18.4,38.4,3.8}; 34 | struct Company s15 = {15,"Black & Blue",1.4,984,5.8,12.7,27.9,10.6}; 35 | struct Company s16 = {16,"Allenby ",52.8,1325,32.9,14.3,47.2,3.9}; 36 | struct Company s17 = {17,"StoneWorks ",16.3,1548,4.6,28.9,51.3,4.1}; 37 | struct Company s18 = {18,"Evans LLC ",51.0,1006,19.6,51.7,43.7,11.7}; 38 | struct Company s19 = {19,"Royle & Co ",19.6,983,14.3,26.2,48.1,12.3}; 39 | struct Company s20 = {20,"Stone Inc ",24.8,1030,8.5,13.5,34.6,5.6}; 40 | struct Company s21 = {21,"WeeksAway ",16.9,547,0.9,12.9,43.9,2.9}; 41 | struct Company s22 = {22,"Owens Co ",45.7,792,2.7,31.6,33.6,1.7}; 42 | struct Company s23 = {23,"PowerTools ",32.6,1563,17.5,29.3,51.8,13.3}; 43 | struct Company s24 = {24,"Bloom ",27.2,1869,23.9,18.7,40.4,9.6}; 44 | 45 | struct Company s28 = {28,"HaverGoodOne",33.8,489,3.6,12.7,43.8,3.7}; 46 | struct Company s29 = {29,"James & Co ",15.5,639,17.4,15.9,36.5,4.5}; 47 | 48 | 49 | /* Open the Companys file */ 50 | 51 | fp = fopen("Companyex.bin", "w"); 52 | 53 | /* Write details of each Company to file*/ 54 | /* From the structures defined above */ 55 | 56 | fwrite(&s10, sizeof(s1), 1, fp); 57 | fwrite(&s11, sizeof(s1), 1, fp); 58 | fwrite(&s12, sizeof(s1), 1, fp); 59 | fwrite(&s13, sizeof(s1), 1, fp); 60 | fwrite(&s14, sizeof(s1), 1, fp); 61 | fwrite(&s15, sizeof(s1), 1, fp); 62 | fwrite(&s16, sizeof(s1), 1, fp); 63 | fwrite(&s17, sizeof(s1), 1, fp); 64 | fwrite(&s18, sizeof(s1), 1, fp); 65 | fwrite(&s19, sizeof(s1), 1, fp); 66 | fwrite(&s20, sizeof(s1), 1, fp); 67 | fwrite(&s21, sizeof(s1), 1, fp); 68 | fwrite(&s22, sizeof(s1), 1, fp); 69 | fwrite(&s23, sizeof(s1), 1, fp); 70 | fwrite(&s24, sizeof(s1), 1, fp); 71 | 72 | fwrite(&s28, sizeof(s1), 1, fp); 73 | fwrite(&s29, sizeof(s1), 1, fp); 74 | 75 | /* Close the file */ 76 | 77 | fclose(fp); 78 | 79 | /* Reopen the file */ 80 | 81 | fopen("Companyex.bin", "r"); 82 | 83 | /* Read and print out all of the records on the file */ 84 | 85 | for(i=0;i<17;i++) 86 | { 87 | 88 | numread=fread(&s2, sizeof(s2), 1, fp); 89 | 90 | if(numread == 1) 91 | { 92 | 93 | 94 | 95 | 96 | /*printf( "Number of items read = %d ", numread );*/ 97 | 98 | printf("\nCompanyID : %d", s2.CompanyID); 99 | printf("\ncompanyname : %s", s2.companyname); 100 | printf("\nprofit as a percentage of sales : %f", s2.salesprofitpct); 101 | printf("\ntotal populations countries for sales (in millions) %f ", s2.totalctrypop); 102 | printf("\nAdvertising as a percentage of sales %f ", s2.advertpct); 103 | printf("\nTotal salaries as a percentage of profit %f ", s2.salprofpct); 104 | printf("\nWomen as a percentage of total workers %f ", s2.mwpct); 105 | printf("\nForeign workers as a percentage of total %f ", s2.alienwpct); 106 | 107 | } 108 | else { 109 | /* If an error occurred on read then print out message */ 110 | 111 | if (feof(fp)) 112 | 113 | printf("Error reading Companys.bin : unexpected end of file fp is %p\n",fp); 114 | 115 | else if (ferror(fp)) 116 | { 117 | perror("Error reading Companys.bin"); 118 | } 119 | } 120 | 121 | 122 | 123 | 124 | 125 | } 126 | /* Close the file */ 127 | 128 | fclose(fp); 129 | 130 | 131 | 132 | 133 | 134 | } -------------------------------------------------------------------------------- /Ch09/filewritepatients.c: -------------------------------------------------------------------------------- 1 | /* filewritepatients */ 2 | /* reads from file */ 3 | /* prints out the records sequentially */ 4 | 5 | #define _CRT_SECURE_NO_WARNINGS 6 | #include 7 | 8 | struct Patient { 9 | int PatientID; 10 | char name[13]; 11 | int BloodPressure; 12 | char allergies; 13 | char leukaemia; 14 | char anaemia; 15 | char asthma; 16 | char epilepsy; 17 | char famepil; 18 | }; 19 | 20 | int main() 21 | { 22 | int i, numread; 23 | FILE *fp; 24 | struct Patient s1; 25 | struct Patient s2; 26 | 27 | /* Preset data for each patient */ 28 | 29 | struct Patient s10 = { 10,"Brown ",50,'y','n','n','y','n','y' }; 30 | struct Patient s11 = { 11,"Jones ",51,'y','y','n','y','y','n' }; 31 | struct Patient s12 = { 12,"White ",52,'y','n','y','y','n','n' }; 32 | struct Patient s13 = { 13,"Green ",53,'y','n','y','y','n','n' }; 33 | struct Patient s14 = { 14,"Smith ",54,'y','y','n','y','y','n' }; 34 | struct Patient s15 = { 15,"Black ",55,'y','n','n','y','n','n' }; 35 | struct Patient s16 = { 16,"Allen ",56,'y','n','n','y','n','y' }; 36 | struct Patient s17 = { 17,"Stone ",57,'y','n','n','y','y','n' }; 37 | struct Patient s18 = { 18,"Evans ",58,'y','y','n','y','n','n' }; 38 | struct Patient s19 = { 19,"Royle ",59,'y','n','y','y','n','n' }; 39 | struct Patient s20 = { 20,"Stone ",60,'y','y','n','y','n','n' }; 40 | struct Patient s21 = { 21,"Weeks ",61,'y','n','n','y','y','y' }; 41 | struct Patient s22 = { 22,"Owens ",62,'y','n','n','y','y','y' }; 42 | struct Patient s23 = { 23,"Power ",63,'y','n','n','y','n','n' }; 43 | struct Patient s24 = { 24,"Bloom ",63,'y','n','y','y','n','n' }; 44 | struct Patient s28 = { 28,"Haver ",68,'y','y','n','y','n','n' }; 45 | struct Patient s29 = { 29,"James ",69,'y','y','n','y','n','n' }; 46 | 47 | 48 | /* Open the Patients file */ 49 | 50 | fp = fopen("patientex.bin", "w"); 51 | 52 | /* Write details of each patient to file*/ 53 | /* From the structures defined above */ 54 | 55 | fwrite(&s10, sizeof(s1), 1, fp); 56 | fwrite(&s11, sizeof(s1), 1, fp); 57 | fwrite(&s12, sizeof(s1), 1, fp); 58 | fwrite(&s13, sizeof(s1), 1, fp); 59 | fwrite(&s14, sizeof(s1), 1, fp); 60 | fwrite(&s15, sizeof(s1), 1, fp); 61 | fwrite(&s16, sizeof(s1), 1, fp); 62 | fwrite(&s17, sizeof(s1), 1, fp); 63 | fwrite(&s18, sizeof(s1), 1, fp); 64 | fwrite(&s19, sizeof(s1), 1, fp); 65 | fwrite(&s20, sizeof(s1), 1, fp); 66 | fwrite(&s21, sizeof(s1), 1, fp); 67 | fwrite(&s22, sizeof(s1), 1, fp); 68 | fwrite(&s23, sizeof(s1), 1, fp); 69 | fwrite(&s24, sizeof(s1), 1, fp); 70 | 71 | fwrite(&s28, sizeof(s1), 1, fp); 72 | fwrite(&s29, sizeof(s1), 1, fp); 73 | 74 | /* Close the file */ 75 | 76 | fclose(fp); 77 | 78 | /* Reopen the file */ 79 | 80 | fopen("patientex.bin", "r"); 81 | 82 | /* Read and print out all of the records on the file */ 83 | 84 | for (i = 0;i < 17;i++) 85 | { 86 | 87 | numread = fread(&s2, sizeof(s2), 1, fp); 88 | 89 | if (numread == 1) 90 | { 91 | /*printf( "Number of items read = %d ", numread );*/ 92 | 93 | printf("\nPatientID : %d", s2.PatientID); 94 | printf("\nName : %s", s2.name); 95 | printf("\nBloodPressure : %d", s2.BloodPressure); 96 | printf("\nAllergies %c leukaemia %c anaemia %c", s2.allergies, s2.leukaemia, s2.anaemia); 97 | printf("\nAsthma %c epilepsy %c famely epilepsy %c", s2.asthma, s2.epilepsy, s2.famepil); 98 | } 99 | 100 | else { 101 | /* If an error occurred on read then print out message */ 102 | 103 | if (feof(fp)) 104 | 105 | printf("Error reading patients.bin : unexpected end of file fp is %p\n", fp); 106 | 107 | 108 | else if (ferror(fp)) 109 | { 110 | perror("Error reading patients.bin"); 111 | } 112 | } 113 | 114 | 115 | 116 | 117 | 118 | } 119 | /* Close the file */ 120 | 121 | fclose(fp); 122 | 123 | 124 | 125 | } 126 | -------------------------------------------------------------------------------- /Ch09/radioact4a.c: -------------------------------------------------------------------------------- 1 | /* radioactive decay simulation */ 2 | #define _CRT_SECURE_NO_WARNINGS 3 | #include 4 | #include 5 | #include 6 | #include 7 | main() 8 | { 9 | 10 | int j,timelimit,nuc; 11 | 12 | double randnumber,timeinc,lambda,timecount,probunittime; 13 | FILE *fptr; /* pointer to file */ 14 | time_t t; 15 | srand((unsigned) time(&t)); /* random number generator seed */ 16 | fptr=fopen("radioact.dat","w"); 17 | 18 | 19 | /* Ask user to input specific data */ 20 | /* initial number of nuclei, the value of lambda, time for experiment */ 21 | 22 | printf("Enter initial number of nuclei : "); 23 | scanf("%d",&nuc); 24 | 25 | printf("Enter lambda : "); 26 | scanf("%lf",&lambda); 27 | 28 | printf("Enter time : "); 29 | scanf("%d",&timelimit); 30 | 31 | /* time increment of loop */ 32 | 33 | timeinc=0.001/lambda; 34 | 35 | printf("Time increment :%lf",timeinc); 36 | 37 | /* (delta t * lambda) */ 38 | 39 | probunittime=0.001*lambda; 40 | timecount=0; 41 | 42 | /* Monte Carlo loop */ 43 | while(timecount<=timelimit) 44 | { 45 | 46 | 47 | fprintf(fptr,"%lf %d\n",timecount,nuc);/* write two items to file */ 48 | 49 | timecount=timecount+timeinc; 50 | 51 | for(j=0;j<=nuc;j++) 52 | { 53 | randnumber=rand()%1000; 54 | randnumber=randnumber/1000; 55 | 56 | /* Monte Carlo method checks random number less than (delta t * lambda) */ 57 | 58 | if(randnumber<=probunittime) 59 | nuc=nuc-1;/* If less, then prob. that nucleus has decayed */ 60 | 61 | if(nuc<=0) 62 | goto nuclimitreached; 63 | 64 | 65 | } 66 | } 67 | nuclimitreached: fclose(fptr); /* nuclei limit or time limit reached */ 68 | 69 | } 70 | 71 | -------------------------------------------------------------------------------- /Ch09/randwalk6.c: -------------------------------------------------------------------------------- 1 | /* simple random walk simulation in 1 dimension */ 2 | #define _CRT_SECURE_NO_WARNINGS 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | 9 | FILE *output; 10 | time_t t; 11 | 12 | main() 13 | { 14 | int i; 15 | double xrand; 16 | 17 | double x,rms[20001]; 18 | 19 | 20 | output= fopen ("randwalk6.dat", "w"); /* external file name */ 21 | 22 | for (i=0; i<=20000; i++) 23 | rms[i]=0.0; /* clear array */ 24 | 25 | 26 | srand((unsigned) time(&t)); /* set the number generator */ 27 | 28 | 29 | 30 | x=0.0; 31 | 32 | 33 | for (i=1;i<=20000; i++) 34 | { 35 | /* generate x random number */ 36 | xrand=rand()%1000; 37 | xrand=xrand/1000; 38 | if(xrand<0.5) 39 | x=x+1.0; 40 | else 41 | x=x-1.0; 42 | 43 | 44 | rms[i] = sqrt(x*x);/* store rms to total */ 45 | 46 | 47 | } 48 | /* Write values to file */ 49 | for (i=0; i<=200; i++) 50 | { 51 | 52 | fprintf(output,"%d %lf\n", i, rms[i*100]); 53 | } 54 | 55 | fclose (output); 56 | } 57 | -------------------------------------------------------------------------------- /Ch10/eulermeCh.c: -------------------------------------------------------------------------------- 1 | #define _CRT_SECURE_NO_WARNINGS 2 | #include 3 | #include 4 | float func(float x, float y); 5 | int main() 6 | { 7 | float F1; 8 | float x0, y0, x, y, h, xn; 9 | printf("Enter initial x, initial y, final x, increment "); 10 | scanf("%f %f %f %f", &x0, &y0, &xn, &h); 11 | x = x0;/* set initial value for loop */ 12 | y = y0;/* set initial value for loop */ 13 | 14 | for (x = x0;x < xn;x = x) 15 | { 16 | F1 = h * func(x, y); /* h f'(x) from our Euler Formula */ 17 | 18 | y = y + F1;/* y(n+1) = yn + h f'(x) from our Euler Formula */ 19 | x = x + h; /* increment the x value for the next pass of loop */ 20 | /*printf("X = %f Y = %f\n",x,y);*/ 21 | } 22 | printf("X = %f Y = %f\n", x, y); 23 | 24 | 25 | } 26 | float func(float x, float y) 27 | { 28 | float funcval; 29 | funcval = 2 * x;/* Function is dy/dx = 2x */ 30 | return funcval; 31 | } -------------------------------------------------------------------------------- /Ch10/eulermeCh2.c: -------------------------------------------------------------------------------- 1 | #define _CRT_SECURE_NO_WARNINGS 2 | #include 3 | #include 4 | float func(float x, float y); 5 | int main() 6 | { 7 | float F1; 8 | float x0, y0, x, y, h, xn; 9 | 10 | FILE *fptr; 11 | fptr = fopen("euler1.dat", "w"); 12 | 13 | printf("Enter initial x, initial y, final x, increment "); 14 | scanf("%f %f %f %f", &x0, &y0, &xn, &h); 15 | x = x0; 16 | y = y0; 17 | 18 | for (x = x0;x < xn;x = x) 19 | { 20 | F1 = h * func(x, y); 21 | 22 | y = y + F1; 23 | x = x + h; 24 | fprintf(fptr, "%f\t%f\n", x, y); 25 | /*printf("X = %f Y = %f\n",x,y);*/ 26 | } 27 | printf("X = %f Y = %f\n", x, y); 28 | 29 | fclose(fptr); 30 | } 31 | float func(float x, float y) 32 | { 33 | float funcval; 34 | funcval = 2 * x; 35 | return funcval; 36 | } -------------------------------------------------------------------------------- /Ch10/rkch1.c: -------------------------------------------------------------------------------- 1 | #define _CRT_SECURE_NO_WARNINGS 2 | #include 3 | #include 4 | 5 | double func(double x, double y); 6 | 7 | int main() 8 | { 9 | double F1, F2, F3, F4; 10 | double x0, y0, x, y, h, xn; 11 | 12 | printf("Enter initial x, initial y, final x, increment "); 13 | scanf("%lf %lf %lf %lf", &x0, &y0, &xn, &h); 14 | 15 | x = x0; 16 | y = y0; 17 | 18 | /* forloop contains Runge-Kutta function */ 19 | 20 | for (x = x0;x < xn;x = x) 21 | { 22 | /* Set the values of F1,F2,F3 and F4 from the formula */ 23 | F1 = h * func(x, y); 24 | F2 = h * func(x + h / 2.0, y + F1 / 2.0); 25 | F3 = h * func(x + h / 2.0, y + F2 / 2.0); 26 | F4 = h * func(x + h, y + F3); 27 | 28 | /* Increment our y value using the formula*/ 29 | y = y + (F1 + 2 * F2 + 2 * F3 + F4) / 6.0; 30 | 31 | /* Increment the x-value by our chosen entered value */ 32 | x = x + h; 33 | 34 | /* Use the following lone of code (currently commented out) */ 35 | /* If you want to monitor the progress of the forloop */ 36 | /*printf("X = %lf Y = %lf\n",x,y);*/ 37 | 38 | 39 | } 40 | printf("X = %lf Y = %lf\n", x, y); 41 | 42 | 43 | } 44 | double func(double x, double y) 45 | { 46 | double funcval; 47 | funcval = 3*pow(x,2); /* function is dy/dx = 3x2 */ 48 | return funcval; 49 | } 50 | -------------------------------------------------------------------------------- /Ch10/rkch2.c: -------------------------------------------------------------------------------- 1 | #define _CRT_SECURE_NO_WARNINGS 2 | #include 3 | #include 4 | double func(double x, double y); 5 | int main() 6 | { 7 | double F1, F2, F3, F4; 8 | double x0, y0, x, y, h, xn; 9 | 10 | FILE *fptr; 11 | fptr = fopen("runge1.dat", "w");/* ! FILE NAME ! */ 12 | 13 | printf("Enter initial x, initial y, final x, increment "); 14 | scanf("%lf %lf %lf %lf", &x0, &y0, &xn, &h); 15 | x = x0; 16 | y = y0; 17 | 18 | /* forloop contains Runge-Kutta function */ 19 | 20 | for (x = x0;x < xn;x = x) 21 | { 22 | F1 = h * func(x, y); 23 | F2 = h * func(x + h / 2.0, y + F1 / 2.0); 24 | F3 = h * func(x + h / 2.0, y + F2 / 2.0); 25 | F4 = h * func(x + h, y + F3); 26 | y = y + (F1 + 2 * F2 + 2 * F3 + F4) / 6.0; 27 | x = x + h; 28 | fprintf(fptr, "%lf\t%lf\n", x, y); 29 | 30 | /*printf("X = %lf Y = %lf\n",x,y);*/ 31 | } 32 | printf("X = %lf Y = %lf\n", x, y); 33 | 34 | fclose(fptr); 35 | } 36 | double func(double x, double y) 37 | { 38 | double funcval; 39 | funcval = 3*pow(x,2); /* function is dy/dx =3 x2 */ 40 | return funcval; 41 | } 42 | -------------------------------------------------------------------------------- /Ch10/runge2me5a.c: -------------------------------------------------------------------------------- 1 | /* Second Order Differential Equation */ 2 | /* Solves d2y/dx2 = dy/dx - 6x^2 + 12x */ 3 | /* Splits the second order DE into two first order DEs */ 4 | #define _CRT_SECURE_NO_WARNINGS 5 | #include 6 | #include 7 | 8 | double y1a,y2; 9 | 10 | double func1(double x,double y); 11 | double func2(double x,double y); 12 | 13 | int main() 14 | 15 | { 16 | double D1F1,D1F2,D1F3,D1F4; 17 | double D2F1,D2F2,D2F3,D2F4; 18 | double x0,x,h,xn; 19 | 20 | FILE *fptr1; 21 | 22 | 23 | printf("Enter initial x, initial y1,initial y2 ,final x, increment "); 24 | scanf("%lf %lf %lf %lf %lf",&x0,&y1a,&y2,&xn,&h); 25 | x=x0; 26 | 27 | 28 | fptr1=fopen("rk25.dat","w"); 29 | 30 | for(x=x0;x