Chapter 1 Getting Started Structured Programming 1

Similar documents
UEE1302 (1102) F10: Introduction to Computers and Programming

Multiple Choice Questions ( 1 mark)

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

Lecture 2 Tao Wang 1

It is necessary to have a single function main in every C program, along with other functions used/defined by the programmer.

CSc Introduction to Computing

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

Fundamental Data Types. CSE 130: Introduction to Programming in C Stony Brook University

CCE RR REVISED & UN-REVISED KARNATAKA SECONDARY EDUCATION EXAMINATION BOARD, MALLESWARAM, BANGALORE G È.G È.G È..

Tutorial No. 2 - Solution (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

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

Characters in C consist of any printable or nonprintable character in the computer s character set including lowercase letters, uppercase letters,

CS 326 Operating Systems C Programming. Greg Benson Department of Computer Science University of San Francisco

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

Chapter 21: Introduction to C Programming Language

CAMBRIDGE SCHOOL, NOIDA ASSIGNMENT 1, TOPIC: C++ PROGRAMMING CLASS VIII, COMPUTER SCIENCE

C Programming Multiple. Choice

Intro to Computer Programming (ICP) Rab Nawaz Jadoon

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

C Programs: Simple Statements and Expressions

Programming and Data Structure

C: How to Program. Week /Mar/05

B.V. Patel Institute of Business Management, Computer & Information Technology, Uka Tarsadia University

Fundamentals of Programming Session 4

Programming in C. Part 1: Introduction

Lecture 3 Tao Wang 1

6-1 (Function). (Function) !*+!"#!, Function Description Example. natural logarithm of x (base e) rounds x to smallest integer not less than x

Fundamentals of Programming. Lecture 3: Introduction to C Programming

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

COMPUTER SCIENCE HIGHER SECONDARY FIRST YEAR. VOLUME II - CHAPTER 10 PROBLEM SOLVING TECHNIQUES AND C PROGRAMMING 1,2,3 & 5 MARKS

Chapter 2 - Introduction to C Programming

C Functions. 5.2 Program Modules in C

Chapter 2: Introduction to C++

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

Programming for Engineers Introduction to C

Chapter 2. Lexical Elements & Operators

Basic C Programming. Bin Li Assistant Professor Dept. of Electrical, Computer and Biomedical Engineering University of Rhode Island

Introduction to C Language

Programming Fundamentals for Engineers Functions. Muntaser Abulafi Yacoub Sabatin Omar Qaraeen. Modular programming.

Lecture 2: C Programming Basic

Functions. Systems Programming Concepts

M4.1-R3: PROGRAMMING AND PROBLEM SOLVING THROUGH C LANGUAGE

Programming and Data Structures

Strings and Library Functions

AIR FORCE SCHOOL,BAMRAULI COMPUTER SCIENCE (083) CLASS XI Split up Syllabus (Session ) Contents

Computer Programming : C++

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

Introduction to C Programming. Chih-Wei Tang ( 唐之瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan

Computer System and programming in C

Programming Fundamentals (CS 302 ) Dr. Ihsan Ullah. Lecturer Department of Computer Science & IT University of Balochistan

INDORE INDIRA SCHOOL OF CAREER STUDIES C LANGUAGE Class B.Sc. - IIND Sem

Introduction to C programming. By Avani M. Sakhapara Asst Professor, IT Dept, KJSCE

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

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

Course Outline Introduction to C-Programming

Computing and Statistical Data Analysis Lecture 3

Introduction to Java Applications

Introduction Objectives: 1) differentiate between high-level, low-level, and machine language; 2) explain why it is necessary to translate a program

IECD Institute for Entrepreneurship and Career Development Bharathidasan University, Tiruchirappalli 23.

Chapter 2: Overview of C. Problem Solving & Program Design in C

BIL 104E Introduction to Scientific and Engineering Computing. Lecture 4

CHAPTER 3 BASIC INSTRUCTION OF C++

Use of scanf. scanf("%d", &number);

Technical Questions. Q 1) What are the key features in C programming language?

Fundamental of Programming (C)

Unit. Programming Fundamentals. School of Science and Technology INTRODUCTION

Split up Syllabus (Session )

LOGO BASIC ELEMENTS OF A COMPUTER PROGRAM

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

The births of the generations are as follow. First generation, 1945 machine language Second generation, mid 1950s assembly language.

C Language, Token, Keywords, Constant, variable

Lectures 5-6: Introduction to C

ANSI C Programming Simple Programs

2 nd Week Lecture Notes

Visual Studio. Visual Studio is an extremely complex interactive development environment capable of handling many languages and tools.

Data Types and Variables in C language

