45
COMPUTERS IN CHEMISTRY: ‘C’ LANGUAGE Minshiya P Asst. Professor Department of Chemistry MES Keveeyam College Valanchery 1

COMPUTERS IN CHEMISTRY: ‘C’ LANGUAGE Minshiya P Asst. Professor Department of Chemistry MES Keveeyam College Valanchery 1

Embed Size (px)

Citation preview

Page 1: COMPUTERS IN CHEMISTRY: ‘C’ LANGUAGE Minshiya P Asst. Professor Department of Chemistry MES Keveeyam College Valanchery 1

1

COMPUTERS IN CHEMISTRY: ‘C’ LANGUAGE

Minshiya P

Asst. Professor

Department of Chemistry

MES Keveeyam College Valanchery

Page 2: COMPUTERS IN CHEMISTRY: ‘C’ LANGUAGE Minshiya P Asst. Professor Department of Chemistry MES Keveeyam College Valanchery 1

2

Computer LanguagesComputer languages are more

preciseStrictly adhere to the grammar

of the languageCant correct wrong decisions

Low level languageHigh level language

Page 3: COMPUTERS IN CHEMISTRY: ‘C’ LANGUAGE Minshiya P Asst. Professor Department of Chemistry MES Keveeyam College Valanchery 1

3

Low level language Machine level

Program written in the form of ‘0’s & ‘1’s are called machine language program

Instructions are coded (binary codes) & stored in the memory in the form of ‘0’s & ‘1’s

Each instruction: Address

Operations & addresses are represented in machin’s memory as a string of binary digit

Page 4: COMPUTERS IN CHEMISTRY: ‘C’ LANGUAGE Minshiya P Asst. Professor Department of Chemistry MES Keveeyam College Valanchery 1

4

Low level languageAssembly level

Use memory aids (mnemonics)Program can be easily written using

alphanumeric symbols instead of 0s & 1sMeaningful & easily memorable symbols are

selectedEg: ADD – addition, SUB – substraction

CMP- comparison

1001 – multiply- MLT• During execution the mnemonics translated to

binary form• Translation is done by a program - assembler

Page 5: COMPUTERS IN CHEMISTRY: ‘C’ LANGUAGE Minshiya P Asst. Professor Department of Chemistry MES Keveeyam College Valanchery 1

5

High level language

◦ Using simple English words & conventional mathematical notations

◦ Eg:- FORTRAN, BASIC, PASCAL, COBOL, etc.

◦ HLL has to be translated to machine level language before execution

◦ Depending on the HLL, translator : Compiler or Interpreter

◦ Compiler: translate the entire program to binary code before execution – FORTRAN

Page 6: COMPUTERS IN CHEMISTRY: ‘C’ LANGUAGE Minshiya P Asst. Professor Department of Chemistry MES Keveeyam College Valanchery 1

6

◦ Interpreter- convert one instruction at a time and execute that instruction- BASIC

◦ Take longer time for completion

◦ Easy to locate the point of error

◦ No need for a separate compiling run after each program

Page 7: COMPUTERS IN CHEMISTRY: ‘C’ LANGUAGE Minshiya P Asst. Professor Department of Chemistry MES Keveeyam College Valanchery 1

7

Algorithm

Computer cant think on its own

The programmer write the procedure how to solve the problem

Procedure must be in the form of a series of steps in a logical sequence

A precise statement of the procedure required for solving a problem- algorithm

Page 8: COMPUTERS IN CHEMISTRY: ‘C’ LANGUAGE Minshiya P Asst. Professor Department of Chemistry MES Keveeyam College Valanchery 1

8

Flow charts:◦Pictorial representation of algorithms

◦A diagram or a picture: define the procedure for solving a problem

◦Shows the order of operation & relationship between the sections of the program

◦Independent of a particular computer or a computer language

Page 9: COMPUTERS IN CHEMISTRY: ‘C’ LANGUAGE Minshiya P Asst. Professor Department of Chemistry MES Keveeyam College Valanchery 1

9

