+ All Categories
Home > Documents > Senem Kumova Metin // FALL 2008-2009 CS115 Introduction to Programming Inst. Senem Kumova Metin...

Senem Kumova Metin // FALL 2008-2009 CS115 Introduction to Programming Inst. Senem Kumova Metin...

Date post: 01-Apr-2015
Category:
Upload: curtis-barbary
View: 222 times
Download: 3 times
Share this document with a friend
30
Senem Kumova Metin // FAL L 2008-2009 CS115 Introduction to Programming Inst. Senem Kumova Metin [email protected] Textbook : A Book on C, A. Kelly and I.Pohl Lecture Notes : http://homes.ieu.edu.tr/~skumova/ Office hours : TBA Office : 306
Transcript
Page 1: Senem Kumova Metin // FALL 2008-2009 CS115 Introduction to Programming Inst. Senem Kumova Metin senem.kumova@ieu.edu.tr Textbook : A Book on C, A. Kelly.

Senem Kumova Metin // FALL 2008-2009

CS115 Introduction to Programming

Inst. Senem Kumova [email protected]

Textbook : A Book on C, A. Kelly and I.Pohl

Lecture Notes : http://homes.ieu.edu.tr/~skumova/

Office hours : TBA

Office : 306

Page 2: Senem Kumova Metin // FALL 2008-2009 CS115 Introduction to Programming Inst. Senem Kumova Metin senem.kumova@ieu.edu.tr Textbook : A Book on C, A. Kelly.

Senem Kumova Metin // FALL 2008-2009

WHAT is LANGUAGE ?

Page 3: Senem Kumova Metin // FALL 2008-2009 CS115 Introduction to Programming Inst. Senem Kumova Metin senem.kumova@ieu.edu.tr Textbook : A Book on C, A. Kelly.

Senem Kumova Metin // FALL 2008-2009

WHAT is PROGRAMMING??scheduling or performing a task or / and event

WHAT is COMPUTER PROGRAMMING??creating a sequence of steps for a computer to follow in performing a task

Page 4: Senem Kumova Metin // FALL 2008-2009 CS115 Introduction to Programming Inst. Senem Kumova Metin senem.kumova@ieu.edu.tr Textbook : A Book on C, A. Kelly.

Senem Kumova Metin // FALL 2008-2009

WHAT is a PROGRAMMING LANGUAGE ?

A set of rules, symbols, and special words used to construct a computer program

Page 5: Senem Kumova Metin // FALL 2008-2009 CS115 Introduction to Programming Inst. Senem Kumova Metin senem.kumova@ieu.edu.tr Textbook : A Book on C, A. Kelly.

Senem Kumova Metin // FALL 2008-2009

Programming language rules consist of:

Rules of Syntax which specify how valid instructions are written in the language(like natural language rules subject + verb +object )

Rules of Semantics which determine the meaning of the instructions (what the computer will do)

(like natural language rules A book has bitten a car )

Page 6: Senem Kumova Metin // FALL 2008-2009 CS115 Introduction to Programming Inst. Senem Kumova Metin senem.kumova@ieu.edu.tr Textbook : A Book on C, A. Kelly.

Senem Kumova Metin // FALL 2008-2009

A COMPUTER PROGRAM ? A set of machine instructions which in turn are

represented as sequences of binary digits (0001010….111011)

The execution sequence of a group of machine instructions is known as the flow of control.

Page 7: Senem Kumova Metin // FALL 2008-2009 CS115 Introduction to Programming Inst. Senem Kumova Metin senem.kumova@ieu.edu.tr Textbook : A Book on C, A. Kelly.

Senem Kumova Metin // FALL 2008-2009

FLOW OF CONTROL

SCENARIO :

we have 2 integers : x,y

if x is greater than 0 then

do x= y+1;

else

do x= y-1;

print the value of x

int x and int y

x > 0

YES NO

x = y+1 x = y-1

print x

Page 8: Senem Kumova Metin // FALL 2008-2009 CS115 Introduction to Programming Inst. Senem Kumova Metin senem.kumova@ieu.edu.tr Textbook : A Book on C, A. Kelly.

Senem Kumova Metin // FALL 2008-2009

Will we write codes in binary ??

