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