King Saud University College of applied studies and community services CSC 1101 Computer Programming I Lecture 2
Course Introduction1 Lecture Outline What Is A Computer Program? Software? Why Program? What Makes A Good Programmer? An Overview of Computer Languages History Of C Why C? Flow of Information During Program Execution The Software Development Method An Example of Meter to Yard Conversion Algorithm Pseudocode Flowcharts
What Is A Computer Program? Software? Program = set of instructions that a computer will follow to perform a specific task. Software = collection of programs. Course Introduction2 Two types of software: System software: used to manage computer hardware to enable us to execute & write application programs/software. Ex. Operating System, Compilers, …etc. Application Software: are meant for solving users own problems. Ex. Word processors, database management…etc.
Course Introduction3 Why Program? A tailor-made program is required. Programmers are in high demand. Programming is an intellectual challenge.
Course Introduction4 What Makes A Good Programmer? Two main factors: Knowledge: data structures, algorithms, tools, when and how to use those tools, & experience. Maturity: patience, humility, & time management. Only the first one can be taught. Good programmers are more than 10 times productive as average ones (does not mean they write 10 times as many lines of code per day).
An Overview Of Computer Languages Course Introduction5 Machine Language Assembly Language High-Level Language Ex.Ex. Ex MOV AX,A A = A ADD AX, MOV A,AX Collection of binarySymbolic form of machine Combines algebraic numberslanguage (I.e. symbolic expressions & symbols taken names are used to represent from English language operations, registers & (ex. C, Pascal, memory locations FORTRAN, …etc) Directly understood by aAssembler Compiler (or interpreter) computerconverts to machine converts to machine language Easier to use More powerful
An Overview Of Computer Languages (Continue) Course Introduction7 C is sometimes called a middle-level language. This is not a reflection on its lack of programming power. It’s a reflection on its capability to access the system’s low-level functions.
History Of C Course Introduction8 Created by Dennis Ritchie at the Bell Telephone laboratories in Evolved from a language called B.
Why Use C? Course Introduction9 C is a powerful and flexible language. C is used for projects as diverse as operating systems, word processors, graphics, spreadsheets, and even compilers for other languages. C is a popular language preferred by professional programmers. As a result, a wide variety of C compilers and helpful accessories are available. C is a portable language. Portable means that a C program written for one computer system can be compiled and run on another system with little or no modification (hardware independent). Portability is enhanced by the ANSI standard for C, the set of rules for C compilers.
Why Use C? Course Introduction10 C is a language of few words, containing only a handful of terms called keywords, which serve as the base on which the language’s functionality is built. C is modular, C code should be written in routines called functions. These functions can be reused in other applications or programs. By passing pieces of information to the functions, you can create useful, reusable code.
Software Development Method
The Software Development Method Course Introduction7 1. Specify the problem requirements. 2. Analyze the problem. 3. Design the algorithm to solve the problem. 4. Implement the algorithm. 5. Test and verify the completed program. 6. Maintain and update the program.
An Example of Meter to Yard Conversion Course Introduction8 1. Specify the problem requirements 2. Analyze the problem Problem Input: Problem Output: Formulas: size in square meters (decimal) A program is required to convert square meters to square yards. size in square yards (decimal) square yards = * square meters 3. Design the algorithm to solve the problem 1. Read the fabric size in square meters. 2. Convert the fabric size to square yards using the formula. 3. Display the fabric size in square yards.
Course Introduction9 4. Implement the algorithm (i.e. write the program) 5. Test and verify the completed program. 6. Maintain and update the program. An Example of Meter to Yard Conversion
Algorithm Course Introduction10 An Algorithm is a procedure for solving a problem in terms of: 1. the actions to be executed 2- the order in which these actions are executed
Pseudocode Course Introduction11 Pseudocode is an artificial and informal language that helps programmer develop algorithms. It is similar to everyday English. It is not a computer programming language. Example Get out of Bed Take a shower Get dressed Eat breakfast Drive to work
Flowcharts Course Introduction12 A flowchart is a graphical representation of an algorithm. They are drawn using rectangles, diamonds, ovals, and small circles.
Flowcharts (Continue) Course Introduction13 Start or End Input or Output Process or Calculation Decision
C Language Elements Exercise Write a program to process a collection of daily temperatures. Your program should count and print the number of hot days (85 or higher), the number of pleasant days ( ), the number of cold days (less than 60). It should also display the category of each temperature. 19
C Language Elements Exercise 1. Specify the problem requirements 2. Analyze the problem Input: Output: temperature A program is required to process daily temperatures and provide the total number of hot, pleasant, and cold days. total number of days in each category 3. Design the algorithm to solve the problem 1. Get the temperature. 2. Determine category of that temperature and increment counter for that particular category. 3. Display categories and total number of days that fell under the category. 20
C Language Elements >=85? Print Hot, Cold, and Pleasant End Add 1 to Hot Yes Add 1 to Cold Yes <=60? No Add 1 to Pleasant No Get Temp Exercise Start 21
C Language Elements
Lecture Outline C Language Elements Preprocessor Directives Function Main Declarations Executable Statements Reserved Words, Standard Identifiers & User- Defined Identifier User-Defined Identifier Rules 23
C Language Elements Preprocessor Directive A C program line beginning with # that provides an instruction to the preprocessor. Preprocessor A system program that modifies a C program prior to its compilation. Two important preprocessor directives are: #include #define C Language Elements 24
#include Preprocessor Directive C Language Elements Syntax #include Examples #include 25
C Language Elements #define Preprocessor Directive Syntax #define NAME value Examples #define PI 3.14 #define section
C Language Elements Syntax int main(void) { } (declarations) (Executable statements) It marks the beginning of any C program. Function Main 27
C Language Elements The part of a program that tells the compiler the names of memory cells in a program Declarations Executable Statements Program commands that are converted to machine code by the compiler. 28
Declarations C Language Elements Syntax Data type variable; Examples int count, large; float area; char first; int count, large; 29
Data Types C Language Elements It is a set of values. It defines operations that can be performed on those values. The most used data types are: int float char 30
int C Language Elements The int data type represents integers in C. Integers are whole numbers. ANSI defined the range of integers as being between and Examples:
Float C Language Elements The float data type represents real numbers in C. A real number has an integral part and a fractional part separated by a decimal point. Examples:
char C Language Elements The char represents one individual character in C. Examples: 'D' 'd' '5' '*' 33
Reserved Words, Standard Identifiers & User-Defined Identifier C Language Elements A reserved word is a word that has a special meaning in C and cannot be used for other purposes. Example: int, double, char, void. A standard identifier is a word that has a special meaning in C but can be used for other purposes or redefined. Example: printf, scanf. A user-defined identifier is a name given to a variable. Does not have a special meaning in C and needs to be declared. Example: PI, large, hot. 34
User-Defined Identifier Rules C Language Elements An identifier must consist only of letters, digits, and underscores. An Identifier cannot begin with a digit. A C reserved word cannot be used as an identifier. A standard identifier should not de redefined. 35
User-Defined Identifier Rules C Language Elements Examples: letter2 21etter letter_2 int joe's variable cent_per_inch Begins with a digit Reserved word Character ’ not allowed 36
C Language Elements #include #define KMS_PER_MILE int main(void) { float miles, kms; } (Executable statements) 37