lec04-cs110 computational engineering

Post on 20-Jan-2015

44 Views

Category:

Education

1 Downloads

Preview:

Click to see full reader

DESCRIPTION

A keynote on Problem Solving using Computers

TRANSCRIPT

CS110: Models of Computing

Lecture 4V. 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 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

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.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);}

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? */

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 */

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 */

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

ALU CUCPU

Memory OutputDevice

Data pathControl 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 DecimalInformation Code

• Every key in your keyboard has an 8-bitequivalent code

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

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

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

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

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

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 representedusing n bits, how many values can berepresented if the integer is– Unsigned– Signed

• Write a program that shall add any twogiven 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 %d\n”,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 %d\n”,sum);}Readbility enhanced and Syntax errors rectified

Creative Problem - 2

• The Gossip Problem

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.

For n = 4 (Step 1)

1,2

3,4 3,4

1,2

For n = 4 (Step 2)

1,2,3,4

1,2,3,4 1,2,3,4

1,2,3,4

Thank You

top related