SCENARIO :

we have 2 integers : x,y

if x is greater than 0 then

do x= y+ 1;

else

do x= y-1;

print the value of x

000…110001

100010….01

YES NO

10…1010000 1011..0101

100..11100010

Page 9: Senem Kumova Metin // FALL 2008-2009 CS115 Introduction to Programming Inst. Senem Kumova Metin senem.kumova@ieu.edu.tr Textbook : A Book on C, A. Kelly.

Senem Kumova Metin // FALL 2008-2009

ASSEMBLY LANGUAGE Assembly language (or assembler code) was our

first attempt at producing a mechanism for writing programs that was more palatable to ourselves

; the easiest way to print "hello, world!"

name "hi"

org 100h

jmp start ; jump over string declaration

msg db "hello, world!",0Dh,0Ah, 24h

start:

lea dx, msg ; load effective address of msg into dx.

mov ah, 09h ; print function is 9.

int 21h ; do it!

mov ah, 0 int 16h ; wait for any key any....

ret ; return to operating system.

• Of course a program

written in

assembly code,

in order to “run”,

must first be

translated

(assembled) into

machine code.

Page 10: Senem Kumova Metin // FALL 2008-2009 CS115 Introduction to Programming Inst. Senem Kumova Metin senem.kumova@ieu.edu.tr Textbook : A Book on C, A. Kelly.

Senem Kumova Metin // FALL 2008-2009

HIGH LEVEL LANGUAGE

A more problem-oriented (rather than machine-oriented) mechanism for creating computer programs would also be desirable.

Hence the advent of high(er) level languages starts with the introduction of “Autocodes”, and going on to Algol, Fortran, Pascal, Basic, Ada, C, etc.

Page 11: Senem Kumova Metin // FALL 2008-2009 CS115 Introduction to Programming Inst. Senem Kumova Metin senem.kumova@ieu.edu.tr Textbook : A Book on C, A. Kelly.

Senem Kumova Metin // FALL 2008-2009

ASSEMBLY versus HIGH LEVEL LANGUAGE

; the easiest way to print "hello, world!"

name "hi"

org 100h

jmp start ; jump over string declaration

msg db "hello, world!",0Dh,0Ah, 24h

start:

lea dx, msg ; load effective address of msg into dx.

mov ah, 09h ; print function is 9.

int 21h ; do it!

mov ah, 0 int 16h ; wait for any key any....

ret ; return to operating system.

/* easiest way to

print “hello, world” */

#include<stdio.h> // library file

main()

{ printf(“hello,world”); }

HelloWorld.asm HelloWorld.c

Page 12: Senem Kumova Metin // FALL 2008-2009 CS115 Introduction to Programming Inst. Senem Kumova Metin senem.kumova@ieu.edu.tr Textbook : A Book on C, A. Kelly.

Senem Kumova Metin // FALL 2008-2009

ASSEMBLY versus HIGH LEVEL LANGUAGE

HelloWorld.asm

Source Code

HelloWorld.c

Source Code

Machine Code (binary) Machine Code (binary)

Object Codeassembled

compiled

Library

(stdio.h)linked

Page 13: Senem Kumova Metin // FALL 2008-2009 CS115 Introduction to Programming Inst. Senem Kumova Metin senem.kumova@ieu.edu.tr Textbook : A Book on C, A. Kelly.

Senem Kumova Metin // FALL 2008-2009

A typical C development environment 1/2 Phase1. Create a program

Use some editor to create your .c file (emacs, vi, pico etc.) Save the file in disk .c file is known as source code

Phase2 and 3. Preprocess and Compile Preprocessor manipulates program code (examples: includes other files,

performs text replacements) Compiler generates .o file known as the object code

Phase4. Linking Linker links the library files and creates executable file

To compile your .c file type “gcc –c myfile.c” , this will create .o file

To compile & link type “gcc –o myfile myfile.c” this will create myfile.exe file and executable myfile.exe

Page 14: Senem Kumova Metin // FALL 2008-2009 CS115 Introduction to Programming Inst. Senem Kumova Metin senem.kumova@ieu.edu.tr Textbook : A Book on C, A. Kelly.

Senem Kumova Metin // FALL 2008-2009

A typical C development environment 2/2

