Chapter 2
Upcoming SlideShare
Loading in...5
×
 

Chapter 2

on

  • 217 views

 

Statistics

Views

Total Views
217
Views on SlideShare
217
Embed Views
0

Actions

Likes
0
Downloads
1
Comments
0

0 Embeds 0

No embeds

Accessibility

Categories

Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

Chapter 2 Chapter 2 Presentation Transcript

  • Faculty of Mechanical and ManufacturingPrepared by Alish Ahmad Al-shahabalishahmad.alshahab@gmail.com
  • ◊ Understand and implement the basic structure of computer programming.◊ Write a computer program using C programming language.◊ Convert algorithm into computer program.
  • Program Basic SyntaxDevelopment of ProgrammingEnvironment CHAPTER 2 Data Types VariableDeclarations
  • Involves translating high-level language (programminglanguage such as C,C++, Visual Basic, C#) Because computers do NOT understand high level language! 00011101 01010101 11011111
  • Edit/Write Preprocess Compile Link Load Execute
  • o Types or creates program in an editor Editor or texto Makes corrections if necessary editor is a type ofo Saves or stores program on disk program used such as C: for editing or A: etc. plain text files. E.g dev/turbo
  • Turbo C editor (free)
  • DEV C++(free)
  • SelectCompile
  • Terms DescriptionMachine language Binary number codes understood by a specific CPU.High-level Machine-independent programminglanguage language that combines algebraic expressions and English symbols.Source file File containing a program written in a high- level language; the input for compilerCompiler Software that translates a high-level language program into machine language.Linker Software that combines object files and create an executable machine language program.
  • 1 Syntax Error Error occurred during compilation normally due to syntax problem Misplaced else. Declaration syntax error Undefined symbol „_main‟ in module. Statement missing in function main()
  • 2 Logic Error
  • 3 Run-time Error
  • C preprocessor directive Program block components:main function 1. Preprocessor{ directive //Identifiers/Variables 2. Program body //C statements 3. Main function} 4. Identifiers/Variable 5. C statements 6. Comment
  •  Utility program which link files from compiler library to the program code. Must be included in the first line of a computer program. Must be started with the symbol #, otherwise syntax errors will be occurred. Two types of common preprocessor directive: #include and #define.
  • #include <header file> or #include “user defined files” Example #include <stdio.h> #include <conio.h> #include “jam.h”
  • Called from#include <stdio.h> standard library
  • Consists of built-in functions Functions contains standard instructions Function will be called and linked to program via header file Header file List of functionsstdio.h printf(), scanf(),fflush(), dllconio.h clrscr(),putch().getc().dllmath.h sqrt(),pow(), log(),dll
  •  Contain functions defined by programmer.  Developed by expert programmers. List of header file and its Header file List of user-defined function functionsutama.h cetak(),baca(),papar(),dllkira.h plus(),minus(), divide(),dll
  • Format:#define “file name” or #define constant_name constant_value Example #define MAX 100
  •  The part in which the program code will be started to execute. Consists of main function, C statements and identifiers. Use { to start the program code and } to end the program code. main function { //identifiers //C statements }
  • void main( ) { …………..}int main( ) main( ){ { return 0; return 0;} }
  • Write the most basic structures of C programming. #include <stdio.h> void main() { }
  • Instructions to be executed by computersEvery statements must be ended with semicolon Function Declaration statement statement Control Types Input/Output statement statement Compound statement
  • Statement in program code that will be ignored by compilerDiffers in terms of colour : grey To increase To program document readability a program Function As a To provide future additional referen information ces
  • Using references from any C book, finthe following concepts (definition an A) Reserved Word B) Variable C) Constant
  • Standard/special word in standard libraryContain special meaning understood by compiler Rules Case –sensitive Cannot be used as ide Must be written in small case or variables
  • Example: void intRefer to the function that will not The acronym for integer return any value case default switch break for continue float double return while if do int
  • Representing particular name in programmingStore values to be used in programmingRefers to the storage in computer
  • Standard Special built-in wordsidentifier Referred as function name which will called from C library printf() scanf() puts() gets()
  • Name given to the declaration ofUser-defined data to be used in program Refer to the storage name identifier Store data values/result/output Constant Type Variable
  • RULESIdentifiers name can only consists of name, numberand underscoreIdentifiers name cannot be started with numbersSymbol cannot be used in identifier nameCannot contains spaces between two identifiersnameIdentifiers name should be uniqueIdentifiers is not case sensitive
  • WHY? Invalid identifiers 8Century BIT 1033 Two*four ‘Sixsense’ voidWHY? WHY? WHY?
  • Name which used to store data value Refer to name of one cell in computer storage Contants value is fixedHow to give name to a Follow identifiers rules constant value?
  • Declaration format: 1const data_type const_name = const_value; ConstantReserved word const float pi = 3.142; Value
  • Declaration format: 2#define const_name const_value; #define pi 3.142; Reserved word
  • Example of constant: #define minimum 0; #define MAX 100; const int counter = 100; const char alphabet = ‘J’; const float value = 4.5;
  • Name which used to store data/input value Refer to the name of one cell in computer storage Variable’s value can be modified/changed during executionDeclaration Format: data_type variable_name;
  • Declaration Example Declaration of a variable number of integer data type. Declaration of a variable weight of floating point data type. Declaration of a variable alphabet of character data type.
  • Variable/constant declaration example //Variable and constant declration #include <stdio.h> int number; float weight; Variable declaration void main() { const float pi =3.142; Constant declaration int bilangan; float berat; Variable declaration char abjad; }
  • //Variable and constant declaration#include <stdio.h>const float pi=3.142;void main(){ int bilangan, bil, bilang; float berat, kg; char A; }
  • Method to give/assign value to variable Interactive Initialization  Assign value to variable during declaration. Input/enter data through input devices  Assign value to variable. Use input statement  Use assign symbol, “=“ (scanf/gets)
  • Assigning value to variables#include <stdio.h>void main(){ Initialize a variable int number = 10; float weight; weight = 60.00; Interactive printf(“Enter the value of number :”); scanf(“%d”,&number); number = 50.00; Initialize a variable }
  • Represents types of data can be stored in computerTypes of data to be stored and used in programming should be informed to the compiler/system Types Integer Character Floating point
  • Represents any round number with +/- values.Integer Divided into short and long integer. Reserved word for integer – int Valid until 5 places of integer number. Example:age is used to represent the age of studentsbetween 18 and 25 years old. The declarationfor thevariable is as follow: int age;
  • Floating Represents any floating point numbers +/- number Reserved word– double /float Example: height is used to represent the student’s height between 150 cm and 180 cm. The declaration for the variable is as follow: float height;
  • Character Represents character data. Reserved word – char Example: gender is used to represent the gender of a student. The declaration for the variable is as follow: char gender;
  • Determine whether the following identifiers isvalid or invalid. Give reason for invalid cases.1) Parit Raja2) 20thCentury3) int4) INTEGER5) _BMW20036) Reservedword7) BIT10338) markah_pelajar9) jam*kredit10) printf
  • Write a suitable variable declaration for eachof the following statement:i. Salary of an employeeii. Student’s mark for programming subjectiii. ATM pin numberiv. Phone number v. Price of one item vi. Bus seat number vii. Student name
  • Based on the following problem, determine theappropriatevariables can be declared:Given the value of x is 10 and a is 12,find the result of the following equation: y = 2x + a - 6
  • Based on the following problem, determine the appropriate variables can be declared:Mrs Leeya needs to determine her students grade forprogramming subject based on the mark scored duringfinal examination. The ‘A’ grade will be given if theMark scored is between 85 to 100. If a student has scored90 marks, what is the grade should Mrs Leeyagive to the student?
  • Based on the following problem, determine the appropriatevariables can be declared:A box has height, width and length.Calculate the volume of a box.
  • Based on the following problem, determine the appropriate variables can be declared:Uncle Degawan wants to buy 5 tins of paint fromCinda’s shop. The price of each tin of the paint isRM 15.60. Calculate the price which Uncle Degawanhaveto pay for all the tin of paints he bought.