SlideShare a Scribd company logo
1 of 30
Download to read offline
CS110: Models of Computing
Lecture 4
V. Kamakoti
8th January 2008
• 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
Readability of Code
/* TITLE (COMMENT) */

/* program to calculate area of a circle */
/*LIBRARY FILE ACCESS*/

#include <stdio.h>
/* DEFINED CONSTANTS */

#define PI 3.14159
Readability of Code
/* FUNCTION HEADING */

main() {
float radius, area; /* Indentation */
printf(“Radius = ? “);
scanf(“%f”, &radius);
area = PI * radius * radius;
printf(“Area = %f”, area);
}
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
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 ? */
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);
}
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? */
Intelligent Compilers
#include <stdio.h>
main() {
float a;
a = 1.2345;
printf("%fn",a);
printf("%.2fn",a);
}
/* Output is 1.234500 and 1.23 */
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 */
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
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
Block Diagram of A Computer

Input Device

Memory

Output
Device

ALU CU
CPU
Data path
Control path
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
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
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
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
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
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
Variables
• Can be of several types (Datatypes)
– int
• Integers
• Variants: short, long, unsigned

– char
• Character

– float
• Real numbers
• Variants: double (precision)
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
Answers
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.
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);
}
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
Creative Problem - 2
• The Gossip Problem
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.
For n = 4 (Step 1)
1,2

1,2

3,4

3,4
For n = 4 (Step 2)
1,2,3,4

1,2,3,4

1,2,3,4

1,2,3,4
Thank You

More Related Content

What's hot

INTRODUCTION TO FUNCTIONS IN PYTHON
INTRODUCTION TO FUNCTIONS IN PYTHONINTRODUCTION TO FUNCTIONS IN PYTHON
INTRODUCTION TO FUNCTIONS IN PYTHONvikram mahendra
 
USE OF PRINT IN PYTHON PART 2
USE OF PRINT IN PYTHON PART 2USE OF PRINT IN PYTHON PART 2
USE OF PRINT IN PYTHON PART 2vikram mahendra
 
USER DEFINE FUNCTIONS IN PYTHON
USER DEFINE FUNCTIONS IN PYTHONUSER DEFINE FUNCTIONS IN PYTHON
USER DEFINE FUNCTIONS IN PYTHONvikram mahendra
 
c++ exp 1 Suraj...pdf
c++ exp 1 Suraj...pdfc++ exp 1 Suraj...pdf
c++ exp 1 Suraj...pdfayush616992
 
New microsoft office word document (2)
New microsoft office word document (2)New microsoft office word document (2)
New microsoft office word document (2)Syed Umair
 
Numerical analysis
Numerical analysisNumerical analysis
Numerical analysisVishal Singh
 
C programs
C programsC programs
C programsMinu S
 
Compiler design lab
Compiler design labCompiler design lab
Compiler design labilias ahmed
 
Python programming- Part IV(Functions)
Python programming- Part IV(Functions)Python programming- Part IV(Functions)
Python programming- Part IV(Functions)Megha V
 
Python programming –part 3
Python programming –part 3Python programming –part 3
Python programming –part 3Megha V
 
Expressions using operator in c
Expressions using operator in cExpressions using operator in c
Expressions using operator in cSaranya saran
 
C programming pointer
C  programming pointerC  programming pointer
C programming pointerargusacademy
 
Decision making and branching
Decision making and branchingDecision making and branching
Decision making and branchingSaranya saran
 
Managing input and output operations in c
Managing input and output operations in cManaging input and output operations in c
Managing input and output operations in cniyamathShariff
 

What's hot (20)

INTRODUCTION TO FUNCTIONS IN PYTHON
INTRODUCTION TO FUNCTIONS IN PYTHONINTRODUCTION TO FUNCTIONS IN PYTHON
INTRODUCTION TO FUNCTIONS IN PYTHON
 
USE OF PRINT IN PYTHON PART 2
USE OF PRINT IN PYTHON PART 2USE OF PRINT IN PYTHON PART 2
USE OF PRINT IN PYTHON PART 2
 