MA 511: Computer Programming Lecture 2: Partha Sarathi Mandal

CpSc 1111 Lab 6 Conditional Statements, Loops, the Math Library, and Random Numbers What s the Point?

A function is a named group of statements developed to solve a sub-problem and returns a value to other functions when it is called.

KARMAYOGI ENGINEERING COLLEGE, Shelve Pandharpur

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:

.. Cal Poly CPE 101: Fundamentals of Computer Science I Alexander Dekhtyar..

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

Program Organization and Comments

Lectures 5-6: Introduction to C

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

INTRODUCTION TO C++ FUNCTIONS. Dept. of Electronic Engineering, NCHU. Original slides are from

UNIT IV 2 MARKS. ( Word to PDF Converter - Unregistered ) FUNDAMENTALS OF COMPUTING & COMPUTER PROGRAMMING

Chapter 2, Part I Introduction to C Programming

AMCAT Automata Coding Sample Questions And Answers

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

H192 Midterm 1 Review. Tom Zajdel

Computer Programming 5th Week loops (do-while, for), Arrays, array operations, C libraries

Getting started with C++ (Part 2)

BIL 104E Introduction to Scientific and Engineering Computing. Lecture 1

Chapter 1 INTRODUCTION

Contents. A Review of C language. Visual C Visual C++ 6.0

C programming basics T3-1 -

Questions Bank. 14) State any four advantages of using flow-chart

Transcription:

Chapter 1 Getting Started 204112 Structured Programming 1

Outline Introduction to Programming Algorithm Programming Style The printf( ) Function Common Programming Errors Introduction to Modularity Top-Down Program Development Chapter Summary 204112 Structured Programming 2

Introduction to Programming Computer program is a sequence of instructions used to operate a computer to produce a specific result Programming language is a set of instructions used to construct a program 204112 Structured Programming 3

IPO: Input, Process, Output Main Hardware Component Input Devices Central Processing Unit Main Memory Output Devices Secondary Storage Devices 204112 Structured Programming 4

Set of Instructions & Library Functions 204112 Structured Programming 5

FORmula TRANslation (FORTRAN) 204112 Structured Programming 6

COmmon Business Oriented Language (COBOL) Current trend Programming Languages that can manipulate database: VB JAVA etc. 204112 Structured Programming 7

Algorithm is a step-by-step sequence of instructions that describes how to perform a computation a solution to a computer programming problem Example if we want to calculate the sum of whole numbers from 1 through 100 204112 Structured Programming 8

Summing the numbers 1 through 100 204112 Structured Programming 9

Summing the numbers 1 through 100 (cont.) 204112 Structured Programming 10

Algorithms for summing the numbers 1 through 100 Set n equal to 100 Set a equal to 1 Set b equal to 100 Calculate sum = n(a+b)/2 Formula Print the sum 204112 Structured Programming 11

Pseudocode & Flowchart Pseudocode is English phrases used to describe the algorithm (the processing steps) English-like steps that describes the solution Flowchart provides a pictorial representation of the algorithm using the symbols picture with specific blocks detailing out the logical flow of the solution 204112 Structured Programming 12

From Algorithms to Programs Coding 204112 Structured Programming 13

From Algorithms to Programs C Source Code #include <stdio.h> #include <conio.h> int main() { int n, remainder; clrscr(); printf("input n : "); scanf("%d", &n); remainder = n % 2; if (remainder == 0) printf ("n is even number\n"); else printf ("n is odd number\n"); getch(); return 0; } Problem Statement Read a number from the keyboard. Check and output if a given number N is ODD or EVEN Answer <- EVEN Input N Remainder = 0? Output Answer 204112 Structured Programming 14 Yes Start Remainder <- N modulo 2 End No Answer <- ODD

Programming Style Standard form int main( ) { program statements in here; return 0; } Remark: At the end of the program, Zero is usually returned to indicate error-free function termination. clarity and ease in reading 204112 Structured Programming 15

Programming Style (cont.) Poor programming style int main ( ) { printf ( Hello there world! ); return 0;} difficult to read and understand 204112 Structured Programming 16

Comments are explanatory remarks made within a program have no effect on program execution start of comment with /* end of comment via */ Example /* this is a comment */ /* this program prints out a message */ 204112 Structured Programming 17

Pgm1-3.cpp & Output Comment Output 204112 Structured Programming 18

The printf( ) Function 204112 Structured Programming 19

Pgm1-1.cpp & Output printf( ) Preprocessor command Output 204112 Structured Programming 20

Preprocessor Command Begin with a pound sign #, do not end with ; e.g. #include<stdio.h> The first step in the C program compilation stage The #include preprocessor command causes the contents of header file in <> to be inserted where the #include command appears. 204112 Structured Programming 21