Standard symbols used in drawing Flow chart

Start / end

processing

Input / out put

data

Decision logic

Page 10: COMPUTERS IN CHEMISTRY: ‘C’ LANGUAGE Minshiya P Asst. Professor Department of Chemistry MES Keveeyam College Valanchery 1

10

Page 11: COMPUTERS IN CHEMISTRY: ‘C’ LANGUAGE Minshiya P Asst. Professor Department of Chemistry MES Keveeyam College Valanchery 1

11

‘C’ Language

1970’s: Dennis RitchieHigh level languagePrograms are fast & efficientUniversally availableGeneral purpose programming

language

Page 12: COMPUTERS IN CHEMISTRY: ‘C’ LANGUAGE Minshiya P Asst. Professor Department of Chemistry MES Keveeyam College Valanchery 1

12

Operators in ‘C’

Symbol consisting of 1 or 2 characters Tells the computer to perform mathematical or logical

manipulations ‘C’ does not have an exponentiation operatorCarried out using library function pow

Arithmetic operator Operation

+ (plus) Addition

- (minus) Substraction

* (asterisk) Multiplication

/ (slash) Division

% (percentage) Modulo-division

Page 13: COMPUTERS IN CHEMISTRY: ‘C’ LANGUAGE Minshiya P Asst. Professor Department of Chemistry MES Keveeyam College Valanchery 1

13

Relational operators: comparison of 2 quantities so as to take certain decision.

Relational operators

Operation

< Less than

> Geater than

<= Less than or equal to

>= Geater than or equal to

== Equal to

!= Not equal to

Page 14: COMPUTERS IN CHEMISTRY: ‘C’ LANGUAGE Minshiya P Asst. Professor Department of Chemistry MES Keveeyam College Valanchery 1

14

Logical operators

&& Logical AND

|| Logical OR

! Logical NOTUnary operators

++ Increment operator

-- decrement operatorAssignment operators

=, +=, -=, *=, /= and %=

Page 15: COMPUTERS IN CHEMISTRY: ‘C’ LANGUAGE Minshiya P Asst. Professor Department of Chemistry MES Keveeyam College Valanchery 1

15

Basic elements

C character setIdentifiers & keywordsData typesConstantsVariablesStatements

Page 16: COMPUTERS IN CHEMISTRY: ‘C’ LANGUAGE Minshiya P Asst. Professor Department of Chemistry MES Keveeyam College Valanchery 1

16

Character setAny letter, digit, punctuation

mark, or any special symbolUsed to construct statementsCharacter set of ‘C’1. Alphabets: Upper / Lower case:

A to Z2. Numerals: 0 to 93. Special characters:

Page 17: COMPUTERS IN CHEMISTRY: ‘C’ LANGUAGE Minshiya P Asst. Professor Department of Chemistry MES Keveeyam College Valanchery 1

17

Special characters:

/ , : ‘ ! { ] space

