summer school - facweb.iitkgp.ac.ingoutam/sss/lect/lect1.pdf · machine language program a nite...

109
Summer School Computer Sc & Engg: IIT Kharagpur 1 Summer School on Introduction to Programming through C and OCAML Lect 1 Goutam Biswas

Upload: others

Post on 15-Aug-2020

3 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Summer School - facweb.iitkgp.ac.ingoutam/sss/lect/lect1.pdf · Machine Language Program A nite sequence of machine instructions is called a machine language program. It is stored

Summer School Computer Sc & Engg: IIT Kharagpur 1'

&

$

%

Summer School

�� ��on

�� ��Introduction to Programming

�� ��through

�� ��C and OCAML

Lect 1 Goutam Biswas

Page 2: Summer School - facweb.iitkgp.ac.ingoutam/sss/lect/lect1.pdf · Machine Language Program A nite sequence of machine instructions is called a machine language program. It is stored

Summer School Computer Sc & Engg: IIT Kharagpur 2'

&

$

%

�� ��Instructors

Goutam Biswas, Naveen Kumar Sharma, Sibasish

Dey, Shambhobi Bhattacharya, Arpita Routray�� ��Technical Support

Dr. D Roy, Alokesh Chattopadhyay, Mithun

Sarkar, B. P. Bandoghati, Dinanath Ghosh,

Debdash Bandyopadhyay, & Shibabroto Banerjee

Lect 1 Goutam Biswas

Page 3: Summer School - facweb.iitkgp.ac.ingoutam/sss/lect/lect1.pdf · Machine Language Program A nite sequence of machine instructions is called a machine language program. It is stored

Summer School Computer Sc & Engg: IIT Kharagpur 3'

&

$

%

Computer System: An Overview

Lect 1 Goutam Biswas

Page 4: Summer School - facweb.iitkgp.ac.ingoutam/sss/lect/lect1.pdf · Machine Language Program A nite sequence of machine instructions is called a machine language program. It is stored

Summer School Computer Sc & Engg: IIT Kharagpur 4'

&

$

%

VDU

Keyboard

ProcessorMemoryDisk etc.

Mouse

Network

Lect 1 Goutam Biswas

Page 5: Summer School - facweb.iitkgp.ac.ingoutam/sss/lect/lect1.pdf · Machine Language Program A nite sequence of machine instructions is called a machine language program. It is stored

Summer School Computer Sc & Engg: IIT Kharagpur 5'

&

$

%

Primergy� �� �� �� �� �� �� �� �

� �� �� �� �� �� �� �

� � � � � � � � � � � � � � � � �

� � � � � � � � � � � � � � � � �

� � � � � � � � � � � � � � � �

� � � � � � � � � � � � � � � �� � � � � �

� � � � � �

� � � � � �

� � � � � �

� � � � � �

� � � � � �

Lect 1 Goutam Biswas

Page 6: Summer School - facweb.iitkgp.ac.ingoutam/sss/lect/lect1.pdf · Machine Language Program A nite sequence of machine instructions is called a machine language program. It is stored

Summer School Computer Sc & Engg: IIT Kharagpur 6'

&

$

%

CPU−Memory Bus

MMUDMA etc.

IO Bus

IO Controller

Disk II

IO Controller

IO Controller

CPU

Graphics VDU

Main Memory

Cache Memory

Disk I

Mouse

Network

Lect 1 Goutam Biswas

Page 7: Summer School - facweb.iitkgp.ac.ingoutam/sss/lect/lect1.pdf · Machine Language Program A nite sequence of machine instructions is called a machine language program. It is stored

Summer School Computer Sc & Engg: IIT Kharagpur 7'

&

$

%

Types of Digital Computers

• General-purpose computers.

• Special-purpose computers - in washing

machine, refrigerator, music system, car,

mobile phone, camera, toy, instrument and

there is no end.

Lect 1 Goutam Biswas

Page 8: Summer School - facweb.iitkgp.ac.ingoutam/sss/lect/lect1.pdf · Machine Language Program A nite sequence of machine instructions is called a machine language program. It is stored

Summer School Computer Sc & Engg: IIT Kharagpur 8'

&

$

%

Stored Program Computer

• A computer is used to process data.

• Computer cannot do anything on its own.

• It is given a sequence of instructions, called

a program, to process any data.

• Both program and data are stored in the

computer memory.

Lect 1 Goutam Biswas

Page 9: Summer School - facweb.iitkgp.ac.ingoutam/sss/lect/lect1.pdf · Machine Language Program A nite sequence of machine instructions is called a machine language program. It is stored

Summer School Computer Sc & Engg: IIT Kharagpur 9'

&

$

%

Computer Models

von Neumann Model: A computer model whereboth data and program are stored in the samememory is called von Neumann architecturea

Harvard Model: Data and program are storedin different memory in the Harvardarchitectureb

aThe first draft of a report on the EDVAC at the Moore School of Electrical

Engineering, by John von Neuman, and the design of ENIAC by J. P. Eckert

and John W. Mauchly at the University of Pennsylvania.bHarvard Mark I electromechanical computer stored instructions on punched

tape and data in electro-mechanical counters.

Lect 1 Goutam Biswas

Page 10: Summer School - facweb.iitkgp.ac.ingoutam/sss/lect/lect1.pdf · Machine Language Program A nite sequence of machine instructions is called a machine language program. It is stored

Summer School Computer Sc & Engg: IIT Kharagpur 10'

&

$

%

CPU Instruction Set

The set of instructions that the CPU of acomputer understands is called its machineinstructions.

Lect 1 Goutam Biswas

Page 11: Summer School - facweb.iitkgp.ac.ingoutam/sss/lect/lect1.pdf · Machine Language Program A nite sequence of machine instructions is called a machine language program. It is stored

Summer School Computer Sc & Engg: IIT Kharagpur 11'

&

$

%

Machine Language Program

A finite sequence of machine instructions iscalled a machine language program. It is storedin the main memory for execution.

Lect 1 Goutam Biswas

Page 12: Summer School - facweb.iitkgp.ac.ingoutam/sss/lect/lect1.pdf · Machine Language Program A nite sequence of machine instructions is called a machine language program. It is stored

Summer School Computer Sc & Engg: IIT Kharagpur 12'

&

$

%

Memory Location & Address

Program and data while execution are stored inthe main memory.Memory is divided into equal size blocksa calledmemory locations.Each memory location has a unique address.The CPU access a location by sending itsaddress to the memory subsystem.

aTypical size of each block is 1, 2, 4, or 8 bytes. One byte consists of eight

binary digit (8-bit).

Lect 1 Goutam Biswas

Page 13: Summer School - facweb.iitkgp.ac.ingoutam/sss/lect/lect1.pdf · Machine Language Program A nite sequence of machine instructions is called a machine language program. It is stored

