# Lec04-CS110 Computational Engineering

60 %
40 %
Information about Lec04-CS110 Computational Engineering
Education

Published on March 3, 2014

Author: SriHarsha1508

Source: slideshare.net

## Description

A keynote on Problem Solving using Computers

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

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

## Add a comment

 User name: Comment:

April 21, 2018

April 21, 2018

April 21, 2018

April 21, 2018

April 21, 2018

April 21, 2018

## Related pages

### Lec04-CS110 Computational Engineering - Education

5. The C Preprocessor • Preprocessor executes before compilation – Substitutes the value of PI at all places – You can decide on the precision by ...

### Lec26-CS110 Computational Engineering - Education

2. Pointers and Multidimensional Arrays • A two dimensional array is a collection of CONTIGUOUS one dimensional arrays. • int x[10][20] - can be ...

### Lec14-CS110 Computational Engineering - Education

Lec14-CS110 Computational Engineering Nov 11, 2014 Education sriharsha-p. ... Lec04-CS110 Computational Engineering. Engineering Computational Ecosystems.

### Lec10-CS110 Computational Engineering - Education

2. Arrays • Matrix is – A two dimensional array – Array of one dimensional arrays• Stored in memory – Row-wise (Row Major order) – Column-wise ...

### Computational Engineering | LinkedIn

View 5399 Computational Engineering posts, presentations, experts, and more. Get the professional knowledge you need on LinkedIn.

### Lec12-CS110 Computational Engineering - Education

1. CS110: Arrays in C Lecture 12 V. Kamakoti 4th February 2008. 2. Loops • for (j = 1; j <= 10; j++) – The initial and final conditions are known.

### CS-110 Computational Engineering Part 3 - Documents

CS-110 Computational Engineering Part 3. A. Avudainayagam Department of Mathematics. Root Finding: f(x)=0. Method 1: The Bisection method Th: If f(x) is ...