\ . ; * ? } [

- ( =>

| ~(tilde)

% &

+ ) $ < ^ _ # “

Combination characters

Page 18: COMPUTERS IN CHEMISTRY: ‘C’ LANGUAGE Minshiya P Asst. Professor Department of Chemistry MES Keveeyam College Valanchery 1

18

Keywords

Sequence of charactersHave standard meaningCan not be changedEach can be used for its intended purposeStandard keywords are

auto, for, const, float, char, long, double, while, else, int, do, goto, …..

Page 19: COMPUTERS IN CHEMISTRY: ‘C’ LANGUAGE Minshiya P Asst. Professor Department of Chemistry MES Keveeyam College Valanchery 1

19

Variables & Data types

Name that represent a numeric quantity or a string whose value may change during the execution of a program.

Name is chosen by programmer

Page 20: COMPUTERS IN CHEMISTRY: ‘C’ LANGUAGE Minshiya P Asst. Professor Department of Chemistry MES Keveeyam College Valanchery 1

20

Rules for naming variables

1. First character must be a letter of the alphabet (upper or lower case) or underscore

2. First character must be followed by a sequence of letters or and/or digits

3. No variable name be a keyword4. Must not contain white space ( blank space,

horizontal tab, new line) or characters (, ; etc) eg:- sum, b5, tax_rate, VOL, _vol

Page 21: COMPUTERS IN CHEMISTRY: ‘C’ LANGUAGE Minshiya P Asst. Professor Department of Chemistry MES Keveeyam College Valanchery 1

21

Data typesTypes of data that a variable can

hold data types in ‘C’ are:

Data type keyword

function

integer int Represent whole number (=ve & -ve)

character Char Single characters

Floating point float Numbers containing decimal point and/or an exponent

Double-precision floating point

double Floating with more significant figures and/ or large exponent

Page 22: COMPUTERS IN CHEMISTRY: ‘C’ LANGUAGE Minshiya P Asst. Professor Department of Chemistry MES Keveeyam College Valanchery 1

22

Declaration of variables

All variables used in a program must be declared before they can appear in the executable statements

Tells the variable name to computerPrecisely specifies the data type

carried by the variableAllows the programmer to conserve

memory space

Page 23: COMPUTERS IN CHEMISTRY: ‘C’ LANGUAGE Minshiya P Asst. Professor Department of Chemistry MES Keveeyam College Valanchery 1

23

Variables of the same type can be combined in a single declaration

Declaration statement: data type followed by variable names separated by , ending with a ;

Data-type variable1, variable2,….;

Eg: int number; float boil_point, press; char dt;

Page 24: COMPUTERS IN CHEMISTRY: ‘C’ LANGUAGE Minshiya P Asst. Professor Department of Chemistry MES Keveeyam College Valanchery 1

24

Assignment statement

Assigning values to variablesUsing assignment operators: =, +=, -=, *=, /=

and %=Most commonly used one is =Format: variable_name = value;

eg: x = 4;

i = i + 1;

area = 3.14*radius*radius;

Page 25: COMPUTERS IN CHEMISTRY: ‘C’ LANGUAGE Minshiya P Asst. Professor Department of Chemistry MES Keveeyam College Valanchery 1

25

Constants in ‘C’

A value or a unit contain sequence of charactersValue does not change during the execution of

the program

constants

Numeric

integer Floating point/ real

Character

Single character string

Page 26: COMPUTERS IN CHEMISTRY: ‘C’ LANGUAGE Minshiya P Asst. Professor Department of Chemistry MES Keveeyam College Valanchery 1

26

1. Numeric constantsNumeric quantity (number) : integer or decimalCan be preceded by ‘+’ or ‘–’Commas not allowedCan contain an exponent

a) Integer constants

Integer number Sequence of digits without a

decimal point Magnitude ranges from ‘0’ to

some maximum(+ or -) eg: - 234 -4567 0 +12

b) Floating point / real

Sequence of digits with a decimal point and / or an exponent

Exponential notation of a real number mantissa E/ e exponent

eg: - 125. 0.345 -765.897.453e-7 = 7.453x10 -7

Page 27: COMPUTERS IN CHEMISTRY: ‘C’ LANGUAGE Minshiya P Asst. Professor Department of Chemistry MES Keveeyam College Valanchery 1

27

a) Single character cont. Single character enclosed in ‘ ‘

eg:- ‘s’ ‘b’ ’x’ ‘;’ ‘ ‘ Blank space also considered

as a single character cont

2. Character constants

b) String cont. Sequence of characters

enclosed in “ “ May be letters, numbers,

special characters and blank space

eg:- “A” “molal” “sec”

Page 28: COMPUTERS IN CHEMISTRY: ‘C’ LANGUAGE Minshiya P Asst. Professor Department of Chemistry MES Keveeyam College Valanchery 1

28

Declaration of constants

All constants used must be declared before they appear in the executable statements

Keyword ; defineFormat: #define constname constvalueEg: #define avog_no 6.023e23Recent format: const data-type constname =

constvalue

Eg:- const int mass_no = 145:const float pi = 3.1459:

Page 29: COMPUTERS IN CHEMISTRY: ‘C’ LANGUAGE Minshiya P Asst. Professor Department of Chemistry MES Keveeyam College Valanchery 1

29

used to move data from computer memory to input device Printf(“control string” , arg1, arg2,…);Control string Contain certain character groups Each input data : individual character group Each group: conversion specification

% and data type specifier (d, e, f, ….) May contain a number, if field width is to be

specified May also contain blanks & escape sequence (\n, \t,

….)

Output function: ‘printf’ function

Data input & output

Page 30: COMPUTERS IN CHEMISTRY: ‘C’ LANGUAGE Minshiya P Asst. Professor Department of Chemistry MES Keveeyam College Valanchery 1

30

Arguments One for each input dataContain the variable names

Page 31: COMPUTERS IN CHEMISTRY: ‘C’ LANGUAGE Minshiya P Asst. Professor Department of Chemistry MES Keveeyam College Valanchery 1

31

1. Integer output printf (“%d” , number);

“%d” tells the printf function that the variable name

number to be printed is an integer

2. Floating point number outputo printf (“%f” , number);o If the variable number contains the value 32.45,

displayed as 32.450000o printf (“%e” , number); floating point number in

scientific notationo Variable number contain the value 2356.14, displayed as

2.356140e03.

Page 32: COMPUTERS IN CHEMISTRY: ‘C’ LANGUAGE Minshiya P Asst. Professor Department of Chemistry MES Keveeyam College Valanchery 1

32

3. String output printf(“%s” , name);

Conversion specification

Meaning

%c Print a single character

%d Print an integer

%e Print a floating point number in exponent form

%f print a floating point number (without exponent)

%s Print a string

Page 33: COMPUTERS IN CHEMISTRY: ‘C’ LANGUAGE Minshiya P Asst. Professor Department of Chemistry MES Keveeyam College Valanchery 1

33

enter input data from keyboard General format: scanf(“control string” , arg1,

arg2,…);Control string Contain certain character groups Each input data : individual character group Each group: conversion specification

% and data type specifier (d, e, f, ….) May contain a number, if field width is to be

specified May also contain blanks & escape sequence (\n, \

t, ….)

Input function: ‘scanf’ function

Page 34: COMPUTERS IN CHEMISTRY: ‘C’ LANGUAGE Minshiya P Asst. Professor Department of Chemistry MES Keveeyam College Valanchery 1

34

Arguments Specify the addresses of locations where the data items are stored in the memoryOne for each input dataContain the variable namesEach must be precede by &

Page 35: COMPUTERS IN CHEMISTRY: ‘C’ LANGUAGE Minshiya P Asst. Professor Department of Chemistry MES Keveeyam College Valanchery 1

35

1. Integer input

scanf(“%d”, &number); To enter a number

%d = conversion specification

number = variable name

“%d” : tells the scanf function to read in an integer

&number : specifies that the typed response will be placed into the memory location associated with the variable number

scanf (“%3d”, &no.);

3 indicate maximum field width of the input number

ie: the number of characters in the input number should not exceed the specified field width

scanf(“ %d%d%f”, &x,&y,&z);

three input data through one statement

Page 36: COMPUTERS IN CHEMISTRY: ‘C’ LANGUAGE Minshiya P Asst. Professor Department of Chemistry MES Keveeyam College Valanchery 1

36

2. Floating point number input scanf(“%f or %e”, &conc); OR scanf(“%f%f%f”, &num1,& num2, &); Field width not specified Double precision floating point: scanf(“%lf”,

&KE);

3. Character input single character: %c String : %s scanf(“%c”, &symbol); scanf(“%s”, name);‘&’ not used for string variable name

Page 37: COMPUTERS IN CHEMISTRY: ‘C’ LANGUAGE Minshiya P Asst. Professor Department of Chemistry MES Keveeyam College Valanchery 1

37

4. Mixed mode input scanf(“%c%d%f%f” , &symbol, &at_no,

&at_mass, &mol_mass);

Conversion specification

Meaning

%c Read a single character

%d Read an integer

