Computer Programming

Similar documents
Computer Programming

Computer Programming

Computer Programming

Computer Programming

Computer Programming

Computer Programming

Computer Programming

Computer Programming

Computer Programming

Computer Programming

CS101 Computer programming and utilization

Computer Programming

Computer Programming

Computer Programming

Computer Programming

Computer Programming

Understanding main() function Input/Output Streams

Computer Programming

Computer Programming

Computer Programming

Computer Programming : C++

Introduction to Programming

Structured Programming Using C++ Lecture 2 : Introduction to the C++ Language. Dr. Amal Khalifa. Lecture Contents:

Computer Programming

CS 101 Computer Programming and utilization. Dr Deepak B Phatak Subrao Nilekani Chair Professor Department of CSE, Kanwal Rekhi Building IIT Bombay

pointers + memory double x; string a; int x; main overhead int y; main overhead

Computer Programming

Intro to Programming & C Why Program? 1.2 Computer Systems: Hardware and Software. Why Learn to Program?

Chapter 1 INTRODUCTION

Programming in C++ Prof. Partha Pratim Das Department of Computer Science and Engineering Indian Institute of Technology, Kharagpur

Computer Science II Lecture 1 Introduction and Background

CSCE 121 ENGR 112 List of Topics for Exam 1

Computer Programming

LECTURE 02 INTRODUCTION TO C++

Computer Programming

Programming with C++ as a Second Language

C++ basics Getting started with, and Data Types.

Introduction to Programming using C++

Computer Programming

! A program is a set of instructions that the. ! It must be translated. ! Variable: portion of memory that stores a value. char

Exam 1. CSI 201: Computer Science 1 Fall 2018 Professors: Shaun Ramsey

Chapter 2: Introduction to C++

Chapter 2: Special Characters. Parts of a C++ Program. Introduction to C++ Displays output on the computer screen

2 nd Week Lecture Notes

Chapter 1. C++ Basics. Copyright 2010 Pearson Addison-Wesley. All rights reserved

This watermark does not appear in the registered version - Slide 1

download instant at Introduction to C++

C++ Basics. Lecture 2 COP 3014 Spring January 8, 2018

Introduction to Programming EC-105. Lecture 2

What we will learn about this week: Declaring and referencing arrays. arrays as function arguments. Arrays

Fundamentals of Programming CS-110. Lecture 2

4. Structure of a C++ program

Programming. C++ Basics

Basic Computer Programming for ISNE. Santi Phithakkitnukoon ผศ.ดร.ส นต พ ท กษ ก จน ก ร

CSc Introduction to Computing

Lab # 02. Basic Elements of C++ _ Part1

CHAPTER 3 BASIC INSTRUCTION OF C++

Chapter 2: Overview of C++

2.1. Chapter 2: Parts of a C++ Program. Parts of a C++ Program. Introduction to C++ Parts of a C++ Program

CS2141 Software Development using C/C++ C++ Basics

The C++ Language. Arizona State University 1

c) Comments do not cause any machine language object code to be generated. d) Lengthy comments can cause poor execution-time performance.

Functions, Arrays & Structs

BITG 1233: Introduction to C++

do { statements } while (condition);

GE U111 Engineering Problem Solving & Computation Lecture 6 February 2, 2004

Introduction to C++ Dr M.S. Colclough, research fellows, pgtas

LAB: INTRODUCTION TO FUNCTIONS IN C++

1- Write a single C++ statement that: A. Calculates the sum of the two integrates 11 and 12 and outputs the sum to the consol.

o Counter and sentinel controlled loops o Formatting output o Type casting o Top-down, stepwise refinement

Getting started with C++ (Part 2)

CSCI 1061U Programming Workshop 2. C++ Basics

Multiple Choice (Questions 1 13) 26 Points Select all correct answers (multiple correct answers are possible)

Your first C++ program

Object Oriented Design

CS 101 Computer Programming and utilization. Dr Deepak B Phatak Subrao Nilekani Chair Professor Department of CSE, Kanwal Rekhi Building IIT Bombay

EP241 Computer Programming

Outline. Introduction. Arrays declarations and initialization. Const variables. Character arrays. Static arrays. Examples.

Overview. - General Data Types - Categories of Words. - Define Before Use. - The Three S s. - End of Statement - My First Program

Full file at C How to Program, 6/e Multiple Choice Test Bank

Your First C++ Program. September 1, 2010

Reading from and Writing to Files. Files (3.12) Steps to Using Files. Section 3.12 & 13.1 & Data stored in variables is temporary