Summer School Computer Sc & Engg: IIT Kharagpur 13'

&

$

%

Program Execution

Fetch: The CPU fetches the next instructionfrom the memory and decodes (analyzes) it.

Execute: Action is taken according to therequirement of the instruction. It may benecessary to fetch data from the memory, or todo some arithmetic operation etc.

Continuation of this cycle is called programexecution.

Lect 1 Goutam Biswas

Page 14: Summer School - facweb.iitkgp.ac.ingoutam/sss/lect/lect1.pdf · Machine Language Program A nite sequence of machine instructions is called a machine language program. It is stored

Summer School Computer Sc & Engg: IIT Kharagpur 14'

&

$

%

Central Processing Unit (CPU)

Lect 1 Goutam Biswas

Page 15: Summer School - facweb.iitkgp.ac.ingoutam/sss/lect/lect1.pdf · Machine Language Program A nite sequence of machine instructions is called a machine language program. It is stored

Summer School Computer Sc & Engg: IIT Kharagpur 15'

&

$

%Other Registers and Latches

Instruction Set

GPR File FPR File

Controller

FPUALU

PC IR PSW SP etc.

Lect 1 Goutam Biswas

Page 16: Summer School - facweb.iitkgp.ac.ingoutam/sss/lect/lect1.pdf · Machine Language Program A nite sequence of machine instructions is called a machine language program. It is stored

Summer School Computer Sc & Engg: IIT Kharagpur 16'

&

$

%

Central Processing Unit (CPU)

• GPRs: general purpose registers

• FPRs: floating-point registers

• PC or IP: program counter or instruction

pointer

• PSW: program status word

• IR: instruction register

Lect 1 Goutam Biswas

Page 17: Summer School - facweb.iitkgp.ac.ingoutam/sss/lect/lect1.pdf · Machine Language Program A nite sequence of machine instructions is called a machine language program. It is stored

Summer School Computer Sc & Engg: IIT Kharagpur 17'

&

$

%

Program and Data in the Main Memory

OtherProgram

OtherData

CPU

Data Bus

Address Bus

CPU−Memory Bus

Main Memory

MyData

MyProgram

OS

Lect 1 Goutam Biswas

Page 18: Summer School - facweb.iitkgp.ac.ingoutam/sss/lect/lect1.pdf · Machine Language Program A nite sequence of machine instructions is called a machine language program. It is stored

Summer School Computer Sc & Engg: IIT Kharagpur 18'

&

$

%

Program Execution

Lect 1 Goutam Biswas

Page 19: Summer School - facweb.iitkgp.ac.ingoutam/sss/lect/lect1.pdf · Machine Language Program A nite sequence of machine instructions is called a machine language program. It is stored

Summer School Computer Sc & Engg: IIT Kharagpur 19'

&

$

%

Instruction Fetch

The address of the next instruction is sent onthe address bus from a special register calledthe program counter (PC) or the instructionpointer (IP) of the CPU.

Lect 1 Goutam Biswas

Page 20: Summer School - facweb.iitkgp.ac.ingoutam/sss/lect/lect1.pdf · Machine Language Program A nite sequence of machine instructions is called a machine language program. It is stored

Summer School Computer Sc & Engg: IIT Kharagpur 20'

&

$

%

Instruction Fetch

The content of the addressed location(instruction in this case) is read by the memorysubsystem and sent to the CPU on the databus.The CPU saves the instruction in a specialregister called instruction register (IR).

Lect 1 Goutam Biswas

Page 21: Summer School - facweb.iitkgp.ac.ingoutam/sss/lect/lect1.pdf · Machine Language Program A nite sequence of machine instructions is called a machine language program. It is stored

Summer School Computer Sc & Engg: IIT Kharagpur 21'

&

$

%

Low Memory Address

CPU

PC

IR

Instruction Address

Instruction

Inst IInst II

Inst IIIInst IV

Inst XXXII

MyP

rogr

am

MyData

Main memory

Lect 1 Goutam Biswas

Page 22: Summer School - facweb.iitkgp.ac.ingoutam/sss/lect/lect1.pdf · Machine Language Program A nite sequence of machine instructions is called a machine language program. It is stored

Summer School Computer Sc & Engg: IIT Kharagpur 22'

&

$

%

Instruction Decoding & Execution

Once the instruction is available in theinstruction register (IR), the CPU hardware(control circuit) decodes it, and takes asequence of actions. The action sequencedepends on the instruction.

Lect 1 Goutam Biswas

Page 23: Summer School - facweb.iitkgp.ac.ingoutam/sss/lect/lect1.pdf · Machine Language Program A nite sequence of machine instructions is called a machine language program. It is stored

Summer School Computer Sc & Engg: IIT Kharagpur 23'

&

$

%

Data Fetch

If the instruction requires data from thememory, the CPU fetches it by sending theaddress of the dataa on the address bus.Memory subsystem sends the data on the databus.

aData address is already available in the CPU either as a part of the instruc-

tion (IR) or in some other CPU register.

Lect 1 Goutam Biswas

Page 24: Summer School - facweb.iitkgp.ac.ingoutam/sss/lect/lect1.pdf · Machine Language Program A nite sequence of machine instructions is called a machine language program. It is stored

Summer School Computer Sc & Engg: IIT Kharagpur 24'

&

$

%

Data Fetch

The CPU may save the data in one of itsinternal registers and use it for the requiredoperation.

Lect 1 Goutam Biswas

Page 25: Summer School - facweb.iitkgp.ac.ingoutam/sss/lect/lect1.pdf · Machine Language Program A nite sequence of machine instructions is called a machine language program. It is stored

Summer School Computer Sc & Engg: IIT Kharagpur 25'

&

$

%

Inst XXXII

CPU

DA

R Data Address

Data

Main memory

MyP

rogr

amM

yDat

aDR

Lect 1 Goutam Biswas

Page 26: Summer School - facweb.iitkgp.ac.ingoutam/sss/lect/lect1.pdf · Machine Language Program A nite sequence of machine instructions is called a machine language program. It is stored

Summer School Computer Sc & Engg: IIT Kharagpur 26'

&

$

%

Result of an Instruction

The result of an instruction execution may be amodified data that may be stored in an internalregister of the CPU or in the memory.

Lect 1 Goutam Biswas

Page 27: Summer School - facweb.iitkgp.ac.ingoutam/sss/lect/lect1.pdf · Machine Language Program A nite sequence of machine instructions is called a machine language program. It is stored

Summer School Computer Sc & Engg: IIT Kharagpur 27'

&

$

%

Memory Write

The CPU sends the address of the memorylocation and the data to write, on the addressand the data buses respectively. It also sends asignal for memory-write.

Lect 1 Goutam Biswas