Mips1
Mips1Mips1
Mips1
 
USER DEFINE FUNCTIONS IN PYTHON
USER DEFINE FUNCTIONS IN PYTHONUSER DEFINE FUNCTIONS IN PYTHON
USER DEFINE FUNCTIONS IN PYTHON
 
c++ exp 1 Suraj...pdf
c++ exp 1 Suraj...pdfc++ exp 1 Suraj...pdf
c++ exp 1 Suraj...pdf
 
New microsoft office word document (2)
New microsoft office word document (2)New microsoft office word document (2)
New microsoft office word document (2)
 
Numerical analysis
Numerical analysisNumerical analysis
Numerical analysis
 
Ansi c
Ansi cAnsi c
Ansi c
 
C programs
C programsC programs
C programs
 
Compiler design lab
Compiler design labCompiler design lab
Compiler design lab
 
Compiler design lab programs
Compiler design lab programs Compiler design lab programs
Compiler design lab programs
 
Fibonnaci
FibonnaciFibonnaci
Fibonnaci
 
Python programming- Part IV(Functions)
Python programming- Part IV(Functions)Python programming- Part IV(Functions)
Python programming- Part IV(Functions)
 
C Programming
C ProgrammingC Programming
C Programming
 
Python programming –part 3
Python programming –part 3Python programming –part 3
Python programming –part 3
 
Expressions using operator in c
Expressions using operator in cExpressions using operator in c
Expressions using operator in c
 
C programming pointer
C  programming pointerC  programming pointer
C programming pointer
 
Decision making and branching
Decision making and branchingDecision making and branching
Decision making and branching
 
Pointer
PointerPointer
Pointer
 
Managing input and output operations in c
Managing input and output operations in cManaging input and output operations in c
Managing input and output operations in c
 

Viewers also liked

NOBEL PRIZE - ECONOMIC SCIENCES (Part 2)
NOBEL PRIZE - ECONOMIC SCIENCES  (Part 2)NOBEL PRIZE - ECONOMIC SCIENCES  (Part 2)
NOBEL PRIZE - ECONOMIC SCIENCES (Part 2)eslstudents
 
2013 Nobel Prize in Economics
2013 Nobel Prize in Economics2013 Nobel Prize in Economics
2013 Nobel Prize in EconomicsJay Leavitt
 
Nobel prize in medicine
Nobel prize in medicineNobel prize in medicine
Nobel prize in medicinesarojben
 
Let's Chat About Economics - Book Overview
Let's Chat About Economics - Book OverviewLet's Chat About Economics - Book Overview
Let's Chat About Economics - Book OverviewMichelle Balconi
 
Intro to economics
Intro to economicsIntro to economics
Intro to economicsArnel Rivera
 
Elementary Economics (3)
Elementary Economics (3)Elementary Economics (3)
Elementary Economics (3)juselig
 
Mother Teresa: Saint of the Gutters
Mother Teresa: Saint of the GuttersMother Teresa: Saint of the Gutters
Mother Teresa: Saint of the Guttersguimera
 
Nobel Prize in Physiology or Medicine: Ideas Changing The World: 2014
Nobel Prize in Physiology or Medicine: Ideas Changing The World: 2014Nobel Prize in Physiology or Medicine: Ideas Changing The World: 2014
Nobel Prize in Physiology or Medicine: Ideas Changing The World: 2014DR. SAIFUL ALOM SIDDIQUE
 
Introduction to economics
Introduction to economicsIntroduction to economics
Introduction to economicsMichael Noel
 

Viewers also liked (12)

Demo final torok
Demo final torokDemo final torok
Demo final torok
 
NOBEL PRIZE - ECONOMIC SCIENCES (Part 2)
NOBEL PRIZE - ECONOMIC SCIENCES  (Part 2)NOBEL PRIZE - ECONOMIC SCIENCES  (Part 2)
NOBEL PRIZE - ECONOMIC SCIENCES (Part 2)
 
2013 Nobel Prize in Economics
2013 Nobel Prize in Economics2013 Nobel Prize in Economics
2013 Nobel Prize in Economics
 