REVIEW. The C++ Programming Language. CS 151 Review #2

Scientific Computing

Chapter 1 Introduction to Computers and C++ Programming

PIC 10A Pointers, Arrays, and Dynamic Memory Allocation. Ernest Ryu UCLA Mathematics

Lecture 4. 1 Statements: 2 Getting Started with C++: LESSON FOUR

CHAPTER 1.2 INTRODUCTION TO C++ PROGRAMMING. Dr. Shady Yehia Elmashad

Lecture Transcript While and Do While Statements in C++

VARIABLES & ASSIGNMENTS

Chapter 1 Introduction to Computers and Programming

Creating a C++ Program

Objectives. In this chapter, you will:

Dr. Md. Humayun Kabir CSE Department, BUET

C++ Basics. Data Processing Course, I. Hrivnacova, IPN Orsay

Starting Out with C++: Early Objects, 9 th ed. (Gaddis, Walters & Muganda) Chapter 2 Introduction to C++ Chapter 2 Test 1 Key

UEE1302 (1102) F10: Introduction to Computers and Programming

Basic memory model Using functions Writing functions. Basics Prototypes Parameters Return types Functions and memory Names and namespaces

File Operations. Lecture 16 COP 3014 Spring April 18, 2018

Name Section: M/W or T/TH. True or False (14 Points)

Transcription:

Computer Programming Dr. Deepak B Phatak Dr. Supratik Chakraborty Department of Computer Science and Engineering Session: Structure of a Simple C++ Program Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, 1

Quick Recap of Some Relevant Topics Dumbo model of computing Notion of instructions Notion of memory Reading and writing values in memory Notion of input and output Intuitive idea of program 2 Dr. Deepak B. Phatak & Dr. Supratik Chakraborty,

Overview of This Lecture Understand structure of a simple C++ program Some common features Significance of features Example program There s more to structure of C++ programs We ll see these later in the course 3 Dr. Deepak B. Phatak & Dr. Supratik Chakraborty,

Our Friendly C++ Program // file add_two_numbers.cpp #include <iostream> using namespace std; // this program reads two integers // and calculates the sum int main() { int A, B, C; cout << Give two numbers ; cin >> A >> B; C = A + B; cout << Sum is << C; return 0; } A program is a sequence of directives, declarations and instructions Written according to some rules Computer languages also have grammars! Stored in one (or more) files e.g. add_two_numbers.cpp 4 Dr. Deepak B. Phatak & Dr. Supratik Chakraborty,

Our Friendly C++ Program... // file add_two_numbers.cpp #include <iostream> using namespace std; // this program reads two integers // and calculates the sum int main() { int A, B, C; cout << Give two numbers ; cin >> A >> B; C = A + B; cout << Sum is << C; return 0; } Compiler Directives 5 Dr. Deepak B. Phatak & Dr. Supratik Chakraborty,

Compiler Directives Instructions to compiler used when compiling your program to machine language #include <iostream> Include instructions from iostream header file Input/output handled as streams of bytes Input stream converted to internal representation of computer Internal representation converted to stream of displayable characters cin (for keyboard input) and cout (for console output) work because of instructions in iostream header file 6 Dr. Deepak B. Phatak & Dr. Supratik Chakraborty,

Compiler Directives using namespace std; Names of objects in a program can be grouped in namespaces Analogy: Names of students in CS101 grouped in divisions Each namespace can be given a name Analogy: Each division can be given a name When referring to a name, we must indicate which namespace we are referring to Analogy: Shyam from division E C++ programs: mynamespace::myvarname Can use same name in different namespaces Shyam from division E different from Shyam from division A mynamespace1::myvarname different from mynamespace2::myvarname using namespace std asks compiler to use a global namespace called std myvarname refers to std::myvarname 7 Dr. Deepak B. Phatak & Dr. Supratik Chakraborty,

Our Friendly C++ Program... // file add_two_numbers.cpp #include <iostream> using namespace std; // this program reads two integers // and calculates the sum int main() { main function begins here int A, B, C; cout << Give two numbers ; cin >> A >> B; C = A + B; cout << Sum is << C; return 0; } main function ends here 8 Dr. Deepak B. Phatak & Dr. Supratik Chakraborty,

main function Literally, the main part of the program Operating system invokes this function when you run your compiled program Can pass one or more parameters to this function None passed in our example Operating system gets back control of computer when this function ends 9 Dr. Deepak B. Phatak & Dr. Supratik Chakraborty,

