PK I[`! `! # C++ from the Ground/Chapter_2.ipynb{
"metadata": {
"name": "",
"signature": "sha256:05876b24f412fcea817693bad944dc90f3dae4fb5142aeb7d9c2c98569067083"
},
"nbformat": 3,
"nbformat_minor": 0,
"worksheets": [
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"
Chapter 2: An Overview of C++"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Example 2.1, Page Number: 12"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"\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"
]
}
],
"prompt_number": 1
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Example 2.2, Page Number: 17"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
" \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"
]
}
],
"prompt_number": 2
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Example 2.3, Page Number: 18"
]
},
{
"cell_type": "code",
"collapsed": false,
"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"
]
}
],
"prompt_number": 3
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Example 2.4, Page Number: 20"
]
},
{
"cell_type": "code",
"collapsed": false,
"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"
]
}
],
"prompt_number": 4
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Example 2.5, Page Number: 21"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
" \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()\n",
"Inside myfunc() \n",
"Back in main()\n"
]
}
],
"prompt_number": 5
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Example 2.6, Page Number: 22"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
" \n",
"print abs(-10)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"10\n"
]
}
],
"prompt_number": 6
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Example 2.7, Page Number: 23"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
" \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"
]
}
],
"prompt_number": 1
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Example 2.8, Page Number: 24"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
" \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"
]
}
],
"prompt_number": 8
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Example 2.9, Page Number: 26"
]
},
{
"cell_type": "code",
"collapsed": false,
"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\n",
"two\n",
"three four\n"
]
}
],
"prompt_number": 9
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Example 2.10, Page Number: 27"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
" \n",
"#Variable declaration\n",
"a=10 #user input for two numbers\n",
"b=20\n",
"\n",
"#Result\n",
"if aExample 2.11, Page Number: 28"
]
},
{
"cell_type": "code",
"collapsed": false,
"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"
]
}
],
"prompt_number": 3
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Example 2.12, Page Number: 30"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
" \n",
"a=10 #User input for two numbers\n",
"b=20\n",
"\n",
"#Result\n",
"if a