Nobel prize in medicine
Nobel prize in medicineNobel prize in medicine
Nobel prize in medicine
 
Let's Chat About Economics - Book Overview
Let's Chat About Economics - Book OverviewLet's Chat About Economics - Book Overview
Let's Chat About Economics - Book Overview
 
Economics Nobel prize 2014
Economics Nobel prize 2014Economics Nobel prize 2014
Economics Nobel prize 2014
 
Intro to economics
Intro to economicsIntro to economics
Intro to economics
 
Elementary Economics (3)
Elementary Economics (3)Elementary Economics (3)
Elementary Economics (3)
 
Mother Teresa: Saint of the Gutters
Mother Teresa: Saint of the GuttersMother Teresa: Saint of the Gutters
Mother Teresa: Saint of the Gutters
 
Nobel prize
Nobel prizeNobel prize
Nobel prize
 
Nobel Prize in Physiology or Medicine: Ideas Changing The World: 2014
Nobel Prize in Physiology or Medicine: Ideas Changing The World: 2014Nobel Prize in Physiology or Medicine: Ideas Changing The World: 2014
Nobel Prize in Physiology or Medicine: Ideas Changing The World: 2014
 
Introduction to economics
Introduction to economicsIntroduction to economics
Introduction to economics
 

Similar to Lec04-CS110 Computational Engineering

C cheat sheet for varsity (extreme edition)
C cheat sheet for varsity (extreme edition)C cheat sheet for varsity (extreme edition)
C cheat sheet for varsity (extreme edition)Saifur Rahman
 
C++ process new
C++ process newC++ process new
C++ process new敬倫 林
 
Unit I - 1R introduction to R program.pptx
Unit I - 1R introduction to R program.pptxUnit I - 1R introduction to R program.pptx
Unit I - 1R introduction to R program.pptxSreeLaya9
 
The best every notes on c language is here check it out
The best every notes on c language is here check it outThe best every notes on c language is here check it out
The best every notes on c language is here check it outrajatryadav22
 
Learning python
Learning pythonLearning python
Learning pythonFraboni Ec
 
Learning python
Learning pythonLearning python
Learning pythonJames Wong
 
Java Foundations: Data Types and Type Conversion
Java Foundations: Data Types and Type ConversionJava Foundations: Data Types and Type Conversion
Java Foundations: Data Types and Type ConversionSvetlin Nakov
 
Pointers in C Language
Pointers in C LanguagePointers in C Language
Pointers in C Languagemadan reddy
 
FALLSEM2022-23_BCSE202L_TH_VL2022230103292_Reference_Material_I_08-08-2022_C_...
FALLSEM2022-23_BCSE202L_TH_VL2022230103292_Reference_Material_I_08-08-2022_C_...FALLSEM2022-23_BCSE202L_TH_VL2022230103292_Reference_Material_I_08-08-2022_C_...
FALLSEM2022-23_BCSE202L_TH_VL2022230103292_Reference_Material_I_08-08-2022_C_...AntareepMajumder
 

Similar to Lec04-CS110 Computational Engineering (20)

c-programming
c-programmingc-programming
c-programming
 
C cheat sheet for varsity (extreme edition)
C cheat sheet for varsity (extreme edition)C cheat sheet for varsity (extreme edition)
C cheat sheet for varsity (extreme edition)
 
C++ process new
C++ process newC++ process new
C++ process new
 
Unit I - 1R introduction to R program.pptx
Unit I - 1R introduction to R program.pptxUnit I - 1R introduction to R program.pptx
Unit I - 1R introduction to R program.pptx
 
C++
C++C++
C++
 
C
CC
C
 
The best every notes on c language is here check it out
The best every notes on c language is here check it outThe best every notes on c language is here check it out
The best every notes on c language is here check it out
 
Learning python
Learning pythonLearning python
Learning python
 
Learning python
Learning pythonLearning python
Learning python
 
Learning python
Learning pythonLearning python
Learning python
 
Learning python
Learning pythonLearning python
Learning python
 
Learning python
Learning pythonLearning python
Learning python
 
