summaryrefslogtreecommitdiff
path: root/C++_from_the_Ground/Chapter_2(1).ipynb
diff options
context:
space:
mode:
Diffstat (limited to 'C++_from_the_Ground/Chapter_2(1).ipynb')
-rw-r--r--C++_from_the_Ground/Chapter_2(1).ipynb243
1 files changed, 204 insertions, 39 deletions
diff --git a/C++_from_the_Ground/Chapter_2(1).ipynb b/C++_from_the_Ground/Chapter_2(1).ipynb
index ca5a3177..603f2bab 100644
--- a/C++_from_the_Ground/Chapter_2(1).ipynb
+++ b/C++_from_the_Ground/Chapter_2(1).ipynb
@@ -1,6 +1,7 @@
{
"metadata": {
- "name": "Chapter 2"
+ "name": "",
+ "signature": "sha256:2822ffa73df44943f6be3bedb4cf89d60d9228e7b719790e40dca7bc4236f0b9"
},
"nbformat": 3,
"nbformat_minor": 0,
@@ -10,24 +11,34 @@
{
"cell_type": "markdown",
"metadata": {},
- "source": "<h1>Chapter 2: An Overview of C++<h1>"
+ "source": [
+ "<h1>Chapter 2: An Overview of C++<h1>"
+ ]
},
{
"cell_type": "markdown",
"metadata": {},
- "source": "<h3>Example 2.1, Page Number: 12<h3>"
+ "source": [
+ "<h3>Example 2.1, Page Number: 12<h3>"
+ ]
},
{
"cell_type": "code",
"collapsed": false,
- "input": "'''Program 1:A first Python(C++) program'''\n\n#Result\nprint \"This is my first C++ program.\"",
+ "input": [
+ "\n",
+ "#Result\n",
+ "print \"This is my first C++ program.\""
+ ],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
- "text": "This is my first C++ program.\n"
+ "text": [
+ "This is my first C++ program.\n"
+ ]
}
],
"prompt_number": 1
@@ -35,19 +46,34 @@
{
"cell_type": "markdown",
"metadata": {},
- "source": "<h3>Example 2.2, Page Number: 17<h3>"
+ "source": [
+ "<h3>Example 2.2, Page Number: 17<h3>"
+ ]
},
{
"cell_type": "code",
"collapsed": false,
- "input": "'''Program 2: Using a variable'''\n\n#Variable Declaration\nx=None \n\nx=1023 #this assigns 1023 to x\n\n#Result\nprint \"This program prints the value of x: \",x #prints x,i.e, 1023\n",
+ "input": [
+ "'\n",
+ "\n",
+ "\n",
+ "#Variable Declaration\n",
+ "x=None \n",
+ "\n",
+ "x=1023 #this assigns 1023 to x\n",
+ "\n",
+ "#Result\n",
+ "print \"This program prints the value of x: \",x #prints x,i.e, 1023\n"
+ ],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
- "text": "This program prints the value of x: 1023\n"
+ "text": [
+ "This program prints the value of x: 1023\n"
+ ]
}
],
"prompt_number": 2
@@ -55,19 +81,34 @@
{
"cell_type": "markdown",
"metadata": {},
- "source": "<h3>Example 2.3, Page Number: 18<h3>"
+ "source": [
+ "<h3>Example 2.3, Page Number: 18<h3>"
+ ]
},
{
"cell_type": "code",
"collapsed": false,
- "input": "'''This program converts gallons to liters'''\n\n#Variable declaration\ngallons=10 #User input\nliters=None\n\nliters=gallons*4 #convert to liters\n\n#Result\nprint \"Liters: \",liters",
+ "input": [
+ "\n",
+ "\n",
+ "#Variable declaration\n",
+ "gallons=10 #User input\n",
+ "liters=None\n",
+ "\n",
+ "liters=gallons*4 #convert to liters\n",
+ "\n",
+ "#Result\n",
+ "print \"Liters: \",liters"
+ ],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
- "text": "Liters: 40\n"
+ "text": [
+ "Liters: 40\n"
+ ]
}
],
"prompt_number": 3
@@ -75,19 +116,35 @@
{
"cell_type": "markdown",
"metadata": {},
- "source": "<h3>Example 2.4, Page Number: 20<h3>"
+ "source": [
+ "<h3>Example 2.4, Page Number: 20<h3>"
+ ]
},
{
"cell_type": "code",
"collapsed": false,
- "input": "'''This program converts gallons to liters using floating point numbers'''\n\n#Variable declaration\ngallons=10.20 #User input\nliters=None\n\nliters=gallons*3.7854 #convert to liters\n\n#Result\nprint \"Liters: \",liters",
+ "input": [
+ "\n",
+ "\n",
+ "\n",
+ "#Variable declaration\n",
+ "gallons=10.20 #User input\n",
+ "liters=None\n",
+ "\n",
+ "liters=gallons*3.7854 #convert to liters\n",
+ "\n",
+ "#Result\n",
+ "print \"Liters: \",liters"
+ ],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
- "text": "Liters: 38.61108\n"
+ "text": [
+ "Liters: 38.61108\n"
+ ]
}
],
"prompt_number": 4
@@ -95,19 +152,36 @@
{
"cell_type": "markdown",
"metadata": {},
- "source": "<h3>Example 2.5, Page Number: 21<h3>"
+ "source": [
+ "<h3>Example 2.5, Page Number: 21<h3>"
+ ]
},
{
"cell_type": "code",
"collapsed": false,
- "input": "'''This program uses a function'''\n\n#myfunc's definition\ndef myfunc():\n print \"Inside myfunc() \"\n \nprint \"In main()\"\nmyfunc() #call myfunc()\nprint \"Back in main()\"",
+ "input": [
+ "\n",
+ "\n",
+ "\n",
+ "#myfunc's definition\n",
+ "def myfunc():\n",
+ " print \"Inside myfunc() \"\n",
+ " \n",
+ "print \"In main()\"\n",
+ "myfunc() #call myfunc()\n",
+ "print \"Back in main()\""
+ ],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
- "text": "In main()\nInside myfunc() \nBack in main()\n"
+ "text": [
+ "In main()\n",
+ "Inside myfunc() \n",
+ "Back in main()\n"
+ ]
}
],
"prompt_number": 5
@@ -115,19 +189,29 @@
{
"cell_type": "markdown",
"metadata": {},
- "source": "<h3>Example 2.6, Page Number: 22<h3>"
+ "source": [
+ "<h3>Example 2.6, Page Number: 22<h3>"
+ ]
},
{
"cell_type": "code",
"collapsed": false,
- "input": "'''Using the abs() function'''\n\n#Result\nprint abs(-10)",
+ "input": [
+ "\n",
+ "\n",
+ "\n",
+ "#Result\n",
+ "print abs(-10)"
+ ],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
- "text": "10\n"
+ "text": [
+ "10\n"
+ ]
}
],
"prompt_number": 6
@@ -135,19 +219,34 @@
{
"cell_type": "markdown",
"metadata": {},
- "source": "<h3>Example 2.7, Page Number: 23<h3>"
+ "source": [
+ "<h3>Example 2.7, Page Number: 23<h3>"
+ ]
},
{
"cell_type": "code",
"collapsed": false,
- "input": "'''A simple program that demonstrates mul()'''\n\n#mul's definition\ndef mul(x,y):\n print x*y,\n\n#calling mul\nmul(10,20)\nmul(5,6)\nmul(8,9)",
+ "input": [
+ "\n",
+ "\n",
+ "#mul's definition\n",
+ "def mul(x,y):\n",
+ " print x*y,\n",
+ "\n",
+ "#calling mul\n",
+ "mul(10,20)\n",
+ "mul(5,6)\n",
+ "mul(8,9)"
+ ],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
- "text": "200 30 72\n"
+ "text": [
+ "200 30 72\n"
+ ]
}
],
"prompt_number": 1
@@ -155,19 +254,35 @@
{
"cell_type": "markdown",
"metadata": {},
- "source": "<h3>Example 2.8, Page Number: 24<h3>"
+ "source": [
+ "<h3>Example 2.8, Page Number: 24<h3>"
+ ]
},
{
"cell_type": "code",
"collapsed": false,
- "input": "'''Returning a value'''\n\n#mul()'s definition; this function returns a value\ndef mul(x,y):\n return x*y #return product of x and y\n\n#Variable declaration\nanswer=mul(10,11) #assign return values\n\n#Result\nprint \"The answer is: \",answer\n",
+ "input": [
+ "\n",
+ "\n",
+ "#mul()'s definition; this function returns a value\n",
+ "def mul(x,y):\n",
+ " return x*y #return product of x and y\n",
+ "\n",
+ "#Variable declaration\n",
+ "answer=mul(10,11) #assign return values\n",
+ "\n",
+ "#Result\n",
+ "print \"The answer is: \",answer\n"
+ ],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
- "text": "The answer is: 110\n"
+ "text": [
+ "The answer is: 110\n"
+ ]
}
],
"prompt_number": 8
@@ -175,19 +290,31 @@
{
"cell_type": "markdown",
"metadata": {},
- "source": "<h3>Example 2.9, Page Number: 26<h3>"
+ "source": [
+ "<h3>Example 2.9, Page Number: 26<h3>"
+ ]
},
{
"cell_type": "code",
"collapsed": false,
- "input": "'''This demonstrates how to got to the next line'''\n\nprint \"one\"\nprint \"two\" #prints in different line\nprint \"three\",\"four\" #prints all in same line",
+ "input": [
+ "\n",
+ "\n",
+ "print \"one\"\n",
+ "print \"two\" #prints in different line\n",
+ "print \"three\",\"four\" #prints all in same line"
+ ],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
- "text": "one\ntwo\nthree four\n"
+ "text": [
+ "one\n",
+ "two\n",
+ "three four\n"
+ ]
}
],
"prompt_number": 9
@@ -195,19 +322,33 @@
{
"cell_type": "markdown",
"metadata": {},
- "source": "<h3>Example 2.10, Page Number: 27<h3>"
+ "source": [
+ "<h3>Example 2.10, Page Number: 27<h3>"
+ ]
},
{
"cell_type": "code",
"collapsed": false,
- "input": "'''This program illustrates the if statement'''\n\n#Variable declaration\na=10 #user input for two numbers\nb=20\n\n#Result\nif a<b:\n print \"First number is less than second. \"",
+ "input": [
+ "'''This program illustrates the if statement'''\n",
+ "\n",
+ "#Variable declaration\n",
+ "a=10 #user input for two numbers\n",
+ "b=20\n",
+ "\n",
+ "#Result\n",
+ "if a<b:\n",
+ " print \"First number is less than second. \""
+ ],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
- "text": "First number is less than second. \n"
+ "text": [
+ "First number is less than second. \n"
+ ]
}
],
"prompt_number": 10
@@ -215,19 +356,28 @@
{
"cell_type": "markdown",
"metadata": {},
- "source": "<h3>Example 2.11, Page Number: 28<h3>"
+ "source": [
+ "<h3>Example 2.11, Page Number: 28<h3>"
+ ]
},
{
"cell_type": "code",
"collapsed": false,
- "input": "'''A program that illustrates the for loop'''\n\nfor count in range(1,100+1):\n print count,",
+ "input": [
+ "\n",
+ "\n",
+ "for count in range(1,100+1):\n",
+ " print count,"
+ ],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
- "text": "1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100\n"
+ "text": [
+ "1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100\n"
+ ]
}
],
"prompt_number": 3
@@ -235,19 +385,34 @@
{
"cell_type": "markdown",
"metadata": {},
- "source": "<h3>Example 2.12, Page Number: 30<h3>"
+ "source": [
+ "<h3>Example 2.12, Page Number: 30<h3>"
+ ]
},
{
"cell_type": "code",
"collapsed": false,
- "input": "'''This program demonstrates a block of code'''\n\n#Variable declaration\na=10 #User input for two numbers\nb=20\n\n#Result\nif a<b:\n print \"First number is less than second\"\n print \"Their difference is: \",b-a\n",
+ "input": [
+ "\n",
+ "#Variable declaration\n",
+ "a=10 #User input for two numbers\n",
+ "b=20\n",
+ "\n",
+ "#Result\n",
+ "if a<b:\n",
+ " print \"First number is less than second\"\n",
+ " print \"Their difference is: \",b-a\n"
+ ],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
- "text": "First number is less than second\nTheir difference is: 10\n"
+ "text": [
+ "First number is less than second\n",
+ "Their difference is: 10\n"
+ ]
}
],
"prompt_number": 12
@@ -255,7 +420,7 @@
{
"cell_type": "code",
"collapsed": false,
- "input": "",
+ "input": [],
"language": "python",
"metadata": {},
"outputs": []