Phase5. Loading Before a program can be executes, it must be first

placed to memory by Loader

Phase6. Execution Under control of CPU, a program may be executed

To load and execute your .exe file type “./myfile”

Page 15: Senem Kumova Metin // FALL 2008-2009 CS115 Introduction to Programming Inst. Senem Kumova Metin senem.kumova@ieu.edu.tr Textbook : A Book on C, A. Kelly.

Senem Kumova Metin // FALL 2008-2009

Compilation with gcc compiler in Linux && Execution

Step1 : gcc –c myfile.c myfile.c + myfile.o

Step2 : gcc –o myfile myfile.c myfile.c + myfile.o +myfile.exe

Step3 : ./myfile

or

Step1 : gcc –c myfile myfile.c myfile.c +myfile.exe

Step2 : ./myfile

You may use also cc compiler instead of gcc

Page 16: Senem Kumova Metin // FALL 2008-2009 CS115 Introduction to Programming Inst. Senem Kumova Metin senem.kumova@ieu.edu.tr Textbook : A Book on C, A. Kelly.

Senem Kumova Metin // FALL 2008-2009

LIBRARIES

Libraries (in computer programming terms) contain chunks of precompiled (object) code for various functions and procedures that come with a programming language that requires compilation

For example functions and procedures to facilitate I/O.

Page 17: Senem Kumova Metin // FALL 2008-2009 CS115 Introduction to Programming Inst. Senem Kumova Metin senem.kumova@ieu.edu.tr Textbook : A Book on C, A. Kelly.

Senem Kumova Metin // FALL 2008-2009

Why C? Native language of UNIX Standard development language for

personal computers Portable (can be moved to other

machine !) Powerful set of operators and powerful

libraries (some operators: ++,--….) Basis for Java, C++…..

Page 18: Senem Kumova Metin // FALL 2008-2009 CS115 Introduction to Programming Inst. Senem Kumova Metin senem.kumova@ieu.edu.tr Textbook : A Book on C, A. Kelly.

Senem Kumova Metin // FALL 2008-2009

A SHORT BREAK !

20 min …

Page 19: Senem Kumova Metin // FALL 2008-2009 CS115 Introduction to Programming Inst. Senem Kumova Metin senem.kumova@ieu.edu.tr Textbook : A Book on C, A. Kelly.

Senem Kumova Metin // FALL 2008-2009

INTRODUCTION TO C

Your First C programs

Basic I/O functions : printf / scanfIncluding librariesWriting commentsDefining variables ….if statements

Page 20: Senem Kumova Metin // FALL 2008-2009 CS115 Introduction to Programming Inst. Senem Kumova Metin senem.kumova@ieu.edu.tr Textbook : A Book on C, A. Kelly.

Senem Kumova Metin // FALL 2008-2009

Learn printf

#include <stdio.h> // library file

void main(void)

{

printf("from sea to shining C\n");

}

Page 21: Senem Kumova Metin // FALL 2008-2009 CS115 Introduction to Programming Inst. Senem Kumova Metin senem.kumova@ieu.edu.tr Textbook : A Book on C, A. Kelly.

Senem Kumova Metin // FALL 2008-2009

Learn printf

#include <stdio.h>

void main(void)

{

printf("from sea ”);

printf(“to shining C\n");

}

Page 22: Senem Kumova Metin // FALL 2008-2009 CS115 Introduction to Programming Inst. Senem Kumova Metin senem.kumova@ieu.edu.tr Textbook : A Book on C, A. Kelly.

Senem Kumova Metin // FALL 2008-2009

Learn printf / scanf#include <stdio.h>void main(void){

int x=0;printf(“x= %d”,x); // print x = 0scanf(“%d”,&x); /* scan the value

from screen and assign this value to x */

printf(“%d”,x); }

Page 23: Senem Kumova Metin // FALL 2008-2009 CS115 Introduction to Programming Inst. Senem Kumova Metin senem.kumova@ieu.edu.tr Textbook : A Book on C, A. Kelly.

Senem Kumova Metin // FALL 2008-2009

Comments/* Ignored part by

the compiler */

// Ignored part by the compiler (only this line)

void main(){ //…..

}

Page 24: Senem Kumova Metin // FALL 2008-2009 CS115 Introduction to Programming Inst. Senem Kumova Metin senem.kumova@ieu.edu.tr Textbook : A Book on C, A. Kelly.

Senem Kumova Metin // FALL 2008-2009

Comments Comments are arbitrary strings of symbols

placed between the delimiters /* and */ Comments are not tokens but white spaces for

the C compiler

d)  /*************/      /*  a comment */    /*************/

