Transaction Management in Database Management System
Lec04-CS110 Computational Engineering
1. CS110: Models of Computing
Lecture 4
V. Kamakoti
8th January 2008
2. • More of C
• Writing Readable codes
• More programming constructs
• More complex problem solving
• Desirable Program Characteristics
• Reference Books for the course
• Text Book: Programming with C - Byron S.
Gottfried, Schaum’s outline series
• Reference Book: C programming Kerningham and RitchieReaching the Lab
3. Readability of Code
/* TITLE (COMMENT) */
/* program to calculate area of a circle */
/*LIBRARY FILE ACCESS*/
#include <stdio.h>
/* DEFINED CONSTANTS */
#define PI 3.14159
4. Readability of Code
/* FUNCTION HEADING */
main() {
float radius, area; /* Indentation */
printf(“Radius = ? “);
scanf(“%f”, &radius);
area = PI * radius * radius;
printf(“Area = %f”, area);
}
5. The C Preprocessor
• Preprocessor executes before
compilation
– Substitutes the value of PI at all places
– You can decide on the precision by
changing the value of PI at the top
– Imagine using PI at different points in the
program
• You need not change at all the points
6. Use of #define
#define PI 3.14159
main() {
float radius, area; /* Indentation */
printf(“Radius = ? “);
scanf(“%f”, &radius);
area1 = PI * radius * radius;
printf(“Area = %f”, area);
area2 = PI * 2 * radius * 2 * radius;
printf(“Big circle area = %f”, area2);
} /*Something Wrong here ? */
7. Use of #define
/*Variable not declared. Error would be undefined variables */
#define PI 3.14159
main() {
float radius, area1, area2; /* Indentation */
printf(“Radius = ? “);
scanf(“%f”, &radius);
area1 = PI * radius * radius;
printf(“Area = %f”, area);
area2 = PI * 2 * radius * 2 * radius;
printf(“Big circle area = %f”, area2);
}
8. Sensible Outputs
main() {
float gross, tax, net;
printf(“Gross Salary: “);
scanf(“%f”,&gross);
tax = 0.14 * gross;
net = gross - tax;
printf(“Taxes withheld: %.2fn”,tax);
printf(“Net Salary: %.2f”,net);
} /* I am interested upto 2 decimals only */
/* Can I pay 2.155 Rs? */
10. Intelligent Compilers
#include <stdio.h>
main() {
float a;
a = 1.2365;
printf("%fn",a);
printf("%.2fn",a);
}
/* Output is 1.236500 and 1.24 */
/* It does rounding for improving accuracy */
/* 1.24 is more closer to 1.2365 than 1.23 */
11. Desirable Program Characteristics
• Integrity
– Accuracy of calculations
• Clarity
– Another person should be able to read and
enhance your code.
• Simplicity
• Efficiency
– Execution speed and memory utilization
12. Desirable Program Characteristics
• Modularity
– Break into subtasks and write separate functions
– Everything need not be written inside the main()
– main() calls other functions written by others
• Typically large software systems are developed like this
– printf() and scanf() - examples of modularity
• Generality
– Do not hardcode constants
– Makes it specific to a context
– Pass them as variables
13. Block Diagram of A Computer
Input Device
Memory
Output
Device
ALU CU
CPU
Data path
Control path
14. Input to the system
• What you type is saved as ASCII codes
– American Standard Code for Information
Interchange
• IBM uses EBCDIC
– Extended Binary Coded Decimal
Information Code
• Every key in your keyboard has an 8-bit
equivalent code
15. With “n” bits….
• You can represent 2n different decimal
numbers and hence entities.
• For example with 1 bit
– 0 - green
– 1 - red
– So 21 colors
• With 2-bits
– 00 - Black, 01- red, 10 - green, 11 - blue
– So 22 colors
16. With “8” bits….
• You can represent 256 different decimal
numbers and hence entities.
• So 256 different characters can be
represented in the ASCII Code
• For example
– The character ‘A’ is represented by 01000001
– The complete set is available in Table 2.1 - Pg.
2.19 of the reference book - Schaum Series…
– Wikipedia
17. Strings
• String is a collection of one or more
characters
• The programs you write have several strings
• These strings may be classified as
–
–
–
–
–
–
Identifiers or variables
Keywords or reserved words
Starts with Alphabet or underscore ( _ )
It can have numeric digits inbetween
X, y12, sum_1, _temp are variables
4th, order-no, error flag - are not
18. Identifiers and keywords
main() {
float num;
num = 1.2365;
printf("%fn",num);
printf("%.2fn",num);
}
Try using printf as a variable - that is declare
float a, printf; in the above program and compile
19. Keywords
auto, extern, sizeof, break, float, static,
char, case, switch, int, long, register, do
double, signed, short, while, struct,
typedef ……
A list is available in pg. 2.5 of the Schaum
Series book
20. Variables
• Can be of several types (Datatypes)
– int
• Integers
• Variants: short, long, unsigned
– char
• Character
– float
• Real numbers
• Variants: double (precision)
21. Test Your Understanding
• Suppose an integer is represented
using n bits, how many values can be
represented if the integer is
– Unsigned
– Signed
• Write a program that shall add any two
given integer numbers
23. Question 1
• n-bits
– Unsigned 0 to 2n - 1
– For eg. 2 bits
• 00 - 0, 01 - 1, 10 - 2, 11 - 3
– Signed -2n-1 to +2n-1
– For eg. 3 bits (first bit for sign)
•
•
•
•
111 = -3, 110 = -2, 101 = -1, 100 = -0
000 = +0, 001 = +1, 010 = +2, 011 = +3
We are wasting one entry - which one?
Can do better - 2’s complement - next class.
24. Question 2
#include <stdio.h>
main() {
int num1, num2, num3;
printf(“Enter the two integer values to be
added”);
scanf(“%d %d”, num1, num2);
num3 = num1 + num2;
printf(“Sum of the two numbers is %dn”,num3);
}
25. Question 2 - Correct answer
#include <stdio.h>
main() {
int num1, num2, sum;
printf(“Enter the two integer values to be added”);
scanf(“%d %d”, &num1, &num2);
sum = num1 + num2;
printf(“Sum of the two numbers is %dn”,sum);
}
Readbility enhanced and Syntax errors rectified
27. The problem
• Suppose there are n = 2k persons, each with
a certain item of information. In each step,
each person can communicate with another
person and share all the information he or
she knows (including information learned in
previous steps). A person cannot
communicate with more than one person in
any step. Design a communication (gossip)
pattern such that after log2n (= k) steps,
everyone knows everything.