Page 28: Summer School - facweb.iitkgp.ac.ingoutam/sss/lect/lect1.pdf · Machine Language Program A nite sequence of machine instructions is called a machine language program. It is stored

Summer School Computer Sc & Engg: IIT Kharagpur 28'

&

$

%

With a Grain of Salt

�� ��The actual process is more complex

Lect 1 Goutam Biswas

Page 29: Summer School - facweb.iitkgp.ac.ingoutam/sss/lect/lect1.pdf · Machine Language Program A nite sequence of machine instructions is called a machine language program. It is stored

Summer School Computer Sc & Engg: IIT Kharagpur 29'

&

$

%

A Machine Instruction of Pentium

�� ��1000 0011 1110 1100 0000 1000

There is an internal register called esp in the

Pentium CPUa, that can store an integer value.

This instruction subtracts 8 from the data

stored in esp. �� ��esp ← esp - 8

aIt is a CPU and some more thing fabricated on silicon by Intel.

Lect 1 Goutam Biswas

Page 30: Summer School - facweb.iitkgp.ac.ingoutam/sss/lect/lect1.pdf · Machine Language Program A nite sequence of machine instructions is called a machine language program. It is stored

Summer School Computer Sc & Engg: IIT Kharagpur 30'

&

$

%

Binary World

World inside a computer is mostly binary - itsalphabet has only two symbols 0 and 1.

But a language in binary may cause nightmareto human being.

Lect 1 Goutam Biswas

Page 31: Summer School - facweb.iitkgp.ac.ingoutam/sss/lect/lect1.pdf · Machine Language Program A nite sequence of machine instructions is called a machine language program. It is stored

Summer School Computer Sc & Engg: IIT Kharagpur 31'

&

$

%

The Charge Of The Light Brigade

“Theirs not to make reply, Theirs not to reasonwhy, Theirs but to do & die”

The same text in binary inside a computerlooks as follows

01010100 01101000 01100101 0110100101110010 · · · · · · 00100000 01100100 0110100101100101 00001010

Lect 1 Goutam Biswas

Page 32: Summer School - facweb.iitkgp.ac.ingoutam/sss/lect/lect1.pdf · Machine Language Program A nite sequence of machine instructions is called a machine language program. It is stored

Summer School Computer Sc & Engg: IIT Kharagpur 32'

&

$

%

Assembly Language Instruction

Machine language instructions are given a

human understandable symbolic representations

called assembly language instructions.�� ��1000 0011 1110 1100 0000 1000 ⇒ sub 8, esp

Lect 1 Goutam Biswas

Page 33: Summer School - facweb.iitkgp.ac.ingoutam/sss/lect/lect1.pdf · Machine Language Program A nite sequence of machine instructions is called a machine language program. It is stored

Summer School Computer Sc & Engg: IIT Kharagpur 33'

&

$

%

Assembly Language Program

A sequence of assembly language instructionsforms an assembly language program.

Lect 1 Goutam Biswas

Page 34: Summer School - facweb.iitkgp.ac.ingoutam/sss/lect/lect1.pdf · Machine Language Program A nite sequence of machine instructions is called a machine language program. It is stored

Summer School Computer Sc & Engg: IIT Kharagpur 34'

&

$

%

Assembly Language to Machine Language

• The CPU does not understand assembly

language instructions.

• A program is written to translate an

assembly language program to the equivalent

machine language program.

• This program is known as assembler.

Lect 1 Goutam Biswas

Page 35: Summer School - facweb.iitkgp.ac.ingoutam/sss/lect/lect1.pdf · Machine Language Program A nite sequence of machine instructions is called a machine language program. It is stored

Summer School Computer Sc & Engg: IIT Kharagpur 35'

&

$

%

High-Level Languages

It is also tedious to write “big programs” inassembly language.

Moreover an assembly language programcompletely depends on a particular CPU andthe computing environment.

Lect 1 Goutam Biswas

Page 36: Summer School - facweb.iitkgp.ac.ingoutam/sss/lect/lect1.pdf · Machine Language Program A nite sequence of machine instructions is called a machine language program. It is stored

Summer School Computer Sc & Engg: IIT Kharagpur 36'

&

$

%

High-Level Languages

More human understandable programminglanguages were designed for the ease of writingprogram and also to make program moresystem independent. Following are the namesof a few such languages:Fortran, Basic, Lisp, Cobol, Algol, PL/I,Pascal, SML, Prolog, C, C++, Java, Haskell,OCAML, Ada, C#, Perl, Python, etc.

Lect 1 Goutam Biswas

Page 37: Summer School - facweb.iitkgp.ac.ingoutam/sss/lect/lect1.pdf · Machine Language Program A nite sequence of machine instructions is called a machine language program. It is stored

Summer School Computer Sc & Engg: IIT Kharagpur 37'

&

$

%

Translation

A program in a high-level language cannotdirectly control the CPU. It is translated to anassembly language or a machine languageprogram.

Lect 1 Goutam Biswas

Page 38: Summer School - facweb.iitkgp.ac.ingoutam/sss/lect/lect1.pdf · Machine Language Program A nite sequence of machine instructions is called a machine language program. It is stored

Summer School Computer Sc & Engg: IIT Kharagpur 38'

&

$

%

Translation

Two different types of translation schemes aregenerally used. One is called compilation andthe other is called interpretation.The software that translates a high-levelprogram to a machine language program iscalled a compiler. In an interpreter thetranslation and the execution goeshand-in-handa.

aSome high-level languages are more suitable for compilation and some other

languages are suitable for interpretation.

Lect 1 Goutam Biswas

Page 39: Summer School - facweb.iitkgp.ac.ingoutam/sss/lect/lect1.pdf · Machine Language Program A nite sequence of machine instructions is called a machine language program. It is stored

Summer School Computer Sc & Engg: IIT Kharagpur 39'

&

$

%

Compiler: A Software Translator

