Upload
ellen-johns
View
215
Download
1
Embed Size (px)
Citation preview
1
COMPUTERS IN CHEMISTRY: ‘C’ LANGUAGE
Minshiya P
Asst. Professor
Department of Chemistry
MES Keveeyam College Valanchery
2
Computer LanguagesComputer languages are more
preciseStrictly adhere to the grammar
of the languageCant correct wrong decisions
Low level languageHigh level language
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
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
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
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
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
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
9
Standard symbols used in drawing Flow chart
Start / end
processing
Input / out put
data
Decision logic
10
11
‘C’ Language
1970’s: Dennis RitchieHigh level languagePrograms are fast & efficientUniversally availableGeneral purpose programming
language
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
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
14
Logical operators
&& Logical AND
|| Logical OR
! Logical NOTUnary operators
++ Increment operator
-- decrement operatorAssignment operators
=, +=, -=, *=, /= and %=
15
Basic elements
C character setIdentifiers & keywordsData typesConstantsVariablesStatements
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:
17
Special characters:
/ , : ‘ ! { ] space
\ . ; * ? } [
- ( =>
| ~(tilde)
% &
+ ) $ < ^ _ # “
Combination characters
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, …..
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
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
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
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
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;
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;
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
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
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”
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:
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
30
Arguments One for each input dataContain the variable names
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.
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
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
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 &
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
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
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
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
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
40
Sum
of
two n
um
bers
41
Free & Open Chemistry Software
Free software (Freeware)= available for use at no cost, fully functional for an unlimited time.
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
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
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
45
Thank You