LOGO BASIC ELEMENTS OF A COMPUTER PROGRAM

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

6.096 Introduction to C++ January (IAP) 2009

C++ Basic Elements of COMPUTER PROGRAMMING. Special symbols include: Word symbols. Objectives. Programming. Symbols. Symbols.

C++ Programming: From Problem Analysis to Program Design, Third Edition

Computer Programming : C++

3. Functions. Modular programming is the dividing of the entire problem into small sub problems that can be solved by writing separate programs.

2 nd Week Lecture Notes

LECTURE 02 INTRODUCTION TO C++

Objectives. Chapter 2: Basic Elements of C++ Introduction. Objectives (cont d.) A C++ Program (cont d.) A C++ Program

Chapter 2: Basic Elements of C++

Chapter 2: Introduction to C++

Chapter 2: Basic Elements of C++ Objectives. Objectives (cont d.) A C++ Program. Introduction

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

Understanding main() function Input/Output Streams

Your First C++ Program. September 1, 2010

UNIT- 3 Introduction to C++

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

Features of C. Portable Procedural / Modular Structured Language Statically typed Middle level language

BITG 1233: Introduction to C++

Chapter 2 Basic Elements of C++

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

Objectives. In this chapter, you will:

Programming Language. Functions. Eng. Anis Nazer First Semester

The sequence of steps to be performed in order to solve a problem by the computer is known as an algorithm.

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

ADARSH VIDYA KENDRA NAGERCOIL COMPUTER SCIENCE. Grade: IX C++ PROGRAMMING. Department of Computer Science 1

Selection Control Structure CSC128: FUNDAMENTALS OF COMPUTER PROBLEM SOLVING

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

1. In C++, reserved words are the same as predefined identifiers. a. True

7/8/10 KEY CONCEPTS. Problem COMP 10 EXPLORING COMPUTER SCIENCE. Algorithm. Lecture 2 Variables, Types, and Programs. Program PROBLEM SOLVING

Creating a C++ Program

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

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

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

Programming. C++ Basics

A First Program - Greeting.cpp

CS 241 Computer Programming. Introduction. Teacher Assistant. Hadeel Al-Ateeq

CS 151 Review #3. // More than one variable can be defined // in a statement. Multiple variables are // separated by a comma.

CSC 307 DATA STRUCTURES AND ALGORITHM ANALYSIS IN C++ SPRING 2011

Expressions, Input, Output and Data Type Conversions

CSc Introduction to Computing

Review: Exam 1. Your First C++ Program. Declaration Statements. Tells the compiler. Examples of declaration statements

BITG 1113: Array (Part 2) LECTURE 9

Programming Fundamentals. With C++ Variable Declaration, Evaluation and Assignment 1

I/O Streams and Standard I/O Devices (cont d.)

Programming - 1. Computer Science Department 011COMP-3 لغة البرمجة 1 لطالب كلية الحاسب اآللي ونظم المعلومات 011 عال- 3

Superior University. Department of Electrical Engineering CS-115. Computing Fundamentals. Experiment No.6

CSc 10200! Introduction to Computing. Lecture 2-3 Edgardo Molina Fall 2013 City College of New York

Lecture 2 Tao Wang 1

Chapter 1 Introduction to Computers and C++ Programming

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

The C++ Language. Arizona State University 1

CSCE 110 PROGRAMMING FUNDAMENTALS

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

Introduction to Programming

3. Except for strings, double quotes, identifiers, and keywords, C++ ignores all white space.

! A literal represents a constant value used in a. ! Numbers: 0, 34, , -1.8e12, etc. ! Characters: 'A', 'z', '!', '5', etc.

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

c++ keywords: ( all lowercase ) Note: cin and cout are NOT keywords.

Your first C++ program

CHRIST THE KING BOYS MATRIC HR. SEC. SCHOOL, KUMBAKONAM CHAPTER 9 C++

BASIC ELEMENTS OF A COMPUTER PROGRAM

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

UEE1302 (1102) F10: Introduction to Computers and Programming

Program Organization and Comments

Tutorial No. 2 - Solution (Overview of C)

Chapter 4: Subprograms Functions for Problem Solving. Mr. Dave Clausen La Cañada High School

Fundamentals of Programming CS-110. Lecture 2

Computer Skills (2) for Science and Engineering Students

Fundamental of Programming (C)

CHAPTER 3 BASIC INSTRUCTION OF C++

Engineering Problem Solving with C++, Etter/Ingber

Introduction to the C++ Programming Language

Chapter 2: Overview of C++

A SHORT COURSE ON C++

C++ Programming: From Problem Analysis to Program Design, Fifth Edition. Chapter 6: User-Defined Functions I

Multiple Choice Questions ( 1 mark)

Introduction to Programming EC-105. Lecture 2

Top-Down Design Predefined Functions Programmer-Defined Functions Procedural Abstraction Local Variables Overloading Function Names

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