b)    /*     * a comment     */

c)   /**********    *  a comment *    ************/

a) /* a comment */

Page 25: Senem Kumova Metin // FALL 2008-2009 CS115 Introduction to Programming Inst. Senem Kumova Metin senem.kumova@ieu.edu.tr Textbook : A Book on C, A. Kelly.

Senem Kumova Metin // FALL 2008-2009

Variables and Assignment

#include<stdio.h>void main(void){ int kurus; // declarations of variables (int is a keyword, takes integer values)int lira=0; // declaration and initialization of a variable int toplam_kurus; lira =13; // Assignment statement, “=“ is the assignment operatorkurus=56; // Assignment

printf(“ Money is %d lira %d kurus\n”, lira, kurus); // printf statementtoplam_kurus = lira*100+ kurus;printf(“ \n Total kurus is %d kurus\n”, toplam_kurus);// first part of the printf statement is a control string

}

Page 26: Senem Kumova Metin // FALL 2008-2009 CS115 Introduction to Programming Inst. Senem Kumova Metin senem.kumova@ieu.edu.tr Textbook : A Book on C, A. Kelly.

Senem Kumova Metin // FALL 2008-2009

Variables and Assignment

OUTPUT:

Money is 13 lira 56 kurus

Total kurus is 1356 kurus

Page 27: Senem Kumova Metin // FALL 2008-2009 CS115 Introduction to Programming Inst. Senem Kumova Metin senem.kumova@ieu.edu.tr Textbook : A Book on C, A. Kelly.

Senem Kumova Metin // FALL 2008-2009

The use of #define

/* Lines starting with # are called preprocessing directives Preprocessor first changes all occurences of identifier PI

to 3.14 */

#include<stdio.h>#define PI 3.14void main(void){ printf(“PI equals : %f\n”,PI);// %f is used for floating numbers}

Page 28: Senem Kumova Metin // FALL 2008-2009 CS115 Introduction to Programming Inst. Senem Kumova Metin senem.kumova@ieu.edu.tr Textbook : A Book on C, A. Kelly.

Senem Kumova Metin // FALL 2008-2009

If Statements

SCENARIO/ MISSION :

you have 2 integers : x,y

if x is greater than 0 then

do x= y+ 1;

else

do x= y-1;

print the value of x

C CODE

void main()

{ int x; int y;

if( x>0)

x=y+1;

else

x=y-1;

printf(“%d”, x); }

Page 29: Senem Kumova Metin // FALL 2008-2009 CS115 Introduction to Programming Inst. Senem Kumova Metin senem.kumova@ieu.edu.tr Textbook : A Book on C, A. Kelly.

Senem Kumova Metin // FALL 2008-2009

if statements

int mymaximum( int a , int b)

{ if(a>b)

return a;

else

return b; }

MISSION :

Write a function called mymaximum that

gets 2 integer values (as input parameters) and returns

back the greater one

Page 30: Senem Kumova Metin // FALL 2008-2009 CS115 Introduction to Programming Inst. Senem Kumova Metin senem.kumova@ieu.edu.tr Textbook : A Book on C, A. Kelly.

Senem Kumova Metin // FALL 2008-2009

Functions and if statements

#include <stdio.h>

int mymaximum(int a, int b); // FUNCTION PROTOTYPE

void main(void){ int max, x, y =7; // DECLARE 3 VARIABLES, INITIALIZE Y

printf("please give the value for x: ");scanf("%d",&x); // GET THE VALUE OF VARIABLE X

max= mymaximum(x,y); // CALL TO THE MYMAXIMUM FUNCTIONprintf("Maximum = %d",max); // PRINT THE OUTPUT}

int mymaximum( int a , int b) // DEFINE SUB-FUNCTION{ if(a>b) return a;

else return b; }


Recommended