c++ chapter 1

24
CHAPTER 1: Programming Fundamentals Fundamentals CSEB113 PRINCIPLES of PROGRAMMING 1 BS (May 2013)

Upload: meon

Post on 18-Jul-2016

8 views

Category:

Documents


0 download

DESCRIPTION

UNITEN Programming

TRANSCRIPT

CHAPTER 1: Programming FundamentalsFundamentals

CSEB113 PRINCIPLES of PROGRAMMING

1BS (May 2013)

Topics1. Computer Programs and Programming

– IntroductionIntroduction

– Why Do Engineering Undergraduates Need to Take Programming Course?

2. Programming Languages– Machine Language ; Assembly Language; High‐level 

Language 

3. The C Language– Your first C Program

– C Programming Environment

2BS (May 2013)

COMPUTER PROGRAMS AND

Topic 1

COMPUTER PROGRAMS AND PROGRAMMING

3BS (May 2013)

Elements of a Computer

1

32

Application software1

4 5software

21

system software

Hardware Software

4BS (May 2013)

Computer and Software• Computers can do a lot of exciting tasks. 

– However, computers must be given instructions in the form of computer programs before they can perform actions and make decisions. 

• Software/program – a set of instructions stored inside a computer that allows the user to do a particular thing.

O ti Mi ft W dUtilit P LOperating Systems

Windows 8

Microsoft WordUtility Programs

Disk Cleanup

Language Translation

Compiler

scanf( "%s", astring );for ( i = 0; i < 10; ++i ){

if ( astring[i] == 'a' ){

printf( "You entered an a!\n" );

}}

5BS (May 2013)

ppApplication SoftwareySystem Software*Complete this list

Embedded Systems• Computer programs that are embedded into hardware to control its operations.p– Example:

• Telecommunication systems

• Consumer electronics

• Missiles and satellite

• Smart cards

• Medical equipment (e.g., electronic stetescope) and medical imaging (e.g. MRI, CT)

• Transportation systemsTransportation systemssscanf( "%s", astring );for ( i = 0; i < 10; ++i ){

if ( astring[i] == 'a' ){

printf( "You entered an a!\n" );}

BS (May 2013) 6

}}

What is Programming?• Computer programming is:

not only about writing computer programs– not only about writing computer  programs

→the iterative process of designing, writing, testing, debugging and maintaining the source code ofdebugging, and maintaining the source code of computer programs 