Types, Values, Variables & Assignment. EECS 211 Winter 2018

WARM UP LESSONS BARE BASICS

Getting started with C++ (Part 2)

PART I. Part II Answer to all the questions 1. What is meant by a token? Name the token available in C++.

Model Viva Questions for Programming in C lab

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

Chapter 2, Part I Introduction to C Programming

Chapter 2 C++ Fundamentals

Chapter 4. Procedural Abstraction and Functions That Return a Value. Copyright 2014 Pearson Addison-Wesley. All rights reserved.

UNIT - I. Introduction to C Programming. BY A. Vijay Bharath

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

4. Structure of a C++ program

CS101: Fundamentals of Computer Programming. Dr. Tejada www-bcf.usc.edu/~stejada Week 1 Basic Elements of C++

CSC 126 FINAL EXAMINATION Spring Total Possible TOTAL 100

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

Characters, c-strings, and the string Class. CS 1: Problem Solving & Program Design Using C++

Main Program. C Programming Notes. #include <stdio.h> main() { printf( Hello ); } Comments: /* comment */ //comment. Dr. Karne Towson University

C++ character set Letters:- A-Z, a-z Digits:- 0 to 9 Special Symbols:- space + - / ( ) [ ] =! = < >, $ # ; :? & White Spaces:- Blank Space, Horizontal

Introduction to C++ Programming Pearson Education, Inc. All rights reserved.

Chapter 3. Numeric Types, Expressions, and Output

Transcription:

LOGO BASIC ELEMENTS OF A COMPUTER PROGRAM

Contents 1. Statements 2. C++ Program Structure 3. Programming process 4. Control Structure

STATEMENTS

ASSIGNMENT STATEMENTS

Assignment statement Assigns a value to respective memory space allocated to a variable. May involve arithmetic expressions. Syntax: variable = value/ variable; Examples a = 3; x = 5.6; y = z ; pi = 3.14; 5 = z; //invalid!!

Assignment statement Syntax: The expressions may contains variables, constant and arithmetic operators, Example: variable = expression; float radius, pi, area; radius = 3; pi = 3.14; area = pi * radius * radius;

Assignment statement A sequence of character: Using assignment statement: char a [20] = sample string ; Using strcpy function: char a [20]; strcpy(a, sample string );

CONVERTING ALGEBRAIC EXPRESSION INTO C++ STATEMENTS

Writing algebraic expression using C++ statement. C++ Statement pow(a,b) Algebraic statement a b sqrt(a - b) (a b) abs(x - y) x y

Examples Algebraic Expression Arithmetic Expression 17 12 + 74 17 12 + 74 23 7 23 7 / 5 5 12 + 1 + 76 (12 + 1 + 76) / 4 4 4 2 pow ( 4, 2 ) ( a b) sqrt (a b ) x y abs ( x y )

