summaryrefslogtreecommitdiff
path: root/Programming_in_C/Chapter_08.ipynb
diff options
context:
space:
mode:
Diffstat (limited to 'Programming_in_C/Chapter_08.ipynb')
-rw-r--r--Programming_in_C/Chapter_08.ipynb80
1 files changed, 0 insertions, 80 deletions
diff --git a/Programming_in_C/Chapter_08.ipynb b/Programming_in_C/Chapter_08.ipynb
index a4113d97..f8d5211b 100644
--- a/Programming_in_C/Chapter_08.ipynb
+++ b/Programming_in_C/Chapter_08.ipynb
@@ -27,8 +27,6 @@
"cell_type": "code",
"collapsed": false,
"input": [
- "#8.1.py\n",
- "#Writing a Function in python\n",
"\n",
"\n",
"def printMessage(): #Function to Print Message\n",
@@ -68,8 +66,6 @@
"cell_type": "code",
"collapsed": false,
"input": [
- "#8.2.py\n",
- "#Calling Functions\n",
"\n",
"\n",
"def printMessage(): #Function to Print Message\n",
@@ -111,8 +107,6 @@
"cell_type": "code",
"collapsed": false,
"input": [
- "#8.3.py\n",
- "#More on Calling Functions\n",
"\n",
"\n",
"def printMessage(): #Function to Print Message\n",
@@ -161,8 +155,6 @@
"cell_type": "code",
"collapsed": false,
"input": [
- "#8.4.py\n",
- "#Calculating the nth Triangular Number\n",
"\n",
"\n",
"def calculateTriangularNumber(n): #Function to calculate triangular number\n",
@@ -210,10 +202,7 @@
"cell_type": "code",
"collapsed": false,
"input": [
- "#8.5.py\n",
- "#Revising the Program to Find the Greatest Common Divisor\n",
"\n",
- "#Import Library \n",
"import sys \n",
"\n",
"def gcd(u,v): #Function to calculate gcd\n",
@@ -261,10 +250,7 @@
"cell_type": "code",
"collapsed": false,
"input": [
- "#8.6.py\n",
- "#Finding the Greatest Common Divisor and Returning the Results\n",
"\n",
- "#Import Library \n",
"import sys \n",
"\n",
"def gcd(u,v): #Function to calculate gcd\n",
@@ -313,12 +299,8 @@
"cell_type": "code",
"collapsed": false,
"input": [
- "#8.7.py\n",
- "#Calculating the Absolute Value\n",
"\n",
- "#yourstory.in\n",
"\n",
- "#Calculations\n",
"def absoluteValue(x): #Function to calculate & return absolute values\n",
" if(x<0):\n",
" x=-x\n",
@@ -330,7 +312,6 @@
" f2=20.0\n",
" f3=-5.0\n",
" il=-716\n",
- "#Result \n",
" result=absoluteValue(f1)\n",
" print(\"result= {0:.2f}\".format(result))\n",
" print(\"f1={0:.2f}\".format(f1))\n",
@@ -347,7 +328,6 @@
" print(\"{0:.2f}\".format(absoluteValue((-6.0)/4)))\n",
"\n",
"\n",
- "#End of Main()\n",
" \n",
"if __name__=='__main__': #Setting Top level conditional script\n",
" main()"
@@ -382,17 +362,13 @@
"cell_type": "code",
"collapsed": false,
"input": [
- "#8.8.py\n",
- "#Calculating the Square Root of a Number using Newton-Raphson Method \n",
"\n",
"\n",
- "#Function to return absolute value\n",
"def absoluteValue(x): \n",
" if(x<0):\n",
" x=-x\n",
" return x\n",
"\n",
- "#function to calculate square root\n",
"def squareRoot(x):\n",
" epsilon=0.0001\n",
" guess=1.0\n",
@@ -400,13 +376,11 @@
" guess=(x/guess+guess)/2.0\n",
" return guess\n",
"\n",
- "#Main()\n",
"def main():\n",
" print(\"squareRoot (2.0) = {0}\".format(squareRoot (2.0)));\n",
" print(\"squareRoot (144.0) = {0}\".format(squareRoot (144.0)));\n",
" print(\"squareRoot (17.5) = {0}\".format(squareRoot (17.5)));\n",
"\n",
- "#Setting top level conditional script\n",
"if __name__=='__main__':\n",
" main()"
],
@@ -437,10 +411,7 @@
"cell_type": "code",
"collapsed": false,
"input": [
- "#8.9.py\n",
- "#Finding the Minimum Value in an Array\n",
"\n",
- "#Function to return minimum value\n",
"def minimum(values):\n",
" minValue=values[0] #Variable Declaration\n",
" for i in range (1,10): #Calculation\n",
@@ -449,7 +420,6 @@
"\n",
" return minValue\n",
" \n",
- "#Main()\n",
"def main():\n",
" scores=[] #Variable Declaration\n",
" print(\"Enter 10 scores:\")\n",
@@ -460,7 +430,6 @@
" minScore=minimum(scores) #Function call & assignment\n",
" print(\"\\nMinimum score is {0}\\n\".format(minScore)) #Result\n",
"\n",
- "#Setting top level conditional script\n",
"if __name__=='__main__':\n",
" main()"
],
@@ -492,10 +461,7 @@
"cell_type": "code",
"collapsed": false,
"input": [
- "#8.10.py\n",
- "#Revising the Function to Find the Minimum Value in an Array\n",
"\n",
- "#Function to return minimum value\n",
"def minimum(values,numberOfElements): \n",
" minValue=values[0] #Variable Declaration\n",
" for i in range(1,numberOfElements): #Iteration/Calculations\n",
@@ -504,7 +470,6 @@
" \n",
" return minValue\n",
"\n",
- "#Main()\n",
"def main():\n",
" #List/Variable Delcaration\n",
" array1 = [ 157, -28, -37, 26, 10 ] \n",
@@ -514,7 +479,6 @@
" print(\"array1 minimum: {0}\".format(minimum (array1, 5)));\n",
" print(\"array2 minimum: {0}\".format(minimum (array2, 7)));\n",
"\n",
- "#Setting top level conditional script \n",
"if __name__=='__main__':\n",
" main()"
],
@@ -544,18 +508,13 @@
"cell_type": "code",
"collapsed": false,
"input": [
- "#8.11.py\n",
- "#Changing Array Elements in Functions\n",
"\n",
- "#Import System Library\n",
"import sys\n",
"\n",
- "#Function to multiply elements by 2\n",
"def multiplyBy2(array,n):\n",
" for i in range (0,n): #Iteration/Calculation\n",
" array[i]*=2\n",
"\n",
- "#Main()\n",
"def main():\n",
"\n",
" #List/Variable Declaration\n",
@@ -566,7 +525,6 @@
" sys.stdout.write(\"{0:.2f} \".format(floatVals[i])) #Result\n",
" print(\"\\n\")\n",
"\n",
- "#Setting top level conditional script\n",
"if __name__=='__main__':\n",
" main()"
],
@@ -596,13 +554,9 @@
"cell_type": "code",
"collapsed": false,
"input": [
- "#8.12.py\n",
- "#\n",
"\n",
- "#Import system Library \n",
"import sys\n",
"\n",
- "#Function to sort the array\n",
"def sort(a,n):\n",
"\n",
" for i in range (0,n-1): #Calculations\n",
@@ -612,7 +566,6 @@
" a[i]=a[j]\n",
" a[j]=temp\n",
"\n",
- "#Main()\n",
"def main():\n",
"\n",
" #List/Variable Declaration \n",
@@ -631,7 +584,6 @@
" print(\"\\n\")\n",
"\n",
"\n",
- "#Setting top level conditional script \n",
"if __name__=='__main__':\n",
" main()"
],
@@ -668,13 +620,9 @@
"cell_type": "code",
"collapsed": false,
"input": [
- "#8.13.py\n",
- "#Using Multidimensional Arrays and Functions\n",
"\n",
- "#Import system Library\n",
"import sys\n",
"\n",
- "#Main()\n",
"def main():\n",
"\n",
" #List/Variable Declaration\n",
@@ -696,14 +644,12 @@
" displayMatrix(sampleMatrix) #Function call-5\n",
"\n",
"\n",
- "#Function to multiply matrix by a scalar quantity \n",
"def scalarMultiply(matrix,scalar):\n",
" for row in range(0,3): #Calculation\n",
" for column in range(0,5):\n",
" matrix[row][column]*=scalar \n",
"\n",
"\n",
- "#Function to display the matrix\n",
"def displayMatrix(matrix):\n",
" for row in range(0,3): #Result\n",
" for column in range(0,5):\n",
@@ -711,7 +657,6 @@
" sys.stdout.write(\"\\n\")\n",
"\n",
"\n",
- "#Setting top level conditional script\n",
"if __name__=='__main__':\n",
" main()"
],
@@ -756,13 +701,9 @@
"cell_type": "code",
"collapsed": false,
"input": [
- "#8.13A.py\n",
- "#Multidimensional Variable-Length Arrays\n",
"\n",
- "#Import system Library\n",
"import sys\n",
"\n",
- "#Main()\n",
"def main():\n",
"\n",
" #List/Variable Declaration\n",
@@ -784,14 +725,12 @@
" displayMatrix(3,5,sampleMatrix) #Function call-5\n",
"\n",
"\n",
- "#Function to multiply matrix by a scalar quantity \n",
"def scalarMultiply(nRows,nCols,matrix,scalar):\n",
" for row in range(0,nRows): #Calculation\n",
" for column in range(0,nCols):\n",
" matrix[row][column]*=scalar \n",
"\n",
"\n",
- "#Function to display the matrix\n",
"def displayMatrix(nRows,nCols,matrix):\n",
" for row in range(0,nRows): #Result\n",
" for column in range(0,nCols):\n",
@@ -799,7 +738,6 @@
" sys.stdout.write(\"\\n\")\n",
"\n",
"\n",
- "#Setting top level conditional script\n",
"if __name__=='__main__':\n",
" main()"
],
@@ -844,13 +782,9 @@
"cell_type": "code",
"collapsed": false,
"input": [
- "#8.14.py\n",
- "#Converting a Positive Integer to Another Base\n",
"\n",
- "#Import system Libraries\n",
"import sys\n",
"\n",
- "#Function to get Number & Base\n",
"def getNumberAndBase():\n",
" \n",
" #Global Reference\n",
@@ -866,7 +800,6 @@
" print(\"Bad base - must be between 2 and 16\\n\");\n",
" base = 10;\n",
"\n",
- "#Conversion Function\n",
"def convertNumber():\n",
" \n",
" #Global Reference\n",
@@ -882,7 +815,6 @@
" numberToConvert/=base\n",
"\n",
"\n",
- "#Function to display\n",
"def displayConvertedNumber():\n",
"\n",
" #Global reference \n",
@@ -904,14 +836,12 @@
"\n",
"\n",
"\n",
- "#Main()\n",
"def main():\n",
" getNumberAndBase() #Function call-1\n",
" convertNumber() #Function call-2\n",
" displayConvertedNumber() #Function call-3\n",
"\n",
"\n",
- "#Setting top level conditional script\n",
"if __name__=='__main__':\n",
" main()"
],
@@ -940,11 +870,8 @@
"cell_type": "code",
"collapsed": false,
"input": [
- "#8.15.py\n",
- "#Illustrating Static and Automatic Variables\n",
"\n",
"\n",
- "#Function to display variables\n",
"def auto_static():\n",
" global staticVar #Global reference\n",
" autoVar=1 #variable Declaration\n",
@@ -954,14 +881,12 @@
" autoVar+=1\n",
" staticVar+=1\n",
"\n",
- "#Main()\n",
"def main():\n",
" global staticVar\n",
" staticVar=1 #Variable Declaration\n",
" for i in range(0,5):\n",
" auto_static()\n",
"\n",
- "#Setting top level conditional script\n",
"if __name__=='__main__':\n",
" main()"
],
@@ -999,10 +924,7 @@
"cell_type": "code",
"collapsed": false,
"input": [
- "#8.16.py\n",
- "#Calculating Factorials Recursively\n",
"\n",
- "#Function to calculate factorial\n",
"def factorial(n):\n",
" if( n == 0 ): #Calculation\n",
" result = 1\n",
@@ -1010,13 +932,11 @@
" result = n * factorial (n - 1)\n",
" return result;\n",
"\n",
- "#Main()\n",
"def main():\n",
" for j in range (0,11):\n",
" print(\"{0:3}! = {1}\\n\".format(j,factorial (j)))\n",
"\n",
"\n",
- "#Setting top level conditional script\n",
"if __name__=='__main__':\n",
" main()"
],