• This source code is written in one or more programming languages (such as C, C++, C#, Java, Python, Smalltalk, etc.)

f h d i– often shortened to programming,                       scripting, or coding

BS (May 2013) 7

Why Should I Know Programming?• “Everybody in this country should learn how to program a computer because it teachesto program a computer … because it teaches you how to think” – Steve Jobs

W t h MWatch Me

Source: http://www youtube com/watch?v=sGyH9tBie4MSource: http://www.youtube.com/watch?v=sGyH9tBie4M

BS (May 2013) 8

Why Take Programming Course?• Programming is not only for computer science people only.

• Writing computer programs to solve complicated engineering problems and to control mechanical devices is a basic skill all engineers must master.

• Introductory programming course isn't trying to teach you to• Introductory programming course isn t trying to teach you to program so that you can be a programmer. 

• Instead, such course just want you to think like programmers j y p gbecause in the future you will run into problems that are too long and complex to do by hand. 

• In those cases you'll have to use MATLAB or Mapple or mathworks or some otherIn those cases, you ll have to use MATLAB or Mapple or mathworks or some other math related application that allows you to solve these problems. 

• Unfortunately, if you can't code your problem into the application, it won't do you any good. 

BS (May 2013) 9

Topic 2

PROGRAMMING LANGUAGES

10BS (May 2013)

What is a Programming Language?1

• Technically, it is an artificial language designed to communicate instructions to, or to control the ,behavior of a machine, particularly a computer.

• It is different from our everyday‐language (natural language ‐NL)– NL : Depends on circumstances; the context – one word 

h i d di h i i (can have many meaning depending on the situation (e.g., OPERATE)

– PL: Very specific (one word means one thing – context– PL: Very specific (one word means one thing – context free) since to 'talk' to a computer; to instruct a computer; our commands must be 100% clear and correct.

11BS (May 2013)

What is a Programming Language?2

• Conceptually, programming language is a framework within which we organize our ideasframework within which we organize our ideas about data and procedures. 

Data: 'objects' or ‘things’ we want to manipulate– Data: objects  or  things  we want to manipulate

– Procedures: 'descriptions' or 'rules'  or                      ‘processes’ that define how to manipulate dataprocesses  that define how to manipulate  data

– Example: • A simple program that determine sum of two numbersA simple program that determine sum of two numbers

– Data ?

– Procedures?

12BS (May 2013)

Types of Programming Languagesprintf (“Hello”);total = quiz + assignment;High‐Level 

L

LOAD A, 9999

printf(“Total = %d”, total);Language

Assembly  LOAD B, 8282SUB BMOV C, A

M hi

yLanguage

1101 0001 1000 0101 1000 01100100 1100 0101 1110 0101 1010

MachineLanguage

BS (May 2013) 13

1. Machine Language• The only language that is directly understandable by a 

computer’s processor.– Certainly difficult for humans to understandCertainly difficult for humans to understand

• Consists of binary codes: 0 and 1.• Example: 1101 0001 1000 0101 1000 0110 1111 0001

0100 1100 0101 1110 0101 1010 0001 1100

• is the language into which all programs must be converted before they can be runi ll hi d d i i f

0100 1100 0101 1110 0101 1010 0001 1100

• is generally machine dependent, as it varies from processor to processor (e.g.,  Intel, AMD and ARM processors)

• is NOT portable – program may sometimes have to be completely rewritten to work on 

different type of processors• Programming in machine language is very slow and tedious 

since it is hard to memorize all the instructions and mistakessince it is hard to memorize all the instructions and mistakes can happen very easily 

14BS (May 2013)

2. Assembly Language• Uses a mnemonic code to represent each machine operation

– in words and numbers LOAD A, 9999LOAD B, 8282SUB B

• An intermediary language that can be understood by humans– but is still quite difficult to use

SUB BMOV C, A

LOAD A, 9999LOAD B, 8282

0000000000010101q

• Cannot be processed directly by a computer, must be converted to machine language using assemblers

SUB BMOV C, A

000101100011010101110111

Assembler

converted to machine language using assemblers• Although easier to use than machine language, programmer is 

required to have a complete understanding of the computer hardware in order to program ithardware in order to program it.

• To do simple tasks such as printing out a message, programmer needs to write a substantial amount of code.

• Processor dependent and not portable.15BS (May 2013)

3. High-level Languages11• Use more English words so easier to program in these 

languages. Example:

h bl i d

printf (“Hello”);total = quiz + test + assignment;

• The programming structure is problem oriented – does not need to know how the computer actually executes the 

instructions.

• Processor independent ‐ the same code can be run on different processors.

P itt i th l t b t l t d i t• Programs written in these languages must be translated into executable machine language using– Compilers – translate instructions to create executable form 

– Interpreters – translate and execute instructions one after another

16BS (May 2013)

3. High-level Languages22• Example:

C PHP Ruby Ada

Java Visual Basic Visual Basic.NET MATLAB

Objective‐C Python LISP Lua

C++ Perl Pascal Bash

• All have set of rules called syntax

C# Javascript Delphi FORTRAN

– must be followed

– describe the form of a valid program and to get an accurate translation into machine language

17BS (May 2013)

Topic 3

THE C LANGUAGE

18BS (May 2013)

The C Language11• It was developed by Dennis Ritchie in the early 1970s at Bell Laboratories (now a part of Lucent ( pTechnologies, Inc).

• A high‐level language that is highly portable.

• C combines the power of high‐level languages with the power of assembly languages.

• It has influenced the newly created high‐level language name Objective‐C– which is the main programming language used by Apple for the OS X and iOS operating systems and their respective APIs Cocoa and Cocoa Touch frameworksrespective APIs, Cocoa and Cocoa Touch frameworks.

19BS (May 2013)

The C Language22• C is a highly imperative programming language. This means:– It expresses what the program should accomplish by prescribing how to do it in terms of sequences of actions to be taken.

– It was designed to be compiled using a relatively straightforward compiler.

Th l h b il bl id• The language has become available on a very wide range of platforms, from embedded microcontrollers to supercomputers.p p

20BS (May 2013)

Your First C Program• A C program (also called C source code):

/**********************************************************/Author: Badariah SolemonDate: 01/01/2013Description: A program that prints the famous

“Hello World” message on the computer screen**********************************************************/#include <stdio h>#include <stdio.h>void main (void){

printf(“****************\n”);printf(“ Hello World\n”);printf(“****************\n”);

}

• It must be translated into machine language. Once the machine language is executed the program

}

the machine language is executed, the program produces an output:

****************Hello World

****************

BS (May 2013) 21

C Programming Environment• A modern C programming often made easy by the use of 

integrated development environments (IDEs).

• IDEs that support C language include Microsoft Visual Studio, Eclipse, and etc.

• Ms Visual Studio 2010 Express or Ms Visual Studio Express• Ms Visual Studio 2010 Express or Ms Visual Studio Express 2012 is free!– Highly recommended for your personal use

– You must register to obtain a free product key for ongoing use

BS (May 2013) 22

Visual Studio C++ Express

BS (May 2013) 23

Summary• Computers must be given instructions in the form of computer programs 

before they can perform actions and make decisions.

P i i th it ti f d i i iti t ti• Programming is the iterative process of designing, writing, testing, debugging, and maintaining the source code of computer programs.

• Conceptually, programming language is a framework within which we organize our ideas about data (object we want to manipulate) andorganize our ideas about data (object we want to manipulate) and procedures (how to manipulate the data).

• In general, programming languages may be categorized as 1) machine language (also called machine code) 2) assembly language and 3) highlanguage (also called machine code), 2) assembly language, and 3) high‐level languages.

• C is a highly imperative programming language.

d f d b h f d• A modern C programming often made easy by the use of integrated development environments (IDEs).

BS (May 2013) 24