HLL Program Machine Program

} {Translation Software

Lect 1 Goutam Biswas

Page 40: Summer School - facweb.iitkgp.ac.ingoutam/sss/lect/lect1.pdf · Machine Language Program A nite sequence of machine instructions is called a machine language program. It is stored

Summer School Computer Sc & Engg: IIT Kharagpur 40'

&

$

%

Compiler: A Software Translator(cont.)

OtherProgramCPU

Data Bus

Address Bus

CPU−Memory Bus

Main Memory

OS

Compiler

Mach. Prog

HLL Prog

Lect 1 Goutam Biswas

Page 41: Summer School - facweb.iitkgp.ac.ingoutam/sss/lect/lect1.pdf · Machine Language Program A nite sequence of machine instructions is called a machine language program. It is stored

Summer School Computer Sc & Engg: IIT Kharagpur 41'

&

$

%

Interpreter: A Software Translator

HLL Program

Translation

Execution Software

and

Lect 1 Goutam Biswas

Page 42: Summer School - facweb.iitkgp.ac.ingoutam/sss/lect/lect1.pdf · Machine Language Program A nite sequence of machine instructions is called a machine language program. It is stored

Summer School Computer Sc & Engg: IIT Kharagpur 42'

&

$

%

Note

We shall see how a compiler translates a Cprogram and how an OCAML program isinterpreted.

Lect 1 Goutam Biswas

Page 43: Summer School - facweb.iitkgp.ac.ingoutam/sss/lect/lect1.pdf · Machine Language Program A nite sequence of machine instructions is called a machine language program. It is stored

Summer School Computer Sc & Engg: IIT Kharagpur 43'

&

$

%

Writing and Storing a HLL Program

It is necessary to get some facility to write andstore a high-level language or an assemblylanguage program in a computer. This isprovided by a software called an editor e.g. vi,emacs etc.

Lect 1 Goutam Biswas

Page 44: Summer School - facweb.iitkgp.ac.ingoutam/sss/lect/lect1.pdf · Machine Language Program A nite sequence of machine instructions is called a machine language program. It is stored

Summer School Computer Sc & Engg: IIT Kharagpur 44'

&

$

%

Writing and Storing a HLL Program

These softwares provide facility to edit a textand store it in the hard disk as a named objectcalled a file.

Lect 1 Goutam Biswas

Page 45: Summer School - facweb.iitkgp.ac.ingoutam/sss/lect/lect1.pdf · Machine Language Program A nite sequence of machine instructions is called a machine language program. It is stored

Summer School Computer Sc & Engg: IIT Kharagpur 45'

&

$

%

int main()

KeyBoard

OS

Editor

HLL Prog

VDU

CPU

Address Bus

Data Bus

Main Memory

Disk

{

first.c

Lect 1 Goutam Biswas

Page 46: Summer School - facweb.iitkgp.ac.ingoutam/sss/lect/lect1.pdf · Machine Language Program A nite sequence of machine instructions is called a machine language program. It is stored

Summer School Computer Sc & Engg: IIT Kharagpur 46'

&

$

%

Operating System

A computer system is very difficult to use unlessa master program called an operating system(OS) is running on it to make it user friendly.It provides a better view of the availableresources and it also manages resource.

Lect 1 Goutam Biswas

Page 47: Summer School - facweb.iitkgp.ac.ingoutam/sss/lect/lect1.pdf · Machine Language Program A nite sequence of machine instructions is called a machine language program. It is stored

Summer School Computer Sc & Engg: IIT Kharagpur 47'

&

$

%

Operating System

We shall work on an OS called Linuxa.aThe initial version of this OS was designed by Linus Torvalds in 1991. But

in the current versions there is large contribution from GNU started by Richard

Stallman in 1984 and may well be called GNU/Linux.

Lect 1 Goutam Biswas

Page 48: Summer School - facweb.iitkgp.ac.ingoutam/sss/lect/lect1.pdf · Machine Language Program A nite sequence of machine instructions is called a machine language program. It is stored

Summer School Computer Sc & Engg: IIT Kharagpur 48'

&

$

%

Command Interpreter

There are other system softwares (utilities) thatare essential for the use of a computer system.One most visible and important software is thecommand interpreter e.g. bash. We shall talkmore about it afterward.

Lect 1 Goutam Biswas

Page 49: Summer School - facweb.iitkgp.ac.ingoutam/sss/lect/lect1.pdf · Machine Language Program A nite sequence of machine instructions is called a machine language program. It is stored

Summer School Computer Sc & Engg: IIT Kharagpur 49'

&

$

%

Our C Programming Environment

• PC with Intel Pentium IV CPU(+ MMU +

cache).

• Operating System, Linux (redhat).

• Command interpreter, bash (Bourne-again

shell).

• Editor, emacs or vi.

Lect 1 Goutam Biswas

Page 50: Summer School - facweb.iitkgp.ac.ingoutam/sss/lect/lect1.pdf · Machine Language Program A nite sequence of machine instructions is called a machine language program. It is stored

Summer School Computer Sc & Engg: IIT Kharagpur 50'

&

$

%

Our C Programming Environment

• Compiler, GNU gcc for C languagea.

• Assembler, as.

• Linker (link-editor), ld.

agcc is actually a C++ compiler.

Lect 1 Goutam Biswas

Page 51: Summer School - facweb.iitkgp.ac.ingoutam/sss/lect/lect1.pdf · Machine Language Program A nite sequence of machine instructions is called a machine language program. It is stored

Summer School Computer Sc & Engg: IIT Kharagpur 51'

&

$

%

Our OCAML Programming Environment

• OCAML (Objective Caml) is a language

designed by INRIA (Institut National de

Recherche en Informatique et Automatique),

France.

• Interpreter, OCAML or CAML Light.

• Editor, emacs or vi.

Lect 1 Goutam Biswas

Page 52: Summer School - facweb.iitkgp.ac.ingoutam/sss/lect/lect1.pdf · Machine Language Program A nite sequence of machine instructions is called a machine language program. It is stored

Summer School Computer Sc & Engg: IIT Kharagpur 52'

&

$

%

First C Program

#include <stdio.h>

int main()

{

printf("The First C Program\n");

return 0;

} // first.c

Lect 1 Goutam Biswas

Page 53: Summer School - facweb.iitkgp.ac.ingoutam/sss/lect/lect1.pdf · Machine Language Program A nite sequence of machine instructions is called a machine language program. It is stored

Summer School Computer Sc & Engg: IIT Kharagpur 53'

&

$

%

Write, Compile and Execute on Linux OS

1. Use an editor e.g. ‘vi’ or ‘emacs’.

• Run (execute) the editor program.

• Key-in the C program text.

• Save the program as a named file e.g.

‘first.c’.

2. Compile the C program to the executable file

‘a.out’.

Lect 1 Goutam Biswas

Page 54: Summer School - facweb.iitkgp.ac.ingoutam/sss/lect/lect1.pdf · Machine Language Program A nite sequence of machine instructions is called a machine language program. It is stored

Summer School Computer Sc & Engg: IIT Kharagpur 54'

&

$

%

3. If there is an error, go back to the editor and

fix it; otherwise run the ‘a.out’ file and get

the output.

Lect 1 Goutam Biswas

Page 55: Summer School - facweb.iitkgp.ac.ingoutam/sss/lect/lect1.pdf · Machine Language Program A nite sequence of machine instructions is called a machine language program. It is stored

Summer School Computer Sc & Engg: IIT Kharagpur 55'

&

$

%

Typical Commands Are

• $ emacs first.c &

• $ cc -Wall first.c

• $ ./a.out

My first program

Lect 1 Goutam Biswas

Page 56: Summer School - facweb.iitkgp.ac.ingoutam/sss/lect/lect1.pdf · Machine Language Program A nite sequence of machine instructions is called a machine language program. It is stored

Summer School Computer Sc & Engg: IIT Kharagpur 56'

&

$

%

Note

• The compiler creates the executable filea

‘a.out’ (assembler output) from the C

program file ‘first.c’.

aIt contains the machine code and some other data.

Lect 1 Goutam Biswas

Page 57: Summer School - facweb.iitkgp.ac.ingoutam/sss/lect/lect1.pdf · Machine Language Program A nite sequence of machine instructions is called a machine language program. It is stored

Summer School Computer Sc & Engg: IIT Kharagpur 57'

&

$

%

OCAML Interpreter

$ ocaml

Objective Caml version 3.10.1

# #quit;;$

Lect 1 Goutam Biswas

Page 58: Summer School - facweb.iitkgp.ac.ingoutam/sss/lect/lect1.pdf · Machine Language Program A nite sequence of machine instructions is called a machine language program. It is stored

Summer School Computer Sc & Engg: IIT Kharagpur 58'

&

$

%

First OCAML Program

$ ocaml

Objective Caml version 3.10.1

#print_string("My first OCAML program\n");;My first OCAML program- : unit = ()#

Lect 1 Goutam Biswas

Page 59: Summer School - facweb.iitkgp.ac.ingoutam/sss/lect/lect1.pdf · Machine Language Program A nite sequence of machine instructions is called a machine language program. It is stored

Summer School Computer Sc & Engg: IIT Kharagpur 59'

&

$

%

Example - II

Write a C program that reads a positive integer

n and finds the sum 0 + 1 + · · ·+ n = n(n+1)2 .

Lect 1 Goutam Biswas

Page 60: Summer School - facweb.iitkgp.ac.ingoutam/sss/lect/lect1.pdf · Machine Language Program A nite sequence of machine instructions is called a machine language program. It is stored

Summer School Computer Sc & Engg: IIT Kharagpur 60'

&

$

%

#include <stdio.h>

int main()

{

int n;

printf("Enter a +ve integer: ");

scanf("%d", &n);

if(n < 0)

printf("Incorrect data: %d\n", n);

else printf("0+ ... +%d = %d\n",n,n*(n+1)/2);

return 0;

} // second.c

Lect 1 Goutam Biswas

Page 61: Summer School - facweb.iitkgp.ac.ingoutam/sss/lect/lect1.pdf · Machine Language Program A nite sequence of machine instructions is called a machine language program. It is stored

Summer School Computer Sc & Engg: IIT Kharagpur 61'

&

$

%

Example - IIa

Write an OCAML program that takes n as aparameter and computes the sum

0 + 1 + · · ·+ n = n(n+1)2 .

Lect 1 Goutam Biswas

Page 62: Summer School - facweb.iitkgp.ac.ingoutam/sss/lect/lect1.pdf · Machine Language Program A nite sequence of machine instructions is called a machine language program. It is stored

Summer School Computer Sc & Engg: IIT Kharagpur 62'

&

$

%

$ ocaml

Objective Caml version 3.10.1

# let sumN = function n -> n*(n+1)/2;;

val sumN : int -> int = <fun>

# sumN 10;;

- : int = 55

# sumN (-10);;

- : int = 45

#

Lect 1 Goutam Biswas

Page 63: Summer School - facweb.iitkgp.ac.ingoutam/sss/lect/lect1.pdf · Machine Language Program A nite sequence of machine instructions is called a machine language program. It is stored

Summer School Computer Sc & Engg: IIT Kharagpur 63'

&

$

%

Laboratory Session

Lect 1 Goutam Biswas

Page 64: Summer School - facweb.iitkgp.ac.ingoutam/sss/lect/lect1.pdf · Machine Language Program A nite sequence of machine instructions is called a machine language program. It is stored

Summer School Computer Sc & Engg: IIT Kharagpur 64'

&

$

%

Let’s Start At The Very Beginning

• How to Start a Computer?

• How to write a C and OCAML Program?

• How to Compile a C Program to an

Executable Module?

• How to Run (Create a Process with) an

executable Module?

Lect 1 Goutam Biswas

Page 65: Summer School - facweb.iitkgp.ac.ingoutam/sss/lect/lect1.pdf · Machine Language Program A nite sequence of machine instructions is called a machine language program. It is stored

Summer School Computer Sc & Engg: IIT Kharagpur 65'

&

$

%

How to Start & Stop the Machine

Lect 1 Goutam Biswas

Page 66: Summer School - facweb.iitkgp.ac.ingoutam/sss/lect/lect1.pdf · Machine Language Program A nite sequence of machine instructions is called a machine language program. It is stored

Summer School Computer Sc & Engg: IIT Kharagpur 66'

&

$

%

1. Power On the machine (main cabinet and

VDU).

2. Select the Linux option.

3. Enter the login name and then the password.

The login name is user and the password is

user123.

4. Open one or two windows on the VDU (use

the icon).

Lect 1 Goutam Biswas

Page 67: Summer School - facweb.iitkgp.ac.ingoutam/sss/lect/lect1.pdf · Machine Language Program A nite sequence of machine instructions is called a machine language program. It is stored

Summer School Computer Sc & Engg: IIT Kharagpur 67'

&

$

%

1. One window is active at a time - use the

mouse to activate an window.

2. A window may be closed by pressing Ctrl-D.

Lect 1 Goutam Biswas

Page 68: Summer School - facweb.iitkgp.ac.ingoutam/sss/lect/lect1.pdf · Machine Language Program A nite sequence of machine instructions is called a machine language program. It is stored

Summer School Computer Sc & Engg: IIT Kharagpur 68'

&

$

%

Power-off the Machine

1. Close all windows.

2. logout from the machine (use the icon).

3. Shutdown the system.

4. Power-off the VDU (power of the main

cabinet will be put-off automatically).

Lect 1 Goutam Biswas

Page 69: Summer School - facweb.iitkgp.ac.ingoutam/sss/lect/lect1.pdf · Machine Language Program A nite sequence of machine instructions is called a machine language program. It is stored

Summer School Computer Sc & Engg: IIT Kharagpur 69'

&

$

%

How to Start the Emacs Editor

• Select an window and enter the command

emacs & .

• An emacs window is created.

• You may select the font size.

• Close the emacs window selecting the Exit

emacs entry of the File menu.

Lect 1 Goutam Biswas

Page 70: Summer School - facweb.iitkgp.ac.ingoutam/sss/lect/lect1.pdf · Machine Language Program A nite sequence of machine instructions is called a machine language program. It is stored

Summer School Computer Sc & Engg: IIT Kharagpur 70'

&

$

%

Write, Compile and Execute a C Program

1. Start the emacs editor: emacs &

2. Select Open File of the File menu.

3. Give a file name: first.c (at the bottom of

the emacs window).

4. Type the following C Program and finally

save the buffer as a file first.c : Save Buffer

of File menu.

Lect 1 Goutam Biswas

Page 71: Summer School - facweb.iitkgp.ac.ingoutam/sss/lect/lect1.pdf · Machine Language Program A nite sequence of machine instructions is called a machine language program. It is stored

Summer School Computer Sc & Engg: IIT Kharagpur 71'

&

$

%

Starting OCAML Interpreter

1. $ ocaml

2. The interpreter comes up with the following

prompt:

Objective Caml version 3.10.1

#

Lect 1 Goutam Biswas

Page 72: Summer School - facweb.iitkgp.ac.ingoutam/sss/lect/lect1.pdf · Machine Language Program A nite sequence of machine instructions is called a machine language program. It is stored

Summer School Computer Sc & Engg: IIT Kharagpur 72'

&

$

%

Activity - I

Enter the first program (first.c), compile andexecute.Enter the corresponding OCAML program inthe interpreter.

Lect 1 Goutam Biswas

Page 73: Summer School - facweb.iitkgp.ac.ingoutam/sss/lect/lect1.pdf · Machine Language Program A nite sequence of machine instructions is called a machine language program. It is stored

Summer School Computer Sc & Engg: IIT Kharagpur 73'

&

$

%

If There is an Error

#include <stdio.h>

int main()

// Syntax error

printf("The First C Program\n");

return 0;

} // first.c

Lect 1 Goutam Biswas

Page 74: Summer School - facweb.iitkgp.ac.ingoutam/sss/lect/lect1.pdf · Machine Language Program A nite sequence of machine instructions is called a machine language program. It is stored

Summer School Computer Sc & Engg: IIT Kharagpur 74'

&

$

%

Compiler Message

$ cc -Wall first.cfirst.c:4: error: syntax error before"printf"first.c:4: warning: type defaults to‘int’ .... ‘printf’first.c:4: error: conflicting typesfor ’printf’.............

Lect 1 Goutam Biswas

Page 75: Summer School - facweb.iitkgp.ac.ingoutam/sss/lect/lect1.pdf · Machine Language Program A nite sequence of machine instructions is called a machine language program. It is stored

Summer School Computer Sc & Engg: IIT Kharagpur 75'

&

$

%

What to do?

1. Open the file first.c in emacs.

2. Identify and fix the errors.

3. Save the modified file (same name).

4. Compile it again.

Any time you make a change in the file, save itand compile it to get the new executablemodule, a.out.

Lect 1 Goutam Biswas

Page 76: Summer School - facweb.iitkgp.ac.ingoutam/sss/lect/lect1.pdf · Machine Language Program A nite sequence of machine instructions is called a machine language program. It is stored

Summer School Computer Sc & Engg: IIT Kharagpur 76'

&

$

%

Note

1. If there is no more message from the

compiler, the C program is syntactically

correct (well-formed).

2. An well-formed C program may have logical

error. Error-free compilation does not

guarantee the logical correctness of a

program.

Lect 1 Goutam Biswas

Page 77: Summer School - facweb.iitkgp.ac.ingoutam/sss/lect/lect1.pdf · Machine Language Program A nite sequence of machine instructions is called a machine language program. It is stored

Summer School Computer Sc & Engg: IIT Kharagpur 77'

&

$

%

OCAML Error Message

# print strng("First OCAML program\n");;Unbound value print_strng#Incorrect function name.

Lect 1 Goutam Biswas

Page 78: Summer School - facweb.iitkgp.ac.ingoutam/sss/lect/lect1.pdf · Machine Language Program A nite sequence of machine instructions is called a machine language program. It is stored

Summer School Computer Sc & Engg: IIT Kharagpur 78'

&

$

%

OCAML Error Message

#print_string("First OCAML program\n";;Syntax error: ’)’ expected,the highlighted ’(’ might be unmatched#

Lect 1 Goutam Biswas

Page 79: Summer School - facweb.iitkgp.ac.ingoutam/sss/lect/lect1.pdf · Machine Language Program A nite sequence of machine instructions is called a machine language program. It is stored

Summer School Computer Sc & Engg: IIT Kharagpur 79'

&

$

%

Note

An OCAML program can be written in a filewith the file name extension .ml. The file canbe given to the interpreter for interpretation.Let the file name be first.ml with the content

(* First program *)print_string("First OCAML program\n");;

Lect 1 Goutam Biswas

Page 80: Summer School - facweb.iitkgp.ac.ingoutam/sss/lect/lect1.pdf · Machine Language Program A nite sequence of machine instructions is called a machine language program. It is stored

Summer School Computer Sc & Engg: IIT Kharagpur 80'

&

$

%

OCAML Program from a File

$ ocamlObjective Caml version 3.10.1

# #use "first.ml";;First OCAML program- : unit = ()#

Lect 1 Goutam Biswas

Page 81: Summer School - facweb.iitkgp.ac.ingoutam/sss/lect/lect1.pdf · Machine Language Program A nite sequence of machine instructions is called a machine language program. It is stored

Summer School Computer Sc & Engg: IIT Kharagpur 81'

&

$

%

Activity - II

• Enter the second program (second.c),

compile and execute. Write the

corresponding OCAML function sumN

• Modify the second program to compute

12 + 22 + 32 + · · ·+ n2 = n(n+1)(2n+1)6 . Write

the corresponding OCAML function sumNsq.

Lect 1 Goutam Biswas

Page 82: Summer School - facweb.iitkgp.ac.ingoutam/sss/lect/lect1.pdf · Machine Language Program A nite sequence of machine instructions is called a machine language program. It is stored

Summer School Computer Sc & Engg: IIT Kharagpur 82'

&

$

%

Activity - II

File second.ml(* Sum of n natural numbers *)let sumN = function n -> n*(n+1)/2;;

Lect 1 Goutam Biswas

Page 83: Summer School - facweb.iitkgp.ac.ingoutam/sss/lect/lect1.pdf · Machine Language Program A nite sequence of machine instructions is called a machine language program. It is stored

Summer School Computer Sc & Engg: IIT Kharagpur 83'

&

$

%

Integer Operators

C OCAML

+ - * / + - * /

% mod

abs() abs

abs() in C is not an operator but a libraryfunction. Its prototype is defined in stdlib.h.

Lect 1 Goutam Biswas

Page 84: Summer School - facweb.iitkgp.ac.ingoutam/sss/lect/lect1.pdf · Machine Language Program A nite sequence of machine instructions is called a machine language program. It is stored

Summer School Computer Sc & Engg: IIT Kharagpur 84'

&

$

%

Example - III

A program in C that reads a positive number ras the radius of a circle and computes its areausing the formula A = πr2 is as follows.The corresponding OCAML function iscircArea.

Lect 1 Goutam Biswas

Page 85: Summer School - facweb.iitkgp.ac.ingoutam/sss/lect/lect1.pdf · Machine Language Program A nite sequence of machine instructions is called a machine language program. It is stored

Summer School Computer Sc & Engg: IIT Kharagpur 85'

&

$

%

#include <stdio.h>

#include <math.h>

int main() {

double radius, area ;

printf("Enter the radius: ");

scanf("%lf", &radius) ;

area = M_PI*radius*radius ;

printf("Circle-Area = %lf for Radius = %lf\n",

area, radius);

return 0 ;

} // Area of a circle third.c

Lect 1 Goutam Biswas

Page 86: Summer School - facweb.iitkgp.ac.ingoutam/sss/lect/lect1.pdf · Machine Language Program A nite sequence of machine instructions is called a machine language program. It is stored

Summer School Computer Sc & Engg: IIT Kharagpur 86'

&

$

%

OCAML Program third.ml

(* Area of a circle *)let circArea rad = 3.14159 *. rad *. rad;;

(* third.ml *)let cArea r =

print_string("Radius: ");print_float(r);print_string(" => Area: ");print_float(circArea r);print_newline();;

Lect 1 Goutam Biswas

Page 87: Summer School - facweb.iitkgp.ac.ingoutam/sss/lect/lect1.pdf · Machine Language Program A nite sequence of machine instructions is called a machine language program. It is stored

Summer School Computer Sc & Engg: IIT Kharagpur 87'

&

$

%

Note

The floating-point operator ‘*.’ is differentfrom the integer operator ‘*’ - otherflaoting-point operators are ‘+.’ ‘-.’ ‘/.’

Lect 1 Goutam Biswas

Page 88: Summer School - facweb.iitkgp.ac.ingoutam/sss/lect/lect1.pdf · Machine Language Program A nite sequence of machine instructions is called a machine language program. It is stored

Summer School Computer Sc & Engg: IIT Kharagpur 88'

&

$

%

Activity - III

• Enter the C program, compile and execute.

Enter the OCAML function.

• Modify the program to compute the

perimeter of a circle using the formula

C = 2πr. Write the corresponding OCAML

function circPeri.

Lect 1 Goutam Biswas

Page 89: Summer School - facweb.iitkgp.ac.ingoutam/sss/lect/lect1.pdf · Machine Language Program A nite sequence of machine instructions is called a machine language program. It is stored

Summer School Computer Sc & Engg: IIT Kharagpur 89'

&

$

%

Example - IV

We write a C program that reads a temperaturein degree Fahrenheit and converts it to theequivalent temperature in degree Celsius. Weknow that 32◦F ≡ 0◦C and 212◦− 32◦ = 180◦ ofFahrenheit scale is equivalent to100◦ − 0◦ = 100◦ of Celsius scale. So atemperature F in the Fahrenheit scale is relatedto the equivalent temperature C in the Celsiusscale as F−32

180 = C−0100−0 i.e. C = 5

9(F − 32)

Lect 1 Goutam Biswas

Page 90: Summer School - facweb.iitkgp.ac.ingoutam/sss/lect/lect1.pdf · Machine Language Program A nite sequence of machine instructions is called a machine language program. It is stored

Summer School Computer Sc & Engg: IIT Kharagpur 90'

&

$

%

#include <stdio.h>

int main(){

float cel, fah;

printf("Enter temp. in F: ");

scanf("%f", &fah);

cel = 5.0*(fah-32.0)/9.0 ;

printf("%6.2f F = %6.2f C\n", fah, cel);

return 0;

} // Fahrenheit to Celsius: fourth.c

Lect 1 Goutam Biswas

Page 91: Summer School - facweb.iitkgp.ac.ingoutam/sss/lect/lect1.pdf · Machine Language Program A nite sequence of machine instructions is called a machine language program. It is stored

Summer School Computer Sc & Engg: IIT Kharagpur 91'

&

$

%

OCAML Function and Programs

let fToc f=5.0*.(f-.(float)32)/.9.0;;

(* fourth.ml *)

let fahToCel fah =

print_float(fah);

print_string(" F = ");

print_float(fToc fah);

print_string(" C\n");;

Lect 1 Goutam Biswas

Page 92: Summer School - facweb.iitkgp.ac.ingoutam/sss/lect/lect1.pdf · Machine Language Program A nite sequence of machine instructions is called a machine language program. It is stored

Summer School Computer Sc & Engg: IIT Kharagpur 92'

&

$

%

Activity - IV

• Enter the C program, compile and execute.

Enter the OCAML program in interpreter.

• Modify the program to read a temperature in

Celsius scale and convert it to the equivalent

temperature in the Fahrenheit scale.

Lect 1 Goutam Biswas

Page 93: Summer School - facweb.iitkgp.ac.ingoutam/sss/lect/lect1.pdf · Machine Language Program A nite sequence of machine instructions is called a machine language program. It is stored

Summer School Computer Sc & Engg: IIT Kharagpur 93'

&

$

%

Getting the Slides

• Run Mozilla and access

http://facweb.iitkgp.ernet.in/∼goutam

• Follow the link

Summer School for the School Students

... of Summer: 2009.

• Download the correct .ps/.pdf file.

Lect 1 Goutam Biswas

Page 94: Summer School - facweb.iitkgp.ac.ingoutam/sss/lect/lect1.pdf · Machine Language Program A nite sequence of machine instructions is called a machine language program. It is stored

Summer School Computer Sc & Engg: IIT Kharagpur 94'

&

$

%

Display the Slides

• View the slides using either kghostview or

ggv.

• $ kghostview lect1.ps &a

• Select the View → Orientation to Seascape

for proper view.

aOr, $ ggv lect1.ps &. You must use ‘&’. Use xpdf or acroread for .pdf file.

Lect 1 Goutam Biswas

Page 95: Summer School - facweb.iitkgp.ac.ingoutam/sss/lect/lect1.pdf · Machine Language Program A nite sequence of machine instructions is called a machine language program. It is stored

Summer School Computer Sc & Engg: IIT Kharagpur 95'

&

$

%

Example - V

Write a C program to read two integers andprint the larger one.

Lect 1 Goutam Biswas

Page 96: Summer School - facweb.iitkgp.ac.ingoutam/sss/lect/lect1.pdf · Machine Language Program A nite sequence of machine instructions is called a machine language program. It is stored

Summer School Computer Sc & Engg: IIT Kharagpur 96'

&

$

%

#include <stdio.h>

int main() {

int first, second, max ;

printf("Enter two integers :") ;

scanf("%d%d", &first, &second) ;

if(first > second) max = first ;

else max = second ;

printf("\nMax(%d,%d) = %d\n",

first, second, max) ;

return 0 ;

} // Larger fifth.c

Lect 1 Goutam Biswas

Page 97: Summer School - facweb.iitkgp.ac.ingoutam/sss/lect/lect1.pdf · Machine Language Program A nite sequence of machine instructions is called a machine language program. It is stored

Summer School Computer Sc & Engg: IIT Kharagpur 97'

&

$

%

Two OCAML Programs

let larger a b = if a > b then a else b ;;

let lrgr a b = (* fifth.ml *)

if a > b then

begin

print_int(a); print_string(" > ");

print_int(b); print_newline()

end

else

begin

print_int(a); print_string(" <= ");

print_int(b); print_newline()

end;;

Lect 1 Goutam Biswas

Page 98: Summer School - facweb.iitkgp.ac.ingoutam/sss/lect/lect1.pdf · Machine Language Program A nite sequence of machine instructions is called a machine language program. It is stored

Summer School Computer Sc & Engg: IIT Kharagpur 98'

&

$

%

Activity - V

• Enter the C program, compile and execute.

Same for OCAML program.

• Modify the program to print the smaller one.

• Modify the program to read three integers

and print the largest one.

Lect 1 Goutam Biswas

Page 99: Summer School - facweb.iitkgp.ac.ingoutam/sss/lect/lect1.pdf · Machine Language Program A nite sequence of machine instructions is called a machine language program. It is stored

Summer School Computer Sc & Engg: IIT Kharagpur 99'

&

$

%

Example - VI

Following C program reads a two-digit positiveinteger say mn and converts it to the form nm.Same is the effect of the OCAML program.

Lect 1 Goutam Biswas

Page 100: Summer School - facweb.iitkgp.ac.ingoutam/sss/lect/lect1.pdf · Machine Language Program A nite sequence of machine instructions is called a machine language program. It is stored

Summer School Computer Sc & Engg: IIT Kharagpur 100'

&

$

%

#include <stdio.h>

int main()

{

int n ;

printf("Enter a two-digit integer: ");

scanf("%d", &n);

if(n < 10 || n >= 100) printf("Wrong data: %d\n", n);

else printf("%d --> %d\n", n, 10*(n%10)+n/10);

return 0;

} // mn --> nm ex6.c

Lect 1 Goutam Biswas

Page 101: Summer School - facweb.iitkgp.ac.ingoutam/sss/lect/lect1.pdf · Machine Language Program A nite sequence of machine instructions is called a machine language program. It is stored

Summer School Computer Sc & Engg: IIT Kharagpur 101'

&

$

%

OCAML Function

exception OutOfRange;;

let revDig x =

if x < 10 || x >= 100 then raise OutOfRange

else 10*(x mod 10)+x/10;;

Lect 1 Goutam Biswas

Page 102: Summer School - facweb.iitkgp.ac.ingoutam/sss/lect/lect1.pdf · Machine Language Program A nite sequence of machine instructions is called a machine language program. It is stored

Summer School Computer Sc & Engg: IIT Kharagpur 102'

&

$

%

OCAML Program

let revD x = (* ex6.ml *)

if x < 10 || x >= 100 then raise OutOfRange;

print_string("Reverse(");

print_int(x);

print_string(") = ");

print_int(revDig x);

print_newline();;

Lect 1 Goutam Biswas

Page 103: Summer School - facweb.iitkgp.ac.ingoutam/sss/lect/lect1.pdf · Machine Language Program A nite sequence of machine instructions is called a machine language program. It is stored

Summer School Computer Sc & Engg: IIT Kharagpur 103'

&

$

%

Activity - VI

• Enter the program, compile and execute.

• Modify the program to print the sum of two

digits.

• Modify the program to print the arithmetic

mean of the two digits, m+n

2 .

Lect 1 Goutam Biswas

Page 104: Summer School - facweb.iitkgp.ac.ingoutam/sss/lect/lect1.pdf · Machine Language Program A nite sequence of machine instructions is called a machine language program. It is stored

Summer School Computer Sc & Engg: IIT Kharagpur 104'

&

$

%

Example - VII

Following C program reads a positive integer nand then reads n numbers a1, a2, · · · , an andprints the largest among them. Same for theOCAML program.

Lect 1 Goutam Biswas

Page 105: Summer School - facweb.iitkgp.ac.ingoutam/sss/lect/lect1.pdf · Machine Language Program A nite sequence of machine instructions is called a machine language program. It is stored

Summer School Computer Sc & Engg: IIT Kharagpur 105'

&

$

%

#include <stdio.h>

int main()

{

int n, i;

float data, max;

printf("Enter a positive integer: ");

scanf("%d", &n);

if(n < 1) {

printf("Wrong count: %d\n",n);

return 0;

}

printf("Enter %d data:\n",n);

scanf("%f", &max);

Lect 1 Goutam Biswas

Page 106: Summer School - facweb.iitkgp.ac.ingoutam/sss/lect/lect1.pdf · Machine Language Program A nite sequence of machine instructions is called a machine language program. It is stored

Summer School Computer Sc & Engg: IIT Kharagpur 106'

&

$

%

for(i=2; i<=n; ++i){

scanf("%f", &data);

if(max < data) max = data;

}

printf("The largest data is: %f\n", max);

return 0;

} // largest among n data ex7.c

Lect 1 Goutam Biswas

Page 107: Summer School - facweb.iitkgp.ac.ingoutam/sss/lect/lect1.pdf · Machine Language Program A nite sequence of machine instructions is called a machine language program. It is stored

Summer School Computer Sc & Engg: IIT Kharagpur 107'

&

$

%

OCAML Program

(* Largest among n data. One data per line *)

exception DataCountError

let maxDat() =

print_string("Enter Data Count\n");

let n = read_int() in

if n < 1 then raise DataCountError

else

begin

print_string("Enter data:\n");

let max = ref (read_int()) in

begin

for i=2 to n do

Lect 1 Goutam Biswas

Page 108: Summer School - facweb.iitkgp.ac.ingoutam/sss/lect/lect1.pdf · Machine Language Program A nite sequence of machine instructions is called a machine language program. It is stored

Summer School Computer Sc & Engg: IIT Kharagpur 108'

&

$

%

let data = read_int() in

if !max < data then max := data;

done;

print_string("Largest data: ");

print_int(!max);

print_newline();

end

end;; (*ex7.ml*)

Lect 1 Goutam Biswas

Page 109: Summer School - facweb.iitkgp.ac.ingoutam/sss/lect/lect1.pdf · Machine Language Program A nite sequence of machine instructions is called a machine language program. It is stored

Summer School Computer Sc & Engg: IIT Kharagpur 109'

&

$

%

Activity - VII

• Enter the program, compile and execute.

• Modify the program to print the smallest

among n data.

• Modify the program to print the arithmetic

mean of n data AM = a1+a2+···+an

n.

• Modify the program to find the second

largest among n(≥ 1) data.

Lect 1 Goutam Biswas