Learning python
Learning pythonLearning python
Learning python
 
Learning python
Learning pythonLearning python
Learning python
 
Chapter1.pptx
Chapter1.pptxChapter1.pptx
Chapter1.pptx
 
Java Foundations: Data Types and Type Conversion
Java Foundations: Data Types and Type ConversionJava Foundations: Data Types and Type Conversion
Java Foundations: Data Types and Type Conversion
 
Pointers in C Language
Pointers in C LanguagePointers in C Language
Pointers in C Language
 
FALLSEM2022-23_BCSE202L_TH_VL2022230103292_Reference_Material_I_08-08-2022_C_...
FALLSEM2022-23_BCSE202L_TH_VL2022230103292_Reference_Material_I_08-08-2022_C_...FALLSEM2022-23_BCSE202L_TH_VL2022230103292_Reference_Material_I_08-08-2022_C_...
FALLSEM2022-23_BCSE202L_TH_VL2022230103292_Reference_Material_I_08-08-2022_C_...
 
Advanced C - Part 2
Advanced C - Part 2Advanced C - Part 2
Advanced C - Part 2
 
Ch3
Ch3Ch3
Ch3
 

More from Sri Harsha Pamu

Lec21-CS110 Computational Engineering
Lec21-CS110 Computational EngineeringLec21-CS110 Computational Engineering
Lec21-CS110 Computational EngineeringSri Harsha Pamu
 
Lec19-CS110 Computational Engineering
Lec19-CS110 Computational EngineeringLec19-CS110 Computational Engineering
Lec19-CS110 Computational EngineeringSri Harsha Pamu
 
Lec16-CS110 Computational Engineering
Lec16-CS110 Computational EngineeringLec16-CS110 Computational Engineering
Lec16-CS110 Computational EngineeringSri Harsha Pamu
 
Lec15-CS110 Computational Engineering
Lec15-CS110 Computational EngineeringLec15-CS110 Computational Engineering
Lec15-CS110 Computational EngineeringSri Harsha Pamu
 
Lec14-CS110 Computational Engineering
Lec14-CS110 Computational EngineeringLec14-CS110 Computational Engineering
Lec14-CS110 Computational EngineeringSri Harsha Pamu
 
Lec12-CS110 Computational Engineering
Lec12-CS110 Computational EngineeringLec12-CS110 Computational Engineering
Lec12-CS110 Computational EngineeringSri Harsha Pamu
 
Lec10-CS110 Computational Engineering
Lec10-CS110 Computational EngineeringLec10-CS110 Computational Engineering
Lec10-CS110 Computational EngineeringSri Harsha Pamu
 
Lec09-CS110 Computational Engineering
Lec09-CS110 Computational EngineeringLec09-CS110 Computational Engineering
Lec09-CS110 Computational EngineeringSri Harsha Pamu
 
Lec08-CS110 Computational Engineering
Lec08-CS110 Computational EngineeringLec08-CS110 Computational Engineering
Lec08-CS110 Computational EngineeringSri Harsha Pamu
 
Lec07-CS110 Computational Engineering
Lec07-CS110 Computational EngineeringLec07-CS110 Computational Engineering
Lec07-CS110 Computational EngineeringSri Harsha Pamu
 
Lec06-CS110 Computational Engineering
Lec06-CS110 Computational EngineeringLec06-CS110 Computational Engineering
Lec06-CS110 Computational EngineeringSri Harsha Pamu
 
Lec03-CS110 Computational Engineering
Lec03-CS110 Computational EngineeringLec03-CS110 Computational Engineering
Lec03-CS110 Computational EngineeringSri Harsha Pamu
 
Lec02-CS110 Computational Engineering
Lec02-CS110 Computational EngineeringLec02-CS110 Computational Engineering
Lec02-CS110 Computational EngineeringSri Harsha Pamu
 
Lec01-CS110 Computational Engineering
Lec01-CS110 Computational EngineeringLec01-CS110 Computational Engineering
Lec01-CS110 Computational EngineeringSri Harsha Pamu
 