Our Friendly C++ Program... // file add_two_numbers.cpp #include <iostream> using namespace std; // this program reads two integers // and calculates the sum int main() { int A, B, C; cout << Give two numbers ; cin >> A >> B; C = A + B; cout << Sum is << C; return 0; } Variable declarations 10 Dr. Deepak B. Phatak & Dr. Supratik Chakraborty,

Variables and Declarations Variables Recall the drawers of Dumbo Basic computational objects Declarations Each variable must be named For each variable, we must indicate type of value it can store A variable must be declared before it is used Remember using namespace std int A, B, C; really means int std::a, std::b, std:c; Isn t it convenient to use using namespace std? 11 Dr. Deepak B. Phatak & Dr. Supratik Chakraborty,

Our Friendly C++ Program... // file add_two_numbers.cpp #include <iostream> using namespace std; // this program reads two integers // and calculates the sum int main() { int A, B, C; cout << Give two numbers ; cin >> A >> B; C = A + B; cout << Sum is << C; return 0; } Input/Output statements 12 Dr. Deepak B. Phatak & Dr. Supratik Chakraborty,

Input/Output Statements This is how we interact with the program cin allows input from keyboard cout displays/outputs on console Essential components of most programs we will write 13 Dr. Deepak B. Phatak & Dr. Supratik Chakraborty,

Our Friendly C++ Program... // file add_two_numbers.cpp #include <iostream> using namespace std; // this program reads two integers // and calculates the sum int main() { int A, B, C; cout << Give two numbers ; cin >> A >> B; C = A + B; cout << Sum is << C; return 0; } Assignment statement 14 Dr. Deepak B. Phatak & Dr. Supratik Chakraborty,

Assignment (and other) Statements This is where the program does the really interesting part C = A + B ; A + B: Arithmetic expression C: Destination of assignment Instruction: Add values in variables A and B, and store it in variable C We ll see lots more examples of statements that allow us to do fantastic stuff with our programs 15 Dr. Deepak B. Phatak & Dr. Supratik Chakraborty,

Our Friendly C++ Program... // file add_two_numbers.cpp #include <iostream> using namespace std; // this program reads two integers // and calculates the sum int main() { int A, B, C; cout << Give two numbers ; cin >> A >> B; C = A + B; cout << Sum is << C; return 0; } Return control back to caller (here, OS), and pass the value 0 16 Dr. Deepak B. Phatak & Dr. Supratik Chakraborty,

return Statement A function is called/invoked By operating system (e.g. main function) By another function return returns control to caller In our example, the operating system Can also return a value to caller Useful for returning result of computation Useful for indicating error status 17 Dr. Deepak B. Phatak & Dr. Supratik Chakraborty,

Our Friendly C++ Program... // file add_two_numbers.cpp #include <iostream> using namespace std; // this program reads two integers // and calculates the sum int main() { int A, B, C; cout << Give two numbers ; cin >> A >> B; C = A + B; cout << Sum is << C; return 0; } A logical block of statements 18 Dr. Deepak B. Phatak & Dr. Supratik Chakraborty,

Grouping Statements In C++, statements can be grouped by enclosing them within a pair of { and } Each group is treated as one logical unit of the program Useful for demarcating logical parts of a program Each group can have its own variable declarations 19 Dr. Deepak B. Phatak & Dr. Supratik Chakraborty,

Our Friendly C++ Program... // file add_two_numbers.cpp #include <iostream> using namespace std; // this program reads two integers // and calculates the sum int main() { int A, B, C; { cout << Give two numbers ; cin >> A >> B; } C = A + B; {cout << Sum is << C; return 0; } } Hierarchically grouped blocks 20 Dr. Deepak B. Phatak & Dr. Supratik Chakraborty,

Our Friendly C++ Program... // file add_two_numbers.cpp #include <iostream> using namespace std; // this program reads two integers // and calculates the sum int main() { int A, B, C; cout << Give two numbers ; cin >> A >> B; C = A + B; cout << Sum is << C; return 0; } Comments 21 Dr. Deepak B. Phatak & Dr. Supratik Chakraborty,

Comments Essential for good readability of program Can appear anywhere in program Completely ignored by compiler Good programming practice Insert adequate comments throughout your code Make your program speak its story through good comments 22 Dr. Deepak B. Phatak & Dr. Supratik Chakraborty,

Summary Structure of a simple C++ program Compiler directives main function Variable declarations Input/output statements Assignment (and other) statements return statement Grouping statements into logical blocks Comments Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, 23