Examples: -b ± b 2 4ac 2a Solution : -b + sqrt ((pow (b,2) (4 * a * c))/(2 * a) Or -b - sqrt (b * b 4 * a * c) / (2 * a)

INPUT OUTPUT STATEMENTS

Output statements Used to display information on screen. The header file iostream.h must be included in the preprocessor directives. Syntax: cout<<variable<<variable<< <<variable; The symbol << is called insertion operator

Output statements year = 1999; cout << my year of birth is <<year; Sample output: my year of birth is 1999

Output statements The operand after the symbol << can be a constant, variable or an expression. cout << (2003 year); The following are escape sequence which can be used in a string literal in a cout statement. \n new line \ single quote \t - tab \ - double quote \b - backspace \\ - backslash

Input statement Used to read in information from a keyboard. Syntax: cin>>variable>>variable>> >>variable; The symbol >> is called an extraction operator. The operand after the symbol >> must be a variable.

Input statement: example cout << enter your age and matric number: ; cin>>age>>matric;

Input statement cin only takes input up to the first blank (ignores whitespace such as blanks, tabs). When reading a sequence of character (including whitespace), used: cin.getline(name, 20);

Example Using cin statement: cout<< Enter your name: ; cin>>name; cout<< Welcome, <<name<<! ; Sample output Enter your name: Adam Ahmad Welcome, Adam!

Example Using cin.getline statement: cout<< Enter your name: ; cin.getline(name,20); cout<< Welcome, <<name<<! ; Sample output Enter your name: Adam Ahmad Welcome, Adam Ahmad!

C++ PROGRAM STRUCTURE

Components of a program Preprocessor directives Constant and type definition section Main program heading Begin block Main block Declaration section Statement section End block

Syntax #include <headerfilename> const <datatype><identifier> = <value> datatype <identifier> <functiontype> <functionname> (formal parameter list) { datatype <identifier> } cout << statement; cin >> variables;

A C++ program is a collection of one or more subprograms, called functions A subprogram or a function is a collection of statements that, when activated (executed), accomplishes something Every C++ program has a function called main

Example of program #include <iostream> #include <string> Preprocessor Directives using namespace std; int main() { string nickname; char fullname[50]; int age; main function Variable declaration cout<< "Please enter your nickname:\n"; cin.getline(fullname,50); cout<< "Please enter your fullname:\n"; cin>>nickname; cout<< "Please enter your age:\n"; cin>>age; cout<<endl; cout<< "Your nickname is:"<<nickname<<endl; cout<< "Your fullname is:"<<fullname<<endl; cout<< "Your age is:"<<age<<endl; Input Output } return 0;

Preprocessor directives A program that sets up your source code for the compiler The #include directive causes the preprocessor to include contents of another file in the program Examples: #include <iostream.h> / <iostream> To declares the basic C++ streams (I/O) routines. #include <math.h> / <cmath> Declares prototypes for the math functions and math error handlers. #include <conio.h> Declares various functions used in calling the operating system console I/O routines.

main function Marks the beginning of a function function a block of code that performs a task main name of a function (every program must have only one main function) int stands for integer, indicates the function send an integer value back to the operating system when it finished executing () must ended with this no semicolon ; added at the end of a function return 0; - This sends the integer value 0 back to OS upon the program s completion 0 The value 0 indicates that a program executed successfully.

{ - beginning brace / open brace marks the beginning of the code block that comprises the function. } closing brace marks the end of the code block Everything between the opening and closing set of braces ({ }) belongs to this main function and is referred to as the function body. ; - semicolon marks the end of a complete programming statement

Predefined function - strcmp The strcmp are in the library with the header file string. #include<string> int y = strcmp(s1, s2); char a [20]; strcpy(a, sample string );

Predefined function - strcmp Problem statement: Write a C++ program to display the program of the student based on the code the he or she inputs in the computer. Display Invalid code if the student enter wrong code. Code C A E Program Computer Science Accounting Engineering C++ Program segment: if( code == C ) cout<<. ; else if ( code == E ) cout<<. ;...

Predefined function - strcmp Write a C++ program to display the code of the student based on the program the he or she inputs in the computer. Display Invalid program if the student enter wrong program. Code C A E Program Computer Science Accounting Engineering

Characters ASCII codes

Predefined function - strcmp cout<< "please enter program:"; cin.getline(program, 20); if(program == computer science ) cout<< C ; else if (program == Accounting ) cout<< A ; else if (program == Engineering ) cout<< E ; else cout<< invalid ; Is this the correct way???

Predefined function - strcmp int y = strcmp(s1, s2); strcmp will accept two sequence of characters. It will return an integer. The integer will be: Negative if s1 is less than s2 Zero is s1 and s2 are equal Positive if s1 is greater than s2

Predefined function - strcmp How to use it??? cout<< "please enter program:"; cin.getline(program, 20); int y = strcmp(program, computer science ); if(y == 0) cout<< yes ; else cout<< no ; The output:

Predefined function - strcmp How to use it?? cout<< "please enter program:"; cin.getline(program, 20); if(strcmp(program, computer science ) == 0) cout<< yes ; else cout<< no ;

Predefined function - strcmp cout<< "please enter program:"; cin.getline(program, 20); if(strcmp(program, computer science ) == 0) cout<< C ; else if (strcmp(program, Accounting ) == 0) cout<< A ; else if (strcmp(program, Engineering ) == 0) cout<< E ; else cout<< invalid ;

Characters ASCII codes

4 2 Problem statement: Write a C++ program that compare the brand of two handphone. This program should ask for the brand of handphone that user currently use and then compare the brand with the given brand, which is SAMSUNG. If the brand is same, then display Same like mine!. Otherwise display Not the same!

This is not the correct way to write the program!! 4 3

Characters ASCII codes

int y = strcmp(s1, s2); strcmp will accept two sequence of characters. In the library with the header file string. #include <string> It will return an integer. The integer will be: Negative if s1 is less than s2 Zero is s1 and s2 are equal Positive if s1 is greater than s2

This is the correct way to write the program!! 4 6

Programming process

Compilation process

Running a program

CONTROL STRUCTURES

Sequential Control Structure The simplest of all the structures The program instruction has one starting point and one ending point. Each step is carried out in order of their position and is only done once.

Sequential Control Structure Flowchart Begin Fill a kettle with water Boil the water in the kettle Put tea leaves in the pot Pour boiling water into the pot End

Selection Control Structure The selection structure allows comparison of expression, and based on the comparison, to select certain course of action

Selection Control Structure F Telephone rings? T Continue reading Answer phone

Repetition control structure The repetition structure allow a sequence of instructions to be executed repeatedly until a certain condition is achieved.

Repetition control structure false beg has items? true Take item out

Modular Is a method of dividing a problem into separate tasks, each with a single purpose. Separate task with a single purpose = FUNCTION

Modular Company Accountancy IT Human resource

LOGO