Lec1- CS110 Computational Engineering
Lec1- CS110 Computational EngineeringLec1- CS110 Computational Engineering
Lec1- CS110 Computational EngineeringSri Harsha Pamu
 
Lec25-CS110 Computational Engineering
Lec25-CS110 Computational EngineeringLec25-CS110 Computational Engineering
Lec25-CS110 Computational EngineeringSri Harsha Pamu
 
Android vulnerability study
Android vulnerability studyAndroid vulnerability study
Android vulnerability studySri Harsha Pamu
 

More from Sri Harsha Pamu (20)

Lec21-CS110 Computational Engineering
Lec21-CS110 Computational EngineeringLec21-CS110 Computational Engineering
Lec21-CS110 Computational Engineering
 
Lec19-CS110 Computational Engineering
Lec19-CS110 Computational EngineeringLec19-CS110 Computational Engineering
Lec19-CS110 Computational Engineering
 
Lec16-CS110 Computational Engineering
Lec16-CS110 Computational EngineeringLec16-CS110 Computational Engineering
Lec16-CS110 Computational Engineering
 
Lec15-CS110 Computational Engineering
Lec15-CS110 Computational EngineeringLec15-CS110 Computational Engineering
Lec15-CS110 Computational Engineering
 
Lec14-CS110 Computational Engineering
Lec14-CS110 Computational EngineeringLec14-CS110 Computational Engineering
Lec14-CS110 Computational Engineering
 
Lec13
Lec13Lec13
Lec13
 
Lec12-CS110 Computational Engineering
Lec12-CS110 Computational EngineeringLec12-CS110 Computational Engineering
Lec12-CS110 Computational Engineering
 
Lec10-CS110 Computational Engineering
Lec10-CS110 Computational EngineeringLec10-CS110 Computational Engineering
Lec10-CS110 Computational Engineering
 
Lec09-CS110 Computational Engineering
Lec09-CS110 Computational EngineeringLec09-CS110 Computational Engineering
Lec09-CS110 Computational Engineering
 
Lec08-CS110 Computational Engineering
Lec08-CS110 Computational EngineeringLec08-CS110 Computational Engineering
Lec08-CS110 Computational Engineering
 
Lec07-CS110 Computational Engineering
Lec07-CS110 Computational EngineeringLec07-CS110 Computational Engineering
Lec07-CS110 Computational Engineering
 
Lec06-CS110 Computational Engineering
Lec06-CS110 Computational EngineeringLec06-CS110 Computational Engineering
Lec06-CS110 Computational Engineering
 
Lec03-CS110 Computational Engineering
Lec03-CS110 Computational EngineeringLec03-CS110 Computational Engineering
Lec03-CS110 Computational Engineering
 
Lec02-CS110 Computational Engineering
Lec02-CS110 Computational EngineeringLec02-CS110 Computational Engineering
Lec02-CS110 Computational Engineering
 
Lec01-CS110 Computational Engineering
Lec01-CS110 Computational EngineeringLec01-CS110 Computational Engineering
Lec01-CS110 Computational Engineering
 
Lec1- CS110 Computational Engineering
Lec1- CS110 Computational EngineeringLec1- CS110 Computational Engineering
Lec1- CS110 Computational Engineering
 
Lec25-CS110 Computational Engineering
Lec25-CS110 Computational EngineeringLec25-CS110 Computational Engineering
Lec25-CS110 Computational Engineering
 
Android..imp google
Android..imp googleAndroid..imp google
Android..imp google
 
Android vulnerability study
Android vulnerability studyAndroid vulnerability study
Android vulnerability study
 
Android gui framework
Android gui frameworkAndroid gui framework
Android gui framework
 

Recently uploaded

MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptxMULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptxAnupkumar Sharma
 
Keynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-designKeynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-designMIPLM
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxthorishapillay1
 
How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17Celine George
 
ISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITY
ISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITYISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITY
ISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITYKayeClaireEstoconing
 
Karra SKD Conference Presentation Revised.pptx
Karra SKD Conference Presentation Revised.pptxKarra SKD Conference Presentation Revised.pptx
Karra SKD Conference Presentation Revised.pptxAshokKarra1
 
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...Nguyen Thanh Tu Collection
 
