├── Chapter10 ├── README.md ├── ex10_19.c ├── ex10_20.c ├── ex10_10.c ├── ex10_12.c ├── ex10_18.c ├── ex10_11.c ├── ex10_13.c └── ex10_17.c ├── Chapter11 ├── README.md ├── ex11_07 │ ├── newMast.dat │ ├── oldMast.dat │ ├── trans.dat │ ├── transactions.txt │ ├── accounts.txt │ ├── createTrans.c │ └── createMaster.c ├── ex11_10 │ ├── newMast.dat │ ├── oldMast.dat │ ├── trans.dat │ ├── TODO.txt │ ├── transactions.txt │ ├── newMast1.txt │ ├── newMast2.txt │ └── createMaster.c ├── ex11_14 │ ├── ex11_14.exe │ ├── searchFile.exe │ └── wordlists │ │ └── 2LetterWordlist.txt ├── ex11_12 │ └── hardware.dat ├── ex11_15 │ ├── infile.txt │ ├── outfile.txt │ └── test.txt ├── ex11_16 │ ├── datasize.dat │ └── ex11_16.c └── ex11_17 │ ├── test2.sml │ └── test3.sml ├── Chapter12 ├── README.md ├── ex12_29 │ ├── SimpleBasic_Test_Programs │ │ ├── stringAssignment1.simple │ │ ├── forNextTest.simple │ │ ├── commentTest.simple │ │ ├── inputValidation1.simple │ │ ├── additionTest.simple │ │ ├── intArrayTest1.simple │ │ ├── stringComparisonTest1.simple │ │ ├── stringComparisonTest2.simple │ │ ├── continue1.simple │ │ ├── continue.simple │ │ └── madlib.simple │ ├── RunSML_version_1.0 │ │ ├── oneClickMake.bat │ │ ├── RunSML.exe │ │ ├── docs │ │ │ ├── Data_Dictionary_for_main_c.docx │ │ │ ├── Data_Dictionary_for_SMLOpCodes_h.docx │ │ │ └── Data_Dictionary_for_SimpleTron_h.docx │ │ ├── stringTest1.sml │ │ ├── stringTest2.sml │ │ ├── out.sml │ │ └── src │ │ │ ├── StringMethods.h │ │ │ └── SMLOpCodes.h │ ├── SML_OPCode_Reference.docx │ ├── SimpleTron_version_2.2 │ │ ├── oneClickMake.bat │ │ ├── SimpleTron.exe │ │ ├── docs │ │ │ ├── Data_Dictionary_for_main_c.docx │ │ │ ├── Data_Dictionary_for_SMLOpCodes_h.docx │ │ │ └── Data_Dictionary_for_SimpleTron_h.docx │ │ └── src │ │ │ ├── StringMethods.h │ │ │ └── SMLOpCodes.h │ ├── Simple_Language_Reference.docx │ ├── README.md │ └── SimpleCompiler_version_2.9.3 │ │ ├── RunSML.exe │ │ ├── SimpleTron.exe │ │ ├── SimpleCompiler.exe │ │ ├── oneClickMake.bat │ │ ├── stringTest.simple │ │ ├── docs │ │ ├── Data_Dictionary_for_main_c.docx │ │ ├── Data_Dictionary_for_SMLOpCodes_h.docx │ │ └── Data_Dictionary_for_SimpleCompiler_h.docx │ │ └── src │ │ ├── IntStack.h │ │ ├── ForLoopStack.h │ │ ├── SMLOpCodes.h │ │ └── StringMethods.h ├── ex12_27 │ ├── LIST OF SML OPCODES.docx │ ├── SimpleTronV2.0 │ │ ├── oneClickMake.bat │ │ ├── docs │ │ │ ├── Data_Dictionary_for_main_c.docx │ │ │ ├── Data_Dictionary_for_SMLOpCodes_h.docx │ │ │ └── Data_Dictionary_for_SimpleTron_h.docx │ │ ├── out.sml │ │ ├── test2.sml │ │ ├── test3.sml │ │ └── src │ │ │ ├── StringMethods.h │ │ │ └── SMLOpCodes.h │ ├── SimpleCompilerV2.1 │ │ ├── oneClickMake.bat │ │ ├── docs │ │ │ ├── Data_Dictionary_for_main_c.docx │ │ │ ├── Data_Dictionary_for_SMLOpCodes_h.docx │ │ │ └── Data_Dictionary_for_SimpleCompiler_h.docx │ │ ├── 1.simple │ │ ├── 3.simple │ │ ├── 2.simple │ │ ├── 4.simple │ │ └── src │ │ │ ├── IntStack.h │ │ │ ├── StringMethods.h │ │ │ └── SMLOpCodes.h │ ├── Example_Simple_Programs │ │ ├── 1.simple │ │ ├── 3.simple │ │ ├── 2.simple │ │ ├── 6.simple │ │ ├── 7.simple │ │ ├── 4.simple │ │ ├── 5.simple │ │ ├── 8.simple │ │ └── 9.simple │ └── Example_SML_Programs │ │ ├── test2.sml │ │ └── test3.sml ├── ex12_28 │ ├── SimpleCompilerV2.2 │ │ ├── oneClickMake.bat │ │ ├── docs │ │ │ ├── Data_Dictionary_for_main_c.docx │ │ │ ├── Data_Dictionary_for_SMLOpCodes_h.docx │ │ │ └── Data_Dictionary_for_SimpleCompiler_h.docx │ │ ├── out.sml │ │ ├── test.simple │ │ └── src │ │ │ ├── IntStack.h │ │ │ ├── StringMethods.h │ │ │ └── SMLOpCodes.h │ └── README.md └── ex12_26 │ ├── ex12_26_a.simple │ ├── ex12_26_b.simple │ ├── ex12_26_a1.simple │ ├── ex12_26_c.simple │ ├── ex12_26_f.simple │ ├── ex12_26_g.simple │ ├── ex12_26_d.simple │ └── ex12_26_e.simple ├── Chapter13 ├── README.md ├── ex13_08.c ├── ex13_05.c ├── ex13_04.c ├── ex13_09.c └── ex13_10.c ├── Chapter14 ├── README.md ├── ex14_03.c └── ex14_02.c ├── Chapter2 ├── README.md ├── ex02_29.c ├── ex02_31.c ├── ex02_17.c ├── ex02_25.c ├── ex02_20.c ├── ex02_24.c ├── ex02_26.c ├── ex02_30.c ├── ex02_27.c └── ex02_18.c ├── Chapter3 ├── README.md ├── ex03_37.c ├── ex03_26.c ├── ex03_25.c ├── ex03_38.c ├── ex03_23.c ├── ex03_41.c ├── ex03_22.c ├── ex03_40.c ├── ex03_24.c ├── ex03_33.c ├── ex03_27.c ├── ex03_19.c ├── ex03_34.c ├── ex03_20.c ├── ex03_39.c ├── ex03_36.c ├── ex03_42.c ├── ex03_28.c ├── ex03_21.c ├── ex03_17.c ├── ex03_32.c ├── ex03_35.c └── ex03_44.c ├── Chapter4 ├── README.md ├── ex04_39.c ├── ex04_12.c ├── ex04_13.c ├── ex04_21.c ├── ex04_37.c ├── ex04_29.c ├── ex04_26.c ├── ex04_31.c ├── ex04_14.c ├── ex04_34.c ├── ex04_11.c ├── ex04_18.c ├── ex04_27.c ├── ex04_24.c ├── ex04_23.c ├── ex04_25.c ├── ex04_15.c ├── ex04_09.c ├── ex04_10.c └── ex04_33.c ├── Chapter5 ├── README.md ├── ex05_43.c ├── ex05_10.c ├── ex05_37.c ├── ex05_45.c ├── ex05_20.c ├── ex05_42.c ├── ex05_18.c ├── ex05_39.c ├── ex05_29.c ├── ex05_19.c ├── ex05_30.c ├── ex05_25.c ├── ex05_48.c ├── ex05_31.c ├── ex05_27.c ├── ex05_08.c ├── ex05_16.c ├── ex05_17.c ├── ex05_15.c ├── ex05_24.c ├── ex05_28.c ├── ex05_26.c ├── ex05_41.c ├── ex05_40.c ├── ex05_32.c └── ex05_38.c ├── Chapter6 ├── README.md ├── ex06_38.c ├── ex06_17.c ├── ex06_18.c ├── ex06_19.c ├── ex06_39.c ├── ex06_37.c ├── ex06_30.c ├── ex06_15.c ├── ex06_28.c ├── ex06_34.c ├── ex06_11.c └── ex06_32.c ├── Chapter7 ├── README.md └── SML Programs │ ├── example1.sml │ ├── modulusTestProgram.sml │ ├── exponentiationTestProgram.sml │ ├── example2.sml │ └── ex07_17a.sml ├── Chapter8 ├── README.md ├── ex08_06.c ├── ex08_23.c ├── ex08_24.c ├── ex08_33.c ├── ex08_09.c ├── ex08_16.c ├── ex08_07.c ├── ex08_08.c ├── ex08_13.c └── ex08_15.c ├── Chapter9 ├── README.md ├── ex09_19.c ├── ex09_11.c ├── ex09_08.c ├── ex09_13.c ├── ex09_15.c ├── ex09_10.c ├── ex09_20.c ├── ex09_14.c ├── ex09_07.c ├── ex09_18.c ├── ex09_09.c ├── ex09_17.c └── ex09_12.c ├── Various ├── README.md ├── getAsciiCode.c ├── intToString.c ├── doubleToString.c ├── reverseString.c ├── SieveOfEratosthenes.c ├── stringToInt.c ├── binaryToDecimal.c ├── strcmpAndStrncmp.c ├── stringEndsWith.c ├── HexToInt.c ├── getLine.c ├── strlenVersions.c ├── displayBits.c ├── fileCopy.c ├── getInteger.c └── printListOfDatatypeSizes.c └── README.md /Chapter10/README.md: -------------------------------------------------------------------------------- 1 | C Programming exercises from Chapter 10 2 | -------------------------------------------------------------------------------- /Chapter11/README.md: -------------------------------------------------------------------------------- 1 | C Programming exercises from Chapter 11 2 | -------------------------------------------------------------------------------- /Chapter12/README.md: -------------------------------------------------------------------------------- 1 | C Programming exercises from Chapter 12 2 | -------------------------------------------------------------------------------- /Chapter13/README.md: -------------------------------------------------------------------------------- 1 | C Programming exercises from Chapter 13 2 | -------------------------------------------------------------------------------- /Chapter14/README.md: -------------------------------------------------------------------------------- 1 | C Programming exercises from Chapter 14 2 | -------------------------------------------------------------------------------- /Chapter2/README.md: -------------------------------------------------------------------------------- 1 | C Programming exercises from Chapter 2 2 | -------------------------------------------------------------------------------- /Chapter3/README.md: -------------------------------------------------------------------------------- 1 | C Programming exercises from Chapter 3 2 | -------------------------------------------------------------------------------- /Chapter4/README.md: -------------------------------------------------------------------------------- 1 | C Programming exercises from Chapter 4 2 | -------------------------------------------------------------------------------- /Chapter5/README.md: -------------------------------------------------------------------------------- 1 | C Programming exercises from Chapter 5 2 | -------------------------------------------------------------------------------- /Chapter6/README.md: -------------------------------------------------------------------------------- 1 | C Programming exercises from Chapter 6 2 | -------------------------------------------------------------------------------- /Chapter7/README.md: -------------------------------------------------------------------------------- 1 | C Programming exercises from Chapter 7 2 | -------------------------------------------------------------------------------- /Chapter8/README.md: -------------------------------------------------------------------------------- 1 | C Programming exercises from Chapter 8 2 | -------------------------------------------------------------------------------- /Chapter9/README.md: -------------------------------------------------------------------------------- 1 | C Programming exercises from Chapter 9 2 | -------------------------------------------------------------------------------- /Various/README.md: -------------------------------------------------------------------------------- 1 | Various other programs written whilst I was working through these exercises. 2 | -------------------------------------------------------------------------------- /Chapter11/ex11_07/newMast.dat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AndrewLaing/CHowToProgramExercises/HEAD/Chapter11/ex11_07/newMast.dat -------------------------------------------------------------------------------- /Chapter11/ex11_07/oldMast.dat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AndrewLaing/CHowToProgramExercises/HEAD/Chapter11/ex11_07/oldMast.dat -------------------------------------------------------------------------------- /Chapter11/ex11_07/trans.dat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AndrewLaing/CHowToProgramExercises/HEAD/Chapter11/ex11_07/trans.dat -------------------------------------------------------------------------------- /Chapter11/ex11_10/newMast.dat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AndrewLaing/CHowToProgramExercises/HEAD/Chapter11/ex11_10/newMast.dat -------------------------------------------------------------------------------- /Chapter11/ex11_10/oldMast.dat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AndrewLaing/CHowToProgramExercises/HEAD/Chapter11/ex11_10/oldMast.dat -------------------------------------------------------------------------------- /Chapter11/ex11_10/trans.dat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AndrewLaing/CHowToProgramExercises/HEAD/Chapter11/ex11_10/trans.dat -------------------------------------------------------------------------------- /Chapter11/ex11_14/ex11_14.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AndrewLaing/CHowToProgramExercises/HEAD/Chapter11/ex11_14/ex11_14.exe -------------------------------------------------------------------------------- /Chapter11/ex11_12/hardware.dat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AndrewLaing/CHowToProgramExercises/HEAD/Chapter11/ex11_12/hardware.dat -------------------------------------------------------------------------------- /Chapter11/ex11_10/TODO.txt: -------------------------------------------------------------------------------- 1 | create an all-in-one for create/populate master 2 | check for all necessary files at beginning of main() 3 | -------------------------------------------------------------------------------- /Chapter11/ex11_14/searchFile.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AndrewLaing/CHowToProgramExercises/HEAD/Chapter11/ex11_14/searchFile.exe -------------------------------------------------------------------------------- /Chapter11/ex11_15/infile.txt: -------------------------------------------------------------------------------- 1 | This is from the infile 2 | This is where the text 3 | Was input from 4 | One more line 5 | The end 6 | -------------------------------------------------------------------------------- /Chapter11/ex11_15/outfile.txt: -------------------------------------------------------------------------------- 1 | This is from the infile 2 | This is where the text 3 | Was input from 4 | One more line 5 | The end 6 | -------------------------------------------------------------------------------- /Chapter11/ex11_15/test.txt: -------------------------------------------------------------------------------- 1 | This is from the infile 2 | This is where the text 3 | Was input from 4 | One more line 5 | The end 6 | -------------------------------------------------------------------------------- /Chapter12/ex12_29/SimpleBasic_Test_Programs/stringAssignment1.simple: -------------------------------------------------------------------------------- 1 | 1 dim a$(20) 2 | 2 let a$ = "Fuckin ell lad" 3 | 5 print a$ 4 | 10 end -------------------------------------------------------------------------------- /Chapter11/ex11_10/transactions.txt: -------------------------------------------------------------------------------- 1 | Acct Transaction 2 | 100 23.23 3 | 100 2222.10 4 | 100 2222.10 5 | -------------------------------------------------------------------------------- /Chapter12/ex12_27/LIST OF SML OPCODES.docx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AndrewLaing/CHowToProgramExercises/HEAD/Chapter12/ex12_27/LIST OF SML OPCODES.docx -------------------------------------------------------------------------------- /Chapter12/ex12_29/RunSML_version_1.0/oneClickMake.bat: -------------------------------------------------------------------------------- 1 | @echo off 2 | cmd /k gcc -m32 -Wall -g3 -fverbose-asm -g src/StringMethods.c src/main.c -o RunSML.exe -------------------------------------------------------------------------------- /Chapter12/ex12_29/SML_OPCode_Reference.docx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AndrewLaing/CHowToProgramExercises/HEAD/Chapter12/ex12_29/SML_OPCode_Reference.docx -------------------------------------------------------------------------------- /Chapter12/ex12_27/SimpleTronV2.0/oneClickMake.bat: -------------------------------------------------------------------------------- 1 | @echo off 2 | cmd /k gcc -m32 -Wall -g3 -fverbose-asm -g src/StringMethods.c src/main.c -o SimpleTronV2.0.exe -------------------------------------------------------------------------------- /Chapter12/ex12_29/SimpleTron_version_2.2/oneClickMake.bat: -------------------------------------------------------------------------------- 1 | @echo off 2 | cmd /k gcc -m32 -Wall -g3 -fverbose-asm -g src/StringMethods.c src/main.c -o SimpleTron.exe -------------------------------------------------------------------------------- /Chapter12/ex12_29/RunSML_version_1.0/RunSML.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AndrewLaing/CHowToProgramExercises/HEAD/Chapter12/ex12_29/RunSML_version_1.0/RunSML.exe -------------------------------------------------------------------------------- /Chapter12/ex12_29/Simple_Language_Reference.docx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AndrewLaing/CHowToProgramExercises/HEAD/Chapter12/ex12_29/Simple_Language_Reference.docx -------------------------------------------------------------------------------- /Chapter11/ex11_07/transactions.txt: -------------------------------------------------------------------------------- 1 | Acct Transaction 2 | 100 27.14 3 | 300 62.11 4 | 400 100.56 5 | 900 82.17 6 | -------------------------------------------------------------------------------- /Chapter12/ex12_27/SimpleCompilerV2.1/oneClickMake.bat: -------------------------------------------------------------------------------- 1 | @echo off 2 | cmd /k gcc -m32 -Wall -g3 -fverbose-asm -g src/IntStack.c src/StringMethods.c src/main.c -o SimpleCompilerV2.1.exe -------------------------------------------------------------------------------- /Chapter12/ex12_28/SimpleCompilerV2.2/oneClickMake.bat: -------------------------------------------------------------------------------- 1 | @echo off 2 | cmd /k gcc -m32 -Wall -g3 -fverbose-asm -g src/IntStack.c src/StringMethods.c src/main.c -o SimpleCompilerV2.2.exe -------------------------------------------------------------------------------- /Chapter12/ex12_29/SimpleTron_version_2.2/SimpleTron.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AndrewLaing/CHowToProgramExercises/HEAD/Chapter12/ex12_29/SimpleTron_version_2.2/SimpleTron.exe -------------------------------------------------------------------------------- /Chapter12/ex12_29/README.md: -------------------------------------------------------------------------------- 1 | This is an implementation of a SIMPLE compiler, plus an improved version of the SimpleTron, 2 | and RunSML which takes a .sml file as an arg and runs it. 3 | -------------------------------------------------------------------------------- /Chapter12/ex12_29/SimpleCompiler_version_2.9.3/RunSML.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AndrewLaing/CHowToProgramExercises/HEAD/Chapter12/ex12_29/SimpleCompiler_version_2.9.3/RunSML.exe -------------------------------------------------------------------------------- /Chapter12/ex12_28/README.md: -------------------------------------------------------------------------------- 1 | This is an optimised version of the Simple Compiler that deals with several types of productions, ( STORE x, LOAD x, STORE x ),
2 | before they occur. 3 | -------------------------------------------------------------------------------- /Chapter12/ex12_29/SimpleCompiler_version_2.9.3/SimpleTron.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AndrewLaing/CHowToProgramExercises/HEAD/Chapter12/ex12_29/SimpleCompiler_version_2.9.3/SimpleTron.exe -------------------------------------------------------------------------------- /Chapter12/ex12_29/SimpleCompiler_version_2.9.3/SimpleCompiler.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AndrewLaing/CHowToProgramExercises/HEAD/Chapter12/ex12_29/SimpleCompiler_version_2.9.3/SimpleCompiler.exe -------------------------------------------------------------------------------- /Chapter12/ex12_27/SimpleTronV2.0/docs/Data_Dictionary_for_main_c.docx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AndrewLaing/CHowToProgramExercises/HEAD/Chapter12/ex12_27/SimpleTronV2.0/docs/Data_Dictionary_for_main_c.docx -------------------------------------------------------------------------------- /Chapter12/ex12_29/SimpleCompiler_version_2.9.3/oneClickMake.bat: -------------------------------------------------------------------------------- 1 | @echo off 2 | cmd /k gcc -m32 -Wall -g3 -fverbose-asm -g src/ForLoopStack.c src/IntStack.c src/StringMethods.c src/main.c -o SimpleCompiler.exe -------------------------------------------------------------------------------- /Chapter12/ex12_27/SimpleCompilerV2.1/docs/Data_Dictionary_for_main_c.docx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AndrewLaing/CHowToProgramExercises/HEAD/Chapter12/ex12_27/SimpleCompilerV2.1/docs/Data_Dictionary_for_main_c.docx -------------------------------------------------------------------------------- /Chapter12/ex12_28/SimpleCompilerV2.2/docs/Data_Dictionary_for_main_c.docx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AndrewLaing/CHowToProgramExercises/HEAD/Chapter12/ex12_28/SimpleCompilerV2.2/docs/Data_Dictionary_for_main_c.docx -------------------------------------------------------------------------------- /Chapter12/ex12_29/RunSML_version_1.0/docs/Data_Dictionary_for_main_c.docx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AndrewLaing/CHowToProgramExercises/HEAD/Chapter12/ex12_29/RunSML_version_1.0/docs/Data_Dictionary_for_main_c.docx -------------------------------------------------------------------------------- /Chapter12/ex12_29/SimpleCompiler_version_2.9.3/stringTest.simple: -------------------------------------------------------------------------------- 1 | 10 print "Do you liek mudkips?" 2 | 20 newline 3 | 30 print "Do you liek mudkips?" 4 | 40 newline 5 | 50 print "Do you liek mudkips?" 6 | 100 end -------------------------------------------------------------------------------- /Chapter12/ex12_27/SimpleTronV2.0/docs/Data_Dictionary_for_SMLOpCodes_h.docx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AndrewLaing/CHowToProgramExercises/HEAD/Chapter12/ex12_27/SimpleTronV2.0/docs/Data_Dictionary_for_SMLOpCodes_h.docx -------------------------------------------------------------------------------- /Chapter12/ex12_27/SimpleTronV2.0/docs/Data_Dictionary_for_SimpleTron_h.docx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AndrewLaing/CHowToProgramExercises/HEAD/Chapter12/ex12_27/SimpleTronV2.0/docs/Data_Dictionary_for_SimpleTron_h.docx -------------------------------------------------------------------------------- /Chapter12/ex12_27/SimpleCompilerV2.1/docs/Data_Dictionary_for_SMLOpCodes_h.docx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AndrewLaing/CHowToProgramExercises/HEAD/Chapter12/ex12_27/SimpleCompilerV2.1/docs/Data_Dictionary_for_SMLOpCodes_h.docx -------------------------------------------------------------------------------- /Chapter12/ex12_28/SimpleCompilerV2.2/docs/Data_Dictionary_for_SMLOpCodes_h.docx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AndrewLaing/CHowToProgramExercises/HEAD/Chapter12/ex12_28/SimpleCompilerV2.2/docs/Data_Dictionary_for_SMLOpCodes_h.docx -------------------------------------------------------------------------------- /Chapter12/ex12_29/RunSML_version_1.0/docs/Data_Dictionary_for_SMLOpCodes_h.docx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AndrewLaing/CHowToProgramExercises/HEAD/Chapter12/ex12_29/RunSML_version_1.0/docs/Data_Dictionary_for_SMLOpCodes_h.docx -------------------------------------------------------------------------------- /Chapter12/ex12_29/RunSML_version_1.0/docs/Data_Dictionary_for_SimpleTron_h.docx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AndrewLaing/CHowToProgramExercises/HEAD/Chapter12/ex12_29/RunSML_version_1.0/docs/Data_Dictionary_for_SimpleTron_h.docx -------------------------------------------------------------------------------- /Chapter12/ex12_29/SimpleBasic_Test_Programs/forNextTest.simple: -------------------------------------------------------------------------------- 1 | 10 for a = 1 to 10 2 | 20 print a 3 | 30 print ": green bottle" 4 | 40 newline 5 | 50 next 6 | 60 print "Standing on the wall" 7 | 70 newline 8 | 80 end 9 | -------------------------------------------------------------------------------- /Chapter12/ex12_29/SimpleTron_version_2.2/docs/Data_Dictionary_for_main_c.docx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AndrewLaing/CHowToProgramExercises/HEAD/Chapter12/ex12_29/SimpleTron_version_2.2/docs/Data_Dictionary_for_main_c.docx -------------------------------------------------------------------------------- /Chapter12/ex12_27/SimpleCompilerV2.1/docs/Data_Dictionary_for_SimpleCompiler_h.docx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AndrewLaing/CHowToProgramExercises/HEAD/Chapter12/ex12_27/SimpleCompilerV2.1/docs/Data_Dictionary_for_SimpleCompiler_h.docx -------------------------------------------------------------------------------- /Chapter12/ex12_28/SimpleCompilerV2.2/docs/Data_Dictionary_for_SimpleCompiler_h.docx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AndrewLaing/CHowToProgramExercises/HEAD/Chapter12/ex12_28/SimpleCompilerV2.2/docs/Data_Dictionary_for_SimpleCompiler_h.docx -------------------------------------------------------------------------------- /Chapter12/ex12_29/SimpleBasic_Test_Programs/commentTest.simple: -------------------------------------------------------------------------------- 1 | 5 rem do some mathematics 2 | 10 let a = 30 * 9 / 5 + 32 3 | 11 rem display the answer 4 | 15 print "30 * 9 / 5 + 32 = " 5 | 20 print a 6 | 25 newline 7 | 30 end 8 | -------------------------------------------------------------------------------- /Chapter12/ex12_29/SimpleCompiler_version_2.9.3/docs/Data_Dictionary_for_main_c.docx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AndrewLaing/CHowToProgramExercises/HEAD/Chapter12/ex12_29/SimpleCompiler_version_2.9.3/docs/Data_Dictionary_for_main_c.docx -------------------------------------------------------------------------------- /Chapter12/ex12_29/SimpleTron_version_2.2/docs/Data_Dictionary_for_SMLOpCodes_h.docx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AndrewLaing/CHowToProgramExercises/HEAD/Chapter12/ex12_29/SimpleTron_version_2.2/docs/Data_Dictionary_for_SMLOpCodes_h.docx -------------------------------------------------------------------------------- /Chapter12/ex12_29/SimpleTron_version_2.2/docs/Data_Dictionary_for_SimpleTron_h.docx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AndrewLaing/CHowToProgramExercises/HEAD/Chapter12/ex12_29/SimpleTron_version_2.2/docs/Data_Dictionary_for_SimpleTron_h.docx -------------------------------------------------------------------------------- /Chapter12/ex12_29/SimpleCompiler_version_2.9.3/docs/Data_Dictionary_for_SMLOpCodes_h.docx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AndrewLaing/CHowToProgramExercises/HEAD/Chapter12/ex12_29/SimpleCompiler_version_2.9.3/docs/Data_Dictionary_for_SMLOpCodes_h.docx -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # CHowToProgramExercises 2 | My solutions to the C Programming exercises from the book "C How To Program - Third Edition Deitel & Deitel"
3 | Deitel, H. and Deitel, P. (2001). C. 3rd ed. Upper Saddle River, N.J.: Prentice-Hall. 4 | -------------------------------------------------------------------------------- /Chapter12/ex12_29/SimpleCompiler_version_2.9.3/docs/Data_Dictionary_for_SimpleCompiler_h.docx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AndrewLaing/CHowToProgramExercises/HEAD/Chapter12/ex12_29/SimpleCompiler_version_2.9.3/docs/Data_Dictionary_for_SimpleCompiler_h.docx -------------------------------------------------------------------------------- /Chapter12/ex12_29/SimpleBasic_Test_Programs/inputValidation1.simple: -------------------------------------------------------------------------------- 1 | 5 print "Enter a number between 1 and 10 > " 2 | 10 input a 3 | 20 if a < 1 goto 40 4 | 25 if a > 10 goto 40 5 | 30 print "Well done anon." 6 | 35 goto 50 7 | 40 print "You dun goofed." 8 | 50 end -------------------------------------------------------------------------------- /Chapter11/ex11_07/accounts.txt: -------------------------------------------------------------------------------- 1 | Acct Last Name First Name Balance 2 | 100 Jones Alan 348.17 3 | 300 Smith Mary 27.19 4 | 500 Sharp Sam 0.00 5 | 700 Green Suzy -14.22 6 | -------------------------------------------------------------------------------- /Chapter11/ex11_10/newMast1.txt: -------------------------------------------------------------------------------- 1 | Acct Last Name First Name Balance 2 | 100 Jones Alan 348.17 3 | 300 Smith Mary 27.19 4 | 500 Sharp Sam 0.00 5 | 700 Green Suzy -14.22 6 | -------------------------------------------------------------------------------- /Chapter11/ex11_10/newMast2.txt: -------------------------------------------------------------------------------- 1 | Acct Last Name First Name Balance 2 | 100 Jones Alan 4815.60 3 | 300 Smith Mary 27.19 4 | 500 Sharp Sam 0.00 5 | 700 Green Suzy -14.22 6 | -------------------------------------------------------------------------------- /Chapter12/ex12_29/SimpleBasic_Test_Programs/additionTest.simple: -------------------------------------------------------------------------------- 1 | 5 print "Enter the value of x > " 2 | 10 input x 3 | 15 print "Enter the value of y > " 4 | 20 input y 5 | 30 let r = x * y 6 | 40 newline 7 | 50 print "x * y = " 8 | 60 print r 9 | 99 end 10 | -------------------------------------------------------------------------------- /Chapter12/ex12_29/SimpleBasic_Test_Programs/intArrayTest1.simple: -------------------------------------------------------------------------------- 1 | 10 dim a(3) 2 | 20 let a(1) = 33 3 | 30 let a(2) = 44 4 | 40 let c = a(1) + a(2) 5 | 45 print a(1) 6 | 46 print " + " 7 | 47 print a(2) 8 | 48 print " = " 9 | 50 print c 10 | 55 newline 11 | 60 end 12 | -------------------------------------------------------------------------------- /Chapter12/ex12_27/SimpleTronV2.0/out.sml: -------------------------------------------------------------------------------- 1 | 00 +100999 2 | 01 +200999 3 | 02 +310998 4 | 03 +520011 5 | 04 +200999 6 | 05 +330999 7 | 06 +210995 8 | 07 +200995 9 | 08 +210996 10 | 09 +110996 11 | 10 +500000 12 | 11 +540000 13 | 994 +20 14 | 997 +99 15 | 998 -9999 16 | -------------------------------------------------------------------------------- /Chapter12/ex12_28/SimpleCompilerV2.2/out.sml: -------------------------------------------------------------------------------- 1 | 00 +100999 2 | 01 +200998 3 | 02 +310999 4 | 03 +520011 5 | 04 +200998 6 | 05 +300996 7 | 06 +210998 8 | 07 +200994 9 | 08 +300998 10 | 09 +210994 11 | 10 +500001 12 | 11 +110994 13 | 12 +540000 14 | 992 +20 15 | 996 +1 16 | 997 +60 17 | -------------------------------------------------------------------------------- /Chapter12/ex12_28/SimpleCompilerV2.2/test.simple: -------------------------------------------------------------------------------- 1 | 5 rem sum 1 to x 2 | 10 input x 3 | 15 rem check y == x 4 | 20 if y == x goto 60 5 | 25 rem increment y 6 | 30 let y = y + 1 7 | 35 rem add y to total 8 | 40 let t = t + y 9 | 45 rem loop y 10 | 50 goto 20 11 | 55 rem output result 12 | 60 print t 13 | 99 end -------------------------------------------------------------------------------- /Chapter9/ex09_19.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Filename: ex09_19.c 3 | * Author: Andrew Laing 4 | * Email: parisianconnections@gmail.com 5 | * Date: 18/07/2017. 6 | */ 7 | 8 | #include 9 | 10 | int main() 11 | { 12 | printf("A question mark: %c\n", '?'); 13 | 14 | return 0; 15 | } 16 | 17 | -------------------------------------------------------------------------------- /Chapter12/ex12_29/SimpleBasic_Test_Programs/stringComparisonTest1.simple: -------------------------------------------------------------------------------- 1 | 1 dim a$(20) 2 | 2 dim b$(20) 3 | 3 let a$ = "Frog" 4 | 4 let b$ = "Frog" 5 | 5 if a$ == b$ goto 9 6 | 6 if a$ != b$ goto 11 7 | 7 print "Comparison Failed" 8 | 8 goto 12 9 | 9 print "a$ == b$" 10 | 10 goto 12 11 | 11 print "a$ != b$" 12 | 12 newline 13 | 13 end -------------------------------------------------------------------------------- /Chapter12/ex12_29/SimpleBasic_Test_Programs/stringComparisonTest2.simple: -------------------------------------------------------------------------------- 1 | 1 dim a$(20) 2 | 2 dim b$(20) 3 | 3 let a$ = "Frog" 4 | 4 let b$ = "Frop" 5 | 5 if a$ == b$ goto 9 6 | 6 if a$ != b$ goto 11 7 | 7 print "Comparison Failed" 8 | 8 goto 12 9 | 9 print "a$ == b$" 10 | 10 goto 12 11 | 11 print "a$ != b$" 12 | 12 newline 13 | 13 end -------------------------------------------------------------------------------- /Chapter12/ex12_27/SimpleCompilerV2.1/1.simple: -------------------------------------------------------------------------------- 1 | 10 rem determine and print the sum of two integers 2 | 15 rem 3 | 20 rem input the two integers 4 | 30 input a 5 | 40 input b 6 | 45 rem 7 | 50 rem add integers and store result in c 8 | 60 let c = a + b 9 | 65 rem 10 | 70 rem print the results 11 | 80 print c 12 | 90 rem terminate program execution 13 | 100 end -------------------------------------------------------------------------------- /Chapter12/ex12_27/SimpleCompilerV2.1/3.simple: -------------------------------------------------------------------------------- 1 | 10 rem calculate the squares of several integers 2 | 20 input j 3 | 23 rem 4 | 25 rem test for sentinel value 5 | 30 if j == -9999 goto 99 6 | 33 rem 7 | 35 rem calculate square of j and assign result to k 8 | 40 let k = j * j 9 | 50 print k 10 | 53 rem 11 | 55 rem loop to get next j 12 | 60 goto 20 13 | 99 end 14 | -------------------------------------------------------------------------------- /Chapter12/ex12_27/Example_Simple_Programs/1.simple: -------------------------------------------------------------------------------- 1 | 10 rem determine and print the sum of two integers 2 | 15 rem 3 | 20 rem input the two integers 4 | 30 input a 5 | 40 input b 6 | 45 rem 7 | 50 rem add integers and store result in c 8 | 60 let c = a + b 9 | 65 rem 10 | 70 rem print the results 11 | 80 print c 12 | 90 rem terminate program execution 13 | 100 end -------------------------------------------------------------------------------- /Chapter12/ex12_27/Example_Simple_Programs/3.simple: -------------------------------------------------------------------------------- 1 | 10 rem calculate the squares of several integers 2 | 20 input j 3 | 23 rem 4 | 25 rem test for sentinel value 5 | 30 if j == -9999 goto 99 6 | 33 rem 7 | 35 rem calculate square of j and assign result to k 8 | 40 let k = j * j 9 | 50 print k 10 | 53 rem 11 | 55 rem loop to get next j 12 | 60 goto 20 13 | 99 end 14 | -------------------------------------------------------------------------------- /Chapter12/ex12_27/SimpleCompilerV2.1/2.simple: -------------------------------------------------------------------------------- 1 | 10 rem determine the larger of two integers 2 | 20 input s 3 | 30 input t 4 | 32 rem 5 | 35 rem test if s >= t 6 | 40 if s >= t goto 90 7 | 45 rem 8 | 50 rem t is greater than s so print t 9 | 60 print t 10 | 70 goto 99 11 | 75 rem 12 | 80 rem s is greater than or equal to t so print s 13 | 90 print s 14 | 99 end 15 | -------------------------------------------------------------------------------- /Chapter12/ex12_27/Example_Simple_Programs/2.simple: -------------------------------------------------------------------------------- 1 | 10 rem determine the larger of two integers 2 | 20 input s 3 | 30 input t 4 | 32 rem 5 | 35 rem test if s >= t 6 | 40 if s >= t goto 90 7 | 45 rem 8 | 50 rem t is greater than s so print t 9 | 60 print t 10 | 70 goto 99 11 | 75 rem 12 | 80 rem s is greater than or equal to t so print s 13 | 90 print s 14 | 99 end 15 | -------------------------------------------------------------------------------- /Chapter12/ex12_27/Example_Simple_Programs/6.simple: -------------------------------------------------------------------------------- 1 | 10 rem calculate the squares of several integers 2 | 20 input j 3 | 23 rem 4 | 25 rem test for sentinel value 5 | 30 if j < -9999 goto 99 6 | 33 rem 7 | 35 rem calculate square of j and assign result to k 8 | 40 let k = (j * j) - j 9 | 50 print k 10 | 53 rem 11 | 55 rem loop to get next j 12 | 60 goto 20 13 | 99 end 14 | -------------------------------------------------------------------------------- /Chapter12/ex12_27/Example_Simple_Programs/7.simple: -------------------------------------------------------------------------------- 1 | 10 rem calculate the squares of several integers 2 | 20 input j 3 | 23 rem 4 | 25 rem test for sentinel value 5 | 30 if j > -9999 goto 99 6 | 33 rem 7 | 35 rem calculate square of j and assign result to k 8 | 40 let k = (j * j) - j 9 | 50 print k 10 | 53 rem 11 | 55 rem loop to get next j 12 | 60 goto 20 13 | 99 end 14 | -------------------------------------------------------------------------------- /Chapter12/ex12_27/SimpleCompilerV2.1/4.simple: -------------------------------------------------------------------------------- 1 | 10 rem calculate the squares of several integers 2 | 20 input j 3 | 23 rem 4 | 25 rem test for sentinel value 5 | 30 if j == -9999 goto 99 6 | 33 rem 7 | 35 rem calculate square of j and assign result to k 8 | 40 let k = (j * j) - j 9 | 50 print k 10 | 53 rem 11 | 55 rem loop to get next j 12 | 60 goto 20 13 | 99 end 14 | -------------------------------------------------------------------------------- /Chapter13/ex13_08.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Filename: ex13_08.c 3 | * Author: Andrew Laing 4 | * Email: parisianconnections@gmail.com 5 | * Date: 26/10/2017 6 | * Description: Macro 7 | */ 8 | 9 | #include 10 | #define PRINT(x) printf(x); 11 | 12 | 13 | int main() 14 | { 15 | PRINT("Hello World"); 16 | return 0; 17 | } 18 | 19 | 20 | -------------------------------------------------------------------------------- /Chapter12/ex12_27/Example_Simple_Programs/4.simple: -------------------------------------------------------------------------------- 1 | 10 rem calculate the squares of several integers 2 | 20 input j 3 | 23 rem 4 | 25 rem test for sentinel value 5 | 30 if j == -9999 goto 99 6 | 33 rem 7 | 35 rem calculate square of j and assign result to k 8 | 40 let k = (j * j) - j 9 | 50 print k 10 | 53 rem 11 | 55 rem loop to get next j 12 | 60 goto 20 13 | 99 end 14 | -------------------------------------------------------------------------------- /Chapter12/ex12_27/Example_Simple_Programs/5.simple: -------------------------------------------------------------------------------- 1 | 10 rem calculate the squares of several integers 2 | 20 input j 3 | 23 rem 4 | 25 rem test for sentinel value 5 | 30 if j != -9999 goto 99 6 | 33 rem 7 | 35 rem calculate square of j and assign result to k 8 | 40 let k = (j * j) - j 9 | 50 print k 10 | 53 rem 11 | 55 rem loop to get next j 12 | 60 goto 20 13 | 99 end 14 | -------------------------------------------------------------------------------- /Chapter12/ex12_27/Example_Simple_Programs/8.simple: -------------------------------------------------------------------------------- 1 | 10 rem calculate the squares of several integers 2 | 20 input j 3 | 23 rem 4 | 25 rem test for sentinel value 5 | 30 if j <= -9999 goto 99 6 | 33 rem 7 | 35 rem calculate square of j and assign result to k 8 | 40 let k = (j * j) - j 9 | 50 print k 10 | 53 rem 11 | 55 rem loop to get next j 12 | 60 goto 20 13 | 99 end 14 | -------------------------------------------------------------------------------- /Chapter12/ex12_27/Example_Simple_Programs/9.simple: -------------------------------------------------------------------------------- 1 | 10 rem calculate the squares of several integers 2 | 20 input j 3 | 23 rem 4 | 25 rem test for sentinel value 5 | 30 if j >= -9999 goto 99 6 | 33 rem 7 | 35 rem calculate square of j and assign result to k 8 | 40 let k = (j * j) - j 9 | 50 print k 10 | 53 rem 11 | 55 rem loop to get next j 12 | 60 goto 20 13 | 99 end 14 | -------------------------------------------------------------------------------- /Chapter12/ex12_29/SimpleBasic_Test_Programs/continue1.simple: -------------------------------------------------------------------------------- 1 | 10 dim a$(10) 2 | 20 print "Enter a number > " 3 | 30 input b 4 | 35 let c = b 5 | 40 let b = c * c 6 | 50 print "The square of " 7 | 60 print c 8 | 70 print " is " 9 | 80 print b 10 | 90 newline 11 | 100 print "Do you want to enter another value? (y/n) > " 12 | 110 input a$ 13 | 120 if a$ == "y" goto 20 14 | 130 end 15 | -------------------------------------------------------------------------------- /Chapter13/ex13_05.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Filename: ex13_05.c 3 | * Author: Andrew Laing 4 | * Email: parisianconnections@gmail.com 5 | * Date: 24/10/2017 6 | * Description: Macro 7 | */ 8 | 9 | #include 10 | #define SUM(x,y) printf("The sum of x and y is %d\n",x+y); 11 | 12 | 13 | int main() 14 | { 15 | SUM(5,8); 16 | return 0; 17 | } 18 | 19 | 20 | -------------------------------------------------------------------------------- /Chapter12/ex12_29/SimpleBasic_Test_Programs/continue.simple: -------------------------------------------------------------------------------- 1 | 10 dim a$(10) 2 | 20 print "Do you want to continue? (yes/no) > " 3 | 30 input a$ 4 | 40 if a$ == "y" goto 60 5 | 50 goto 80 6 | 60 print "You want to continue. You entered '" 7 | 65 print a$ 8 | 66 print "'." 9 | 70 goto 90 10 | 80 print "You do NOT want to continue. You entered '" 11 | 81 print a$ 12 | 82 print "'." 13 | 90 newline 14 | 100 end -------------------------------------------------------------------------------- /Chapter5/ex05_43.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Filename: ex05_43.c 3 | * Author: Andrew Laing 4 | * Email: parisianconnections@gmail.com 5 | * Date: 06/05/2017 6 | */ 7 | 8 | #include 9 | 10 | 11 | int main() 12 | { 13 | static int count=1; 14 | 15 | printf("Count = %d\n", count++); 16 | while(count < 6) { 17 | main(); 18 | } 19 | 20 | return 0; 21 | } 22 | 23 | -------------------------------------------------------------------------------- /Various/getAsciiCode.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Filename: getAsciiCode.c 3 | * Author: Andrew Laing 4 | * Email: parisianconnections@gmail.com 5 | * Date: 06/08/2017 6 | */ 7 | 8 | #include 9 | 10 | int main() 11 | { 12 | char c; 13 | printf("Enter a character: "); 14 | c=getchar(); 15 | fflush(stdin); 16 | printf("%c = %d", c, c ); 17 | 18 | return 0; 19 | } 20 | 21 | 22 | -------------------------------------------------------------------------------- /Chapter12/ex12_29/SimpleBasic_Test_Programs/madlib.simple: -------------------------------------------------------------------------------- 1 | 10 dim a$(20) 2 | 20 dim b$(20) 3 | 30 dim c$(20) 4 | 40 print "Enter a male name > " 5 | 50 input a$ 6 | 60 print "Enter an emotion > " 7 | 70 input b$ 8 | 80 print "Enter an action > " 9 | 90 input c$ 10 | 100 newline 11 | 110 print a$ 12 | 120 print " was feeling " 13 | 130 print b$ 14 | 140 print " so he decided to " 15 | 150 print c$ 16 | 160 newline 17 | 170 end 18 | -------------------------------------------------------------------------------- /Chapter3/ex03_37.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Filename: ex03_37.c 3 | * Author: Andrew Laing 4 | * Email: parisianconnections@gmail.com 5 | * Date: 16/04/2017 6 | */ 7 | 8 | #include 9 | 10 | 11 | int main() 12 | { 13 | int i = 1; 14 | 15 | while(i<=3000000) { 16 | if(i%1000000 == 0) 17 | printf("%d\n", i); 18 | i++; 19 | } 20 | 21 | return 0; 22 | } 23 | -------------------------------------------------------------------------------- /Chapter3/ex03_26.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Filename: ex03_26.c 3 | * Author: Andrew Laing 4 | * Email: parisianconnections@gmail.com 5 | * Date: 15/04/2017 6 | */ 7 | 8 | #include 9 | 10 | int main() 11 | { 12 | int a; 13 | 14 | printf("A\tA+2\tA+4\tA+6\n\n"); 15 | 16 | for(a=3; a<=15; a+=3) 17 | printf("%d\t%d\t%d\t%d\n", a, (a+2), (a+4), (a+6)); 18 | 19 | return 0; 20 | } 21 | -------------------------------------------------------------------------------- /Chapter11/ex11_16/datasize.dat: -------------------------------------------------------------------------------- 1 | Data Type Size 2 | char 1 3 | unsigned char 1 4 | short int 2 5 | unsigned short int 2 6 | int 4 7 | unsigned int 4 8 | long int 4 9 | unsigned long int 4 10 | float 4 11 | double 8 12 | long double 12 13 | -------------------------------------------------------------------------------- /Chapter3/ex03_25.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Filename: ex03_25.c 3 | * Author: Andrew Laing 4 | * Email: parisianconnections@gmail.com 5 | * Date: 15/04/2017 6 | */ 7 | 8 | #include 9 | 10 | int main() 11 | { 12 | int n; 13 | 14 | printf("N\t10*N\t100*N\t1000*N\n\n"); 15 | 16 | for(n=1; n<=10; n++) 17 | printf("%d\t%d\t%d\t%d\n", n, (n*10), (n*100), (n*1000)); 18 | 19 | return 0; 20 | } 21 | -------------------------------------------------------------------------------- /Chapter3/ex03_38.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Filename: ex03_38.c 3 | * Author: Andrew Laing 4 | * Email: parisianconnections@gmail.com 5 | * Date: 16/04/2017 6 | */ 7 | 8 | #include 9 | 10 | 11 | int main() 12 | { 13 | int i = 1; 14 | 15 | while(i<=100) 16 | { 17 | printf("*"); 18 | 19 | if(i++%10 == 0) 20 | printf("\n"); 21 | } 22 | 23 | return 0; 24 | } 25 | -------------------------------------------------------------------------------- /Chapter3/ex03_23.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Filename: ex03_23.c 3 | * Author: Andrew Laing 4 | * Email: parisianconnections@gmail.com 5 | * Date: 15/04/2017 6 | */ 7 | 8 | #include 9 | 10 | int main() 11 | { 12 | int i; 13 | 14 | for(i=1; i<=10; i++) 15 | { 16 | if(i<10) 17 | printf("%d ", i); 18 | else 19 | printf("%d", i); 20 | } 21 | 22 | return 0; 23 | } 24 | -------------------------------------------------------------------------------- /Chapter12/ex12_26/ex12_26_a.simple: -------------------------------------------------------------------------------- 1 | 10 rem calculate the average of three numbers 2 | 11 rem 3 | 15 rem start by inputting the three values (a,b,c) 4 | 20 input a 5 | 30 input b 6 | 40 input c 7 | 45 rem 8 | 46 rem next add the numbers together to get the total (t) 9 | 50 let t = a + b + c 10 | 55 rem 11 | 56 rem next calculate the average (v) 12 | 60 let v = t / 3 13 | 65 rem 14 | 66 rem print out the result 15 | 70 print v 16 | 80 end 17 | 18 | -------------------------------------------------------------------------------- /Chapter9/ex09_11.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Filename: ex09_11.c 3 | * Author: Andrew Laing 4 | * Email: parisianconnections@gmail.com 5 | * Date: 17/07/2017. 6 | */ 7 | 8 | #include 9 | 10 | int main() 11 | { 12 | double a = 100.453627; 13 | 14 | 15 | printf("%.0f\n", a); 16 | printf("%.1f\n", a); 17 | printf("%.2f\n", a); 18 | printf("%.3f\n", a); 19 | printf("%.4f\n", a); 20 | 21 | return 0; 22 | } 23 | 24 | 25 | 26 | -------------------------------------------------------------------------------- /Chapter14/ex14_03.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Filename: ex14_03.c 3 | * Author: Andrew Laing 4 | * Email: parisianconnections@gmail.com 5 | * Date: 27/10/2017 6 | * Description: Command line args 7 | */ 8 | 9 | #include 10 | 11 | int main( int argc, char *argv[]) 12 | { 13 | int i; 14 | 15 | /* argv[0] is the name of the program */ 16 | 17 | for(i=1; i 11 | 12 | 13 | int main() 14 | { 15 | int i = 2; 16 | 17 | while(i>0) { 18 | printf("%d ", i); 19 | i*=2; 20 | } 21 | 22 | return 0; 23 | } 24 | -------------------------------------------------------------------------------- /Chapter4/ex04_39.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Filename: ex04_39.c 3 | * Author: Andrew Laing 4 | * Email: parisianconnections@gmail.com 5 | * Date: 24/04/2017 6 | */ 7 | 8 | #include 9 | 10 | 11 | int main() 12 | { 13 | int x; 14 | 15 | for(x = 1; x <= 10; x++) 16 | { 17 | if(x != 5) 18 | printf("%d ", x ); 19 | } 20 | 21 | printf("\nUsed continue to skip printing the value 5\n"); 22 | 23 | return 0; 24 | } 25 | -------------------------------------------------------------------------------- /Chapter7/SML Programs/example1.sml: -------------------------------------------------------------------------------- 1 | # This program inputs two numbers and prints out 2 | # the result to the terminal. 3 | 00 +1007 # Read into A 4 | 01 +1008 # Read into B 5 | 02 +2007 # Load A into Accumulator 6 | 03 +3008 # Add B to Accumulator 7 | 04 +2109 # Store Accumulator value into C 8 | 05 +1109 # Write C to terminal 9 | 06 +4300 # Halt program 10 | 07 +0000 # Variable A 11 | 08 +0000 # Variable B 12 | 09 +0000 # Variable C 13 | -------------------------------------------------------------------------------- /Chapter7/SML Programs/modulusTestProgram.sml: -------------------------------------------------------------------------------- 1 | # This program inputs two numbers and prints out 2 | # the result to the terminal. 3 | 00 +1007 # Read into A 4 | 01 +1008 # Read into B 5 | 02 +2007 # Load A into Accumulator 6 | 03 +3408 # Modulo A by B 7 | 04 +2109 # Store Accumulator value into C 8 | 05 +1109 # Write C to terminal 9 | 06 +4300 # Halt program 10 | 07 +0000 # Variable A 11 | 08 +0000 # Variable B 12 | 09 +0000 # Variable C 13 | -------------------------------------------------------------------------------- /Chapter9/ex09_08.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Filename: ex09_08.c 3 | * Author: Andrew Laing 4 | * Email: parisianconnections@gmail.com 5 | * Date: 16/07/2017. 6 | */ 7 | 8 | #include 9 | 10 | 11 | int main() 12 | { 13 | int i, x, y; 14 | 15 | for(i=0; i<4; i++) 16 | { 17 | printf("Enter the two numbers: "); 18 | scanf("%i%d", &x, &y); 19 | printf("%d %d\n", x, y); 20 | } 21 | 22 | return 0; 23 | } 24 | 25 | 26 | 27 | 28 | -------------------------------------------------------------------------------- /Chapter7/SML Programs/exponentiationTestProgram.sml: -------------------------------------------------------------------------------- 1 | # This program inputs two numbers and prints out 2 | # the result to the terminal. 3 | 00 +1007 # Read into A 4 | 01 +1008 # Read into B 5 | 02 +2007 # Load A into Accumulator 6 | 03 +3508 # Raise A to the power of B 7 | 04 +2109 # Store Accumulator value into C 8 | 05 +1109 # Write C to terminal 9 | 06 +4300 # Halt program 10 | 07 +0000 # Variable A 11 | 08 +0000 # Variable B 12 | 09 +0000 # Variable C 13 | -------------------------------------------------------------------------------- /Chapter12/ex12_29/RunSML_version_1.0/stringTest1.sml: -------------------------------------------------------------------------------- 1 | 00 +160989 2 | 01 +100999 3 | 02 +160973 4 | 03 +110999 5 | 04 +540000 6 | 973 +29116 7 | 974 +104101 8 | 975 +32110 9 | 976 +117109 10 | 977 +98101 11 | 978 +114032 12 | 979 +121111 13 | 980 +117032 14 | 981 +101110 15 | 982 +116101 16 | 983 +114101 17 | 984 +100032 18 | 985 +119097 19 | 986 +115032 20 | 987 +45032 21 | 989 +16105 22 | 990 +110112 23 | 991 +117116 24 | 992 +32097 25 | 993 +32110 26 | 994 +117109 27 | 995 +98101 28 | 996 +114058 29 | 997 +32000 30 | -------------------------------------------------------------------------------- /Chapter12/ex12_29/RunSML_version_1.0/stringTest2.sml: -------------------------------------------------------------------------------- 1 | 00 +160990 2 | 01 +100988 3 | 02 +160972 4 | 03 +110988 5 | 04 +540000 6 | 972 +29116 7 | 973 +104101 8 | 974 +32110 9 | 975 +117109 10 | 976 +98101 11 | 977 +114032 12 | 978 +121111 13 | 979 +117032 14 | 980 +101110 15 | 981 +116101 16 | 982 +114101 17 | 983 +100032 18 | 984 +119097 19 | 985 +115032 20 | 986 +45032 21 | 990 +16105 22 | 991 +110112 23 | 992 +117116 24 | 993 +32097 25 | 994 +32110 26 | 995 +117109 27 | 996 +98101 28 | 997 +114058 29 | 998 +32000 30 | -------------------------------------------------------------------------------- /Chapter5/ex05_10.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Filename: ex05_10.c 3 | * Author: Andrew Laing 4 | * Email: parisianconnections@gmail.com 5 | * Date: 26/04/2017 6 | */ 7 | 8 | #include 9 | #include 10 | 11 | 12 | int main() 13 | { 14 | int i; 15 | float num; 16 | 17 | for(i=0; i<3; i++) 18 | { 19 | printf("Enter a float: "); 20 | scanf("%f", &num); 21 | printf("Num = %f Rounded = %f\n", num, floor(num + .5)); 22 | } 23 | 24 | return 0; 25 | } 26 | 27 | -------------------------------------------------------------------------------- /Chapter3/ex03_22.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Filename: ex03_22.c 3 | * Author: Andrew Laing 4 | * Email: parisianconnections@gmail.com 5 | * Date: 15/04/2017 6 | */ 7 | 8 | #include 9 | 10 | int main() 11 | { 12 | int c = 5; 13 | 14 | printf("c = %d\n", c); 15 | printf("c-- = %d\n", c--); 16 | printf("c = %d\n\n", c); 17 | 18 | c = 5; 19 | 20 | printf(" c = %d\n", c); 21 | printf("--c = %d\n", --c); 22 | printf(" c = %d\n", c); 23 | 24 | return 0; 25 | } 26 | 27 | -------------------------------------------------------------------------------- /Chapter11/ex11_17/test2.sml: -------------------------------------------------------------------------------- 1 | # This program inputs two numbers, raises the first 2 | # to the power of the second, and prints out the result 3 | # to the terminal. 4 | 00 +100007 # Read into A 5 | 01 +100008 # Read into B 6 | 02 +200007 # Load A into Accumulator 7 | 03 +350008 # Raise A to the power of B 8 | 04 +210009 # Store Accumulator value into C 9 | 05 +110009 # Write C to terminal 10 | 06 +530000 # Halt program 11 | 07 +000000 # Variable A 12 | 08 +000000 # Variable B 13 | 09 +000000 # Variable C 14 | -------------------------------------------------------------------------------- /Chapter11/ex11_17/test3.sml: -------------------------------------------------------------------------------- 1 | # This program inputs two numbers, raises the first 2 | # to the power of the second, and prints out the result 3 | # to the terminal. 4 | 00 +100007 # Read into A 5 | 01 +100008 # Read into B 6 | 02 +200007 # Load A into Accumulator 7 | 03 +350008 # Raise A to the power of B 8 | 04 +210009 # Store Accumulator value into C 9 | 05 +110009 # Write C to terminal 10 | 06 +530000 # Halt program 11 | 07 +000000 # Variable A 12 | 08 +000000 # Variable B 13 | 09 +000000 # Variable C 14 | -------------------------------------------------------------------------------- /Chapter12/ex12_26/ex12_26_b.simple: -------------------------------------------------------------------------------- 1 | 10 rem Use a sentinel controlled loop to input 10 integers, 2 | 20 rem compute and print their sum (s) 3 | 21 rem 4 | 22 rem set sum (s) to 0 5 | 30 let s = 0 6 | 31 rem 7 | 32 rem Input a number into a, if it is a sentinel value (less than zero) 8 | 33 rem then print out the result, else add it to the sum and continue 9 | 34 rem inputting numbers 10 | 40 input a 11 | 50 if a < 0 goto 80 12 | 60 let s = s + a 13 | 70 goto 30 14 | 71 rem 15 | 72 rem print out the result and quit 16 | 80 print s 17 | 90 end 18 | -------------------------------------------------------------------------------- /Chapter13/ex13_04.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Filename: ex13_04.c 3 | * Author: Andrew Laing 4 | * Email: parisianconnections@gmail.com 5 | * Date: 24/10/2017 6 | * Description: Macro 7 | */ 8 | 9 | #include 10 | #define PI 3.14159 11 | #define SPHEREVOLUME(x) ( (4.0 / 3) * PI * ( (x) * (x) * (x) ) ) 12 | 13 | 14 | int main() 15 | { 16 | int i; 17 | printf("Radius\tVolume\n\n"); 18 | for(i=1; i<=10; i++) 19 | { 20 | printf("%3d\t%lf\n", i, SPHEREVOLUME(i)); 21 | } 22 | return 0; 23 | } 24 | 25 | 26 | -------------------------------------------------------------------------------- /Chapter12/ex12_27/SimpleTronV2.0/test2.sml: -------------------------------------------------------------------------------- 1 | # This program inputs two numbers, raises the first 2 | # to the power of the second, and prints out the result 3 | # to the terminal. 4 | 00 +100007 # Read into A 5 | 01 +100008 # Read into B 6 | 02 +200007 # Load A into Accumulator 7 | 03 +350008 # Raise A to the power of B 8 | 04 +210009 # Store Accumulator value into C 9 | 05 +110009 # Write C to terminal 10 | 06 +540000 # Halt program 11 | 07 +000000 # Variable A 12 | 08 +000000 # Variable B 13 | 09 +000000 # Variable C 14 | -------------------------------------------------------------------------------- /Chapter12/ex12_27/Example_SML_Programs/test2.sml: -------------------------------------------------------------------------------- 1 | # This program inputs two numbers, raises the first 2 | # to the power of the second, and prints out the result 3 | # to the terminal. 4 | 00 +100007 # Read into A 5 | 01 +100008 # Read into B 6 | 02 +200007 # Load A into Accumulator 7 | 03 +350008 # Raise A to the power of B 8 | 04 +210009 # Store Accumulator value into C 9 | 05 +110009 # Write C to terminal 10 | 06 +540000 # Halt program 11 | 07 +000000 # Variable A 12 | 08 +000000 # Variable B 13 | 09 +000000 # Variable C 14 | -------------------------------------------------------------------------------- /Chapter4/ex04_12.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Filename: ex04_12.c 3 | * Author: Andrew Laing 4 | * Email: parisianconnections@gmail.com 5 | * Date: 19/04/2017 6 | * Description: Write a program that calculates and prints the sum 7 | * of the even integers from 2 to 30. 8 | */ 9 | 10 | #include 11 | 12 | 13 | int main() 14 | { 15 | int sum = 0, number; 16 | 17 | for(number = 2; number <= 30; number += 2) 18 | sum += number; 19 | 20 | printf("Sum is %d\n", sum ); 21 | 22 | return 0; 23 | } 24 | 25 | -------------------------------------------------------------------------------- /Chapter4/ex04_13.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Filename: ex04_13.c 3 | * Author: Andrew Laing 4 | * Email: parisianconnections@gmail.com 5 | * Date: 19/04/2017 6 | * Description: Write a program that calculates and prints the sum 7 | * of the odd integers from 1 to 15. 8 | */ 9 | 10 | #include 11 | 12 | 13 | int main() 14 | { 15 | int sum = 0, number; 16 | 17 | for(number = 1; number <= 15; number += 2) 18 | sum += number; 19 | 20 | printf("Sum is %d\n", sum ); 21 | 22 | return 0; 23 | } 24 | 25 | -------------------------------------------------------------------------------- /Chapter9/ex09_13.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Filename: ex09_13.c 3 | * Author: Andrew Laing 4 | * Email: parisianconnections@gmail.com 5 | * Date: 17/07/2017. 6 | */ 7 | 8 | #include 9 | 10 | double fahrenheitToCelsius( double fahrenheit ); 11 | 12 | int main() 13 | { 14 | int i; 15 | for(i=0; i<=212; i++) 16 | printf("%10d%+10.3f\n", i, fahrenheitToCelsius(i) ); 17 | 18 | return 0; 19 | } 20 | 21 | 22 | double fahrenheitToCelsius( double fahrenheit ) 23 | { 24 | return (fahrenheit-32)/1.8; 25 | } 26 | 27 | 28 | -------------------------------------------------------------------------------- /Chapter12/ex12_27/SimpleTronV2.0/test3.sml: -------------------------------------------------------------------------------- 1 | # This program inputs two numbers, raises the first 2 | # to the power of the second, and prints out the result 3 | # to the terminal. 4 | 00 +100007 # Read into A 5 | 01 +100008 # Read into B 6 | 02 +200007 # Load A into Accumulator 7 | 03 +350008 # Raise A to the power of B 8 | 04 +210009 # Store Accumulator value into C 9 | 05 +110009 # Write C to terminal 10 | 06 +540000 # Halt program 11 | 07 +000000 # Variable A 12 | 08 +000000 # Variable B 13 | 09 +000000 # Variable C 14 | -------------------------------------------------------------------------------- /Chapter7/SML Programs/example2.sml: -------------------------------------------------------------------------------- 1 | # This program inputs two numbers, determines the smallest 2 | # then prints the result to the terminal. 3 | 00 +1009 # Read into A 4 | 01 +1010 # Read into B 5 | 02 +2009 # Load A into Accumulator 6 | 03 +3110 # Subtract B from Accumulator 7 | 04 +4107 # Branch to 07 if Accumulator is negative 8 | 05 +1109 # Write A to terminal 9 | 06 +4300 # Halt program 10 | 07 +1110 # Write B to terminal 11 | 08 +4300 # Halt program 12 | 09 +0000 # Variable A 13 | 10 +0000 # Variable B -------------------------------------------------------------------------------- /Chapter12/ex12_27/Example_SML_Programs/test3.sml: -------------------------------------------------------------------------------- 1 | # This program inputs two numbers, raises the first 2 | # to the power of the second, and prints out the result 3 | # to the terminal. 4 | 00 +100007 # Read into A 5 | 01 +100008 # Read into B 6 | 02 +200007 # Load A into Accumulator 7 | 03 +350008 # Raise A to the power of B 8 | 04 +210009 # Store Accumulator value into C 9 | 05 +110009 # Write C to terminal 10 | 06 +540000 # Halt program 11 | 07 +000000 # Variable A 12 | 08 +000000 # Variable B 13 | 09 +000000 # Variable C 14 | -------------------------------------------------------------------------------- /Chapter2/ex02_29.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Filename: ex02_29.c 3 | * Author: Andrew Laing 4 | * Email: parisianconnections@gmail.com 5 | * Date: 13/04/2017 6 | * Description: Display the integer equivalents of letters. 7 | */ 8 | 9 | #include 10 | 11 | 12 | int main() 13 | { 14 | char* letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ abcdefghijklmnopqrstuvwxyz"; 15 | int i; 16 | for(i=0; i<53; i++) { 17 | printf("%d\t", letters[i]); 18 | if((i+1) % 5 == 0) 19 | printf("\n"); 20 | } 21 | 22 | return 0; 23 | } 24 | -------------------------------------------------------------------------------- /Chapter5/ex05_37.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Filename: ex05_37.c 3 | * Author: Andrew Laing 4 | * Email: parisianconnections@gmail.com 5 | * Date: 04/05/2017 6 | */ 7 | 8 | #include 9 | 10 | int power( int, int ); 11 | 12 | 13 | int main() 14 | { 15 | int n=3, exp=4; 16 | 17 | printf("power(3,4) = %d\n", power(n, exp)); 18 | 19 | return 0; 20 | } 21 | 22 | 23 | int power( int num, int exponent) 24 | { 25 | if(exponent==1) { 26 | return num; 27 | } 28 | else 29 | return num * power(num, exponent-1); 30 | } 31 | 32 | -------------------------------------------------------------------------------- /Chapter9/ex09_15.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Filename: ex09_15.c 3 | * Author: Andrew Laing 4 | * Email: parisianconnections@gmail.com 5 | * Date: 17/07/2017. 6 | */ 7 | 8 | #include 9 | 10 | 11 | 12 | int main() 13 | { 14 | /* Note: see compiler warning. Had trigraphs been enabled 15 | * the top line would have printed a pipe symbol 16 | * instead of ??! */ 17 | printf("trigraph ??! trigraph\n"); 18 | printf("trigraph \?\?! trigraph\n"); 19 | printf("trigraph ?" "?! trigraph\n"); 20 | return 0; 21 | } 22 | 23 | 24 | -------------------------------------------------------------------------------- /Chapter4/ex04_21.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Filename: ex04_21.c 3 | * Author: Andrew Laing 4 | * Email: parisianconnections@gmail.com 5 | * Date: 21/04/2017 6 | * Description: Rewrite the program of fig04_02 so that the initialisation 7 | * of the variable counter is done in the declaration instead 8 | * of the for structure. 9 | */ 10 | 11 | #include 12 | 13 | 14 | int main() 15 | { 16 | int counter = 1; 17 | 18 | for( ; counter <= 10; counter++) 19 | printf("%d\n", counter ); 20 | 21 | return 0; 22 | } 23 | 24 | -------------------------------------------------------------------------------- /Chapter5/ex05_45.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Filename: ex05_45.c 3 | * Author: Andrew Laing 4 | * Email: parisianconnections@gmail.com 5 | * Date: 07/05/2017 6 | */ 7 | 8 | #include 9 | #include 10 | 11 | 12 | double distance(double x1, double x2, double y1, double y2) 13 | { 14 | return sqrt( pow((x2-x1), 2) + pow((y2-y1), 2) ); 15 | } 16 | 17 | 18 | int main() 19 | { 20 | double x1=1.0, y1=1.0; 21 | double x2=6.0, y2=6.0; 22 | 23 | printf("Distance between 1,1 and 6,6 = %.2f\n", distance(x1, x2, y1, y2)); 24 | 25 | return 0; 26 | } 27 | 28 | -------------------------------------------------------------------------------- /Chapter6/ex06_38.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Filename: ex06_38.c 3 | * Author: Andrew Laing 4 | * Email: parisianconnections@gmail.com 5 | * Date: 21/05/2017 6 | */ 7 | 8 | #include 9 | 10 | void stringReverse( char[]); 11 | 12 | 13 | int main() 14 | { 15 | char str[] = "Test String 3"; 16 | 17 | stringReverse( str ); 18 | 19 | return 0; 20 | } 21 | 22 | 23 | void stringReverse( char str[] ) 24 | { 25 | if(str[0]=='\0') 26 | return; 27 | else 28 | stringReverse(&str[1]); 29 | 30 | printf("%c", str[0]); 31 | } 32 | 33 | -------------------------------------------------------------------------------- /Chapter3/ex03_40.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Filename: ex03_40.c 3 | * Author: Andrew Laing 4 | * Email: parisianconnections@gmail.com 5 | * Date: 16/04/2017 6 | */ 7 | 8 | #include 9 | 10 | 11 | int main() 12 | { 13 | int i = 1; 14 | int addSpace = 1; 15 | 16 | while(i<=64) 17 | { 18 | printf("* "); 19 | 20 | if(i%8==0) { 21 | printf("\n"); 22 | addSpace++; 23 | 24 | if(addSpace%2==0) 25 | printf(" "); 26 | } 27 | i++; 28 | } 29 | 30 | return 0; 31 | } 32 | -------------------------------------------------------------------------------- /Chapter4/ex04_37.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Filename: ex04_37.c 3 | * Author: Andrew Laing 4 | * Email: parisianconnections@gmail.com 5 | * Date: 24/04/2017 6 | */ 7 | 8 | #include 9 | 10 | 11 | int main() 12 | { 13 | int x; 14 | int leftLoopAt=0; 15 | 16 | for(x = 1; x <= 10; x++) 17 | { 18 | if(x == 5) { 19 | leftLoopAt = x; 20 | x=11; 21 | } 22 | else 23 | printf("%d ", x ); 24 | } 25 | 26 | printf("\nBroke out of loop at x == %d\n", leftLoopAt ); 27 | 28 | return 0; 29 | } 30 | 31 | -------------------------------------------------------------------------------- /Chapter12/ex12_26/ex12_26_a1.simple: -------------------------------------------------------------------------------- 1 | 10 rem calculate the average of three numbers version 2 2 | 11 rem 3 | 12 rem initialise total(t) to 0, max(m) to 3 and count(c) to 0 4 | 20 let t = 0 5 | 30 let m = 3 6 | 40 let c = 0 7 | 50 if c == m goto 100 8 | 51 rem 9 | 52 rem while count < max input a number and add it to the total 10 | 60 input a 11 | 70 let t = t + a 12 | 71 rem 13 | 72 rem increment the count and go back to the start of the loop 14 | 80 let c = c + 1 15 | 90 goto 50 16 | 91 rem 17 | 92 rem calculate the average (v) and print it out 18 | 100 let v = t / m 19 | 110 print v 20 | 120 end 21 | 22 | -------------------------------------------------------------------------------- /Chapter3/ex03_24.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Filename: ex03_24.c 3 | * Author: Andrew Laing 4 | * Email: parisianconnections@gmail.com 5 | * Date: 15/04/2017 6 | */ 7 | 8 | #include 9 | 10 | int main() 11 | { 12 | int i; 13 | int counter, number; 14 | int largest = 0; 15 | 16 | for(i=0; i<10; i++) 17 | { 18 | printf("Input number: "); 19 | scanf("%d", &number); 20 | 21 | if(number > largest) 22 | largest = number; 23 | } 24 | 25 | printf("The largest number entered was %d", largest); 26 | 27 | return 0; 28 | } 29 | 30 | -------------------------------------------------------------------------------- /Chapter5/ex05_20.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Filename: ex05_20.c 3 | * Author: Andrew Laing 4 | * Email: parisianconnections@gmail.com 5 | * Date: 27/04/2017 6 | */ 7 | 8 | #include 9 | 10 | 11 | void fillCharacter( int, char ); 12 | 13 | 14 | int main() 15 | { 16 | fillCharacter(5, '#'); 17 | return 0; 18 | } 19 | 20 | 21 | void fillCharacter(int n, char c) 22 | { 23 | int i, j; 24 | 25 | for(i=0; i 9 | #define SIZE 10 10 | 11 | int whatIsThis( const int[], int ); 12 | 13 | 14 | int main() 15 | { 16 | int x, a[ SIZE ] = {1,2,3,4,5,6,7,8,9,10}; 17 | 18 | x = whatIsThis( a, SIZE ); 19 | printf("Result is %d\n", x); 20 | 21 | return 0; 22 | } 23 | 24 | 25 | int whatIsThis( const int b[], int p ) 26 | { 27 | if(p==1) 28 | return b[0]; 29 | else 30 | return b[p-1] + whatIsThis(b, p-1); 31 | } 32 | -------------------------------------------------------------------------------- /Chapter2/ex02_31.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Filename: ex02_31.c 3 | * Author: Andrew Laing 4 | * Email: parisianconnections@gmail.com 5 | * Date: 13/04/2017 6 | * Description: Write a program which calculates and prints the squares 7 | * and cubes of the numbers from 0 to 10 and uses tabs to 8 | * display them in a table of values. 9 | */ 10 | 11 | #include 12 | 13 | 14 | int main() 15 | { 16 | int i; 17 | 18 | /* Print column names */ 19 | printf("number\tsquare\tcube\n"); 20 | 21 | for(i=0; i<=10; i++) 22 | printf("%d\t%d\t%d\n", i, i*i, i*i*i); 23 | 24 | return 0; 25 | } 26 | -------------------------------------------------------------------------------- /Chapter6/ex06_18.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Filename: ex06_18.c 3 | * Author: Andrew Laing 4 | * Email: parisianconnections@gmail.com 5 | * Date: 11/05/2017 6 | */ 7 | 8 | #include 9 | #define SIZE 10 10 | 11 | void someFunction( const int[], int ); 12 | 13 | int main() 14 | { 15 | int a[ SIZE ] = {8,3,1,2,6,0,9,7,4,5}; 16 | 17 | printf("Answer is:\n"); 18 | someFunction( a, SIZE ); 19 | printf("\n"); 20 | 21 | return 0; 22 | } 23 | 24 | void someFunction( const int b[], int size ) 25 | { 26 | if( size > 0) 27 | { 28 | someFunction( &b[ 1 ], size-1 ); 29 | printf("%d ", b[ 0 ]); 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /Chapter5/ex05_42.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Filename: ex05_42.c 3 | * Author: Andrew Laing 4 | * Email: parisianconnections@gmail.com 5 | * Date: 06/05/2017 6 | */ 7 | 8 | #include 9 | int DEBUG = 1; 10 | int start = 1; 11 | 12 | int gcd( int, int ); 13 | 14 | int main() 15 | { 16 | printf("gcd(81, 9) = %d\n", gcd(81, 9)); 17 | printf("gcd(7, 21) = %d\n", gcd(7, 21)); 18 | printf("gcd(17, 6) = %d\n", gcd(17, 6)); 19 | printf("gcd(116, 96) = %d\n", gcd(116, 96)); 20 | 21 | return 0; 22 | } 23 | 24 | int gcd( int x, int y) 25 | { 26 | if(y==0) 27 | return x; 28 | else 29 | return gcd(y, x%y); 30 | } 31 | 32 | -------------------------------------------------------------------------------- /Chapter3/ex03_33.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Filename: ex03_33.c 3 | * Author: Andrew Laing 4 | * Email: parisianconnections@gmail.com 5 | * Date: 15/04/2017 6 | */ 7 | 8 | #include 9 | 10 | int main() 11 | { 12 | int size, i, j; 13 | 14 | printf( "Enter size of square: " ); 15 | scanf( "%d", &size ); 16 | 17 | if(size < 1 || size > 20) { 18 | printf("Size must be in the range 1 through 20"); 19 | return 0; 20 | } 21 | 22 | for(i=0; i 10 | #define PRINTARRAY(arr,lenArr) int printArrayCounter=0; \ 11 | for( ; printArrayCounter 12 | #include 13 | 14 | void intToString(char [], int ); 15 | 16 | int main() 17 | { 18 | int num = 341234; 19 | char output[__SIZEOF_INT__]; 20 | 21 | intToString(output, num); 22 | printf("int is = %s", output); 23 | 24 | return 0; 25 | 26 | } 27 | 28 | void intToString(char s[], int num) 29 | { 30 | sprintf(s, "%d", num); 31 | } 32 | 33 | -------------------------------------------------------------------------------- /Chapter12/ex12_27/SimpleCompilerV2.1/src/IntStack.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Filename: IntStack.h 3 | * Author: Andrew Laing 4 | * Email: parisianconnections@gmail.com 5 | * Date: 31/08/2017. 6 | * Description: A stack implementation to store integers. 7 | */ 8 | 9 | #ifndef INT_STACK_H 10 | #define INT_STACK_H 11 | 12 | struct stackNode { 13 | int data; 14 | struct stackNode *nextPtr; 15 | }; 16 | 17 | typedef struct stackNode StackNode; 18 | typedef StackNode *StackNodePtr; 19 | 20 | void push( StackNodePtr *, int ); 21 | int pop( StackNodePtr * ); 22 | int stackTop( StackNodePtr ); 23 | int isEmpty( StackNodePtr ); 24 | void printStack( StackNodePtr ); 25 | 26 | 27 | #endif 28 | 29 | -------------------------------------------------------------------------------- /Chapter12/ex12_28/SimpleCompilerV2.2/src/IntStack.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Filename: IntStack.h 3 | * Author: Andrew Laing 4 | * Email: parisianconnections@gmail.com 5 | * Date: 31/08/2017. 6 | * Description: A stack implementation to store integers. 7 | */ 8 | 9 | #ifndef INT_STACK_H 10 | #define INT_STACK_H 11 | 12 | struct stackNode { 13 | int data; 14 | struct stackNode *nextPtr; 15 | }; 16 | 17 | typedef struct stackNode StackNode; 18 | typedef StackNode *StackNodePtr; 19 | 20 | void push( StackNodePtr *, int ); 21 | int pop( StackNodePtr * ); 22 | int stackTop( StackNodePtr ); 23 | int isEmpty( StackNodePtr ); 24 | void printStack( StackNodePtr ); 25 | 26 | 27 | #endif 28 | 29 | -------------------------------------------------------------------------------- /Chapter9/ex09_10.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Filename: ex09_10.c 3 | * Author: Andrew Laing 4 | * Email: parisianconnections@gmail.com 5 | * Date: 17/07/2017. 6 | */ 7 | 8 | #include 9 | 10 | int main() 11 | { 12 | int a = 12345; 13 | double b = 1.2345; 14 | 15 | printf("%.10d\n", a); 16 | printf("%.8d\n", a); 17 | printf("%.6d\n", a); 18 | printf("%.3d\n", a); 19 | printf("%11.1d\n", a); 20 | 21 | printf("%.5f\n", b); 22 | printf("%.4f\n", b); 23 | printf("%.3f\n", b); 24 | printf("%.2f\n", b); 25 | printf("%.1f\n", b); 26 | printf("%.0f\n", b); 27 | 28 | return 0; 29 | } 30 | 31 | 32 | 33 | -------------------------------------------------------------------------------- /Chapter12/ex12_29/SimpleCompiler_version_2.9.3/src/IntStack.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Filename: IntStack.h 3 | * Author: Andrew Laing 4 | * Email: parisianconnections@gmail.com 5 | * Date: 31/08/2017. 6 | * Description: A stack implementation to store integers. 7 | */ 8 | 9 | #ifndef INT_STACK_H 10 | #define INT_STACK_H 11 | 12 | struct stackNode { 13 | int data; 14 | struct stackNode *nextPtr; 15 | }; 16 | 17 | typedef struct stackNode StackNode; 18 | typedef StackNode *StackNodePtr; 19 | 20 | void push( StackNodePtr *, int ); 21 | int pop( StackNodePtr * ); 22 | int stackTop( StackNodePtr ); 23 | int isEmpty( StackNodePtr ); 24 | void printStack( StackNodePtr ); 25 | 26 | 27 | #endif 28 | 29 | -------------------------------------------------------------------------------- /Chapter5/ex05_18.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Filename: ex05_18.c 3 | * Author: Andrew Laing 4 | * Email: parisianconnections@gmail.com 5 | * Date: 27/04/2017 6 | */ 7 | 8 | #include 9 | 10 | 11 | int isEven( int ); 12 | 13 | 14 | int main() 15 | { 16 | int i; 17 | int num; 18 | 19 | for(i=0; i<3; i++) 20 | { 21 | printf("Enter a number: "); 22 | scanf("%d", &num); 23 | 24 | if (isEven(num) ) 25 | printf("%d is even\n\n", num); 26 | else 27 | printf("%d is odd\n\n", num); 28 | } 29 | 30 | return 0; 31 | } 32 | 33 | 34 | int isEven(int n) 35 | { 36 | return (n%2==0); 37 | } 38 | 39 | -------------------------------------------------------------------------------- /Chapter9/ex09_20.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Filename: ex09_20.c 3 | * Author: Andrew Laing 4 | * Email: parisianconnections@gmail.com 5 | * Date: 18/07/2017. 6 | */ 7 | 8 | #include 9 | 10 | 11 | int main() 12 | { 13 | double num = 9876.12345; 14 | printf("Precision 1: %0.1lg\n", num); 15 | printf("Precision 2: %0.2lg\n", num); 16 | printf("Precision 3: %0.3lg\n", num); 17 | printf("Precision 4: %0.4lg\n", num); 18 | printf("Precision 5: %0.5lg\n", num); 19 | printf("Precision 6: %0.6lg\n", num); 20 | printf("Precision 7: %0.7lg\n", num); 21 | printf("Precision 8: %0.8lg\n", num); 22 | printf("Precision 9: %0.9lg\n", num); 23 | 24 | return 0; 25 | } 26 | 27 | -------------------------------------------------------------------------------- /Various/doubleToString.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Filename: doubleToString.c 3 | * Author: Andrew Laing 4 | * Email: parisianconnections@gmail.com 5 | * Date: 04/06/2017. 6 | * Description: Convert a double to a string 7 | * Accurate to 6 decimal places 8 | */ 9 | 10 | 11 | #include 12 | #include 13 | 14 | void doubleToString(char [], double ); 15 | 16 | int main() 17 | { 18 | double num = 341234.123456; 19 | char output[__SIZEOF_FLOAT__]; 20 | 21 | doubleToString(output, num); 22 | printf("double is = %s", output); 23 | 24 | return 0; 25 | 26 | } 27 | 28 | void doubleToString(char s[], double num) 29 | { 30 | sprintf(s, "%f", num); 31 | } 32 | 33 | -------------------------------------------------------------------------------- /Chapter5/ex05_39.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Filename: ex05_39.c 3 | * Author: Andrew Laing 4 | * Email: parisianconnections@gmail.com 5 | * Date: 04/05/2017 6 | */ 7 | 8 | #include 9 | 10 | void towerOfHanoi( int, int, int, int ); 11 | 12 | 13 | /* Recursive towerOfHanoi function */ 14 | void towerOfHanoi(int numDisks, int startPeg, int endPeg, int tempPeg ) 15 | { 16 | if(numDisks > 0) { 17 | towerOfHanoi(numDisks-1, startPeg, tempPeg, endPeg ); 18 | printf("%d --> %d\n", startPeg, endPeg); 19 | towerOfHanoi(numDisks-1, tempPeg, endPeg, startPeg ); 20 | } 21 | 22 | } 23 | 24 | int main() 25 | { 26 | towerOfHanoi( 17, 1, 3, 2 ); 27 | 28 | return 0; 29 | } 30 | 31 | -------------------------------------------------------------------------------- /Chapter6/ex06_19.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Filename: ex06_19.c 3 | * Author: Andrew Laing 4 | * Email: parisianconnections@gmail.com 5 | * Date: 11/05/2017 6 | */ 7 | 8 | #include 9 | #include 10 | #include 11 | 12 | #define SIDES 6 13 | 14 | int getRollResult( ); 15 | 16 | 17 | int getRollResult() 18 | { 19 | return 1 + rand()%SIDES; 20 | } 21 | 22 | 23 | int main() 24 | { 25 | srand( (time(NULL)) ); 26 | int totals[13] = {0}; 27 | int i; 28 | 29 | for(i=0; i<=36000; i++) 30 | ++totals[( getRollResult() + getRollResult() )]; 31 | 32 | for(i=2; i<=12; i++) { 33 | printf("%d:\t%d\n", i, totals[i]); 34 | } 35 | 36 | return 0; 37 | } 38 | 39 | -------------------------------------------------------------------------------- /Chapter5/ex05_29.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Filename: ex05_29.c 3 | * Author: Andrew Laing 4 | * Email: parisianconnections@gmail.com 5 | * Date: 03/05/2017 6 | * Description: Greatest Common Denominator 7 | */ 8 | 9 | #include 10 | 11 | 12 | int gcd( int, int ); 13 | 14 | 15 | int main() 16 | { 17 | int a, b; 18 | 19 | printf("Enter the first number: "); 20 | scanf("%d", &a); 21 | 22 | printf("Enter the second number: "); 23 | scanf("%d", &b); 24 | 25 | printf("The gcd = %d\n", gcd( a, b )); 26 | 27 | return 0; 28 | } 29 | 30 | 31 | int gcd( int a, int b ) 32 | { 33 | if(a%b==0) 34 | return b; 35 | else 36 | return gcd(b, a%b); 37 | } 38 | 39 | -------------------------------------------------------------------------------- /Chapter6/ex06_39.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Filename: ex06_39.c 3 | * Author: Andrew Laing 4 | * Email: parisianconnections@gmail.com 5 | * Date: 21/05/2017 6 | */ 7 | 8 | #include 9 | 10 | int minimum( int[], int ); 11 | 12 | 13 | int main() 14 | { 15 | int arrLen = 10; 16 | int array[10] = {27,5,3,6,44,15,5,7,25,14}; 17 | 18 | printf("Minimum = %d\n", minimum(array, arrLen)); 19 | 20 | return 0; 21 | } 22 | 23 | 24 | int minimum( int array[], int arrLen ) 25 | { 26 | if(arrLen == 1) 27 | return array[0]; 28 | else if(array[0] < array[arrLen-1]) 29 | return minimum(array, arrLen-1); 30 | else 31 | return minimum(++array, arrLen-1); 32 | } 33 | 34 | -------------------------------------------------------------------------------- /Chapter8/ex08_06.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Filename: ex08_06.c 3 | * Author: Andrew Laing 4 | * Email: parisianconnections@gmail.com 5 | * Date: 20/06/2017. 6 | * Description: Using gets, tolower, and toupper 7 | */ 8 | 9 | #include 10 | #include /* Used for strlen */ 11 | #include 12 | 13 | 14 | 15 | int main() 16 | { 17 | int i = 0; 18 | char s[100]; 19 | 20 | printf("Enter a line of text: "); 21 | gets( s ); 22 | 23 | while(s[i]!='\0') 24 | printf("%c", tolower(s[i++])); 25 | printf("\n"); 26 | 27 | i = 0; 28 | while(s[i]!='\0') 29 | printf("%c", toupper(s[i++])); 30 | printf("\n"); 31 | 32 | return 0; 33 | } 34 | 35 | 36 | -------------------------------------------------------------------------------- /Chapter6/ex06_37.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Filename: ex06_37.c 3 | * Author: Andrew Laing 4 | * Email: parisianconnections@gmail.com 5 | * Date: 21/05/2017 6 | */ 7 | 8 | #include 9 | #define SIZE 15 10 | 11 | void printArray( int[], int ); 12 | 13 | 14 | int main() 15 | { 16 | int i, array[ SIZE ]; 17 | 18 | /* Initialise array */ 19 | for(i=0; i<=SIZE-1; i++) 20 | array[ i ] = 2 * i; 21 | 22 | printArray(array, SIZE); 23 | 24 | return 0; 25 | } 26 | 27 | 28 | void printArray( int array[], int arrLen ) 29 | { 30 | if(arrLen==0) { 31 | return; 32 | } 33 | else { 34 | printf("%d ", array[0]); 35 | printArray(++array, arrLen-1); 36 | } 37 | } 38 | 39 | -------------------------------------------------------------------------------- /Various/reverseString.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Filename: reverseString.c 3 | * Author: Andrew Laing 4 | * Email: parisianconnections@gmail.com 5 | * Date: 25/06/2017. 6 | */ 7 | 8 | 9 | #include 10 | #include 11 | 12 | 13 | void reverse( char []); 14 | 15 | /* reverse a string in place */ 16 | void reverse( char s[]) 17 | { 18 | int c, i, j; 19 | for(i=0, j=strlen(s)-1; i 9 | 10 | 11 | void side( int ); 12 | 13 | 14 | int main() 15 | { 16 | int i; 17 | int num; 18 | 19 | for(i=0; i<3; i++) 20 | { 21 | printf("Enter a number: "); 22 | scanf("%d", &num); 23 | 24 | side(num); 25 | } 26 | 27 | return 0; 28 | } 29 | 30 | 31 | void side(int n) 32 | { 33 | int i, j; 34 | 35 | for(i=0; i 10 | #define SIZE 1000 11 | 12 | 13 | int main() 14 | { 15 | int array[ SIZE ]; 16 | int i, j; 17 | 18 | for(i=0; i 9 | 10 | int main() 11 | { 12 | int i; 13 | int counter, number; 14 | int largest = 0, secondLargest = 0; 15 | 16 | for(i=0; i<10; i++) 17 | { 18 | printf("Input number: "); 19 | scanf("%d", &number); 20 | if(number > largest) { 21 | secondLargest = largest; 22 | largest = number; 23 | } 24 | else if(number > secondLargest) 25 | secondLargest = number; 26 | } 27 | 28 | printf("The two largest numbers entered were %d and %d\n", secondLargest, largest); 29 | 30 | return 0; 31 | } 32 | -------------------------------------------------------------------------------- /Chapter5/ex05_30.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Filename: ex05_30.c 3 | * Author: Andrew Laing 4 | * Email: parisianconnections@gmail.com 5 | * Date: 03/05/2017 6 | */ 7 | 8 | #include 9 | 10 | int qualityPoints( int ); 11 | 12 | 13 | int main() 14 | { 15 | int n; 16 | 17 | printf("Enter the student's average: "); 18 | scanf("%d", &n); 19 | 20 | printf("The quality points of the students = %d\n", qualityPoints( n )); 21 | 22 | return 0; 23 | } 24 | 25 | 26 | int qualityPoints( int n ) 27 | { 28 | if( n >= 90 ) 29 | return 4; 30 | else if( n >= 80 ) 31 | return 3; 32 | else if( n >= 70 ) 33 | return 2; 34 | else if( n >= 60 ) 35 | return 1; 36 | else 37 | return 0; 38 | } 39 | 40 | -------------------------------------------------------------------------------- /Chapter4/ex04_29.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Filename: ex04_29.c 3 | * Author: Andrew Laing 4 | * Email: parisianconnections@gmail.com 5 | * Date: 23/04/2017 6 | */ 7 | 8 | #include 9 | 10 | 11 | int main() 12 | { 13 | int a=0, b=1, g=5, i=4, j=7, x=9, y=6, Y=0; 14 | 15 | if( ( !(x<5) && !(y>=7) ) == !( (x<5) || (y>=7) ) ) 16 | printf("First expression passes\n"); 17 | 18 | if( ( !(a==b) || !(g!=5) ) == !( (a==b) && (g!=5) ) ) 19 | printf("Second expression passes\n"); 20 | 21 | if( !( (x<=8) && (Y>4)) == ( !(x<=8) || !(Y>4)) ) 22 | printf("Third expression passes\n"); 23 | 24 | if( !( (i>4) || (j<=6)) == ( !(i>4) && !(j<=6)) ) 25 | printf("Fourth expression passes\n"); 26 | 27 | return 0; 28 | } 29 | 30 | -------------------------------------------------------------------------------- /Chapter5/ex05_25.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Filename: ex05_25.c 3 | * Author: Andrew Laing 4 | * Email: parisianconnections@gmail.com 5 | * Date: 01/05/2017 6 | */ 7 | 8 | #include 9 | 10 | 11 | float smallestFloat( float, float, float ); 12 | 13 | 14 | 15 | int main() 16 | { 17 | float a, b, c; 18 | 19 | printf("Enter 3 floats separated by spaces: "); 20 | scanf("%f%f%f", &a, &b, &c); 21 | printf("The smallest number is = %f\n", smallestFloat(a,b,c)); 22 | 23 | return 0; 24 | } 25 | 26 | 27 | float smallestFloat( float a, float b, float c ) 28 | { 29 | float smallest = a; 30 | 31 | if(b < smallest) 32 | smallest = b; 33 | if(c < smallest) 34 | smallest = c; 35 | 36 | return smallest; 37 | } 38 | 39 | -------------------------------------------------------------------------------- /Various/SieveOfEratosthenes.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Filename: ex06_30.c 3 | * Author: Andrew Laing 4 | * Email: parisianconnections@gmail.com 5 | * Date: 16/05/2017 6 | * Description: Sieve of Eratosthenes 7 | */ 8 | 9 | 10 | #include 11 | #define SIZE 1000 12 | 13 | 14 | int main() 15 | { 16 | int array[ SIZE ]; 17 | int i, j; 18 | 19 | for(i=0; i 9 | 10 | 11 | 12 | int mystery( int, int ); 13 | 14 | int main() 15 | { 16 | int x, y; 17 | printf("Enter two integers: "); 18 | scanf("%d%d", &x, &y); 19 | printf("The result is %d\n", mystery(x,y) ); 20 | 21 | return 0; 22 | } 23 | 24 | /* mystery multiplies x by y */ 25 | int mystery( int x, int y ) 26 | { 27 | if( y == 1 ) 28 | return x; 29 | else { 30 | if(y>1) 31 | return x + mystery(x, y-1); 32 | else { 33 | y = abs(y); 34 | return -(x + mystery(x, y-1)); 35 | } 36 | } 37 | } 38 | 39 | -------------------------------------------------------------------------------- /Chapter9/ex09_14.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Filename: ex09_14.c 3 | * Author: Andrew Laing 4 | * Email: parisianconnections@gmail.com 5 | * Date: 17/07/2017. 6 | */ 7 | 8 | #include 9 | 10 | int main() 11 | { 12 | printf("single quote - \'\n"); 13 | printf("double quote - \"\n"); 14 | printf("question mark - \?\n"); 15 | printf("back slash - \\\n"); 16 | printf("bell - \a\n"); 17 | printf("backspace - 1\b23\n"); 18 | printf("next page - 1\f2\n"); /* Not on this terminal */ 19 | printf("newline - 1\n2\n"); 20 | printf("beginning of current line - \r123\n"); 21 | printf("horizontal tab - 1\t2\n"); 22 | printf("vertical tab - 1\v2\n"); /* Not on this terminal */ 23 | 24 | return 0; 25 | } 26 | 27 | 28 | -------------------------------------------------------------------------------- /Chapter12/ex12_26/ex12_26_c.simple: -------------------------------------------------------------------------------- 1 | 10 rem Use a counter controlled loop to input 7 integers, 2 | 20 rem some positive and some negative, and compute and 3 | 21 rem print their average 4 | 22 rem 5 | 23 rem set the values of the variables sum (s), max (m) 6 | 24 rem and count (c) 7 | 30 let s = 0 8 | 40 let m = 7 9 | 50 let c = 0 10 | 51 rem 11 | 52 rem if 7 numbers have been input go and print the result 12 | 53 rem else input a number and add it to the total then 13 | 54 rem increment the counter 14 | 60 if c == m goto 110 15 | 70 input a 16 | 80 let s = s + a 17 | 90 let c = c + 1 18 | 100 goto 60 19 | 101 rem 20 | 102 rem print the sum (s) of the inputted numbers, then calculate 21 | 103 rem and print the average (v) 22 | 110 print s 23 | 120 let v = s / m 24 | 130 print v 25 | 140 end 26 | -------------------------------------------------------------------------------- /Chapter3/ex03_19.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Filename: ex03_19.c 3 | * Author: Andrew Laing 4 | * Email: parisianconnections@gmail.com 5 | * Date: 14/04/2017 6 | */ 7 | 8 | #include 9 | 10 | int main() 11 | { 12 | float sales, commission, salary; 13 | const float rate = 0.09; 14 | const float basicPay = 200.0; 15 | 16 | printf( "Enter sales in dollars (-1 to end): " ); 17 | scanf( "%f", &sales); 18 | 19 | while( (int)sales != -1) 20 | { 21 | salary = basicPay; 22 | commission = sales * rate; 23 | salary += commission; 24 | 25 | printf( "Salary is: $%.2f\n", salary ); 26 | 27 | printf( "\n\nEnter sales in dollars (-1 to end): " ); 28 | scanf( "%f", &sales); 29 | } 30 | 31 | return 0; 32 | } 33 | 34 | -------------------------------------------------------------------------------- /Chapter6/ex06_15.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Filename: ex06_15.c 3 | * Author: Andrew Laing 4 | * Email: parisianconnections@gmail.com 5 | * Date: 11/05/2017 6 | */ 7 | 8 | #include 9 | #define SIZE 20 10 | 11 | int main() 12 | { 13 | short input[SIZE] = {10,20,51,66,10,88,66,45,46,99, 14 | 33,55,45,68,12,11,45,65,77,44}; 15 | short alreadyRead[SIZE] = {0}; 16 | int nextIndex = 0; 17 | int i, j, printNum; 18 | 19 | for(i=0; i<=SIZE-1; i++) { 20 | printNum = 1; 21 | 22 | for(j=0; j /* for Sleep() */ 9 | #include 10 | #include 11 | #include 12 | 13 | #define SIZE 20 14 | 15 | int getRandomNum( int ); 16 | 17 | 18 | int main() 19 | { 20 | srand( (time(NULL)) ); 21 | 22 | int i; 23 | int noDuplicates[SIZE] = {0}; 24 | 25 | for(i=0; i 0) 30 | printf("%d ",i+1); 31 | } 32 | printf("\n"); 33 | 34 | return 0; 35 | } 36 | 37 | 38 | int getRandomNum(int max) 39 | { 40 | return rand()%max; 41 | } 42 | 43 | -------------------------------------------------------------------------------- /Chapter12/ex12_29/RunSML_version_1.0/out.sml: -------------------------------------------------------------------------------- 1 | 00 +160990 2 | 01 +160977 3 | 02 +100999 4 | 03 +200999 5 | 04 +310989 6 | 05 +510011 7 | 06 +200999 8 | 07 +310988 9 | 08 +530011 10 | 09 +160968 11 | 10 +500012 12 | 11 +160958 13 | 12 +540000 14 | 958 +9121 15 | 959 +111117 16 | 960 +32103 17 | 961 +111116 18 | 962 +32105 19 | 963 +116032 20 | 964 +119114 21 | 965 +111110 22 | 966 +103046 23 | 968 +8119 24 | 969 +101108 25 | 970 +108032 26 | 971 +100111 27 | 972 +110101 28 | 973 +32097 29 | 974 +110111 30 | 975 +110046 31 | 977 +10098 32 | 978 +101116 33 | 979 +119101 34 | 980 +101110 35 | 981 +32049 36 | 982 +32097 37 | 983 +110100 38 | 984 +32049 39 | 985 +48032 40 | 986 +62032 41 | 988 +10 42 | 989 +1 43 | 990 +8101 44 | 991 +110116 45 | 992 +101114 46 | 993 +32097 47 | 994 +32110 48 | 995 +117109 49 | 996 +98101 50 | 997 +114032 51 | -------------------------------------------------------------------------------- /Chapter5/ex05_31.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Filename: ex05_31.c 3 | * Author: Andrew Laing 4 | * Email: parisianconnections@gmail.com 5 | * Date: 03/05/2017 6 | */ 7 | 8 | #include 9 | #include 10 | #include 11 | 12 | int flip( void ); 13 | 14 | 15 | int main() 16 | { 17 | srand( (time(NULL)) ); 18 | int total = 0; 19 | int i; 20 | 21 | for(i=0; i<100; i++) 22 | total += flip(); 23 | 24 | printf("\n\nThe total number of heads was = %d\n", total); 25 | printf("The total number of tails was = %d\n", 100-total); 26 | 27 | return 0; 28 | } 29 | 30 | 31 | 32 | int flip( ) 33 | { 34 | int res = rand()%2; 35 | 36 | if(res) 37 | printf("Heads "); 38 | else 39 | printf("Tails "); 40 | 41 | return res; 42 | } 43 | 44 | -------------------------------------------------------------------------------- /Chapter11/ex11_07/createTrans.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Filename: createTrans.c 3 | * Author: Andrew Laing 4 | * Email: parisianconnections@gmail.com 5 | * Date: 24/07/2017. 6 | */ 7 | 8 | #include 9 | 10 | struct clientTransaction { 11 | int acctNum; 12 | double transaction; 13 | }; 14 | 15 | int main() 16 | { 17 | int i; 18 | struct clientTransaction blankClient = { 0, 0.0 }; 19 | FILE *cfPtr; 20 | 21 | if ( ( cfPtr = fopen( "trans.dat", "wb" ) ) == NULL ) 22 | printf( "File could not be opened.\n" ); 23 | else { 24 | 25 | for ( i = 1; i <= 1000; i++ ) 26 | fwrite( &blankClient, 27 | sizeof( struct clientTransaction ), 1, cfPtr ); 28 | 29 | fclose ( cfPtr ); 30 | printf("File \"trans.dat\" created.\n"); 31 | } 32 | 33 | return 0; 34 | } 35 | 36 | -------------------------------------------------------------------------------- /Chapter12/ex12_26/ex12_26_f.simple: -------------------------------------------------------------------------------- 1 | 10 rem Calculate and print the sum of the even Integers 2 | 11 rem from 2 to 30 3 | 12 rem 4 | 13 rem set the value of the variables number (n) to 2, 5 | 14 rem sum (s) to 0, increment value (i) to 2, and max (m) to 30 6 | 20 let n = 2 7 | 30 let s = 0 8 | 40 let i = 2 9 | 50 let m = 30 10 | 51 rem LOOP to add the numbers, if the current number is greater 11 | 52 rem than 30 then go and print the result 12 | 60 if n > m goto 100 13 | 61 rem 14 | 62 rem ADD n to the sum 15 | 70 let s = s + n 16 | 71 rem 17 | 72 rem Increment n by 2 to get the next even number, then 18 | 73 rem return to the beginning of the loop 19 | 80 let n = n + i 20 | 90 goto 60 21 | 91 rem 22 | 92 rem print out the sum of the even integers and quit 23 | 100 print s 24 | 110 end 25 | 26 | 27 | 28 | 29 | -------------------------------------------------------------------------------- /Chapter12/ex12_26/ex12_26_g.simple: -------------------------------------------------------------------------------- 1 | 10 rem Calculate and print the product of the odd Integers 2 | 11 rem from 1 to 9 3 | 12 rem 4 | 13 rem set the value of the variables number (n) to 1, 5 | 14 rem sum (s) to 0, increment value (i) to 2, and max (m) to 9 6 | 20 let n = 1 7 | 30 let s = 0 8 | 40 let i = 2 9 | 50 let m = 9 10 | 51 rem LOOP to add the numbers, if the current number is greater 11 | 52 rem than 9 then go and print the result 12 | 60 if n > m goto 100 13 | 61 rem 14 | 62 rem ADD n to the sum 15 | 70 let s = s + n 16 | 71 rem 17 | 72 rem Increment n by 2 to get the next odd number, then 18 | 73 rem return to the beginning of the loop 19 | 80 let n = n + i 20 | 90 goto 60 21 | 91 rem 22 | 92 rem print out the sum of the odd integers and quit 23 | 100 print s 24 | 110 end 25 | 26 | 27 | 28 | 29 | -------------------------------------------------------------------------------- /Chapter4/ex04_26.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Filename: ex04_26.c 3 | * Author: Andrew Laing 4 | * Email: parisianconnections@gmail.com 5 | * Date: 22/04/2017 6 | * Description: Calculate pi 7 | */ 8 | 9 | #include 10 | 11 | float calculatePi( int ); 12 | 13 | 14 | int main() 15 | { 16 | int i; 17 | 18 | for(i=2; i<200000; i++) 19 | { 20 | printf("Pi calculated using %d terms = %f\n", i, calculatePi(i)); 21 | } 22 | 23 | return 0; 24 | } 25 | 26 | 27 | float calculatePi(int terms) 28 | { 29 | int i; 30 | float pi = 4; 31 | float divisor = 3; 32 | for(i=0; i 9 | 10 | #define printStar printf( "*" ); 11 | #define printSpace printf( " " ); 12 | #define newLine printf( "\n" ); 13 | 14 | int main() 15 | { 16 | int i, j, k; 17 | 18 | int max=2; 19 | 20 | for(i=0; i<=max; i++) 21 | { 22 | for(j=0; j<(max-i); j++) 23 | printSpace; 24 | for(k=0; k<(1+(i*2)); k++) 25 | printStar; 26 | newLine; 27 | } 28 | 29 | for(i=max-1; i>=0; i--) 30 | { 31 | for(j=0; j<(max-i); j++) 32 | printSpace; 33 | for(k=0; k<(1+(i*2)); k++) 34 | printStar; 35 | newLine; 36 | } 37 | 38 | return 0; 39 | } 40 | 41 | -------------------------------------------------------------------------------- /Chapter5/ex05_27.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Filename: ex05_27.c 3 | * Author: Andrew Laing 4 | * Email: parisianconnections@gmail.com 5 | * Date: 01/05/2017 6 | */ 7 | 8 | #include 9 | #include 10 | 11 | int isPrime( int ); 12 | 13 | 14 | int main() 15 | { 16 | int n; 17 | 18 | for(n=1; n<=10000; n++) 19 | isPrime(n); 20 | 21 | return 0; 22 | } 23 | 24 | 25 | int isPrime( int n ) 26 | { 27 | int high = (int)sqrt(n); 28 | int i; 29 | 30 | if(n<=2) { 31 | printf("%d is a prime number\n", n); 32 | return 1; 33 | } 34 | 35 | if(n%2==0) 36 | return 0; 37 | 38 | for(i=3; i<=high; i+=2) 39 | { 40 | if(n%i==0) 41 | return 0; 42 | } 43 | 44 | printf("%d is a prime number\n", n); 45 | return 1; 46 | } 47 | 48 | -------------------------------------------------------------------------------- /Chapter3/ex03_34.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Filename: ex03_34.c 3 | * Author: Andrew Laing 4 | * Email: parisianconnections@gmail.com 5 | * Date: 15/04/2017 6 | */ 7 | 8 | #include 9 | 10 | int main() 11 | { 12 | int size, i, j; 13 | 14 | printf( "Enter size of square: " ); 15 | scanf( "%d", &size ); 16 | 17 | if(size < 1 || size > 20) { 18 | printf("Size must be in the range 1 through 20"); 19 | return 0; 20 | } 21 | 22 | for(i=0; i 10 | 11 | int multiple( int num ); 12 | 13 | 14 | int main() 15 | { 16 | int y; 17 | 18 | printf("Enter an integer between 1 and 32000: "); 19 | scanf("%d", &y); 20 | 21 | if( multiple( y )) 22 | printf("%d is a multiple of X\n", y); 23 | else 24 | printf("%d is not a multiple of X\n", y); 25 | 26 | return 0; 27 | } 28 | 29 | int multiple( int num ) 30 | { 31 | int i, mask=1, mult=1; 32 | 33 | for( i=1; i<=10; i++, mask<<=1 ) 34 | if( (num & mask )!=0 ) { 35 | mult = 0; 36 | break; 37 | } 38 | 39 | return mult; 40 | } 41 | -------------------------------------------------------------------------------- /Chapter11/ex11_07/createMaster.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Filename: createMaster.c 3 | * Author: Andrew Laing 4 | * Email: parisianconnections@gmail.com 5 | * Date: 24/07/2017. 6 | */ 7 | 8 | #include 9 | 10 | struct clientData { 11 | int acctNum; 12 | char lastName[ 15 ]; 13 | char firstName[ 10 ]; 14 | double balance; 15 | }; 16 | 17 | int main() 18 | { 19 | int i; 20 | struct clientData blankClient = { 0, "", "", 0.0 }; 21 | FILE *cfPtr; 22 | 23 | if ( ( cfPtr = fopen( "oldMast.dat", "wb" ) ) == NULL ) 24 | printf( "File could not be opened.\n" ); 25 | else { 26 | 27 | for ( i = 1; i <= 1000; i++ ) 28 | fwrite( &blankClient, 29 | sizeof( struct clientData ), 1, cfPtr ); 30 | 31 | fclose ( cfPtr ); 32 | printf("File \"oldMast.dat\" created.\n"); 33 | } 34 | 35 | return 0; 36 | } 37 | 38 | -------------------------------------------------------------------------------- /Chapter11/ex11_10/createMaster.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Filename: createMaster.c 3 | * Author: Andrew Laing 4 | * Email: parisianconnections@gmail.com 5 | * Date: 24/07/2017. 6 | */ 7 | 8 | #include 9 | 10 | struct clientData { 11 | int acctNum; 12 | char lastName[ 15 ]; 13 | char firstName[ 10 ]; 14 | double balance; 15 | }; 16 | 17 | int main() 18 | { 19 | int i; 20 | struct clientData blankClient = { 0, "", "", 0.0 }; 21 | FILE *cfPtr; 22 | 23 | if ( ( cfPtr = fopen( "oldMast.dat", "wb" ) ) == NULL ) 24 | printf( "File could not be opened.\n" ); 25 | else { 26 | 27 | for ( i = 1; i <= 1000; i++ ) 28 | fwrite( &blankClient, 29 | sizeof( struct clientData ), 1, cfPtr ); 30 | 31 | fclose ( cfPtr ); 32 | printf("File \"oldMast.dat\" created.\n"); 33 | } 34 | 35 | return 0; 36 | } 37 | 38 | -------------------------------------------------------------------------------- /Chapter2/ex02_17.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Filename: ex02_17.c 3 | * Author: Andrew Laing 4 | * Email: parisianconnections@gmail.com 5 | * Date: 12/04/2017 6 | * Description: Write a program that prints the numbers 1 to 4 7 | * on the same line. Write the program using the 8 | * following methods; 9 | * a) Using one printf statement with 10 | * no conversion specifiers. 11 | * b) Using one printf statement with 12 | * four conversion specifiers. 13 | * a) Using four printf statements. 14 | */ 15 | 16 | #include 17 | 18 | 19 | int main() 20 | { 21 | printf( "1 2 3 4\n"); 22 | printf( "%d %d %d %d\n",1, 2, 3, 4 ); 23 | printf( "1 " ); 24 | printf( "2 " ); 25 | printf( "3 " ); 26 | printf( "4\n" ); 27 | 28 | return 0; 29 | } 30 | -------------------------------------------------------------------------------- /Chapter5/ex05_08.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Filename: ex05_08.c 3 | * Author: Andrew Laing 4 | * Email: parisianconnections@gmail.com 5 | * Date: 26/04/2017 6 | */ 7 | 8 | #include 9 | #include 10 | 11 | 12 | int main() 13 | { 14 | float x; 15 | 16 | x = fabs(7.5); 17 | printf("fabs(7.5) = %.3f\n", x); 18 | 19 | x = floor(7.5); 20 | printf("floor(7.5) = %.3f\n", x); 21 | 22 | x = fabs(0.0); 23 | printf("fabs(0.0) = %.3f\n", x); 24 | 25 | x = ceil(0.0); 26 | printf("ceil(0.0) = %.3f\n", x); 27 | 28 | x = fabs(-6.4); 29 | printf("fabs(-6.4) = %.3f\n", x); 30 | 31 | x = ceil(-6.4); 32 | printf("ceil(-6.4) = %.3f\n", x); 33 | 34 | x = ceil( -fabs( -8 + floor( -5.5 ) ) ); 35 | printf("ceil( -fabs( -8 + floor( -5.5 ) ) ) = %.3f\n", x); 36 | 37 | return 0; 38 | } 39 | 40 | -------------------------------------------------------------------------------- /Chapter5/ex05_16.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Filename: ex05_16.c 3 | * Author: Andrew Laing 4 | * Email: parisianconnections@gmail.com 5 | * Date: 27/04/2017 6 | * Description: Calculate Integer exponent 7 | */ 8 | 9 | #include 10 | 11 | 12 | int integerPower(int base, int exponent); 13 | 14 | 15 | int main() 16 | { 17 | int base, exponent, result; 18 | 19 | printf("Enter the base: "); 20 | scanf("%d", &base); 21 | 22 | printf("Enter the exponent: "); 23 | scanf("%d", &exponent); 24 | 25 | result = integerPower(base, exponent); 26 | printf("The result is %d\n", result); 27 | 28 | return 0; 29 | } 30 | 31 | 32 | int integerPower(int base, int exponent) 33 | { 34 | int result = base; 35 | int i; 36 | 37 | for(i=1; i 14 | #include 15 | #include 16 | #include 17 | 18 | 19 | // Prototypes ////////////////////////////// 20 | 21 | int getLine( char [], int ); 22 | 23 | int startsWith( const char *, char ); 24 | int endsWith( const char *, char ); 25 | int endsWithSuffix(const char *, const char * ); 26 | int isOperator( char c ); 27 | void removeEndChar( char *s ); 28 | 29 | int strlen2( const char* s ); 30 | char *strcpy2( char *, const char * ); 31 | int strcmp2( const char *, const char * ); 32 | 33 | #endif 34 | 35 | -------------------------------------------------------------------------------- /Chapter12/ex12_29/RunSML_version_1.0/src/StringMethods.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Filename: StringMethods.h 3 | * Author: Andrew Laing 4 | * Email: parisianconnections@gmail.com 5 | * Date: 31/08/2017. 6 | * Description: Various string and character related methods. 7 | */ 8 | 9 | #ifndef STRING_METHODS_H 10 | #define STRING_METHODS_H 11 | 12 | 13 | #include 14 | #include 15 | #include 16 | #include 17 | 18 | 19 | // Prototypes ////////////////////////////// 20 | 21 | int getLine( char [], int ); 22 | 23 | int startsWith( const char *, char ); 24 | int endsWith( const char *, char ); 25 | int endsWithSuffix(const char *, const char * ); 26 | int isOperator( char c ); 27 | void removeEndChar( char *s ); 28 | 29 | int strlen2( const char* s ); 30 | char *strcpy2( char *, const char * ); 31 | int strcmp2( const char *, const char * ); 32 | 33 | #endif 34 | 35 | -------------------------------------------------------------------------------- /Chapter10/ex10_20.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Filename: ex10_20.c 3 | * Author: Andrew Laing 4 | * Email: parisianconnections@gmail.com 5 | * Date: 22/07/2017. 6 | * Returns 0 if the integer contains an odd number of bits, else 1 7 | * eg 3 (0011) has an even number of bits so returns 1 8 | * 8 (1000) has an odd number of bits so returns 0 9 | */ 10 | 11 | #include 12 | 13 | int mystery( unsigned bits ); 14 | 15 | 16 | int main() 17 | { 18 | unsigned x; 19 | 20 | printf("Enter an integer: "); 21 | scanf("%u", &x); 22 | printf("The result is %d\n", mystery(x) ); 23 | 24 | return 0; 25 | } 26 | 27 | int mystery( unsigned bits ) 28 | { 29 | int i, mask=1<<31, total=0; 30 | 31 | for( i=1; i<=32; i++, bits<<=1 ) 32 | if( (bits & mask ) == mask ) { 33 | ++total; 34 | } 35 | 36 | return !( total % 2) ? 1 : 0; 37 | } 38 | -------------------------------------------------------------------------------- /Chapter12/ex12_27/SimpleCompilerV2.1/src/StringMethods.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Filename: StringMethods.h 3 | * Author: Andrew Laing 4 | * Email: parisianconnections@gmail.com 5 | * Date: 31/08/2017. 6 | * Description: Various string and character related methods. 7 | */ 8 | 9 | 10 | #ifndef STRING_METHODS_H 11 | #define STRING_METHODS_H 12 | 13 | #include 14 | #include 15 | #include 16 | #include 17 | #include "IntStack.h" 18 | 19 | 20 | int getLine( char [], int ); 21 | 22 | int startsWith( const char *, char ); 23 | int endsWith( const char *, char ); 24 | int endsWithSuffix(const char *s, const char *); 25 | int isOperator( char c ); 26 | void removeEndChar( char *s ); 27 | int strToInt( const char * ); 28 | 29 | int strlen2( const char* s ); 30 | char *strcpy2( char *, const char * ); 31 | int strcmp2( const char *, const char * ); 32 | 33 | #endif 34 | 35 | -------------------------------------------------------------------------------- /Chapter12/ex12_28/SimpleCompilerV2.2/src/StringMethods.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Filename: StringMethods.h 3 | * Author: Andrew Laing 4 | * Email: parisianconnections@gmail.com 5 | * Date: 31/08/2017. 6 | * Description: Various string and character related methods. 7 | */ 8 | 9 | 10 | #ifndef STRING_METHODS_H 11 | #define STRING_METHODS_H 12 | 13 | #include 14 | #include 15 | #include 16 | #include 17 | #include "IntStack.h" 18 | 19 | 20 | int getLine( char [], int ); 21 | 22 | int startsWith( const char *, char ); 23 | int endsWith( const char *, char ); 24 | int endsWithSuffix(const char *s, const char *); 25 | int isOperator( char c ); 26 | void removeEndChar( char *s ); 27 | int strToInt( const char * ); 28 | 29 | int strlen2( const char* s ); 30 | char *strcpy2( char *, const char * ); 31 | int strcmp2( const char *, const char * ); 32 | 33 | #endif 34 | 35 | -------------------------------------------------------------------------------- /Chapter12/ex12_29/SimpleTron_version_2.2/src/StringMethods.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Filename: StringMethods.h 3 | * Author: Andrew Laing 4 | * Email: parisianconnections@gmail.com 5 | * Date: 31/08/2017. 6 | * Description: Various string and character related methods. 7 | */ 8 | 9 | #ifndef STRING_METHODS_H 10 | #define STRING_METHODS_H 11 | 12 | 13 | #include 14 | #include 15 | #include 16 | #include 17 | 18 | 19 | // Prototypes ////////////////////////////// 20 | 21 | int getLine( char [], int ); 22 | 23 | int startsWith( const char *, char ); 24 | int endsWith( const char *, char ); 25 | int endsWithSuffix(const char *, const char * ); 26 | int isOperator( char c ); 27 | void removeEndChar( char *s ); 28 | 29 | int strlen2( const char* s ); 30 | char *strcpy2( char *, const char * ); 31 | int strcmp2( const char *, const char * ); 32 | 33 | #endif 34 | 35 | -------------------------------------------------------------------------------- /Chapter3/ex03_20.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Filename: ex03_20.c 3 | * Author: Andrew Laing 4 | * Email: parisianconnections@gmail.com 5 | * Date: 14/04/2017 6 | */ 7 | 8 | #include 9 | 10 | int main() 11 | { 12 | float principal, rate, days, interest; 13 | const int yearInDays = 365; 14 | 15 | printf( "Enter loan principal (-1 to end): " ); 16 | scanf( "%f", &principal ); 17 | 18 | while( (int)principal != -1) 19 | { 20 | printf( "Enter interest rate: " ); 21 | scanf( "%f", &rate ); 22 | printf( "Enter term of the loan in days: " ); 23 | scanf( "%f", &days ); 24 | 25 | interest = principal * rate * days / yearInDays; 26 | printf( "The interest charge is $%.2f\n", interest ); 27 | 28 | printf( "\n\nEnter loan principal (-1 to end): " ); 29 | scanf( "%f", &principal ); 30 | } 31 | 32 | return 0; 33 | } 34 | 35 | -------------------------------------------------------------------------------- /Chapter9/ex09_07.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Filename: ex09_07.c 3 | * Author: Andrew Laing 4 | * Email: parisianconnections@gmail.com 5 | * Date: 16/07/2017. 6 | */ 7 | 8 | #include 9 | #include 10 | #include 11 | 12 | int getRandomNumber( int max ); 13 | 14 | int main() 15 | { 16 | srand( time(NULL) ); 17 | int i, sum=0, x[10]; 18 | 19 | for(i=0; i<10; i++) 20 | { 21 | x[i] = getRandomNumber(1000) ; 22 | } 23 | 24 | 25 | printf("%s\t%s\n", "Value", "Total characters"); 26 | 27 | for(i=0; i<10; i++) 28 | { 29 | sum += printf("%d", x[i]); 30 | printf("\t%d\n", sum); 31 | x[i] = getRandomNumber(1000) ; 32 | } 33 | return 0; 34 | } 35 | 36 | 37 | /* Returns a random number in the range 0 to max-1*/ 38 | int getRandomNumber( int max ) 39 | { 40 | return (rand()%max)+1; 41 | } 42 | 43 | 44 | -------------------------------------------------------------------------------- /Chapter10/ex10_10.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Filename: ex10_10.c 3 | * Author: Andrew Laing 4 | * Email: parisianconnections@gmail.com 5 | * Date: 18/07/2017. 6 | */ 7 | 8 | #include 9 | 10 | void displayBits( int ); 11 | 12 | int main() 13 | { 14 | int number1; 15 | 16 | number1 = 65535; 17 | printf("The result of right shifting the following\n"); 18 | displayBits( number1 ); 19 | printf("is\n"); 20 | displayBits( number1 >> 4 ); 21 | 22 | return 0; 23 | } 24 | 25 | 26 | void displayBits( int value ) 27 | { 28 | unsigned c, displayMask = 1<<31; 29 | 30 | printf( "%7u = ", value ); 31 | 32 | for( c=1; c<=32; c++ ) 33 | { 34 | putchar( value & displayMask ? '1' : '0' ); 35 | value <<= 1; 36 | 37 | if(c % 8 == 0) 38 | putchar( ' ' ); 39 | } 40 | 41 | putchar( '\n' ); 42 | } 43 | 44 | 45 | -------------------------------------------------------------------------------- /Chapter5/ex05_17.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Filename: ex05_17.c 3 | * Author: Andrew Laing 4 | * Email: parisianconnections@gmail.com 5 | * Date: 27/04/2017 6 | * Description: Calculate Integer exponent 7 | */ 8 | 9 | #include 10 | 11 | 12 | int multiple(int i, int j); 13 | 14 | 15 | int main() 16 | { 17 | int i; 18 | int num1, num2; 19 | 20 | for(i=0; i<3; i++) 21 | { 22 | printf("Enter the first number: "); 23 | scanf("%d", &num1); 24 | 25 | printf("Enter the second number: "); 26 | scanf("%d", &num2); 27 | 28 | if (multiple(num1, num2)) 29 | printf("%d is a multiple of %d\n\n", num2, num1); 30 | else 31 | printf("%d is NOT a multiple of %d\n\n", num2, num1); 32 | } 33 | 34 | return 0; 35 | } 36 | 37 | 38 | int multiple(int i, int j) 39 | { 40 | return (j%i==0); 41 | } 42 | 43 | -------------------------------------------------------------------------------- /Chapter5/ex05_15.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Filename: ex05_15.c 3 | * Author: Andrew Laing 4 | * Email: parisianconnections@gmail.com 5 | * Date: 27/04/2017 6 | * Description: Calculate hypotenuse length 7 | */ 8 | 9 | #include 10 | #include 11 | 12 | 13 | double calcHypotenuse(double side1, double side2); 14 | 15 | 16 | int main() 17 | { 18 | float side1, side2, hypotenuse; 19 | 20 | printf("Enter length of side 1: "); 21 | scanf("%f", &side1); 22 | 23 | printf("Enter length of side 2: "); 24 | scanf("%f", &side2); 25 | 26 | hypotenuse = calcHypotenuse(side1, side2); 27 | printf("The length of the hypotenuse is %.2f\n", hypotenuse); 28 | 29 | return 0; 30 | } 31 | 32 | 33 | double calcHypotenuse(double side1, double side2) 34 | { 35 | double squareHypotenuse = (side1 * side1) + (side2 * side2); 36 | 37 | return sqrt(squareHypotenuse); 38 | } 39 | 40 | -------------------------------------------------------------------------------- /Chapter4/ex04_14.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Filename: ex04_14.c 3 | * Author: Andrew Laing 4 | * Email: parisianconnections@gmail.com 5 | * Date: 19/04/2017 6 | * Description: The factorial function is used frequently in 7 | * probability problems. The factorial of a positive 8 | * integer n (written n! and pronounced n factorial) 9 | * is equal to the product of the positive integers 10 | * from from 1 to n. Write a program that calculates 11 | * the factorials of the integers from 1 to 5. Print 12 | * the results in tabular format. 13 | */ 14 | 15 | #include 16 | 17 | int main() 18 | { 19 | int factorial = 1, max = 5; 20 | int n; 21 | 22 | printf("N\tFactorial N\n"); 23 | 24 | for(n = 1; n <= max; n++) { 25 | factorial *= n; 26 | printf("%d\t%d\n", n, factorial); 27 | } 28 | 29 | return 0; 30 | } 31 | 32 | -------------------------------------------------------------------------------- /Chapter4/ex04_34.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Filename: ex04_34.c 3 | * Author: Andrew Laing 4 | * Email: parisianconnections@gmail.com 5 | * Date: 24/04/2017 6 | */ 7 | 8 | #include 9 | 10 | void printDecimalAsBinary( int ); 11 | 12 | 13 | int main() 14 | { 15 | int i; 16 | for(i=0; i<=256; i++) 17 | { 18 | printf("%-9d", i); 19 | printDecimalAsBinary(i); 20 | printf("%9o%9X\n", i, i); 21 | } 22 | 23 | return 0; 24 | } 25 | 26 | 27 | void printDecimalAsBinary(int dec) 28 | { 29 | int pos=1; 30 | 31 | while(pos<=dec) 32 | pos*=2; 33 | 34 | pos/=2; 35 | 36 | while(pos>1) 37 | { 38 | if(dec>=pos) { 39 | printf("%d",(dec/pos)); 40 | dec-=pos; 41 | } 42 | else 43 | printf("0"); 44 | 45 | pos/=2; 46 | } 47 | 48 | printf("%d", dec); 49 | } 50 | 51 | -------------------------------------------------------------------------------- /Chapter2/ex02_25.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Filename: ex02_25.c 3 | * Author: Andrew Laing 4 | * Email: parisianconnections@gmail.com 5 | * Date: 13/04/2017 6 | * Description: Print your initials in block letters down the page. 7 | * Construct each block letter out of the letter 8 | * it represents. 9 | */ 10 | 11 | #include 12 | 13 | 14 | int main() 15 | { 16 | printf( "AAAAA\n" ); 17 | printf( " A A\n" ); 18 | printf( " A A\n" ); 19 | printf( " A A\n" ); 20 | printf( "AAAAA\n" ); 21 | printf( "\n\n" ); 22 | printf( " WWWWW\n" ); 23 | printf( "WW\n" ); 24 | printf( " WWWW\n" ); 25 | printf( "WW\n" ); 26 | printf( " WWWWW\n" ); 27 | printf( "\n\n" ); 28 | printf( "LLLLLLL\n" ); 29 | printf( "L\n" ); 30 | printf( "L\n" ); 31 | printf( "L\n" ); 32 | printf( "L\n\n" ); 33 | 34 | return 0; 35 | } 36 | -------------------------------------------------------------------------------- /Chapter8/ex08_23.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Filename: ex08_23.c 3 | * Author: Andrew Laing 4 | * Email: parisianconnections@gmail.com 5 | * Date: 25/06/2017. 6 | */ 7 | 8 | 9 | #include 10 | #include 11 | #define SIZE 16 12 | 13 | 14 | char *towns[SIZE] = { "liverpool", "southport", "birkenhead", "bootle", 15 | "wallasey", "prescott", "heswall", "hoylake", 16 | "formby", "woolton", "litherland", "bebington", 17 | "billinge", "crosby", "frankby", "thurstaston"}; 18 | 19 | 20 | void printBWords( char * [SIZE], int ); 21 | 22 | 23 | void printBWords( char *toPrint[SIZE], int arrLen ) 24 | { 25 | int i; 26 | for(i=0; i 14 | #include 15 | 16 | struct forLoopNode { 17 | int addressOfCountVariable; 18 | int addressOfStepValue; 19 | int startOfLoopAddress; 20 | int jmpGTAddress; 21 | struct forLoopNode *nextPtr; 22 | }; 23 | 24 | typedef struct forLoopNode ForLoopNode; 25 | typedef ForLoopNode *ForLoopNodePtr; 26 | 27 | void pushFL( ForLoopNodePtr *, int, int, int, int ); 28 | void popFL( ForLoopNodePtr *, int[] ); 29 | int isEmptyFL( ForLoopNodePtr ); 30 | void printStackFL( ForLoopNodePtr ); 31 | 32 | #endif 33 | 34 | -------------------------------------------------------------------------------- /Chapter2/ex02_20.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Filename: ex02_20.c 3 | * Author: Andrew Laing 4 | * Email: parisianconnections@gmail.com 5 | * Date: 13/04/2017 6 | * Description: Write a program that reads in the radius of a circle 7 | * and prints the circle's diameter, circumference and 8 | * area. Use the constant value of 3.14159 for PI. Do 9 | * each of the calculations inside the printf statements 10 | * and use the conversion specifier %f. 11 | */ 12 | 13 | #include 14 | 15 | 16 | int main() 17 | { 18 | const float PI = 3.14159; 19 | float radius; 20 | 21 | /* Get two numbers from the user */ 22 | printf( "Input the radius: " ); 23 | scanf("%f", &radius); 24 | 25 | printf( "Diameter = %f\n", (2 * radius) ); 26 | printf( "Circumference = %f\n", (2 * PI * radius) ); 27 | printf( "Circumference = %f\n", (PI * (radius * radius)) ); 28 | 29 | return 0; 30 | } 31 | -------------------------------------------------------------------------------- /Chapter12/ex12_26/ex12_26_d.simple: -------------------------------------------------------------------------------- 1 | 10 rem Input a series of numbers and determine and print 2 | 11 rem the largest. The first integer input indicates how 3 | 12 rem many numbers should be processed. 4 | 13 rem 5 | 14 rem set the value of the variables largest (l) to 0 6 | 15 rem and count (c) to 0 7 | 20 let l = 0 8 | 30 let c = 0 9 | 31 rem 10 | 32 rem input the number of numbers to be processed (n) 11 | 40 input n 12 | 41 rem 13 | 42 rem check whether all numbers have been input 14 | 43 rem if yes go print the result, else input a number 15 | 50 if c == n goto 120 16 | 60 input a 17 | 70 if a > l goto 100 18 | 71 rem increment the counter and return to the beginning 19 | 72 rem of the input loop 20 | 80 let c = c + 1 21 | 90 goto 50 22 | 91 rem 23 | 92 rem if a is larger than the current largest number (l) 24 | 93 rem set l equal to a and return back to the input loop 25 | 100 let l = a 26 | 110 goto 80 27 | 120 print l 28 | 130 end -------------------------------------------------------------------------------- /Chapter2/ex02_24.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Filename: ex02_24.c 3 | * Author: Andrew Laing 4 | * Email: parisianconnections@gmail.com 5 | * Date: 13/04/2017 6 | * Description: Write a program that reads an integer and determines 7 | * and prints whther it is odd or even. 8 | * Note: No booleans in C. A comparison returns 1 for true 9 | * or 0 for false; 10 | */ 11 | 12 | #include 13 | 14 | 15 | /* The method isEven returns 1 if the number is even, 16 | * otherwise it returns 0 17 | */ 18 | int isEven(int num) 19 | { 20 | return num%2==0; 21 | } 22 | 23 | 24 | int main() 25 | { 26 | int num; 27 | 28 | /* Get a number from the user */ 29 | printf( "Input a number: " ); 30 | scanf("%d", &num); 31 | 32 | /* 0 is false */ 33 | if(isEven(num)) 34 | printf( "%d is even.\n", num ); 35 | else 36 | printf( "%d is odd.\n", num ); 37 | 38 | return 0; 39 | } 40 | -------------------------------------------------------------------------------- /Chapter3/ex03_39.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Filename: ex03_39.c 3 | * Author: Andrew Laing 4 | * Email: parisianconnections@gmail.com 5 | * Date: 16/04/2017 6 | */ 7 | 8 | #include 9 | 10 | int numberOfSevens( int ); 11 | 12 | 13 | int main() 14 | { 15 | int num; 16 | 17 | /* Get a number from the user */ 18 | printf( "Input a number: " ); 19 | scanf("%d", &num); 20 | 21 | /* display the binary version */ 22 | printf("The number of sevens in your number is %d\n", numberOfSevens(num) ); 23 | 24 | return 0; 25 | } 26 | 27 | 28 | /* binToDecimal converts a binary number to a decimal 29 | * and returns the result.. */ 30 | int numberOfSevens(int num) 31 | { 32 | int total = 0; 33 | int remainder; 34 | 35 | while(num > 0) { 36 | remainder = num % 10; 37 | num /= 10; 38 | 39 | if(remainder == 7) 40 | total++; 41 | } 42 | 43 | return total; 44 | } 45 | 46 | -------------------------------------------------------------------------------- /Chapter5/ex05_24.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Filename: ex05_24.c 3 | * Author: Andrew Laing 4 | * Email: parisianconnections@gmail.com 5 | * Date: 01/05/2017 6 | */ 7 | 8 | #include 9 | 10 | 11 | int fahrenheitToCelsius( int ); 12 | int celsiusToFahrenheit( int ); 13 | 14 | 15 | int main() 16 | { 17 | int celsius, fahrenheit; 18 | 19 | printf("Enter temperature in celsius: "); 20 | scanf("%d", &celsius); 21 | printf("Temperature converted to fahrenheit = %d\n", celsiusToFahrenheit(celsius)); 22 | 23 | printf("\nEnter temperature in fahrenheit: "); 24 | scanf("%d", &fahrenheit); 25 | printf("Temperature converted to fahrenheit = %d\n", fahrenheitToCelsius(fahrenheit)); 26 | 27 | return 0; 28 | } 29 | 30 | 31 | int fahrenheitToCelsius( int fahrenheit ) 32 | { 33 | return (int) ((5.0/9.0)*(fahrenheit-32)); 34 | } 35 | 36 | int celsiusToFahrenheit( int celsius ) 37 | { 38 | return (int)(celsius * (9.0/5.0) + 32); 39 | } 40 | 41 | -------------------------------------------------------------------------------- /Chapter3/ex03_36.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Filename: ex03_36.c 3 | * Author: Andrew Laing 4 | * Email: parisianconnections@gmail.com 5 | * Date: 16/04/2017 6 | */ 7 | 8 | #include 9 | 10 | int binToDecimal( int ); 11 | 12 | 13 | int main() 14 | { 15 | int num; 16 | 17 | /* Get a number from the user */ 18 | printf( "Input a binary number: " ); 19 | scanf("%d", &num); 20 | 21 | /* display the binary version */ 22 | printf("The decimal version is %d\n", binToDecimal(num) ); 23 | 24 | return 0; 25 | } 26 | 27 | /* binToDecimal converts a binary number to a decimal 28 | * and returns the result.. */ 29 | int binToDecimal(int num) 30 | { 31 | int total = 0; 32 | int multiplier = 1; 33 | int remainder; 34 | 35 | while(num > 0) { 36 | remainder = num % 10; 37 | num /= 10; 38 | total += remainder * multiplier; 39 | multiplier *= 2; 40 | } 41 | 42 | return total; 43 | } 44 | 45 | -------------------------------------------------------------------------------- /Various/stringToInt.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Filename: stringToInt.c 3 | * Author: Andrew Laing 4 | * Email: parisianconnections@gmail.com 5 | * Date: 15/06/2017. 6 | * Description: Convert a string to an integer 7 | */ 8 | 9 | 10 | #include 11 | #include 12 | 13 | int stringToInt(char []); 14 | 15 | int main() 16 | { 17 | char str[] = "15634"; 18 | int result = stringToInt(str); 19 | 20 | printf("int is = %d\n", result); 21 | 22 | return 0; 23 | 24 | } 25 | 26 | 27 | /* Convert a char array to an integer 28 | * Note; sscanf returns the number of items converted, this can be 29 | * used to check for invalid input 30 | */ 31 | int stringToInt(char str[]) 32 | { 33 | int result; 34 | sscanf(str, "%d", &result); 35 | 36 | /* If number is out of bounds return Error code */ 37 | if( (result<-999999 && result!=-9999999) || (result > 999999) ) 38 | return -8888888; 39 | 40 | return result; 41 | } 42 | 43 | 44 | -------------------------------------------------------------------------------- /Chapter2/ex02_26.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Filename: ex02_26.c 3 | * Author: Andrew Laing 4 | * Email: parisianconnections@gmail.com 5 | * Date: 13/04/2017 6 | * Description: Write a program that reads in two integers and determines 7 | * and prints if the first is a multiple of the second. 8 | */ 9 | 10 | #include 11 | 12 | 13 | /* The method isMultiple returns 0 if num1 is a multiple 14 | * of num2, otherwise it returns the remainder 15 | */ 16 | int isMultiple(int num1, int num2) 17 | { 18 | return num1 % num2 == 0; 19 | } 20 | 21 | 22 | int main() 23 | { 24 | int num1, num2; 25 | 26 | /* Get a number from the user */ 27 | printf( "Input two numbers: " ); 28 | scanf("%d%d", &num1, &num2); 29 | 30 | /* 0 is false */ 31 | if(isMultiple(num1, num2)) 32 | printf( "%d is a multiple of %d.\n", num1, num2 ); 33 | else 34 | printf( "%d is NOT a multiple of %d.\n", num1, num2 ); 35 | 36 | return 0; 37 | } 38 | 39 | -------------------------------------------------------------------------------- /Chapter4/ex04_11.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Filename: ex04_11.c 3 | * Author: Andrew Laing 4 | * Email: parisianconnections@gmail.com 5 | * Date: 19/04/2017 6 | * Description: Write a program that finds the smallest of 7 | * several integers.Assume the first integer read specifies 8 | * the number of values remaining to be entered. 9 | */ 10 | 11 | #include 12 | 13 | 14 | int main() 15 | { 16 | int count, num; 17 | int smallest = 0; 18 | 19 | printf("Enter the number of values to be entered: "); 20 | scanf("%d", &count); 21 | if(count>0) { 22 | printf("Enter number: "); 23 | scanf("%d", &smallest); 24 | } 25 | 26 | while(count-- > 1) { 27 | printf("Enter number: "); 28 | scanf("%d", &num); 29 | if(num < smallest) 30 | smallest = num; 31 | } 32 | 33 | printf("\nThe smallest of the entered numbers is %d\n", smallest); 34 | 35 | return 0; 36 | } 37 | 38 | -------------------------------------------------------------------------------- /Various/binaryToDecimal.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Filename: ex03_36.c 3 | * Author: Andrew Laing 4 | * Email: parisianconnections@gmail.com 5 | * Date: 16/04/2017 6 | */ 7 | 8 | #include 9 | 10 | int binToDecimal( int ); 11 | 12 | 13 | int main() 14 | { 15 | int num; 16 | 17 | /* Get a number from the user */ 18 | printf( "Input a binary number: " ); 19 | scanf("%d", &num); 20 | 21 | /* display the binary version */ 22 | printf("The decimal version is %d\n", binToDecimal(num) ); 23 | 24 | return 0; 25 | } 26 | 27 | /* binToDecimal converts a binary number to a decimal 28 | * and returns the result.. */ 29 | int binToDecimal(int num) 30 | { 31 | int total = 0; 32 | int multiplier = 1; 33 | int remainder; 34 | 35 | while(num > 0) { 36 | remainder = num % 10; 37 | num /= 10; 38 | total += remainder * multiplier; 39 | multiplier *= 2; 40 | } 41 | 42 | return total; 43 | } 44 | 45 | -------------------------------------------------------------------------------- /Chapter10/ex10_12.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Filename: ex10_12.c 3 | * Author: Andrew Laing 4 | * Email: parisianconnections@gmail.com 5 | * Date: 21/07/2017. 6 | */ 7 | 8 | #include 9 | 10 | int power2( int, int ); 11 | void displayBits( unsigned ); 12 | 13 | int main() 14 | { 15 | int result, number=6, pow=4; 16 | 17 | result = power2(number, pow); 18 | printf("power2(6, 4) = %d\n", result); 19 | 20 | displayBits( result ); 21 | return 0; 22 | } 23 | 24 | 25 | int power2(int number, int pow) 26 | { 27 | return number * (2< 1) { 39 | reversed += num%10; 40 | num/=10; 41 | if(num>=1) 42 | reversed*=10; 43 | } 44 | 45 | if(num==1) 46 | reversed += 1; 47 | 48 | return reversed; 49 | } 50 | 51 | -------------------------------------------------------------------------------- /Various/strcmpAndStrncmp.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Filename: fig08_21.c 3 | * Author: Andrew Laing 4 | * Email: parisianconnections@gmail.com 5 | * Date: 19/06/2017. 6 | * Description: Using strcmp and strncmp 7 | */ 8 | 9 | #include 10 | #include 11 | 12 | int main() 13 | { 14 | const char *s1 = "Happy New Year"; 15 | const char *s2 = "Happy New Year"; 16 | const char *s3 = "Happy Holidays"; 17 | 18 | printf( "%s%s\n%s%s\n%s%s\n\n%s%2d\n%s%2d\n%s%2d\n\n", 19 | "s1 = ", s1, "s2 = ", s2, "s3 = ", s3, 20 | "strcmp(s1, s2) = ", strcmp(s1, s2), 21 | "strcmp(s1, s3) = ", strcmp(s1, s3), 22 | "strcmp(s3, s1) = ", strcmp(s3, s1) ); 23 | 24 | printf ("%s%2d\n%s%2d\n%s%2d\n", 25 | "strncmp(s1, s2, 6) = ", strncmp(s1, s2, 6), 26 | "strncmp(s1, s3, 7) = ", strncmp(s1, s3, 7), 27 | "strncmp(s3, s1, 7) = ", strncmp(s3, s1, 7) ); 28 | 29 | return 0; 30 | } 31 | 32 | 33 | -------------------------------------------------------------------------------- /Chapter13/ex13_10.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Filename: ex13_10.c 3 | * Author: Andrew Laing 4 | * Email: parisianconnections@gmail.com 5 | * Date: 26/10/2017 6 | * Description: Macro 7 | */ 8 | 9 | #include 10 | #define SUMARRAY(arr,lenArr) i=0; \ 11 | total = 0; \ 12 | for( ; i 10 | 11 | 12 | int main() 13 | { 14 | int a, b, c, d, e; 15 | int count; 16 | 17 | printf("Enter five numbers separated by spaces: "); 18 | scanf("%d%d%d%d%d", &a, &b, &c, &d, &e); 19 | 20 | for(count = 0; count < a; count++) 21 | printf("*"); 22 | 23 | printf("\n"); 24 | 25 | for(count = 0; count < b; count++) 26 | printf("*"); 27 | 28 | printf("\n"); 29 | 30 | for(count = 0; count < c; count++) 31 | printf("*"); 32 | 33 | printf("\n"); 34 | 35 | 36 | for(count = 0; count < d; count++) 37 | printf("*"); 38 | 39 | printf("\n"); 40 | 41 | for(count = 0; count < e; count++) 42 | printf("*"); 43 | 44 | printf("\n"); 45 | 46 | return 0; 47 | } 48 | 49 | -------------------------------------------------------------------------------- /Chapter3/ex03_42.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Filename: ex03_42.c 3 | * Author: Andrew Laing 4 | * Email: parisianconnections@gmail.com 5 | * Date: 16/04/2017 6 | */ 7 | 8 | #include 9 | 10 | const float PI = 3.14159; 11 | 12 | float diameter( float ); 13 | float area( float ); 14 | float circumference( float ); 15 | 16 | 17 | int main() 18 | { 19 | float radius; 20 | 21 | /* Get a number from the user */ 22 | printf( "Input the radius: " ); 23 | scanf("%f", &radius); 24 | 25 | /* display the results */ 26 | printf("Diameter is %.2f\n", diameter(radius) ); 27 | printf("Area is %.2f\n", area(radius) ); 28 | printf("Circumference is %.2f\n", circumference(radius) ); 29 | return 0; 30 | } 31 | 32 | 33 | float diameter(float radius) 34 | { 35 | return radius * 2; 36 | } 37 | 38 | 39 | float area(float radius) 40 | { 41 | return PI * (radius * radius); 42 | } 43 | 44 | 45 | float circumference(float radius) 46 | { 47 | return 2 * PI * radius; 48 | } 49 | 50 | -------------------------------------------------------------------------------- /Chapter6/ex06_34.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Filename: ex06_34.c 3 | * Author: Andrew Laing 4 | * Email: parisianconnections@gmail.com 5 | * Date: 21/05/2017 6 | */ 7 | 8 | #include 9 | #define SIZE 100 10 | 11 | int linearSearch( const int[], int, int ); 12 | 13 | 14 | int main() 15 | { 16 | int a[ SIZE ], x, searchKey, element; 17 | 18 | for(x=0; x<=SIZE-1; x++) 19 | a[ x ] = 2 * x; 20 | 21 | printf( "Enter integer search key: "); 22 | scanf("%d", &searchKey); 23 | element = linearSearch( a, searchKey, SIZE ); 24 | 25 | if( element != -1 ) 26 | printf( "Found value in element %d\n", element ); 27 | else 28 | printf( "Value not found\n" ); 29 | 30 | return 0; 31 | } 32 | 33 | 34 | int linearSearch( const int array[], int key, int size ) 35 | { 36 | if(size <= 0) 37 | return -1; 38 | 39 | if(array[size-1]==key) 40 | return size-1; 41 | else 42 | return linearSearch(array, key, size-1); 43 | } 44 | 45 | -------------------------------------------------------------------------------- /Various/stringEndsWith.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | int string_ends_with(const char *str, const char *suffix); 8 | 9 | int main() 10 | { 11 | char *filename = "drolegna.csv"; 12 | char *filename1 = "puberty.mp4"; 13 | char *suffix = ".csv"; 14 | 15 | if(string_ends_with(filename, suffix)) 16 | printf("%s ends with %s\n", filename, suffix ); 17 | else 18 | printf("%s does not end with %s\n", filename, suffix ); 19 | 20 | if(string_ends_with(filename1, suffix)) 21 | printf("%s ends with %s\n", filename1, suffix ); 22 | else 23 | printf("%s does not end with %s\n", filename1, suffix ); 24 | 25 | return 0; 26 | } 27 | 28 | int string_ends_with(const char *s, const char *suffix) 29 | { 30 | int sLength = strlen(s); 31 | int suffixLength = strlen(suffix); 32 | 33 | return (sLength >= suffixLength) && 34 | (strcmp(s + (sLength-suffixLength), suffix) == 0); 35 | } 36 | -------------------------------------------------------------------------------- /Chapter12/ex12_27/SimpleCompilerV2.1/src/SMLOpCodes.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Filename: SMLOpCodes.h 3 | * Author: Andrew Laing 4 | * Email: parisianconnections@gmail.com 5 | * Date: 31/08/2017. 6 | * Description: Contains the Op Codes for SML instructions. 7 | */ 8 | 9 | #ifndef SMLOPCODES_H 10 | #define SMLOPCODES_H 11 | 12 | 13 | #define READ 10 14 | #define WRITE 11 15 | #define NEWLINE 12 16 | #define READFLOAT 13 17 | #define WRITEFLOAT 14 18 | #define READSTRING 15 19 | #define WRITESTRING 16 20 | 21 | #define LOAD 20 22 | #define STORE 21 23 | #define LOADFLOAT 22 24 | #define STOREFLOAT 23 25 | 26 | #define ADD 30 27 | #define SUBTRACT 31 28 | #define DIVIDE 32 29 | #define MULTIPLY 33 30 | #define MODULUS 34 31 | #define EXPONENTIATION 35 32 | #define ADDFLOAT 36 33 | #define SUBTRACTFLOAT 37 34 | #define DIVIDEFLOAT 38 35 | #define MULTIPLYFLOAT 39 36 | #define MODULUSFLOAT 40 37 | 38 | #define BRANCH 50 39 | #define BRANCHNEG 51 40 | #define BRANCHZERO 52 41 | #define BRANCHPOS 53 42 | #define HALT 54 43 | 44 | #endif 45 | -------------------------------------------------------------------------------- /Chapter12/ex12_28/SimpleCompilerV2.2/src/SMLOpCodes.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Filename: SMLOpCodes.h 3 | * Author: Andrew Laing 4 | * Email: parisianconnections@gmail.com 5 | * Date: 31/08/2017. 6 | * Description: Contains the Op Codes for SML instructions. 7 | */ 8 | 9 | #ifndef SMLOPCODES_H 10 | #define SMLOPCODES_H 11 | 12 | 13 | #define READ 10 14 | #define WRITE 11 15 | #define NEWLINE 12 16 | #define READFLOAT 13 17 | #define WRITEFLOAT 14 18 | #define READSTRING 15 19 | #define WRITESTRING 16 20 | 21 | #define LOAD 20 22 | #define STORE 21 23 | #define LOADFLOAT 22 24 | #define STOREFLOAT 23 25 | 26 | #define ADD 30 27 | #define SUBTRACT 31 28 | #define DIVIDE 32 29 | #define MULTIPLY 33 30 | #define MODULUS 34 31 | #define EXPONENTIATION 35 32 | #define ADDFLOAT 36 33 | #define SUBTRACTFLOAT 37 34 | #define DIVIDEFLOAT 38 35 | #define MULTIPLYFLOAT 39 36 | #define MODULUSFLOAT 40 37 | 38 | #define BRANCH 50 39 | #define BRANCHNEG 51 40 | #define BRANCHZERO 52 41 | #define BRANCHPOS 53 42 | #define HALT 54 43 | 44 | #endif 45 | -------------------------------------------------------------------------------- /Chapter2/ex02_30.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Filename: ex02_30.c 3 | * Author: Andrew Laing 4 | * Email: parisianconnections@gmail.com 5 | * Date: 13/04/2017 6 | * Description: Write a program which inputs one five-digit number, 7 | * separates the number into its individual digits, and 8 | * prints the digits separated from one another by three 9 | * spaces each. 10 | */ 11 | 12 | #include 13 | 14 | 15 | int main() 16 | { 17 | int num; 18 | 19 | /* Get a number from the user */ 20 | printf( "Input a five-digit number: " ); 21 | scanf("%d", &num); 22 | 23 | printf("%d ", (num/10000)); 24 | num = num - ((num/10000)*10000); 25 | 26 | printf("%d ", (num/1000)); 27 | num = num - ((num/1000)*1000); 28 | 29 | printf("%d ", (num/100)); 30 | num = num - ((num/100)*100); 31 | 32 | printf("%d ", (num/10)); 33 | num = num - ((num/10)*10); 34 | 35 | printf("%d\n", (num%10)); 36 | 37 | return 0; 38 | } 39 | -------------------------------------------------------------------------------- /Chapter4/ex04_27.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Filename: ex04_27.c 3 | * Author: Andrew Laing 4 | * Email: parisianconnections@gmail.com 5 | * Date: 22/04/2017 6 | */ 7 | 8 | #include 9 | 10 | int isValidRATriangle( float, float, float ); 11 | 12 | 13 | int main() 14 | { 15 | int a, b, c; 16 | 17 | for(a=1; a<=500; a++) 18 | { 19 | for(b=1; b<=500; b++) 20 | { 21 | for(c=1; c<=500; c++) 22 | { 23 | if( isValidRATriangle(a, b, c) ) 24 | printf("A=%d B=%d C=%d\n", a, b, c); 25 | } 26 | } 27 | } 28 | 29 | return 0; 30 | } 31 | 32 | 33 | int isValidRATriangle(float a, float b, float c) 34 | { 35 | if(a>b) 36 | { 37 | if(a>c) 38 | return ( (c*c)+(b*b) == (a*a) ); 39 | else 40 | return ( (a*a)+(b*b) == (c*c) ); 41 | } 42 | else if(b>c) 43 | return ( (a*a)+(c*c) == (b*b) ); 44 | else 45 | return ( (a*a)+(b*b) == (c*c) ); 46 | } 47 | 48 | -------------------------------------------------------------------------------- /Chapter12/ex12_27/SimpleTronV2.0/src/SMLOpCodes.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Filename: SMLOpCodes.h 3 | * Author: Andrew Laing 4 | * Email: parisianconnections@gmail.com 5 | * Date: 31/08/2017. 6 | * Description: Contains the Op Codes for SML instructions. 7 | */ 8 | 9 | #ifndef SMLOPCODES_H 10 | #define SMLOPCODES_H 11 | 12 | 13 | #define READ 10 14 | #define WRITE 11 15 | #define NEWLINE 12 16 | #define READFLOAT 13 17 | #define WRITEFLOAT 14 18 | #define READSTRING 15 19 | #define WRITESTRING 16 20 | 21 | #define LOAD 20 22 | #define STORE 21 23 | #define LOADFLOAT 22 24 | #define STOREFLOAT 23 25 | 26 | #define ADD 30 27 | #define SUBTRACT 31 28 | #define DIVIDE 32 29 | #define MULTIPLY 33 30 | #define MODULUS 34 31 | #define EXPONENTIATION 35 32 | #define ADDFLOAT 36 33 | #define SUBTRACTFLOAT 37 34 | #define DIVIDEFLOAT 38 35 | #define MULTIPLYFLOAT 39 36 | #define MODULUSFLOAT 40 37 | 38 | #define BRANCH 50 39 | #define BRANCHNEG 51 40 | #define BRANCHZERO 52 41 | #define BRANCHPOS 53 42 | #define HALT 54 43 | 44 | #endif 45 | 46 | -------------------------------------------------------------------------------- /Chapter10/ex10_18.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Filename: ex10_18.c 3 | * Author: Andrew Laing 4 | * Email: parisianconnections@gmail.com 5 | * Date: 21/07/2017. 6 | */ 7 | 8 | #include 9 | 10 | void displayBits( unsigned ); 11 | 12 | int main() 13 | { 14 | unsigned x; 15 | 16 | printf("Enter an unsigned number: "); 17 | scanf("%u", &x); 18 | fflush(stdin); 19 | displayBits(x); 20 | 21 | return 0; 22 | } 23 | 24 | 25 | void displayBits( unsigned value ) 26 | { 27 | unsigned c, displayMask, max; 28 | 29 | int intSize = sizeof(int); 30 | max = intSize*8; 31 | displayMask = 1<<(max-1); 32 | printf("This system uses %d-byte integers\n", intSize); 33 | 34 | 35 | printf( "%7u = ", value ); 36 | 37 | for( c=1; c<=max; c++ ) 38 | { 39 | putchar( value & displayMask ? '1' : '0' ); 40 | value <<= 1; 41 | 42 | if(c % 8 == 0) 43 | putchar( ' ' ); 44 | } 45 | 46 | putchar( '\n' ); 47 | } 48 | 49 | 50 | -------------------------------------------------------------------------------- /Chapter12/ex12_29/RunSML_version_1.0/src/SMLOpCodes.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Filename: SMLOpCodes.h 3 | * Author: Andrew Laing 4 | * Email: parisianconnections@gmail.com 5 | * Date: 31/08/2017. 6 | * Description: Contains the Op Codes for SML instructions. 7 | */ 8 | 9 | #ifndef SMLOPCODES_H 10 | #define SMLOPCODES_H 11 | 12 | 13 | #define READ 10 14 | #define WRITE 11 15 | #define NEWLINE 12 16 | #define READFLOAT 13 17 | #define WRITEFLOAT 14 18 | #define READSTRING 15 19 | #define WRITESTRING 16 20 | 21 | #define LOAD 20 22 | #define STORE 21 23 | #define LOADFLOAT 22 24 | #define STOREFLOAT 23 25 | 26 | #define ADD 30 27 | #define SUBTRACT 31 28 | #define DIVIDE 32 29 | #define MULTIPLY 33 30 | #define MODULUS 34 31 | #define EXPONENTIATION 35 32 | #define ADDFLOAT 36 33 | #define SUBTRACTFLOAT 37 34 | #define DIVIDEFLOAT 38 35 | #define MULTIPLYFLOAT 39 36 | #define MODULUSFLOAT 40 37 | 38 | #define BRANCH 50 39 | #define BRANCHNEG 51 40 | #define BRANCHZERO 52 41 | #define BRANCHPOS 53 42 | #define HALT 54 43 | 44 | #endif 45 | 46 | -------------------------------------------------------------------------------- /Chapter12/ex12_29/SimpleCompiler_version_2.9.3/src/SMLOpCodes.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Filename: SMLOpCodes.h 3 | * Author: Andrew Laing 4 | * Email: parisianconnections@gmail.com 5 | * Date: 31/08/2017. 6 | * Description: Contains the Op Codes for SML instructions. 7 | */ 8 | 9 | #ifndef SMLOPCODES_H 10 | #define SMLOPCODES_H 11 | 12 | 13 | #define READ 10 14 | #define WRITE 11 15 | #define NEWLINE 12 16 | #define READFLOAT 13 17 | #define WRITEFLOAT 14 18 | #define READSTRING 15 19 | #define WRITESTRING 16 20 | 21 | #define LOAD 20 22 | #define STORE 21 23 | #define LOADFLOAT 22 24 | #define STOREFLOAT 23 25 | 26 | #define ADD 30 27 | #define SUBTRACT 31 28 | #define DIVIDE 32 29 | #define MULTIPLY 33 30 | #define MODULUS 34 31 | #define EXPONENTIATION 35 32 | #define ADDFLOAT 36 33 | #define SUBTRACTFLOAT 37 34 | #define DIVIDEFLOAT 38 35 | #define MULTIPLYFLOAT 39 36 | #define MODULUSFLOAT 40 37 | 38 | #define BRANCH 50 39 | #define BRANCHNEG 51 40 | #define BRANCHZERO 52 41 | #define BRANCHPOS 53 42 | #define HALT 54 43 | 44 | #endif 45 | -------------------------------------------------------------------------------- /Chapter12/ex12_29/SimpleTron_version_2.2/src/SMLOpCodes.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Filename: SMLOpCodes.h 3 | * Author: Andrew Laing 4 | * Email: parisianconnections@gmail.com 5 | * Date: 31/08/2017. 6 | * Description: Contains the Op Codes for SML instructions. 7 | */ 8 | 9 | #ifndef SMLOPCODES_H 10 | #define SMLOPCODES_H 11 | 12 | 13 | #define READ 10 14 | #define WRITE 11 15 | #define NEWLINE 12 16 | #define READFLOAT 13 17 | #define WRITEFLOAT 14 18 | #define READSTRING 15 19 | #define WRITESTRING 16 20 | 21 | #define LOAD 20 22 | #define STORE 21 23 | #define LOADFLOAT 22 24 | #define STOREFLOAT 23 25 | 26 | #define ADD 30 27 | #define SUBTRACT 31 28 | #define DIVIDE 32 29 | #define MULTIPLY 33 30 | #define MODULUS 34 31 | #define EXPONENTIATION 35 32 | #define ADDFLOAT 36 33 | #define SUBTRACTFLOAT 37 34 | #define DIVIDEFLOAT 38 35 | #define MULTIPLYFLOAT 39 36 | #define MODULUSFLOAT 40 37 | 38 | #define BRANCH 50 39 | #define BRANCHNEG 51 40 | #define BRANCHZERO 52 41 | #define BRANCHPOS 53 42 | #define HALT 54 43 | 44 | #endif 45 | 46 | -------------------------------------------------------------------------------- /Chapter5/ex05_26.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Filename: ex05_26.c 3 | * Author: Andrew Laing 4 | * Email: parisianconnections@gmail.com 5 | * Date: 01/05/2017 6 | */ 7 | 8 | #include 9 | 10 | void printFactors( int ); 11 | int isPerfect( int ); 12 | 13 | 14 | int main() 15 | { 16 | int n; 17 | 18 | for(n=1; n<=100000; n++) 19 | { 20 | if(isPerfect(n)) 21 | printFactors( n ); 22 | } 23 | 24 | return 0; 25 | } 26 | 27 | 28 | void printFactors( int n ) 29 | { 30 | int i=1; 31 | int sum = 0; 32 | 33 | while(sum < n) { 34 | sum+=i; 35 | printf("%d ", i); 36 | i++; 37 | } 38 | printf("= %d\n", n); 39 | } 40 | 41 | 42 | int isPerfect( int n ) 43 | { 44 | int perfect = 0; 45 | int i=1; 46 | int sum = 0; 47 | 48 | while(sum <= n) { 49 | sum+=i; 50 | 51 | if(sum==n) 52 | perfect = 1; 53 | 54 | i++; 55 | } 56 | 57 | return perfect; 58 | } 59 | 60 | -------------------------------------------------------------------------------- /Chapter8/ex08_24.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Filename: ex08_24.c 3 | * Author: Andrew Laing 4 | * Email: parisianconnections@gmail.com 5 | * Date: 25/06/2017. 6 | */ 7 | 8 | 9 | #include 10 | #include 11 | #define SIZE 16 12 | 13 | char *words[SIZE] = { "FRUITY", "PRETTIFIED", "MODE", "BOTTLED", 14 | "WALLABY", "PRICED", "WALLED", "LAKE", 15 | "FORM", "WOOLY", "LITHE", "BABE", 16 | "LINGERED", "CROSS", "FRANKED", "THURSDAY"}; 17 | 18 | int endsInED( char *toCheck); 19 | 20 | int endsInED( char *toCheck) 21 | { 22 | int len = strlen(toCheck); 23 | if(len<=1) 24 | return -1; 25 | 26 | if(toCheck[len-2]=='E' && toCheck[len-1]=='D' ) 27 | return 1; 28 | 29 | return -1; 30 | } 31 | 32 | 33 | int main() 34 | { 35 | int i; 36 | for(i=0; i 11 | 12 | int main() 13 | { 14 | printf( "Decimal\tBinary\tOctal\tHex\n" ); 15 | printf( "0\t0\t0\t0\n" ); 16 | printf( "1\t1\t1\t1\n" ); 17 | printf( "2\t10\t2\t2\n" ); 18 | printf( "3\t11\t3\t3\n" ); 19 | printf( "4\t100\t4\t4\n" ); 20 | printf( "5\t101\t5\t5\n" ); 21 | printf( "6\t110\t6\t6\n" ); 22 | printf( "7\t111\t7\t7\n" ); 23 | printf( "8\t1000\t10\t8\n" ); 24 | printf( "9\t1001\t11\t9\n" ); 25 | printf( "10\t1010\t12\tA\n" ); 26 | printf( "11\t1011\t13\tB\n" ); 27 | printf( "12\t1100\t14\tC\n" ); 28 | printf( "13\t1101\t15\tD\n" ); 29 | printf( "14\t1110\t16\tE\n" ); 30 | printf( "15\t1111\t17\tF\n" ); 31 | 32 | return 0; 33 | } 34 | 35 | -------------------------------------------------------------------------------- /Chapter9/ex09_18.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Filename: ex09_18.c 3 | * Author: Andrew Laing 4 | * Email: parisianconnections@gmail.com 5 | * Date: 18/07/2017. 6 | */ 7 | 8 | #include 9 | 10 | 11 | int getLine( char [], int ); 12 | 13 | int main() 14 | { 15 | char a[50], b[50], c[50]; 16 | printf("Please type suzy: "); 17 | getLine(a, 50); 18 | printf("Please type \'suzy\': "); 19 | getLine(b, 50); 20 | printf("Please type \"suzy\": "); 21 | getLine(c, 50); 22 | printf("\ninput1 = %s\n", a); 23 | printf("input2 = %s\n", b); 24 | printf("input3 = %s\n", c); 25 | 26 | 27 | return 0; 28 | } 29 | 30 | 31 | /* Read a string into a character array and return the length of the array */ 32 | int getLine( char s[], int maxLength ) 33 | { 34 | int c, i=0; 35 | 36 | while( ( --maxLength > 0) && ( (c=getchar())!=EOF ) && ( c!='\n' ) ) 37 | s[i++]=c; 38 | 39 | s[i] = '\0'; 40 | 41 | /* Clear any overflow*/ 42 | fflush(stdin); 43 | 44 | return i; 45 | } 46 | -------------------------------------------------------------------------------- /Chapter9/ex09_09.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Filename: ex09_09.c 3 | * Author: Andrew Laing 4 | * Email: parisianconnections@gmail.com 5 | * Date: 17/07/2017. 6 | */ 7 | 8 | #include 9 | 10 | int main() 11 | { 12 | int a = 455; 13 | int *a1; 14 | a1 = &a; 15 | 16 | int b = -455; 17 | int *b1; 18 | b1 = &a; 19 | 20 | short c = 32000; 21 | short *c1; 22 | c1 = &c; 23 | 24 | long int d = 2000000000L; 25 | long int *d1 = &d; 26 | int e = 0455; 27 | unsigned int f = 455; 28 | unsigned int g = -455; 29 | int h = 0x455; 30 | 31 | 32 | 33 | printf("%d\n", &a1); 34 | printf("%i\n", &b1); 35 | printf("%d\n", &a1); 36 | printf("%hd\n", &c1); /* h is short */ 37 | printf("%ld\n", &d1); /* l is long */ 38 | printf("%o\n", &a1); 39 | printf("%u\n", &a1); 40 | printf("%u\n", &b1); 41 | printf("%x\n", &a1); 42 | printf("%X\n", &a1); 43 | printf("%p\n", a1); 44 | 45 | return 0; 46 | } 47 | 48 | 49 | 50 | -------------------------------------------------------------------------------- /Chapter2/ex02_27.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Filename: ex02_27.c 3 | * Author: Andrew Laing 4 | * Email: parisianconnections@gmail.com 5 | * Date: 13/04/2017 6 | * Description: Display the checkerboard pattern with eight printf 7 | * statements and then display the same pattern with as 8 | * few printf statements as possible. 9 | */ 10 | 11 | #include 12 | 13 | 14 | int main() 15 | { 16 | printf( "--VERSION ONE--\n\n" ); 17 | 18 | printf( "* * * * * * * *\n" ); 19 | printf( " * * * * * * * *\n" ); 20 | printf( "* * * * * * * *\n" ); 21 | printf( " * * * * * * * *\n" ); 22 | printf( "* * * * * * * *\n" ); 23 | printf( " * * * * * * * *\n" ); 24 | printf( "* * * * * * * *\n" ); 25 | printf( " * * * * * * * *\n\n" ); 26 | 27 | printf( "--VERSION TWO--\n\n" ); 28 | 29 | char* pattern; 30 | int i; 31 | for(i=2; i<10; i++) { 32 | pattern = (i%2 == 0) ? "* * * * * * * *\n" : " * * * * * * * *\n"; 33 | printf(pattern); 34 | } 35 | 36 | 37 | return 0; 38 | } 39 | -------------------------------------------------------------------------------- /Chapter2/ex02_18.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Filename: ex02_18.c 3 | * Author: Andrew Laing 4 | * Email: parisianconnections@gmail.com 5 | * Date: 12/04/2017 6 | * Description: Write a program that asks the user to enter two integers, 7 | * obtains the numbers from the user, then prints the larger 8 | * number followed by the words "is larger". If the numbers 9 | * are equal, print the message "These numbers are equal." 10 | * Use only the single-selection form of the if statement 11 | * you learned in this chapter. 12 | */ 13 | 14 | #include 15 | 16 | 17 | int main() 18 | { 19 | int num1, num2; 20 | 21 | /* Get two numbers from the user */ 22 | printf( "Enter two integers: " ); 23 | scanf("%d%d", &num1, &num2); 24 | 25 | if(num1 > num2) 26 | printf("%d is larger.", num1); 27 | 28 | if(num1 < num2) 29 | printf("%d is larger.", num2); 30 | 31 | if(num1 == num2) 32 | printf("These numbers are equal."); 33 | 34 | return 0; 35 | } 36 | -------------------------------------------------------------------------------- /Chapter4/ex04_23.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Filename: ex04_23.c 3 | * Author: Andrew Laing 4 | * Email: parisianconnections@gmail.com 5 | * Date: 21/04/2017 6 | * Description: Modify the program in fig04_06 so that it uses only integers 7 | * to calculate the compound interest. 8 | */ 9 | 10 | #include 11 | #include 12 | 13 | int main() 14 | { 15 | int year; 16 | long amount, principal = 100000, rate = 5; 17 | long modifier; 18 | long r = rate+100; 19 | int count; 20 | 21 | printf( "%4s%21s\n", "Year", "Amount on deposit" ); 22 | 23 | for(year = 1; year <= 10; year++ ) 24 | { 25 | modifier = r; 26 | for(count=1; count 9 | 10 | int main() 11 | { 12 | /* Initialisation phase */ 13 | int passes=0, failures=0, student=1, result; 14 | 15 | /* Processing phase */ 16 | while( student <= 10 ) 17 | { 18 | printf( "Enter result ( 1=pass, 2=fail ): " ); 19 | scanf( "%d", &result ); 20 | 21 | if(result == 1) 22 | { 23 | passes = passes + 1; 24 | student = student + 1; 25 | } 26 | else if(result == 2) 27 | { 28 | failures = failures + 1; 29 | student = student + 1; 30 | } 31 | else 32 | printf("<< Invalid input. >>\n"); 33 | } 34 | 35 | /* Termination phase */ 36 | 37 | printf( "Passed %d\n", passes ); 38 | printf( "Failed %d\n", failures ); 39 | 40 | if( passes > 8 ) 41 | printf( "Raise tuition\n" ); 42 | 43 | return 0; 44 | } 45 | 46 | -------------------------------------------------------------------------------- /Chapter4/ex04_25.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Filename: ex04_25.c 3 | * Author: Andrew Laing 4 | * Email: parisianconnections@gmail.com 5 | * Date: 22/04/2017 6 | * Description: Print a table of decimal, binary, octal and 7 | * hexadecimal equivalents. 8 | */ 9 | 10 | #include 11 | 12 | void printDecimalAsBinary( int ); 13 | 14 | 15 | int main() 16 | { 17 | int i; 18 | printf("Decimal\tBinary\tOctal\tHex\n"); 19 | for(i=0; i<20; i++) 20 | { 21 | printf("%d\t", i); 22 | printDecimalAsBinary(i); 23 | printf("\t%o\t%X\n", i, i); 24 | } 25 | 26 | return 0; 27 | } 28 | 29 | void printDecimalAsBinary(int dec) 30 | { 31 | int pos=1; 32 | 33 | while(pos<=dec) 34 | pos*=2; 35 | 36 | pos/=2; 37 | 38 | while(pos>1) 39 | { 40 | if(dec>=pos) { 41 | printf("%d",(dec/pos)); 42 | dec-=pos; 43 | } 44 | else 45 | printf("0"); 46 | 47 | pos/=2; 48 | } 49 | 50 | printf("%d", dec); 51 | } 52 | 53 | -------------------------------------------------------------------------------- /Chapter9/ex09_17.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Filename: ex09_17.c 3 | * Author: Andrew Laing 4 | * Email: parisianconnections@gmail.com 5 | * Date: 18/07/2017. 6 | */ 7 | 8 | #include 9 | 10 | 11 | 12 | int main() 13 | { 14 | float a, b, c; 15 | 16 | printf("a: Enter 1.2345: "); 17 | scanf("%e", &a); 18 | fflush(stdin); 19 | 20 | printf("b: Enter 1.2345: "); 21 | scanf("%f", &b); 22 | fflush(stdin); 23 | 24 | printf("c: Enter 1.2345: "); 25 | scanf("%g", &c); 26 | fflush(stdin); 27 | 28 | 29 | 30 | printf("\n---- a %%e ----\n"); 31 | printf("%%e = %e\n", a); 32 | printf("%%f = %f\n", a); 33 | printf("%%g = %g\n", a); 34 | 35 | 36 | printf("\n---- b %%f ----\n"); 37 | printf("%%e = %e\n", b); 38 | printf("%%f = %f\n", b); 39 | printf("%%g = %g\n", b); 40 | 41 | 42 | printf("\n---- c %%o ----\n"); 43 | printf("%%e = %e\n", c); 44 | printf("%%f = %f\n", c); 45 | printf("%%g = %g\n", c); 46 | 47 | 48 | return 0; 49 | } 50 | 51 | 52 | -------------------------------------------------------------------------------- /Chapter3/ex03_21.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Filename: ex03_21.c 3 | * Author: Andrew Laing 4 | * Email: parisianconnections@gmail.com 5 | * Date: 15/04/2017 6 | */ 7 | 8 | #include 9 | 10 | int main() 11 | { 12 | /* Initialisation phase */ 13 | int hoursWorked; 14 | float hourlyRate, salary; 15 | 16 | printf( "Enter # of hours worked (-1 to end): " ); 17 | scanf( "%d", &hoursWorked ); 18 | 19 | /* Processing phase */ 20 | while( hoursWorked != -1 ) 21 | { 22 | salary = 0; 23 | printf( "Enter hourly rate of the worker ($00.00): " ); 24 | scanf( "%f", &hourlyRate ); 25 | 26 | if(hoursWorked > 40) { 27 | salary += (hoursWorked - 40) * (hourlyRate * 1.5); 28 | hoursWorked = 40; 29 | } 30 | 31 | salary += hoursWorked *hourlyRate; 32 | 33 | printf( "Salary is $%.2f\n", salary ); 34 | 35 | printf( "\n\nEnter # of hours worked (-1 to end): " ); 36 | scanf( "%d", &hoursWorked ); 37 | } 38 | 39 | /* Termination phase */ 40 | return 0; 41 | } 42 | 43 | -------------------------------------------------------------------------------- /Chapter4/ex04_15.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Filename: ex04_15.c 3 | * Author: Andrew Laing 4 | * Email: parisianconnections@gmail.com 5 | * Date: 18/04/2017 6 | * Description: Modify the compound interest program of Section 4.6 7 | * to repeat its steps for iterest rates of 5 percent, 8 | * 6 percent, 7 percent, 8 percent, 9 percent, and 10 percent. 9 | * Use a fdor loop to vary the interest rate. 10 | */ 11 | 12 | #include 13 | #include 14 | 15 | int main() 16 | { 17 | int year, rateInt; 18 | double amount, principal = 1000.0, rate = 0.05; 19 | 20 | for(rateInt=5; rateInt <=10; rateInt++) 21 | { 22 | rate = (float)rateInt / 100; 23 | 24 | printf( "\n--- Interest at %d percent ---\n", rateInt ); 25 | printf( "%4s%21s\n", "Year", "Amount on deposit" ); 26 | 27 | for(year = 1; year <= 10; year++ ) 28 | { 29 | amount = principal * pow( 1.0 + rate, year ); 30 | printf( "%4d%21.2f\n", year, amount ); 31 | } 32 | } 33 | 34 | return 0; 35 | } 36 | 37 | -------------------------------------------------------------------------------- /Chapter14/ex14_02.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Filename: ex14_02.c 3 | * Author: Andrew Laing 4 | * Email: parisianconnections@gmail.com 5 | * Date: 29/10/2017 6 | * Description: Variable length arg list 7 | */ 8 | 9 | #include 10 | #include 11 | 12 | int product(int i, ... ); 13 | 14 | int main() 15 | { 16 | int w = 37, x=22, y=1, z=10; 17 | 18 | printf("%s%d\n%s%d\n%s%d\n%s%d\n\n", 19 | "w = ", w,"x = ", x,"y = ", y,"z = ", z); 20 | printf("%s%d\n%s%d\n%s%d\n", 21 | "The product of w and x is ", product(2,w,x), 22 | "The product of w, x and y is ", product(3,w,x,y), 23 | "The product of w, x, y and z is ", product(4,w,x,y,z) ); 24 | 25 | return 0; 26 | } 27 | 28 | 29 | int product(int i, ... ) 30 | { 31 | int total; 32 | int j; 33 | 34 | va_list ap; 35 | 36 | /* Let total = the first number */ 37 | va_start(ap, i); 38 | total = va_arg(ap, int); 39 | 40 | for(j=2; j<=i; j++) 41 | total *= va_arg(ap, int); 42 | 43 | va_end(ap); 44 | 45 | return total; 46 | } 47 | -------------------------------------------------------------------------------- /Chapter4/ex04_09.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Filename: ex04_09.c 3 | * Author: Andrew Laing 4 | * Email: parisianconnections@gmail.com 5 | * Date: 19/04/2017 6 | * Description: Write a program that sums a sequence of Integers. 7 | * Assume the first integer read with scanf specifies 8 | * the number of values remaining to be entered. Your 9 | * program should read only one value each time scanf 10 | * is executed. A typical input sequence might be 11 | * 5 100 200 300 400 500 12 | * where the 5 indicates that the subsequent 5 values 13 | * are to be summed. 14 | */ 15 | 16 | #include 17 | 18 | 19 | int main() 20 | { 21 | int count, num; 22 | int sum = 0; 23 | 24 | printf("Enter the number of values to be entered: "); 25 | scanf("%d", &count); 26 | 27 | while(count-- > 0) { 28 | printf("Enter number: "); 29 | scanf("%d", &num); 30 | sum += num; 31 | } 32 | 33 | printf("\nThe sum of the entered numbers is %d\n", sum); 34 | 35 | return 0; 36 | } 37 | 38 | -------------------------------------------------------------------------------- /Chapter12/ex12_29/SimpleCompiler_version_2.9.3/src/StringMethods.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Filename: StringMethods.h 3 | * Author: Andrew Laing 4 | * Email: parisianconnections@gmail.com 5 | * Date: 31/08/2017. 6 | * Description: Various string and character related methods. 7 | */ 8 | 9 | 10 | #ifndef STRING_METHODS_H 11 | #define STRING_METHODS_H 12 | 13 | #include 14 | #include 15 | #include 16 | #include 17 | #include "IntStack.h" 18 | 19 | 20 | int getLine( char [], int ); 21 | 22 | int startsWith( const char *, char ); 23 | int endsWith( const char *, char ); 24 | int endsWithSuffix(const char *s, const char *); 25 | int isOperator( char c ); 26 | void removeFirstChar( char *, const char * ); 27 | void removeEndChar( char *s ); 28 | int strToInt( const char * ); 29 | int removeQuotes( char *, const char * ); 30 | 31 | int strlen2( const char* s ); 32 | char *strcpy2( char *, const char * ); 33 | char *strcat2( char *, const char * ); 34 | int strcmp2( const char *, const char * ); 35 | void strToLowercase( char [] ); 36 | char * strToLowercase1( char * ); 37 | 38 | 39 | #endif 40 | 41 | -------------------------------------------------------------------------------- /Chapter4/ex04_10.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Filename: ex04_10.c 3 | * Author: Andrew Laing 4 | * Email: parisianconnections@gmail.com 5 | * Date: 19/04/2017 6 | * Description: Write a program that calculates and prints the 7 | * average of several integers. Assume the last value 8 | * read with scanf is the sentinel 9999. A typical 9 | * input sequence might be 10 | * 10 8 11 7 9 9999 11 | * indicating that the average of all the values 12 | * preceding 9999 is to be calculated. 13 | */ 14 | 15 | #include 16 | 17 | 18 | int main() 19 | { 20 | int count = 0, num; 21 | int sum = 0; 22 | float average = 0; 23 | 24 | printf("Enter each number on a seperate line (enter 9999 to exit): "); 25 | scanf("%d", &num); 26 | 27 | while(num != 9999) { 28 | sum += num; 29 | count++; 30 | scanf("%d", &num); 31 | } 32 | 33 | if(count) 34 | average = (float) sum / count; 35 | 36 | printf("\nThe average of the entered numbers is %f\n", average); 37 | 38 | return 0; 39 | } 40 | 41 | -------------------------------------------------------------------------------- /Chapter9/ex09_12.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Filename: ex09_12.c 3 | * Author: Andrew Laing 4 | * Email: parisianconnections@gmail.com 5 | * Date: 17/07/2017. 6 | */ 7 | 8 | #include 9 | 10 | int getLine( char [], int ); 11 | 12 | int main() 13 | { 14 | char string[50]; 15 | int length; 16 | 17 | printf("--- using scanf ---\n"); 18 | printf("Enter the string: "); 19 | scanf("%s%n", string, &length); 20 | printf("length = %d\n", length); 21 | fflush(stdin); 22 | printf("%*s\n", length*2, string); 23 | 24 | printf("\n--- using getline ---\n"); 25 | printf("Enter the string: "); 26 | length = getLine(string, 50); 27 | printf("%*s\n", length*2, string); 28 | return 0; 29 | } 30 | 31 | 32 | /* Read a string into a character array and return the length of the array */ 33 | int getLine( char s[], int maxLength ) 34 | { 35 | int c, i=0; 36 | 37 | while( ( --maxLength > 0) && ( (c=getchar())!=EOF ) && ( c!='\n' ) ) 38 | s[i++]=c; 39 | 40 | s[i] = '\0'; 41 | 42 | /* Clear any overflow*/ 43 | fflush(stdin); 44 | 45 | return i; 46 | } 47 | -------------------------------------------------------------------------------- /Chapter12/ex12_26/ex12_26_e.simple: -------------------------------------------------------------------------------- 1 | 10 rem Input 10 integers and print the smallest 2 | 11 rem 3 | 14 rem set the value of the variables max (m) to 10, 4 | 15 rem smallest(s) to 0, and count (c) to 0 5 | 20 let m = 10 6 | 25 let s = 0 7 | 30 let c = 0 8 | 31 rem 9 | 32 rem INPUT LOOP - if counter == 10 go and print then quit 10 | 40 if c == m goto 120 11 | 50 input a 12 | 51 rem 13 | 52 rem if this is the first number input then it is 14 | 53 rem the smallest number added so far, so goto the 15 | 54 rem subroutine which sets s to this value 16 | 60 if c == 0 goto 100 17 | 61 rem 18 | 62 rem if the input is smaller than s, goto the 19 | 64 rem subroutine which sets s to this value 20 | 70 if a < s goto 100 21 | 71 rem 22 | 72 rem increment the counter and return to the beginning 23 | 73 rem of the input loop 24 | 80 let c = c + 1 25 | 90 goto 40 26 | 91 rem 27 | 92 rem set the value of smallest (s) and return to the 28 | 93 rem input loop, incrementing the counter 29 | 100 let s = a 30 | 110 goto 70 31 | 111 rem 32 | 112 rem print the smallest value and quit 33 | 120 print s 34 | 130 end 35 | 36 | 37 | 38 | -------------------------------------------------------------------------------- /Various/HexToInt.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Filename: ex07_25.c 3 | * Author: Andrew Laing 4 | * Email: parisianconnections@gmail.com 5 | * Date: 04/06/2017. 6 | * Description: Convert a hex string to an integer 7 | */ 8 | 9 | #include 10 | #include 11 | 12 | int getLine( char [], int ); 13 | 14 | int main() 15 | { 16 | char str[7]; 17 | int num1; 18 | 19 | printf("Enter hex string (MAX 4 CHARS) > "); 20 | getLine(str, 7); 21 | 22 | /* Convert Hexstring to an integer */ 23 | sscanf(str, "%x", &num1); 24 | 25 | /* Print as hex using %x */ 26 | printf("0x%x %d\n", num1, num1); 27 | printf("%08X %i\n", num1, num1); 28 | 29 | return 0; 30 | } 31 | 32 | 33 | /* Read a string into a character array and return the length of the array */ 34 | int getLine( char s[], int maxLength ) 35 | { 36 | int c, i=0; 37 | 38 | while( ( --maxLength > 0) && ( (c=getchar())!=EOF ) && ( c!='\n' ) ) 39 | s[i++]=c; 40 | 41 | if(c=='\n') 42 | s[i++]=c; 43 | s[i] = '\0'; 44 | 45 | /* Clear any overflow*/ 46 | fflush(stdin); 47 | 48 | return i; 49 | } 50 | 51 | 52 | -------------------------------------------------------------------------------- /Various/getLine.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Filename: ex07_25.c 3 | * Author: Andrew Laing 4 | * Email: parisianconnections@gmail.com 5 | * Date: 04/06/2017. 6 | * Description: Convert a hex string to an integer 7 | */ 8 | 9 | #include 10 | #include 11 | 12 | int getLine( char [], int ); 13 | 14 | int main() 15 | { 16 | char str[5]; 17 | int num1; 18 | 19 | printf("Enter hex string (MAX 4 CHARS) > "); 20 | getLine(str, 5); 21 | 22 | /* Convert Hexstring to an integer */ 23 | sscanf(str, "%x", &num1); 24 | 25 | /* Print as hex using %x */ 26 | printf("0x%x %d\n", num1, num1); 27 | printf("%08X %i\n", num1, num1); 28 | 29 | return 0; 30 | } 31 | 32 | 33 | /* Read a string into a character array and return the length of the array */ 34 | int getLine( char s[], int maxLength ) 35 | { 36 | int c, i=0; 37 | 38 | while( ( --maxLength > 0) && ( (c=getchar())!=EOF ) && ( c!='\n' ) ) 39 | s[i++]=c; 40 | 41 | if(c=='\n') 42 | s[i++]=c; 43 | s[i] = '\0'; 44 | 45 | /* Clear any overflow*/ 46 | fflush(stdin); 47 | 48 | return i; 49 | } 50 | 51 | 52 | -------------------------------------------------------------------------------- /Chapter11/ex11_14/wordlists/2LetterWordlist.txt: -------------------------------------------------------------------------------- 1 | aa 2 | ab 3 | ad 4 | ae 5 | ag 6 | ah 7 | ai 8 | al 9 | am 10 | an 11 | ar 12 | as 13 | at 14 | aw 15 | ax 16 | ay 17 | ba 18 | be 19 | bi 20 | bo 21 | by 22 | ch 23 | da 24 | de 25 | di 26 | do 27 | ea 28 | ed 29 | ee 30 | ef 31 | eh 32 | el 33 | em 34 | en 35 | er 36 | es 37 | et 38 | ex 39 | fa 40 | fe 41 | fy 42 | gi 43 | go 44 | gu 45 | ha 46 | he 47 | hi 48 | hm 49 | ho 50 | id 51 | if 52 | in 53 | io 54 | is 55 | it 56 | ja 57 | jo 58 | ka 59 | ki 60 | ko 61 | ky 62 | la 63 | li 64 | lo 65 | ma 66 | me 67 | mi 68 | mm 69 | mo 70 | mu 71 | my 72 | na 73 | ne 74 | no 75 | nu 76 | ny 77 | ob 78 | od 79 | oe 80 | of 81 | oh 82 | oi 83 | om 84 | on 85 | oo 86 | op 87 | or 88 | os 89 | ou 90 | ow 91 | ox 92 | oy 93 | pa 94 | pe 95 | pi 96 | po 97 | qi 98 | re 99 | sh 100 | si 101 | so 102 | st 103 | ta 104 | te 105 | ti 106 | to 107 | ug 108 | uh 109 | um 110 | un 111 | up 112 | ur 113 | us 114 | ut 115 | we 116 | wo 117 | xi 118 | xu 119 | ya 120 | ye 121 | yo 122 | yu 123 | za 124 | zo 125 | -------------------------------------------------------------------------------- /Chapter5/ex05_41.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Filename: ex05_41.c 3 | * Author: Andrew Laing 4 | * Email: parisianconnections@gmail.com 5 | * Date: 06/05/2017 6 | * Description: Recursive factorial function 7 | */ 8 | 9 | #include 10 | int DEBUG = 1; 11 | int start = 1; 12 | 13 | long factorial( long ); 14 | 15 | int main() 16 | { 17 | int i; 18 | 19 | for(i=1; i<=10; i++) 20 | { 21 | start=1; 22 | printf("%2d! = %1d\n\n", i, factorial( i )); 23 | } 24 | 25 | return 0; 26 | } 27 | 28 | long factorial( long number) 29 | { 30 | int rcParam; 31 | static int calls=0; 32 | 33 | if(start) 34 | { 35 | calls=0; 36 | printf("=== First call to function ===\n"); 37 | start=0; 38 | } 39 | 40 | 41 | calls++; 42 | 43 | if(DEBUG) printf("Call #%d: Number = %d\n", calls, number); 44 | 45 | if( number <= 1) 46 | return 1; 47 | else { 48 | rcParam = number * factorial(number - 1); 49 | 50 | if(DEBUG) printf("Call #%d: Recursive call parameter = %d\n", calls, rcParam); 51 | 52 | return rcParam; 53 | } 54 | } 55 | 56 | -------------------------------------------------------------------------------- /Chapter3/ex03_17.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Filename: ex03_17.c 3 | * Author: Andrew Laing 4 | * Email: parisianconnections@gmail.com 5 | * Date: 14/04/2017 6 | */ 7 | 8 | #include 9 | 10 | int main() 11 | { 12 | float totalGallons = 0, totalMiles = 0; 13 | float gallons, miles, average; 14 | 15 | printf( "Enter the gallons used (-1 to end): " ); 16 | scanf( "%f", &gallons ); 17 | 18 | while((int)gallons != -1) 19 | { 20 | printf( "Enter the miles driven: " ); 21 | scanf( "%f", &miles ); 22 | totalGallons += gallons; 23 | totalMiles += miles; 24 | 25 | if(gallons != 0) 26 | average = (float) miles / gallons; 27 | else 28 | average = 0; 29 | 30 | printf( "The miles / gallon for this tank was %f\n\n", average ); 31 | 32 | printf( "Enter the gallons used (-1 to end): " ); 33 | scanf( "%f", &gallons ); 34 | } 35 | 36 | if(totalGallons != 0) 37 | average = (float) totalMiles / totalGallons; 38 | else 39 | average = 0; 40 | 41 | printf( "\nThe overall miles per gallon was %f\n", average ); 42 | 43 | return 0; 44 | } 45 | 46 | -------------------------------------------------------------------------------- /Chapter8/ex08_33.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Filename: ex08_33.c 3 | * Author: Andrew Laing 4 | * Email: parisianconnections@gmail.com 5 | * Date: 04/07/2017. 6 | */ 7 | 8 | #include 9 | 10 | /* Using array subscripting */ 11 | int strlen1( const char* ); 12 | 13 | /* Using pointer arithmetic */ 14 | int strlen2( const char* ); 15 | 16 | int main() 17 | { 18 | char test1[] = "This is only a test string ok"; 19 | char test2[] = "This is a longer test string than the first one"; 20 | 21 | printf("---- strlen1 ----\n"); 22 | printf("test1 length = %d\n", strlen1( test1 )); 23 | printf("test2 length = %d\n", strlen1( test2 )); 24 | 25 | printf("\n---- strlen2 ----\n"); 26 | printf("test1 length = %d\n", strlen2( test1 )); 27 | printf("test2 length = %d\n", strlen2( test2 )); 28 | return 0; 29 | } 30 | 31 | /* Using array subscripting */ 32 | int strlen1( const char* s ) 33 | { 34 | int i=0; 35 | 36 | while(s[i]!='\0') 37 | i++; 38 | 39 | return i; 40 | } 41 | 42 | /* Using pointer arithmetic */ 43 | int strlen2( const char* s ) 44 | { 45 | int i=0; 46 | 47 | while(*s++!='\0') 48 | i++; 49 | 50 | return i; 51 | } 52 | -------------------------------------------------------------------------------- /Various/strlenVersions.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Filename: ex08_33.c 3 | * Author: Andrew Laing 4 | * Email: parisianconnections@gmail.com 5 | * Date: 04/07/2017. 6 | */ 7 | 8 | #include 9 | 10 | /* Using array subscripting */ 11 | int strlen1( const char* ); 12 | 13 | /* Using pointer arithmetic */ 14 | int strlen2( const char* ); 15 | 16 | int main() 17 | { 18 | char test1[] = "This is only a test string ok"; 19 | char test2[] = "This is a longer test string than the first one"; 20 | 21 | printf("---- strlen1 ----\n"); 22 | printf("test1 length = %d\n", strlen1( test1 )); 23 | printf("test2 length = %d\n", strlen1( test2 )); 24 | 25 | printf("\n---- strlen2 ----\n"); 26 | printf("test1 length = %d\n", strlen2( test1 )); 27 | printf("test2 length = %d\n", strlen2( test2 )); 28 | return 0; 29 | } 30 | 31 | /* Using array subscripting */ 32 | int strlen1( const char* s ) 33 | { 34 | int i=0; 35 | 36 | while(s[i]!='\0') 37 | i++; 38 | 39 | return i; 40 | } 41 | 42 | /* Using pointer arithmetic */ 43 | int strlen2( const char* s ) 44 | { 45 | int i=0; 46 | 47 | while(*s++!='\0') 48 | i++; 49 | 50 | return i; 51 | } 52 | -------------------------------------------------------------------------------- /Various/displayBits.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Filename: ex10_10.c 3 | * Author: Andrew Laing 4 | * Email: parisianconnections@gmail.com 5 | * Date: 21/07/2017. 6 | */ 7 | 8 | #include 9 | 10 | void displayBits( unsigned ); 11 | 12 | int main() 13 | { 14 | unsigned number1; 15 | 16 | number1 = 65535; 17 | printf("The result of right shifting the following\n"); 18 | displayBits( number1 ); 19 | printf("is\n"); 20 | displayBits( number1 >> 4 ); 21 | 22 | 23 | printf("\n\nsize of int (same as long int) = %d\n",sizeof(int)); 24 | printf("size of short = %d\n",sizeof(short)); 25 | printf("size of long = %d\n",sizeof(long)); 26 | printf("size of long long int = %d\n",sizeof(long long int)); 27 | return 0; 28 | } 29 | 30 | 31 | void displayBits( unsigned value ) 32 | { 33 | unsigned c, displayMask = 1<<31; 34 | 35 | printf( "%7u = ", value ); 36 | 37 | for( c=1; c<=32; c++ ) 38 | { 39 | putchar( value & displayMask ? '1' : '0' ); 40 | value <<= 1; 41 | 42 | if(c % 8 == 0) 43 | putchar( ' ' ); 44 | } 45 | 46 | putchar( '\n' ); 47 | } 48 | 49 | 50 | -------------------------------------------------------------------------------- /Chapter5/ex05_40.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Filename: ex05_40.c 3 | * Author: Andrew Laing 4 | * Email: parisianconnections@gmail.com 5 | * Date: 06/05/2017 6 | */ 7 | 8 | #include 9 | 10 | void towerOfHanoi( int ); 11 | 12 | /* Iterative binary towerOfHanoi function */ 13 | void towerOfHanoi(int numDisks) 14 | { 15 | int i; 16 | int fromPeg, toPeg; 17 | int numPegs = 3; 18 | 19 | /* Note the left shift produces the number of moves necessary 20 | * So 1 disk would require 2-1 moves 21 | * 3 disks would require 8-1 moves etc 22 | */ 23 | for (i=1; i < (1< %d\n", fromPeg+1, toPeg+1 ); /* Plus 1 otherwise 0-2*/ 28 | } 29 | } 30 | 31 | int main() 32 | { 33 | /* Would this be able to handle as large numbers as the recursive version 34 | * accurately 1<<17 == 131072 which is within the bounds of an integer but 35 | * these numbers would overflow an integer pretty soon afterwards. */ 36 | towerOfHanoi( 5 ); 37 | 38 | return 0; 39 | } 40 | 41 | 42 | -------------------------------------------------------------------------------- /Chapter3/ex03_32.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Filename: ex03_32.c 3 | * Author: Andrew Laing 4 | * Email: parisianconnections@gmail.com 5 | * Date: 15/04/2017 6 | */ 7 | 8 | #include 9 | 10 | int main() 11 | { 12 | int x, y; 13 | 14 | printf( "Enter values for x and y: " ); 15 | scanf( "%d%d", &x, &y ); 16 | 17 | // pattern one 18 | if( y == 8 ) { 19 | if( x == 5 ) 20 | printf("@@@@@\n"); 21 | } 22 | else 23 | printf("#####\n"); 24 | printf("$$$$$\n"); 25 | printf("&&&&&\n"); 26 | 27 | printf("--------------------------------------\n\n"); 28 | 29 | 30 | // pattern two 31 | if( y == 8 ) { 32 | if( x == 5 ) 33 | printf("@@@@@\n"); 34 | } 35 | else { 36 | printf("#####\n"); 37 | printf("$$$$$\n"); 38 | printf("&&&&&\n"); 39 | } 40 | 41 | printf("--------------------------------------\n\n"); 42 | 43 | 44 | // pattern three 45 | if( y == 8 ) { 46 | if( x == 5 ) 47 | printf("@@@@@\n"); 48 | } 49 | else { 50 | printf("#####\n"); 51 | printf("$$$$$\n"); 52 | } 53 | 54 | printf("&&&&&\n"); 55 | 56 | 57 | return 0; 58 | } 59 | 60 | -------------------------------------------------------------------------------- /Chapter8/ex08_09.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Filename: ex08_09.c 3 | * Author: Andrew Laing 4 | * Email: parisianconnections@gmail.com 5 | * Date: 20/06/2017. 6 | * Description: Compare strings 7 | */ 8 | 9 | #include 10 | #include /* for strcmp */ 11 | 12 | int getLine( char [], int ); 13 | 14 | int main() 15 | { 16 | char s1[20], s2[20]; 17 | size_t n; 18 | 19 | printf("Enter string s1: "); 20 | getLine(s1, 20); 21 | printf("Enter string s2: "); 22 | getLine(s2, 20); 23 | 24 | n = strcmp( s1, s2 ); 25 | 26 | if(n==0) { 27 | printf("The two strings are the same\n"); 28 | } 29 | else if(n==-1) { 30 | printf("%s is less than %s\n", s1, s2); 31 | } 32 | else 33 | printf("%s is greater than %s\n", s1, s2); 34 | 35 | return 0; 36 | } 37 | 38 | /* Read a string into a character array and return the length of the array */ 39 | int getLine( char s[], int maxLength ) 40 | { 41 | int c, i=0; 42 | 43 | while( ( --maxLength > 0) && ( (c=getchar())!=EOF ) && ( c!='\n' ) ) 44 | s[i++]=c; 45 | 46 | s[i] = '\0'; 47 | 48 | /* Clear any overflow*/ 49 | fflush(stdin); 50 | 51 | return i; 52 | } 53 | 54 | -------------------------------------------------------------------------------- /Chapter10/ex10_11.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Filename: ex10_11.c 3 | * Author: Andrew Laing 4 | * Email: parisianconnections@gmail.com 5 | * Date: 21/07/2017. 6 | */ 7 | 8 | #include 9 | 10 | void displayBits( unsigned ); 11 | 12 | int main() 13 | { 14 | unsigned number1; 15 | 16 | number1 = 1023; 17 | printf("The result of right shifting the following\n"); 18 | display2BytesInBits( number1 ); 19 | printf("is\n"); 20 | display2BytesInBits( number1 >> 4 ); 21 | 22 | 23 | printf("\n\nsize of int (same as long int) = %d\n",sizeof(int)); 24 | printf("size of short = %d\n",sizeof(short)); 25 | printf("size of long = %d\n",sizeof(long)); 26 | printf("size of long long int = %d\n",sizeof(long long int)); 27 | return 0; 28 | } 29 | 30 | 31 | void display2BytesInBits( unsigned value ) 32 | { 33 | unsigned c, displayMask = 1<<15; 34 | 35 | printf( "%7u = ", value ); 36 | 37 | for( c=1; c<=16; c++ ) 38 | { 39 | putchar( value & displayMask ? '1' : '0' ); 40 | value <<= 1; 41 | 42 | if(c % 8 == 0) 43 | putchar( ' ' ); 44 | } 45 | 46 | putchar( '\n' ); 47 | } 48 | 49 | 50 | -------------------------------------------------------------------------------- /Chapter8/ex08_16.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Filename: ex08_16.c 3 | * Author: Andrew Laing 4 | * Email: parisianconnections@gmail.com 5 | * Date: 24/06/2017. 6 | */ 7 | 8 | #include 9 | #include 10 | #include 11 | #include 12 | 13 | 14 | int getLine( char [], int ); 15 | 16 | 17 | int main() 18 | { 19 | char text[80]; 20 | char searchStr[80]; 21 | char *searchPtr; 22 | 23 | printf("Enter text: "); 24 | getLine(text, 80); 25 | 26 | printf("Enter search string: "); 27 | getLine(searchStr, 80); 28 | 29 | searchPtr = strstr( text, searchStr ); 30 | 31 | while(searchPtr != NULL ) 32 | { 33 | printf("%s\n", searchPtr); 34 | searchPtr = strstr( searchPtr+1, searchStr ); 35 | } 36 | 37 | 38 | return 0; 39 | } 40 | 41 | 42 | 43 | /* Read a string into a character array and return the length of the array */ 44 | int getLine( char s[], int maxLength ) 45 | { 46 | int c, i=0; 47 | 48 | while( ( --maxLength > 0) && ( (c=getchar())!=EOF ) && ( c!='\n' ) ) 49 | s[i++]=c; 50 | 51 | s[i] = '\0'; 52 | 53 | /* Clear any overflow*/ 54 | fflush(stdin); 55 | 56 | return i; 57 | } 58 | 59 | -------------------------------------------------------------------------------- /Chapter5/ex05_32.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Filename: ex05_32.c 3 | * Author: Andrew Laing 4 | * Email: parisianconnections@gmail.com 5 | * Date: 03/05/2017 6 | */ 7 | 8 | #include 9 | #include 10 | #include 11 | 12 | int getRandomNum( void ); 13 | void answerTheQuestion( int, int ); 14 | void askQuestions( int ); 15 | 16 | 17 | int main() 18 | { 19 | srand( (time(NULL)) ); 20 | askQuestions(2); 21 | 22 | return 0; 23 | } 24 | 25 | 26 | int getRandomNum() 27 | { 28 | int res = 1 + rand()%12; 29 | return res; 30 | } 31 | 32 | void answerTheQuestion(int a, int b) 33 | { 34 | int guess; 35 | int correct=0; 36 | int result = a*b; 37 | 38 | do { 39 | printf("What is %d x %d? ",a, b); 40 | scanf("%d", &guess); 41 | 42 | if(guess==result) { 43 | correct = 1; 44 | printf("Well done!\n"); 45 | } else { 46 | printf("No. Please try again.\n"); 47 | } 48 | 49 | } while(!correct); 50 | } 51 | 52 | void askQuestions(int n) 53 | { 54 | int i, a, b; 55 | for(i=0; i 10 | 11 | 12 | int stringToInt(char []); 13 | int getLine( char [], int ); 14 | 15 | int main() 16 | { 17 | char input[10]; 18 | int i, sum=0; 19 | 20 | for(i=1; i<=4; i++) { 21 | printf("Enter number #%d: ", i); 22 | getLine(input, 10); 23 | sum += stringToInt(input); 24 | } 25 | 26 | printf("The sum of the 4 values is %d\n", sum); 27 | 28 | return 0; 29 | } 30 | 31 | /* Read a string into a character array and return the length of the array */ 32 | int getLine( char s[], int maxLength ) 33 | { 34 | int c, i=0; 35 | 36 | while( ( --maxLength > 0) && ( (c=getchar())!=EOF ) && ( c!='\n' ) ) 37 | s[i++]=c; 38 | 39 | if(c=='\n') 40 | s[i++]=c; 41 | s[i] = '\0'; 42 | 43 | /* Clear any overflow*/ 44 | fflush(stdin); 45 | 46 | return i; 47 | } 48 | 49 | 50 | /* Convert a char array to an integer */ 51 | int stringToInt(char str[]) 52 | { 53 | int result; 54 | sscanf(str, "%d", &result); 55 | 56 | return result; 57 | } 58 | -------------------------------------------------------------------------------- /Chapter6/ex06_11.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Filename: ex06_11.c 3 | * Author: Andrew Laing 4 | * Email: parisianconnections@gmail.com 5 | * Date: 10/05/2017 6 | * Description: Bubble Sort Algorithm 7 | */ 8 | 9 | #include 10 | #define SIZE 10 11 | 12 | void bubbleSort( int[] ); 13 | 14 | 15 | int main() 16 | { 17 | int a[ SIZE ] = {2,6,4,8,10,12,89,68,45,37}; 18 | int i; 19 | 20 | printf( "Data items in original order:\n" ); 21 | 22 | for( i=0; i<=SIZE-1; i++) 23 | printf("%4d", a[i]); 24 | 25 | bubbleSort( a ); 26 | 27 | printf( "\nData items in ascending order:\n" ); 28 | 29 | for( i=0; i<=SIZE-1; i++) 30 | printf("%4d", a[i]); 31 | 32 | printf("\n"); 33 | 34 | return 0; 35 | } 36 | 37 | 38 | void bubbleSort( int a[] ) 39 | { 40 | int i, pass, hold; 41 | int switchMade; 42 | 43 | for(pass=1; pass<=SIZE-1; pass++) 44 | { 45 | switchMade = 0; 46 | for(i=0; i<=SIZE-1-pass; i++) 47 | { 48 | if(a[i] > a[i+1]) 49 | { 50 | hold = a[i]; 51 | a[i] = a[i+1]; 52 | a[i+1]=hold; 53 | switchMade = 1; 54 | // printf("\nSwap made!\n"); 55 | } 56 | } 57 | 58 | if(switchMade == 0) 59 | break; 60 | } 61 | } 62 | 63 | -------------------------------------------------------------------------------- /Chapter3/ex03_35.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Filename: ex03_35.c 3 | * Author: Andrew Laing 4 | * Email: parisianconnections@gmail.com 5 | * Date: 15/04/2017 6 | */ 7 | 8 | #include 9 | 10 | int isPalindrome( int ); 11 | 12 | 13 | int main() 14 | { 15 | int num; 16 | 17 | /* Get a number from the user */ 18 | printf( "Input a five-digit number: " ); 19 | scanf("%d", &num); 20 | 21 | if(isPalindrome(num)) 22 | printf("%d is a palindrome.", num); 23 | else 24 | printf("%d is NOT a palindrome.", num); 25 | return 0; 26 | } 27 | 28 | 29 | /* isPalindrome copies a number, produces a reversed version 30 | * of the number and checks if the reversed version is the 31 | * same as the original. */ 32 | int isPalindrome(int num) { 33 | int copy = num; 34 | int reverse = 0; 35 | 36 | reverse += copy/10000; 37 | copy = copy - ((copy / 10000) * 10000); 38 | 39 | reverse += ( (copy/1000) * 10 ); 40 | copy = copy - ((copy / 1000) * 1000); 41 | 42 | reverse += ( (copy/100) * 100 ); 43 | copy = copy - ((copy / 100) * 100); 44 | 45 | reverse += ( (copy/10) * 1000 ); 46 | copy = copy - ((copy / 10) * 10); 47 | 48 | reverse += ( (copy%10) * 10000 ); 49 | 50 | return num==reverse; 51 | } 52 | 53 | -------------------------------------------------------------------------------- /Chapter4/ex04_33.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Filename: ex04_33.c 3 | * Author: Andrew Laing 4 | * Email: parisianconnections@gmail.com 5 | * Date: 24/04/2017 6 | */ 7 | 8 | #include 9 | 10 | 11 | int main() 12 | { 13 | int i, current; 14 | 15 | for(i=1; i<=100; i++) 16 | { 17 | current=i; 18 | 19 | if(current==100) { 20 | printf("C"); 21 | current=0; 22 | } 23 | if(current>=50) { 24 | printf("L"); 25 | current-=50; 26 | } 27 | while(current>=10) { 28 | printf("X"); 29 | current-=10; 30 | } 31 | if(current>=5) { 32 | if(current%10==9) { 33 | printf("IX"); 34 | current-=9; 35 | } 36 | else { 37 | printf("V"); 38 | current-=5; 39 | } 40 | } 41 | while(current>0) 42 | { 43 | if(current%10==4) { 44 | printf("IV"); 45 | current-=4; 46 | } 47 | else { 48 | printf("I"); 49 | current-=1; 50 | } 51 | 52 | } 53 | printf("\n"); 54 | } 55 | 56 | return 0; 57 | } 58 | 59 | -------------------------------------------------------------------------------- /Chapter8/ex08_08.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Filename: ex08_08.c 3 | * Author: Andrew Laing 4 | * Email: parisianconnections@gmail.com 5 | * Date: 20/06/2017. 6 | * Description: Convert strings to doubles 7 | */ 8 | 9 | #include 10 | 11 | double stringToDouble(char []); 12 | int getLine( char [], int ); 13 | 14 | int main() 15 | { 16 | char input[10]; 17 | int i; 18 | double sum=0.0; 19 | 20 | for(i=1; i<=4; i++) { 21 | printf("Enter number #%d: ", i); 22 | getLine(input, 10); 23 | sum += stringToDouble(input); 24 | } 25 | 26 | printf("The sum of the 4 values is %lf\n", sum); 27 | 28 | return 0; 29 | } 30 | 31 | /* Read a string into a character array and return the length of the array */ 32 | int getLine( char s[], int maxLength ) 33 | { 34 | int c, i=0; 35 | 36 | while( ( --maxLength > 0) && ( (c=getchar())!=EOF ) && ( c!='\n' ) ) 37 | s[i++]=c; 38 | 39 | if(c=='\n') 40 | s[i++]=c; 41 | s[i] = '\0'; 42 | 43 | /* Clear any overflow*/ 44 | fflush(stdin); 45 | 46 | return i; 47 | } 48 | 49 | 50 | /* Convert a char array to an integer */ 51 | double stringToDouble(char str[]) 52 | { 53 | double result; 54 | sscanf(str, "%lf", &result); 55 | 56 | return result; 57 | } 58 | -------------------------------------------------------------------------------- /Chapter10/ex10_13.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Filename: ex10_13.c 3 | * Author: Andrew Laing 4 | * Email: parisianconnections@gmail.com 5 | * Date: 21/07/2017. 6 | */ 7 | 8 | #include 9 | 10 | unsigned packCharacters( char, char ); 11 | void displayBits( unsigned ); 12 | 13 | int main() 14 | { 15 | char a, b; 16 | unsigned result; 17 | 18 | printf("Enter character 1: "); 19 | scanf("%c", &a); 20 | fflush(stdin); 21 | 22 | printf("Enter character 2: "); 23 | scanf("%c", &b); 24 | fflush(stdin); 25 | 26 | printf("%c before = ", a); 27 | displayBits( a ); 28 | printf("%c before = ", b); 29 | displayBits( b ); 30 | result = packCharacters(a, b); 31 | printf("Result = "); 32 | displayBits( result ); 33 | return 0; 34 | } 35 | 36 | 37 | unsigned packCharacters( char a, char b ) 38 | { 39 | unsigned result = a; 40 | result <<=8; 41 | return result|=b; /* Inclusive OR */ 42 | } 43 | 44 | void displayBits( unsigned value ) 45 | { 46 | unsigned c, displayMask = 1<<31; 47 | 48 | for( c=1; c<=32; c++ ) 49 | { 50 | putchar( value & displayMask ? '1' : '0' ); 51 | value <<= 1; 52 | 53 | if(c % 8 == 0) 54 | putchar( ' ' ); 55 | } 56 | 57 | putchar( '\n' ); 58 | } 59 | 60 | 61 | -------------------------------------------------------------------------------- /Chapter11/ex11_16/ex11_16.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Filename: ex11_16.c 3 | * Author: Andrew Laing 4 | * Email: parisianconnections@gmail.com 5 | * Date: 30/07/2017. 6 | */ 7 | 8 | #include 9 | 10 | int main(int argc, char **argv) 11 | { 12 | FILE *ofPtr; 13 | 14 | if ( ( ofPtr = fopen( "datasize.dat", "w" ) ) == NULL ) 15 | { 16 | printf("Error - unable to open file \"%s\"\n", argv[1]); 17 | return -1; 18 | } 19 | 20 | 21 | fprintf(ofPtr, "%-25s%5s\n", "Data Type", "Size"); 22 | fprintf(ofPtr, "%-25s%5d\n", "char", sizeof(char)); 23 | fprintf(ofPtr, "%-25s%5d\n", "unsigned char", sizeof(unsigned char)); 24 | fprintf(ofPtr, "%-25s%5d\n", "short int", sizeof(short int)); 25 | fprintf(ofPtr, "%-25s%5d\n", "unsigned short int", sizeof(unsigned short int)); 26 | fprintf(ofPtr, "%-25s%5d\n", "int", sizeof(int)); 27 | fprintf(ofPtr, "%-25s%5d\n", "unsigned int", sizeof(unsigned int)); 28 | fprintf(ofPtr, "%-25s%5d\n", "long int", sizeof(long int)); 29 | fprintf(ofPtr, "%-25s%5d\n", "unsigned long int", sizeof(unsigned long int)); 30 | fprintf(ofPtr, "%-25s%5d\n", "float", sizeof(float)); 31 | fprintf(ofPtr, "%-25s%5d\n", "double", sizeof(double)); 32 | fprintf(ofPtr, "%-25s%5d\n", "long double", sizeof(long double)); 33 | 34 | return 0; 35 | } 36 | 37 | 38 | -------------------------------------------------------------------------------- /Various/fileCopy.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Filename: fileCopy.c 3 | * Author: Andrew Laing 4 | * Email: parisianconnections@gmail.com 5 | * Date: 24/07/2017. 6 | */ 7 | 8 | #include 9 | 10 | 11 | int main() 12 | { 13 | size_t res1; 14 | unsigned char buffer[8192]; /* used to copy contents */ 15 | 16 | FILE *ofPtr = fopen("oldMast.dat", "rb"); 17 | FILE *nfPtr = fopen("newMast.dat", "wb"); 18 | 19 | if( ofPtr == NULL) 20 | printf( "\"oldMast.dat\" could not be opened.\n" ); 21 | else if( nfPtr == NULL ) 22 | printf( "\"newMast.dat\" could not be opened.\n" ); 23 | else 24 | { 25 | while((res1 = fread(buffer, 1, sizeof buffer, ofPtr )) > 0) 26 | { 27 | size_t res2 = fwrite(buffer, 1, res1, nfPtr ); 28 | if(res2 < res1) 29 | { 30 | if(ferror(nfPtr)) { 31 | printf("ERROR - unable to write to file!\n"); 32 | return -1; 33 | } 34 | // handle error 35 | else { 36 | printf("ERROR - media full!\n"); 37 | return -1; 38 | } 39 | } 40 | } 41 | 42 | fclose(ofPtr); 43 | fclose(nfPtr); 44 | } 45 | 46 | return 0; 47 | } 48 | 49 | -------------------------------------------------------------------------------- /Various/getInteger.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Filename: getInteger.c 3 | * Author: Andrew Laing 4 | * Email: parisianconnections@gmail.com 5 | * Date: 14/06/2017. 6 | * Description: Input an Integer 7 | */ 8 | 9 | #include 10 | #include 11 | 12 | #define MAXLEN 7 13 | 14 | int getLine( char [], int ); 15 | int getInteger( void ); 16 | 17 | int main() 18 | { 19 | int num1; 20 | 21 | printf("Enter integer string (MAX 6 CHARS) > "); 22 | num1 = getInteger(); 23 | 24 | printf("num1 = %d\n", num1); 25 | 26 | return 0; 27 | } 28 | 29 | 30 | /* Read a string into a character array and return the length of the array */ 31 | int getLine( char s[], int maxLength ) 32 | { 33 | int c, i=0; 34 | 35 | while( ( --maxLength > 0) && ( (c=getchar())!=EOF ) && ( c!='\n' ) ) 36 | s[i++]=c; 37 | 38 | if(c=='\n') 39 | s[i++]=c; 40 | s[i] = '\0'; 41 | 42 | /* Clear any overflow*/ 43 | fflush(stdin); 44 | 45 | return i; 46 | } 47 | 48 | 49 | /* Returns a valid integer */ 50 | int getInteger() 51 | { 52 | char str[MAXLEN]; 53 | int result=0; 54 | 55 | /* Input a string */ 56 | getLine(str, MAXLEN); 57 | 58 | /* Convert to a float */ 59 | sscanf(str, "%d", &result); 60 | 61 | return result; 62 | } 63 | -------------------------------------------------------------------------------- /Chapter3/ex03_44.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Filename: ex03_44.c 3 | * Author: Andrew Laing 4 | * Email: parisianconnections@gmail.com 5 | * Date: 16/04/2017 6 | */ 7 | 8 | #include 9 | 10 | float getValidLength( int ); 11 | float isValidTriangle( float, float, float ); 12 | 13 | 14 | int main() 15 | { 16 | float a, b, c; 17 | 18 | a = getValidLength(1); 19 | b = getValidLength(2); 20 | c = getValidLength(3); 21 | 22 | /* display the results */ 23 | if( isValidTriangle(a, b, c) ) 24 | printf("The values inputted could represent the sides of a triangle.\n"); 25 | else 26 | printf("The values inputted could NOT represent the sides of a triangle.\n"); 27 | return 0; 28 | } 29 | 30 | 31 | float getValidLength(int side) 32 | { 33 | float sideLength = 0; 34 | while(sideLength<=0) { 35 | printf("Enter the length of side #%d: ", side); 36 | scanf("%f", &sideLength); 37 | 38 | if(sideLength <= 0) 39 | printf("Side length must be greater than zero.\n"); 40 | } 41 | 42 | return sideLength; 43 | } 44 | 45 | 46 | float isValidTriangle(float a, float b, float c) 47 | { 48 | if((a+b) 9 | 10 | int main(int argc, char **argv) 11 | { 12 | FILE *ofPtr; 13 | 14 | if ( ( ofPtr = fopen( "datasize.dat", "w" ) ) == NULL ) 15 | { 16 | printf("Error - unable to open file \"%s\"\n", argv[1]); 17 | return -1; 18 | } 19 | 20 | 21 | fprintf(ofPtr, "%-25s%5s\n", "Data Type", "Size"); 22 | fprintf(ofPtr, "%-25s%5d\n", "char", sizeof(char)); 23 | fprintf(ofPtr, "%-25s%5d\n", "unsigned char", sizeof(unsigned char)); 24 | fprintf(ofPtr, "%-25s%5d\n", "short int", sizeof(short int)); 25 | fprintf(ofPtr, "%-25s%5d\n", "unsigned short int", sizeof(unsigned short int)); 26 | fprintf(ofPtr, "%-25s%5d\n", "int", sizeof(int)); 27 | fprintf(ofPtr, "%-25s%5d\n", "unsigned int", sizeof(unsigned int)); 28 | fprintf(ofPtr, "%-25s%5d\n", "long int", sizeof(long int)); 29 | fprintf(ofPtr, "%-25s%5d\n", "unsigned long int", sizeof(unsigned long int)); 30 | fprintf(ofPtr, "%-25s%5d\n", "float", sizeof(float)); 31 | fprintf(ofPtr, "%-25s%5d\n", "double", sizeof(double)); 32 | fprintf(ofPtr, "%-25s%5d\n", "long double", sizeof(long double)); 33 | 34 | return 0; 35 | } 36 | 37 | 38 | -------------------------------------------------------------------------------- /Chapter5/ex05_38.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Filename: ex05_38.c 3 | * Author: Andrew Laing 4 | * Email: parisianconnections@gmail.com 5 | * Date: 04/05/2017 6 | */ 7 | 8 | #include 9 | 10 | 11 | int fibonacciNR( int ); 12 | double fibonacciDubs( double ); 13 | 14 | int main() 15 | { 16 | int i; 17 | 18 | for(i=1; i<49; i++) { 19 | printf("fibonacciNR(%d) = %d\n", i, fibonacciNR(i)); 20 | } 21 | 22 | for(i=1; i<1479; i++) { 23 | printf("fibonacciDubs(%d) = %.0f\n", i, fibonacciDubs(i)); 24 | } 25 | 26 | return 0; 27 | } 28 | 29 | 30 | /* Non-recursive fibonacci function */ 31 | int fibonacciNR( int num ) 32 | { 33 | int a=0, b=1; 34 | int fib=1; 35 | 36 | int i; 37 | 38 | if(num==1) 39 | return 0; 40 | 41 | for(i=2; i 9 | #include 10 | #include 11 | #include 12 | 13 | int getLine( char [], int ); 14 | void printLatinWord(char *); 15 | 16 | 17 | int main() 18 | { 19 | char str[80]; 20 | char *tokenPtr; 21 | 22 | printf("Enter sentence: "); 23 | getLine(str, 80); 24 | 25 | tokenPtr = strtok( str, " " ); 26 | 27 | 28 | while( tokenPtr != NULL ) { 29 | printLatinWord(tokenPtr); 30 | tokenPtr = strtok( NULL, " " ); 31 | } 32 | 33 | return 0; 34 | } 35 | 36 | 37 | /* Read a string into a character array and return the length of the array */ 38 | int getLine( char s[], int maxLength ) 39 | { 40 | int c, i=0; 41 | 42 | while( ( --maxLength > 0) && ( (c=getchar())!=EOF ) && ( c!='\n' ) ) 43 | s[i++]=c; 44 | 45 | s[i] = '\0'; 46 | 47 | /* Clear any overflow*/ 48 | fflush(stdin); 49 | 50 | return i; 51 | } 52 | 53 | 54 | void printLatinWord(char *word) 55 | { 56 | if(strlen(word)>1) 57 | printf("%s%cay ",&word[1], tolower(word[0])); 58 | else 59 | printf("%cay ", tolower(word[0])); 60 | 61 | } 62 | -------------------------------------------------------------------------------- /Chapter10/ex10_17.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Filename: ex10_17.c 3 | * Author: Andrew Laing 4 | * Email: parisianconnections@gmail.com 5 | * Date: 21/07/2017. 6 | */ 7 | 8 | #include 9 | 10 | unsigned packCharacters( char, char, char, char ); 11 | void unpackCharacters( unsigned ); 12 | void displayBits( unsigned ); 13 | void displayBitsReeversed( unsigned ); 14 | 15 | 16 | int main() 17 | { 18 | unsigned result = 1135; 19 | printf("Original bits:\n"); 20 | displayBits( result ); 21 | 22 | printf("Reversed bits:\n"); 23 | displayBitsReversed( result ); 24 | 25 | return 0; 26 | } 27 | 28 | 29 | 30 | void displayBits( unsigned value ) 31 | { 32 | unsigned c, displayMask = 1<<31; 33 | 34 | for( c=1; c<=32; c++ ) 35 | { 36 | putchar( value & displayMask ? '1' : '0' ); 37 | value <<= 1; 38 | 39 | if(c % 8 == 0) 40 | putchar( ' ' ); 41 | } 42 | 43 | putchar( '\n' ); 44 | } 45 | 46 | 47 | void displayBitsReversed( unsigned value ) 48 | { 49 | unsigned c, displayMask = 1; 50 | 51 | for( c=1; c<=32; c++ ) 52 | { 53 | putchar( value & displayMask ? '1' : '0' ); 54 | value >>= 1; 55 | 56 | if(c % 8 == 0) 57 | putchar( ' ' ); 58 | } 59 | 60 | putchar( '\n' ); 61 | } 62 | 63 | -------------------------------------------------------------------------------- /Chapter6/ex06_32.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Filename: ex06_32.c 3 | * Author: Andrew Laing 4 | * Email: parisianconnections@gmail.com 5 | * Date: 20/05/2017 6 | * Description: Selection Sort 7 | */ 8 | 9 | 10 | #include 11 | #define SIZE 10 12 | 13 | void selectionSort( int[], int ); 14 | 15 | 16 | int main() 17 | { 18 | int i; 19 | int toSort[SIZE] = { 122, 642, 4, 322, 113, 9, 575, 44, 176, 8 }; 20 | 21 | for(i=0; i 9 | #include 10 | #include 11 | #include 12 | 13 | void printTokensInReverse(char *tokenPtr); 14 | int getLine( char [], int ); 15 | 16 | 17 | int main() 18 | { 19 | char str[80]; 20 | char *tokenPtr; 21 | 22 | printf("Enter sentence: "); 23 | getLine(str, 80); 24 | 25 | tokenPtr = strtok( str, " " ); 26 | printTokensInReverse(tokenPtr); 27 | 28 | return 0; 29 | } 30 | 31 | /* Uses recursion to print tokens in reverse order */ 32 | void printTokensInReverse(char *tokenPtr) 33 | { 34 | if(tokenPtr==NULL) 35 | return; 36 | 37 | char *temp = tokenPtr; 38 | 39 | tokenPtr = strtok( NULL, " " ); 40 | printTokensInReverse(tokenPtr); 41 | 42 | printf("%s ", temp); 43 | } 44 | 45 | 46 | /* Read a string into a character array and return the length of the array */ 47 | int getLine( char s[], int maxLength ) 48 | { 49 | int c, i=0; 50 | 51 | while( ( --maxLength > 0) && ( (c=getchar())!=EOF ) && ( c!='\n' ) ) 52 | s[i++]=c; 53 | 54 | s[i] = '\0'; 55 | 56 | /* Clear any overflow*/ 57 | fflush(stdin); 58 | 59 | return i; 60 | } 61 | 62 | --------------------------------------------------------------------------------