summaryrefslogtreecommitdiff
path: root/Programming_in_C/Chapter_07.ipynb
diff options
context:
space:
mode:
Diffstat (limited to 'Programming_in_C/Chapter_07.ipynb')
-rw-r--r--Programming_in_C/Chapter_07.ipynb46
1 files changed, 0 insertions, 46 deletions
diff --git a/Programming_in_C/Chapter_07.ipynb b/Programming_in_C/Chapter_07.ipynb
index de714e04..7c0f6329 100644
--- a/Programming_in_C/Chapter_07.ipynb
+++ b/Programming_in_C/Chapter_07.ipynb
@@ -27,13 +27,9 @@
"cell_type": "code",
"collapsed": false,
"input": [
- "#7.1.py\n",
- "#Working with an Array(or list in python)\n",
"\n",
- "#Variable/List declaration\n",
"values=[0]*10\n",
"\n",
- "#Initialisation/Calculation\n",
"values[0]=197\n",
"values[2]=-100\n",
"values[5]=350\n",
@@ -42,7 +38,6 @@
"values[2]-=1\n",
"i=0\n",
"\n",
- "#Iteration/Result\n",
"for index in values:\n",
" print(\"values[{0}] = {1}\".format(i,index))\n",
" i=i+1"
@@ -81,20 +76,14 @@
"cell_type": "code",
"collapsed": false,
"input": [
- "#7.2.py\n",
- "#Demonstrating an Array of Counters\n",
"\n",
- "#variable/List Declaration\n",
"ratingCounters=[0]*11\n",
"\n",
- "#Loop-1\n",
"for i in range(1,11):\n",
" ratingCounters[i]=0\n",
"\n",
"print(\"Enter your Responses:\")\n",
"\n",
- "#Loop-2\n",
- "#Clculation\n",
"for i in range (1,21):\n",
" response=5 #score=5 for all iterations\n",
" #response=input()\n",
@@ -104,11 +93,9 @@
" else:\n",
" ratingCounters[response]+=1\n",
"\n",
- "#Result\n",
"print(\"\\n\\n Rating | Number of Responses\\n\")\n",
"print(\" ----- -------------------\")\n",
"\n",
- "#Loop-3\n",
"for i in range(1,11):\n",
" print(\" {0} {1}\".format(i,ratingCounters[i]))"
],
@@ -152,20 +139,15 @@
"cell_type": "code",
"collapsed": false,
"input": [
- "#7.3.py\n",
- "#Generating Fibonacci Numbers\n",
"\n",
- "#Variable/List Declaration\n",
"Fibonacci=[0]*15\n",
"\n",
"Fibonacci[0]=0\n",
"Fibonacci[1]=1\n",
"\n",
- "#Loop-1\n",
"for i in range(2,15):\n",
" Fibonacci[i]=Fibonacci[i-2]+Fibonacci[i-1] #Calculation\n",
"\n",
- "#Loop-2\n",
"for i in range(15):\n",
" print(\"{0}\".format(Fibonacci[i])) #Print Result"
],
@@ -208,10 +190,7 @@
"cell_type": "code",
"collapsed": false,
"input": [
- "#7.4.py\n",
- "#Revising the Program to Generate Prime Numbers,Version 2\n",
"\n",
- "#Variable Declaration\n",
"primeIndex=2\n",
"primes=[0]*50\n",
"\n",
@@ -219,7 +198,6 @@
"primes[1]=3\n",
"p=5\n",
"\n",
- "#Calculations\n",
"while(p<=50): #Outer Loop-1\n",
" isPrime=True\n",
" \n",
@@ -235,7 +213,6 @@
" primeIndex+=1\n",
" p=p+2\n",
"\n",
- "#Results\n",
"for i in range (primeIndex): #Outer Loop-2\n",
" print(\"{0}\".format(primes[i]))"
],
@@ -278,18 +255,13 @@
"cell_type": "code",
"collapsed": false,
"input": [
- "#7.5.py\n",
- "#Initializing Arrays\n",
"\n",
- "#Variable/List Declarations\n",
"array_values=[0]*10\n",
"array_values[0:4]=[0,1,4,9,16]\n",
"\n",
- "#Calculations\n",
"for i in range(5,10):\n",
" array_values[i]=i*i\n",
"\n",
- "#Results\n",
"for i in range(10):\n",
" print(\"array values[{0}] = {1}\".format(i,array_values[i]))"
],
@@ -327,16 +299,11 @@
"cell_type": "code",
"collapsed": false,
"input": [
- "#7.6.py\n",
- "#Introducing Character Arrays\n",
"\n",
- "#Import Library\n",
"import sys #For sys.stdout.write()\n",
"\n",
- "#Variable/List Declaration\n",
"word=['H','e','l','l','o','!']\n",
"\n",
- "#Iterator/Result\n",
"for i in word:\n",
" sys.stdout.write(\"{0} \".format(i))\n",
"print(\"\\n\")"
@@ -367,13 +334,9 @@
"cell_type": "code",
"collapsed": false,
"input": [
- "#7.7.py\n",
- "#Converting a Positive Integer to Another Base\n",
"\n",
- "#import Library\n",
"import sys #for sys.stdout.write()\n",
"\n",
- "#Variable Declarations\n",
"baseDigits=['0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F']\n",
"convertedNumber=[0]*10\n",
"index=0\n",
@@ -381,7 +344,6 @@
"numberToConvert=234 #numberToConvert=input(\"Number to be converted?: \")\n",
"base=2 #base=input(\"Base?: \")\n",
"\n",
- "#Calculations\n",
"while True:\n",
" convertedNumber[index]=numberToConvert%base\n",
" index+=1\n",
@@ -389,7 +351,6 @@
" if(numberToConvert==0):\n",
" break\n",
"\n",
- "#Result\n",
"print(\"Converted Number= \")\n",
"index-=1\n",
"while(index>=0):\n",
@@ -426,29 +387,22 @@
"cell_type": "code",
"collapsed": false,
"input": [
- "#7.8.py\n",
- "#Generating Fibonacci Numbers Using Variable-Length Arrays\n",
"\n",
- "#import Libraries\n",
"import sys #For sys.exit() \n",
" \n",
- "#User Input \n",
"numFibs=18 #numFibs=input(\"How many Fibonacci numbers do you want (between 1 and 75)? \")\n",
"\n",
"if(numFibs<1 or numFibs>75):\n",
" print(\"bad number,sorry!\")\n",
" sys.exit()\n",
"\n",
- "#Variable Declarations \n",
"Fibonacci=[0]*numFibs\n",
"Fibonacci[0]=0\n",
"Fibonacci[1]=1\n",
"\n",
- "#Calculation\n",
"for i in range(2,numFibs):\n",
" Fibonacci[i]=Fibonacci[i-2]+Fibonacci[i-1] \n",
" \n",
- "#Result\n",
"for i in range(numFibs):\n",
" print(\"{0}\".format(Fibonacci[i]))"
],