Lecture 03 & 04 Computer Programming

Similar documents
Lecture 01 & 02 Computer Programming

I. Variables and Data Type week 3

These are reserved words of the C language. For example int, float, if, else, for, while etc.

BASIC ELEMENTS OF A COMPUTER PROGRAM

A First Program - Greeting.cpp

Lecture 3 Tao Wang 1

Programming with Java

Introduction to Programming EC-105. Lecture 2

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

Fundamentals of Programming

Datatypes, Variables, and Operations

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

Computer System and programming in C

Professor: Sana Odeh Lecture 3 Python 3.1 Variables, Primitive Data Types & arithmetic operators

Fundamental of Programming (C)

Fundamentals of Programming CS-110. Lecture 2

Course Outline Introduction to C-Programming

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

Class 2: Variables and Memory. Laura Marik Spring 2012 C++ Course Notes (Provided by Jason Minski)

Data and Variables. Data Types Expressions. String Concatenation Variables Declaration Assignment Shorthand operators. Operators Precedence

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

Homework #3 CS2255 Fall 2012

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

Today. o main function. o cout object. o Allocate space for data to be used in the program. o The data can be changed

Chapter 2: Overview of C++

The C++ Language. Arizona State University 1

2 nd Week Lecture Notes

Chapter 2 C++ Fundamentals

LESSON 2 VARIABLES, OPERATORS, EXPRESSIONS, AND USER INPUT

Expressions and Variables

Introduction to the C++ Programming Language

CHAPTER 3 BASIC INSTRUCTION OF C++

Computer Programming : C++

Full file at