Science 7 Quarter 4 Module 2: Natural Resources.pptx
Science 7 Quarter 4 Module 2: Natural Resources.pptxScience 7 Quarter 4 Module 2: Natural Resources.pptx
Science 7 Quarter 4 Module 2: Natural Resources.pptxMaryGraceBautista27
 
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxiammrhaywood
 
4.16.24 21st Century Movements for Black Lives.pptx
4.16.24 21st Century Movements for Black Lives.pptx4.16.24 21st Century Movements for Black Lives.pptx
4.16.24 21st Century Movements for Black Lives.pptxmary850239
 
Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatYousafMalik24
 
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdfLike-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdfMr Bounab Samir
 
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)lakshayb543
 
AUDIENCE THEORY -CULTIVATION THEORY - GERBNER.pptx
AUDIENCE THEORY -CULTIVATION THEORY -  GERBNER.pptxAUDIENCE THEORY -CULTIVATION THEORY -  GERBNER.pptx
AUDIENCE THEORY -CULTIVATION THEORY - GERBNER.pptxiammrhaywood
 
Transaction Management in Database Management System
Transaction Management in Database Management SystemTransaction Management in Database Management System
Transaction Management in Database Management SystemChristalin Nelson
 

Recently uploaded (20)

MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptxMULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
 
Keynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-designKeynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-design
 
YOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptx
YOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptxYOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptx
YOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptx
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptx
 
How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17
 
ISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITY
ISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITYISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITY
ISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITY
 
Karra SKD Conference Presentation Revised.pptx
Karra SKD Conference Presentation Revised.pptxKarra SKD Conference Presentation Revised.pptx
Karra SKD Conference Presentation Revised.pptx
 
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
 
Science 7 Quarter 4 Module 2: Natural Resources.pptx
Science 7 Quarter 4 Module 2: Natural Resources.pptxScience 7 Quarter 4 Module 2: Natural Resources.pptx
Science 7 Quarter 4 Module 2: Natural Resources.pptx
 
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
 
4.16.24 21st Century Movements for Black Lives.pptx
4.16.24 21st Century Movements for Black Lives.pptx4.16.24 21st Century Movements for Black Lives.pptx
4.16.24 21st Century Movements for Black Lives.pptx
 
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
 
Raw materials used in Herbal Cosmetics.pptx
Raw materials used in Herbal Cosmetics.pptxRaw materials used in Herbal Cosmetics.pptx
Raw materials used in Herbal Cosmetics.pptx
 
Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice great
 
LEFT_ON_C'N_ PRELIMS_EL_DORADO_2024.pptx
LEFT_ON_C'N_ PRELIMS_EL_DORADO_2024.pptxLEFT_ON_C'N_ PRELIMS_EL_DORADO_2024.pptx
LEFT_ON_C'N_ PRELIMS_EL_DORADO_2024.pptx
 
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdfLike-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
 
YOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptx
YOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptxYOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptx
YOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptx
 
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
 
AUDIENCE THEORY -CULTIVATION THEORY - GERBNER.pptx
AUDIENCE THEORY -CULTIVATION THEORY -  GERBNER.pptxAUDIENCE THEORY -CULTIVATION THEORY -  GERBNER.pptx
AUDIENCE THEORY -CULTIVATION THEORY - GERBNER.pptx
 
Transaction Management in Database Management System
Transaction Management in Database Management SystemTransaction Management in Database Management System
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? */
  • 9. Intelligent Compilers #include <stdio.h> main() { float a; a = 1.2345; printf("%fn",a); printf("%.2fn",a); } /* Output is 1.234500 and 1.23 */
  • 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
  • 26. Creative Problem - 2 • The Gossip Problem
  • 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.
  • 28. For n = 4 (Step 1) 1,2 1,2 3,4 3,4
  • 29. For n = 4 (Step 2) 1,2,3,4 1,2,3,4 1,2,3,4 1,2,3,4