%e or %f Read a floating point number

%lf Read a double number

%s Read a string

Page 38: COMPUTERS IN CHEMISTRY: ‘C’ LANGUAGE Minshiya P Asst. Professor Department of Chemistry MES Keveeyam College Valanchery 1

38

Sam

ple

pro

gra

m/*calculate the area and perimeter of a circle*/

#include <stdio.h>

#include<conio.h>

main()

{

const float pi = 3.1459;

float r, A, pm;

clrscr();

printf(“enter the radius incm\n”);

scanf(“%f”, &r);

A = pi*r*r;

pm = 2*pi*r;

printf(“\n area in sq.cm = %f”, A);

printf(“\nperimeter in cm = %f” , pm );

getch();

}

Area of circle = π r2

perimeter = 2πr

Page 39: COMPUTERS IN CHEMISTRY: ‘C’ LANGUAGE Minshiya P Asst. Professor Department of Chemistry MES Keveeyam College Valanchery 1

39

#include : preprocessor directives, placed st the beginning of the program, , tells the compiler about the library file that is to be assessed and merged with the source program

Main : used to tell the computer where the program starts Followed by a (): no arguments

{ Clrscr: clears the output screen & makes it ready to show the out put of the

present program only printf Scanf getch(): to appear the output screen immediately after the run of the program

not necessary to get the output upon run. semicolon: statement terminator.

If more than one statement on the same line, each of them terminated with a ; \n: newline character,

placed at the end of the literal or at the beginning of the next printf statement

instructs the computer to get the next line

Page 40: COMPUTERS IN CHEMISTRY: ‘C’ LANGUAGE Minshiya P Asst. Professor Department of Chemistry MES Keveeyam College Valanchery 1

40

Sum

of

two n

um

bers

Page 41: COMPUTERS IN CHEMISTRY: ‘C’ LANGUAGE Minshiya P Asst. Professor Department of Chemistry MES Keveeyam College Valanchery 1

41

Free & Open Chemistry Software

Free software (Freeware)= available for use at no cost, fully functional for an unlimited time.

Page 42: COMPUTERS IN CHEMISTRY: ‘C’ LANGUAGE Minshiya P Asst. Professor Department of Chemistry MES Keveeyam College Valanchery 1

42

Some free & open chemistry software available on the internet

1. Jmol: free, open source molecular viewer for students, educators & researchers in chemistry & biochemistry

• Running on Windows, Mac OS X, & Linux/Unix systems

2. ACD ChemSketch 12 freeware: all purpose chemical drawing & graphics software

oUseful to draw molecules, ions, stereobonds, text, polygons, arrows, laboratory apparatus, etc

oAutomatic calculation of molecular mass, formula, density, refractive index, etc are possible

Page 43: COMPUTERS IN CHEMISTRY: ‘C’ LANGUAGE Minshiya P Asst. Professor Department of Chemistry MES Keveeyam College Valanchery 1

43

3. Accelry’s freeware: Accelry’s Draw: Program that allows to generate structural

formulae for complex molecules.

Resulting image can be exported to variety of graphic formats for use in websites or word-processors documents

Accelry’s Jdraw: freeware for academic and noncommercial use

Allow to draw structures, reactions and copy them to another applications including Accelry’s draw, M word or MS PowerPoint

Page 44: COMPUTERS IN CHEMISTRY: ‘C’ LANGUAGE Minshiya P Asst. Professor Department of Chemistry MES Keveeyam College Valanchery 1

44

4. Chem Axon Tools: Chem Axon produces a wide range of

Java- based chemistry visualization tools

offer free academic licenses through registration

Consists of several sketch and visualization tools

Useful at high school & University levels

5. Avogadro: Advanced molecule editor & visualizer

Useful in computational chemistry, molecular

modeling, bioinformatics, material science & related

areas

Page 45: COMPUTERS IN CHEMISTRY: ‘C’ LANGUAGE Minshiya P Asst. Professor Department of Chemistry MES Keveeyam College Valanchery 1

45

Thank You