Computer Components. Software{ User Programs. Operating System. Hardware

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

CS1500 Algorithms and Data Structures for Engineering, FALL Virgil Pavlu, Jose Annunziato,

Values, Variables, Types & Arithmetic Expressions. Agenda

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

GO MOCK TEST GO MOCK TEST I

Chapter 2 Using Data. Instructor s Manual Table of Contents. At a Glance. Overview. Objectives. Teaching Tips. Quick Quizzes. Class Discussion Topics

CS313D: ADVANCED PROGRAMMING LANGUAGE

Computer Programming, I. Laboratory Manual. Experiment #2. Elementary Programming

CS102: Variables and Expressions

UNIT- 3 Introduction to C++

Lecture 2. Examples of Software. Programming and Data Structure. Programming Languages. Operating Systems. Sudeshna Sarkar

Basic Data Types and Operators CS 8: Introduction to Computer Science, Winter 2019 Lecture #2

Informatica e Sistemi in Tempo Reale

BSc (Hons) Computer Science. with Network Security. Examinations for / Semester1

Fundamentals of Programming

BASIC COMPUTATION. public static void main(string [] args) Fundamentals of Computer Science I

CSCE 120: Learning To Code

Unit 3, Lesson 2 Data Types, Arithmetic,Variables, Input, Constants, & Library Functions. Mr. Dave Clausen La Cañada High School

Ex: If you use a program to record sales, you will want to remember data:

Laboratory 2: Programming Basics and Variables. Lecture notes: 1. A quick review of hello_comment.c 2. Some useful information

Chapter 2 Working with Data Types and Operators

COSC 236 Section 101 Computer Science 1 -- Prof. Michael A. Soderstrand

Module 2 - Part 2 DATA TYPES AND EXPRESSIONS 1/15/19 CSE 1321 MODULE 2 1

Guide for The C Programming Language Chapter 1. Q1. Explain the structure of a C program Answer: Structure of the C program is shown below:

MA 511: Computer Programming Lecture 2: Partha Sarathi Mandal

Fundamentals of Programming (Python) Getting Started with Programming

cs1114 REVIEW of details test closed laptop period

DEPARTMENT OF MATHS, MJ COLLEGE

Overview of C. Basic Data Types Constants Variables Identifiers Keywords Basic I/O

Chapter 2: Data and Expressions

Outline. Data and Operations. Data Types. Integral Types

Programming and Data Structures

Python The way of a program. Srinidhi H Asst Professor Dept of CSE, MSRIT

WARM UP LESSONS BARE BASICS

BITG 1233: Introduction to C++

Operations. Making Things Happen

COMP 202 Java in one week

Lecture 2 Tao Wang 1

VARIABLES & ASSIGNMENTS

By the end of this section you should: Understand what the variables are and why they are used. Use C++ built in data types to create program

CS 106 Introduction to Computer Science I

ECE 122 Engineering Problem Solving with Java

Chapter 3 Syntax, Errors, and Debugging. Fundamentals of Java

Chapter 2 Elementary Programming

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

IT 374 C# and Applications/ IT695 C# Data Structures

Objects and Types. COMS W1007 Introduction to Computer Science. Christopher Conway 29 May 2003

A Java program contains at least one class definition.

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

Overview: Programming Concepts. Programming Concepts. Names, Values, And Variables

Overview: Programming Concepts. Programming Concepts. Chapter 18: Get With the Program: Fundamental Concepts Expressed in JavaScript

CMPE110 - EXPERIMENT 1 * MICROSOFT VISUAL STUDIO AND C++ PROGRAMMING

C: How to Program. Week /Mar/05

Programming for Engineers Introduction to C

Java Notes. 10th ICSE. Saravanan Ganesh

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

9/10/10. Arithmetic Operators. Today. Assigning floats to ints. Arithmetic Operators & Expressions. What do you think is the output?

Chapter 1. Introduction to Computers and Programming. M hiwa ahmad aziz

Getting Started With Python Programming

Lecture 6. Assignments. Java Scanner. User Input 1/29/18. Reading: 2.12, 2.13, 3.1, 3.2, 3.3, 3.4

CSc Introduction to Computing

Add Subtract Multiply Divide

Computer Components. Software{ User Programs. Operating System. Hardware

CIS 110: Introduction to Computer Programming

Contents. Jairo Pava COMS W4115 June 28, 2013 LEARN: Language Reference Manual

Transcription:

Lecture 03 & 04 Computer Programming 15 Computer Systems Engineering Second Semester By: Mr. Ali Asghar Manjotho, Lecturer, CSE-MUET

Contents Problem solving (LL 04) Six steps towards problem solution (LL 04) Basic problem solving concepts (LL 04) Data types (LL 04) Data literals(ll 04) Variables (LL 04) Constants (LL 04) Rules for naming variable and constant (LL 04) Operators (LL 02) LL 02 = Learning Level 02 Understanding, LL 04 = Learning Level 04 Analysis Ali Asghar Manjotho, Lecturer CSE-MUET 2

Problem Solving Ali Asghar Manjotho, Lecturer CSE-MUET 3

Problem Solving In every day life people make decisions to solve many problems. The problems may be unimportant as what to watch on TV or as important as choosing a new profession. If a bad decision is made, time and resources are wasted. So it is important that people know how to make decisions well. Ali Asghar Manjotho, Lecturer CSE-MUET 4

Six Steps Towards Problem Solution There are six steps to follow to ensure the best decision: Identify the problem Understand the problem Identify alternative solutions Select the best solution List instruction to solve problem Evaluate the solution Ali Asghar Manjotho, Lecturer CSE-MUET 5

Step 01: Identify the problem Ali Asghar Manjotho, Lecturer CSE-MUET 6

Step 01: Identify the problem The first step towards solving a problem is to identify the problem. If you don t know what the problem is, you cannot solve it. In a classroom situation, most problems have been identified for you and given to you in the form of written assignments or problems out of a book. However, when you are doing problem solving outside the classroom, you need to make sure you identify the problem before you start solving it. Ali Asghar Manjotho, Lecturer CSE-MUET 7

Step 02: Understand the problem Ali Asghar Manjotho, Lecturer CSE-MUET 8

Step 02: Understand the problem You must understand what is involved in the problem before you can continue toward the solution. You cannot solve a problem if you do not know the subject. For example, to solve a problem involving average of numbers, you must know how to calculate average; to solve a problem of trigonometry, you must know trigonometry. Ali Asghar Manjotho, Lecturer CSE-MUET 9

Step 03: Identify alternative solutions Ali Asghar Manjotho, Lecturer CSE-MUET 10

Step 03: Identify alternative solutions A single problem can be solved in many different ways. Identify and list all the possible solutions to the problem. For example there are multiple ways to sort the numbers as: Insertion sort Selection sort Bubble sort Ali Asghar Manjotho, Lecturer CSE-MUET 11

Step 04: Select the best solution Ali Asghar Manjotho, Lecturer CSE-MUET 12

Step 04: Select the best solution Analyze and identify the pros and cons of every alternative solution. Choose the solution that is optimal and best matches with your requirements. For example out of all the sorting options you choose bubble sort as your best solution. Ali Asghar Manjotho, Lecturer CSE-MUET 13

Step 05: List instructions to solve problem Ali Asghar Manjotho, Lecturer CSE-MUET 14

Step 05: List instructions to solve problem Write down general step by step procedure to solve the problem. For example to solve problem of calculating average of three numbers: Step 01 : Ask numbers a, b, and c from the user Step 02 : Add a, b, c and store result in sum Step 03 : Divide sum by 3 and store result in avg Step 04 : Display avg Ali Asghar Manjotho, Lecturer CSE-MUET 15

Step 06: Evaluate the solution Ali Asghar Manjotho, Lecturer CSE-MUET 16

Step 06: Evaluate the solution Finally evaluate and test the solution, means to check its result to see if it is correct and satisfies the needs. For example, when a person needs a pen to write a letter, buying him marker may be a correct solution, but it may not be very satisfactory. If the result is either incorrect or unsatisfactory, then the problem solver must review the list of instructions to see that they are correct or start the process all over again. Ali Asghar Manjotho, Lecturer CSE-MUET 17

Basic Problem Solving Concepts Ali Asghar Manjotho, Lecturer CSE-MUET 18

Data Types Ali Asghar Manjotho, Lecturer CSE-MUET 19

Data Types Every problem involves some sort of data in it. The data can be: Numbers Integers (e.g. 24, 5874, -547) Floating (e.g. 45.214, 0.2547, -658.748) Characters (e.g. a, f, #,?,!, w ) String (e.g. mehran, computer, MUET CSE ) Logical (e.g. True and False, Yes and No, 0 and 1) The type of data is known as data type of that data item. Ali Asghar Manjotho, Lecturer CSE-MUET 20

Data Types Data Item Example Value Data Type Age of a person 35 Integer Current year 2014 Integer Radius of circle 27.58 Floating Value of PI 3.14159 Floating A vowel e Character A key on keyboard? Character Person s name Ali Asghar String Address of an office Department of CSE-MUET String Is 1 st number greater than 2 nd? True or T or Yes or 1 Logical Is 7 less than 5? False or F or No or 0 Logical Ali Asghar Manjotho, Lecturer CSE-MUET 21

Do it yourself Data Types Data Item Example Value Data Type Number of students Brand name of smartphone Is 5 an even number? Count of cars in parking Speed of a car Your grade in result Is 13 a prime number? Title of book chapter Percentage of marks Option in MCQ Ali Asghar Manjotho, Lecturer CSE-MUET 22

Data Literals A fixed value that any data type can take is called as literal. A number literal is always written without quotes. (15, 68, 25.14, 578.14) A character literal is always written in single quotes. ( a, f, #,?,! ) An string literal is always written in double quotes. ( mehran, computer ) An logical literal is always written without quotes. (True, False, T, F, Yes, No, 1, 0) Ali Asghar Manjotho, Lecturer CSE-MUET 23

Data Literals Data Literal Type of Literal Ali String b Character 25.2 Floating 87.5 String 4 Character 4 Integer 4.0 Floating true String false Logical mehran String Ali Asghar Manjotho, Lecturer CSE-MUET 24

Variables In programming, a variable is the memory (RAM) location that can store the data temporary. Every program uses some sort of data and each data item is stored temporarily in the variables. Every variable has: Name Data type Value Ali Asghar Manjotho, Lecturer CSE-MUET 25

Variables The name of the variable is called as the identifier. Consider two of the examples. In first example, we have a variable whose name is radius, its data types is floating and its value is 25.5. In second example, we have a variable whose name is option, its data type is character and its value is b. radius 25.5 option b Ali Asghar Manjotho, Lecturer CSE-MUET 26

Variables The data type of the variable defines that what type of the data will be stored in a variable. Once the data type of variable is defined, it cannot hold any value of other data type. The value of the variable is changeable but its data type is not. Ali Asghar Manjotho, Lecturer CSE-MUET 27

Constants In programming, a constant is a variable whose value remains fixed through out the program. In certain programs some sort of data remains unchangeable through out the program, hence we use constants to stored that data so that it can not be altered. Ali Asghar Manjotho, Lecturer CSE-MUET 28

Constants PI Consider three of the examples. In first example, we have a constant whose name is PI, its data types is floating and its value is 3.141. In second example, we have a constant whose name is E (Euler s number), its data type is floating and its value is 2.718. 3.141 E 2.718 Ali Asghar Manjotho, Lecturer CSE-MUET 29

Constants SPEEDOFLIGHT In third example, we have a constant whose name is SPEEDOFLIGHT, its data types is floating and its value is 3 x 10 8. 3E8 Ali Asghar Manjotho, Lecturer CSE-MUET 30

Rules for naming Variables and Constants The name of any variable for constant is called as identifier. There are certain rules to follow when setting the identifier. Rule 1: May contain letters (A-Z, a-z), digits (0-9) or an underscore sign. Rule 2: It must start with letter or underscore sign. Rule 3: Should not contain any space in between. Rule 4: Should not be like any keyword of language like, int, cout, float. Rule 5: It is case sensitive i.e. radius is not same as Radius or RADIUS. Ali Asghar Manjotho, Lecturer CSE-MUET 31

Rules for naming Variables and Constants Rule 6: Must be unique i.e. no any two variables have same identifier. Rule 7: Must be relevant i.e. var1 is inappropriate identifier to store the area of circle, use area of area_circle instead. Rule 8: The identifier for constant is written with all capital letters. i.e. PI, SPEEDOFLIGHT, E etc. Ali Asghar Manjotho, Lecturer CSE-MUET 32

Rules for naming Variables and Constants Identifier Valid/Invalid Remarks first_number Valid first number Invalid Must not contain space in between number_1 Valid 1st_number Invalid Must start with a letter or underscore sign first# Invalid Must not contain invalid character $ int Invalid Must not be like keyword firstnumber int_first_number Valid Valid first_number_$ Invalid Must not contain invalid character # 1st_# Invalid Must start with a letter or underscore sign Must not contain invalid character # Ali Asghar Manjotho, Lecturer CSE-MUET 33

Do it yourself Rules for naming Variables and Constants Identifier Valid/Invalid Remarks number of items #_of_items price/item number_items itemcount item#price itemprice_1 5_itemPrice ITEM_PRICE $_per_item Ali Asghar Manjotho, Lecturer CSE-MUET 34

Operators Computer performs several operations on the data. The operations are represented by symbols known as operators. Operands are the data on which the operation is performed. Operand may be constant or stored in one of the variable. Ali Asghar Manjotho, Lecturer CSE-MUET 35

Example 1: a + b + is the operator a and b are the operands Both a and b are variables Example 1: 2 * length * is the operator 2 and length are the operands 2 is constant and length is a variable Operators Ali Asghar Manjotho, Lecturer CSE-MUET 36

Arithmetic Operators Operator Symbol Example Operation Resultant Addition + 10 + 26 36 Subtraction - 10-8 2 Multiplication * 4 * 20 80 Division / 25/5 5 Remainder/Modulus % 13 % 5 3 Power ^ 2^4 16 Ali Asghar Manjotho, Lecturer CSE-MUET 37

Relational Operators Operator Symbol Example Operation Resultant Greater Than > 5 > 6 false Less Than < 10 < 15 true Greater Than or Equals To >= 41 >= 90 true Less Than or Equals To <= 16 <=16 true Equals To == 15 == 19 false Not Equals To!= 14!= 80 true Ali Asghar Manjotho, Lecturer CSE-MUET 38

Logical Operators Operator Symbol Example Operation Resultant AND & true & true true true & false false false & true false false & false false OR true true true true false true false true true false false false NOT!! true false! false true Ali Asghar Manjotho, Lecturer CSE-MUET 39

Logical Operators (AND) A B A & B True True True True False False False True False False False False Ali Asghar Manjotho, Lecturer CSE-MUET 40

Logical Operators (OR) A B A B True True True True False True False True True False False False Ali Asghar Manjotho, Lecturer CSE-MUET 41

Logical Operators (NOT) A True False! A False True Ali Asghar Manjotho, Lecturer CSE-MUET 42