C Libraries - #include Library file #include <stdio.h> #include <io.h> #include <math.h> #include <string.h> Functions Standard I/O functions printf, scanf, getc, fopen, fclose Traditional file operations lock, create, filelength, read, write Arithmetic abs, floor, pow, sqrt Logarithmic log, exp, Trignometric sin, tan, cos, atan Floating point fabs, fmod String operations strcpy, strcat, strcmp, strlen, strrev, strlwr, strupr Without including these libraries, you cannot write C programs that need to use these standard function. Check your reference for details on the C libraries, their functions and how they can be used. 204112 Structured Programming 22

Pgm1-2.cpp & Output new line: \n Output 204112 Structured Programming 23

\n If the backslash was omitted from the second printf( ) call in Pgm1-2.cpp Original Output New Output (without \) 204112 Structured Programming 24

An Example of \n Output 204112 Structured Programming 25

Common Programming Errors Omitting the parentheses after main main main( ) Omitting or incorrectly typing the opening brace { that signifies the start of a function body Omitting or incorrectly typing the closing brace } that signifies the end of a function Misspelling the name of a function print( ) printf( ) 204112 Structured Programming 26

Common Programming Errors (cont.) Forgetting to close the message to printf( ) with a double quote ( ) symbol Omitting the semicolon (;) at the end of each statement Forgetting the \n to indicate a new line 204112 Structured Programming 27

Introduction to Modularity 204112 Structured Programming 28

Introduction to Modularity (cont.) 204112 Structured Programming 29

Functions 204112 Structured Programming 30

Functions (cont.) The names permissible for functions are referred to as identifiers Rules first character of the name must be a letter or underscore ( _ ) only letters, digits, or underscores may follow the initial letter cannot be one of the keywords may have no more than 31 characters 204112 Structured Programming 31

Keywords (Reserved Words) 204112 Structured Programming 32

An Example of Valid & Invalid C Identifiers grosspay taxcalc addnums DegToRad 1AB3 salestax netpay while multtwo E*6 bessel1 Case-sensitive language TOTAL, total, TotaL three distinct names 204112 Structured Programming 33

The main( ) Function 204112 Structured Programming 34

The main( ) Function (cont.) 204112 Structured Programming 35

A Sample main( ) Function 204112 Structured Programming 36

Top-Down Program Development The five steps in the top-down development procedure Step1: determine the desired output Step2: determine the input items Step3: design the program determine an algorithm do a hand calculation Step4: write the program Step5: test the output 204112 Structured Programming 37

An Example of Problem The circumference, C, of a circle is given by the formula C = 2 r, where is the constant 3.1416 (accurate to four decimal places), and r is the radius of the circle. Using this information, write a C program to calculate the circumference of a circle that has a 2-inch radius. 204112 Structured Programming 38

Step1: Determine the Desired Output calculate, print, determine, find, or compare can be used to determine the desired output to calculate the circumference of a circle 204112 Structured Programming 39

Step2: Determine the Input Items input item is the name of an input quantity input value is a specific number input item the radius of the circle input value 2 204112 Structured Programming 40

Step3: Design the Program consists of 2 steps determine an algorithm (pseudocode) Assign a value to r Calculate the circumference using the formula C = 2 r Display the result do a hand calculation substitute radius of 2 inches into the formula 2(3.1416)2 = 12.5664 inches 204112 Structured Programming 41

Pgm1-4.cpp & Output Calculate the circumference of the circle Output 204112 Structured Programming 42

Step5: Test the Output is to verify that a program works correctly and fulfills its requirements The same program can be used over and over with new input data Program error is called a bug Debugging includes locating, correcting, and verifying the correction 204112 Structured Programming 43

Modularity & Top-Down Design 204112 Structured Programming 44

Modularity & Top-Down Design (cont.) 204112 Structured Programming 45

Program Translation 204112 Structured Programming 46

Program Translation (cont.) Interpreter each statement in the source program is translated individually and executed immediately Compiler all the statements in the source program are translated before any one statement is executed The result of compiling a source program is called an executable program 204112 Structured Programming 47

Chapter Summary main( ) printf( ) The body of function { All program statements in here; } 204112 Structured Programming 48

Chapter Summary (cont.) All C statements must be terminated by a semicolon (;) printf( ) can be a message enclosed with double quotes printf( Hello there world! ); 204112 Structured Programming 49

Q&A 204112 Structured Programming 50

Practice Write algorithm to solve the following: input 5 numbers and display how many odd numbers and how many even numbers. Example: If I input 5, 8, 9, 4, 7 The number of odd is 3 The number of even is 2 204112 Structured Programming 51