Lec04-CS110 Computational Engineering
-
Upload
sriharsha-p -
Category
Education
-
view
45 -
download
1
description
Transcript of Lec04-CS110 Computational Engineering
![Page 1: Lec04-CS110 Computational Engineering](https://reader034.fdocuments.net/reader034/viewer/2022051818/54be2ff64a795949228b45ec/html5/thumbnails/1.jpg)
CS110: Models of Computing
Lecture 4V. Kamakoti
8th January 2008
![Page 2: Lec04-CS110 Computational Engineering](https://reader034.fdocuments.net/reader034/viewer/2022051818/54be2ff64a795949228b45ec/html5/thumbnails/2.jpg)
• 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
![Page 3: Lec04-CS110 Computational Engineering](https://reader034.fdocuments.net/reader034/viewer/2022051818/54be2ff64a795949228b45ec/html5/thumbnails/3.jpg)
Readability of Code
/* TITLE (COMMENT) */
/* program to calculate area of a circle *//*LIBRARY FILE ACCESS*/
#include <stdio.h>/* DEFINED CONSTANTS */
#define PI 3.14159
![Page 4: Lec04-CS110 Computational Engineering](https://reader034.fdocuments.net/reader034/viewer/2022051818/54be2ff64a795949228b45ec/html5/thumbnails/4.jpg)
Readability of Code/* FUNCTION HEADING */
main() { float radius, area; /* Indentation */ printf(“Radius = ? “); scanf(“%f”, &radius); area = PI * radius * radius; printf(“Area = %f”, area);}
![Page 5: Lec04-CS110 Computational Engineering](https://reader034.fdocuments.net/reader034/viewer/2022051818/54be2ff64a795949228b45ec/html5/thumbnails/5.jpg)
The C Preprocessor
• Preprocessor executes beforecompilation– 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
![Page 6: Lec04-CS110 Computational Engineering](https://reader034.fdocuments.net/reader034/viewer/2022051818/54be2ff64a795949228b45ec/html5/thumbnails/6.jpg)
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 ? */
![Page 7: Lec04-CS110 Computational Engineering](https://reader034.fdocuments.net/reader034/viewer/2022051818/54be2ff64a795949228b45ec/html5/thumbnails/7.jpg)
Use of #define/*Variable not declared. Error would be undefined variables */#define PI 3.14159main() { 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);}
![Page 8: Lec04-CS110 Computational Engineering](https://reader034.fdocuments.net/reader034/viewer/2022051818/54be2ff64a795949228b45ec/html5/thumbnails/8.jpg)
Sensible Outputsmain() {
float gross, tax, net; printf(“Gross Salary: “); scanf(“%f”,&gross); tax = 0.14 * gross; net = gross - tax; printf(“Taxes withheld: %.2f\n”,tax); printf(“Net Salary: %.2f”,net);} /* I am interested upto 2 decimals only *//* Can I pay 2.155 Rs? */
![Page 9: Lec04-CS110 Computational Engineering](https://reader034.fdocuments.net/reader034/viewer/2022051818/54be2ff64a795949228b45ec/html5/thumbnails/9.jpg)
Intelligent Compilers
#include <stdio.h>main() { float a; a = 1.2345; printf("%f\n",a); printf("%.2f\n",a);}/* Output is 1.234500 and 1.23 */
![Page 10: Lec04-CS110 Computational Engineering](https://reader034.fdocuments.net/reader034/viewer/2022051818/54be2ff64a795949228b45ec/html5/thumbnails/10.jpg)
Intelligent Compilers#include <stdio.h>main() { float a; a = 1.2365; printf("%f\n",a); printf("%.2f\n",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 */
![Page 11: Lec04-CS110 Computational Engineering](https://reader034.fdocuments.net/reader034/viewer/2022051818/54be2ff64a795949228b45ec/html5/thumbnails/11.jpg)
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
![Page 12: Lec04-CS110 Computational Engineering](https://reader034.fdocuments.net/reader034/viewer/2022051818/54be2ff64a795949228b45ec/html5/thumbnails/12.jpg)
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
![Page 13: Lec04-CS110 Computational Engineering](https://reader034.fdocuments.net/reader034/viewer/2022051818/54be2ff64a795949228b45ec/html5/thumbnails/13.jpg)
Block Diagram of A Computer
Input Device
ALU CUCPU
Memory OutputDevice
Data pathControl path
![Page 14: Lec04-CS110 Computational Engineering](https://reader034.fdocuments.net/reader034/viewer/2022051818/54be2ff64a795949228b45ec/html5/thumbnails/14.jpg)
Input to the system
• What you type is saved as ASCII codes– American Standard Code for Information
Interchange• IBM uses EBCDIC
– Extended Binary Coded DecimalInformation Code
• Every key in your keyboard has an 8-bitequivalent code
![Page 15: Lec04-CS110 Computational Engineering](https://reader034.fdocuments.net/reader034/viewer/2022051818/54be2ff64a795949228b45ec/html5/thumbnails/15.jpg)
With “n” bits….
• You can represent 2n different decimalnumbers 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
![Page 16: Lec04-CS110 Computational Engineering](https://reader034.fdocuments.net/reader034/viewer/2022051818/54be2ff64a795949228b45ec/html5/thumbnails/16.jpg)
With “8” bits….
• You can represent 256 different decimalnumbers and hence entities.
• So 256 different characters can berepresented 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
![Page 17: Lec04-CS110 Computational Engineering](https://reader034.fdocuments.net/reader034/viewer/2022051818/54be2ff64a795949228b45ec/html5/thumbnails/17.jpg)
Strings
• String is a collection of one or morecharacters
• 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
![Page 18: Lec04-CS110 Computational Engineering](https://reader034.fdocuments.net/reader034/viewer/2022051818/54be2ff64a795949228b45ec/html5/thumbnails/18.jpg)
Identifiers and keywords
main() { float num; num = 1.2365; printf("%f\n",num); printf("%.2f\n",num);}Try using printf as a variable - that is declarefloat a, printf; in the above program and compile
![Page 19: Lec04-CS110 Computational Engineering](https://reader034.fdocuments.net/reader034/viewer/2022051818/54be2ff64a795949228b45ec/html5/thumbnails/19.jpg)
Keywords
auto, extern, sizeof, break, float, static,char, case, switch, int, long, register, dodouble, signed, short, while, struct,typedef ……
A list is available in pg. 2.5 of the SchaumSeries book
![Page 20: Lec04-CS110 Computational Engineering](https://reader034.fdocuments.net/reader034/viewer/2022051818/54be2ff64a795949228b45ec/html5/thumbnails/20.jpg)
Variables
• Can be of several types (Datatypes)– int
• Integers• Variants: short, long, unsigned
– char• Character
– float• Real numbers• Variants: double (precision)
![Page 21: Lec04-CS110 Computational Engineering](https://reader034.fdocuments.net/reader034/viewer/2022051818/54be2ff64a795949228b45ec/html5/thumbnails/21.jpg)
Test Your Understanding
• Suppose an integer is representedusing n bits, how many values can berepresented if the integer is– Unsigned– Signed
• Write a program that shall add any twogiven integer numbers
![Page 22: Lec04-CS110 Computational Engineering](https://reader034.fdocuments.net/reader034/viewer/2022051818/54be2ff64a795949228b45ec/html5/thumbnails/22.jpg)
Answers
![Page 23: Lec04-CS110 Computational Engineering](https://reader034.fdocuments.net/reader034/viewer/2022051818/54be2ff64a795949228b45ec/html5/thumbnails/23.jpg)
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.
![Page 24: Lec04-CS110 Computational Engineering](https://reader034.fdocuments.net/reader034/viewer/2022051818/54be2ff64a795949228b45ec/html5/thumbnails/24.jpg)
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 %d\n”,num3);}
![Page 25: Lec04-CS110 Computational Engineering](https://reader034.fdocuments.net/reader034/viewer/2022051818/54be2ff64a795949228b45ec/html5/thumbnails/25.jpg)
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 %d\n”,sum);}Readbility enhanced and Syntax errors rectified
![Page 26: Lec04-CS110 Computational Engineering](https://reader034.fdocuments.net/reader034/viewer/2022051818/54be2ff64a795949228b45ec/html5/thumbnails/26.jpg)
Creative Problem - 2
• The Gossip Problem
![Page 27: Lec04-CS110 Computational Engineering](https://reader034.fdocuments.net/reader034/viewer/2022051818/54be2ff64a795949228b45ec/html5/thumbnails/27.jpg)
The problem
• Suppose there are n = 2k persons, each witha certain item of information. In each step,each person can communicate with anotherperson and share all the information he orshe knows (including information learned inprevious steps). A person cannotcommunicate with more than one person inany step. Design a communication (gossip)pattern such that after log2n (= k) steps,everyone knows everything.
![Page 28: Lec04-CS110 Computational Engineering](https://reader034.fdocuments.net/reader034/viewer/2022051818/54be2ff64a795949228b45ec/html5/thumbnails/28.jpg)
For n = 4 (Step 1)
1,2
3,4 3,4
1,2
![Page 29: Lec04-CS110 Computational Engineering](https://reader034.fdocuments.net/reader034/viewer/2022051818/54be2ff64a795949228b45ec/html5/thumbnails/29.jpg)
For n = 4 (Step 2)
1,2,3,4
1,2,3,4 1,2,3,4
1,2,3,4
![Page 30: Lec04-CS110 Computational Engineering](https://reader034.fdocuments.net/reader034/viewer/2022051818/54be2ff64a795949228b45ec/html5/thumbnails/30.jpg)
Thank You