C Programming. Charudatt Kadolkar. IIT Guwahati. C Programming p.1/34



Similar documents
ALGEBRA 2/ TRIGONOMETRY

The C Programming Language

FEEG Applied Programming 5 - Tutorial Session

Chapter One Introduction to Programming

arrays C Programming Language - Arrays

7th Marathon of Parallel Programming WSCAD-SSC/SBAC-PAD-2012

Approximating functions by Taylor Polynomials.

CSE373: Data Structures and Algorithms Lecture 3: Math Review; Algorithm Analysis. Linda Shapiro Winter 2015

Introduction to Matlab

Answers to Review Questions Chapter 7

MATLAB Basics MATLAB numbers and numeric formats

PRE-CALCULUS GRADE 12

Random Fibonacci-type Sequences in Online Gambling

Introduction to Python

Why? A central concept in Computer Science. Algorithms are ubiquitous.

VIDEO SCRIPT: Data Management

Algebra and Geometry Review (61 topics, no due date)

Taylor and Maclaurin Series

PRI-(BASIC2) Preliminary Reference Information Mod date 3. Jun. 2015

Sources: On the Web: Slides will be available on:

Chapter 5 Functions. Introducing Functions

(hours worked,rateofpay, and regular and over time pay) for a list of employees. We have

Display Format To change the exponential display format, press the [MODE] key 3 times.

SOME EXCEL FORMULAS AND FUNCTIONS

SCIENTIFIC CALCULATOR OPERATION GUIDE. <Write View>

CS 141: Introduction to (Java) Programming: Exam 1 Jenny Orr Willamette University Fall 2013

An important theme in this book is to give constructive definitions of mathematical objects. Thus, for instance, if you needed to evaluate.

Senem Kumova Metin & Ilker Korkmaz 1

Conditionals (with solutions)

The C Programming Language course syllabus associate level

Final Exam Review: VBA

Chapter 2: Elements of Java

Data Structures. Algorithm Performance and Big O Analysis

Moving from CS 61A Scheme to CS 61B Java

TI-30XS MultiView and TI-30XB MultiView Scientific Calculator

RELEASED. Student Booklet. Precalculus. Fall 2014 NC Final Exam. Released Items

FX 115 MS Training guide. FX 115 MS Calculator. Applicable activities. Quick Reference Guide (inside the calculator cover)

(!' ) "' # "*# "!(!' +,

Solutions to Exercises, Section 5.1

Regression Verification: Status Report

USE OF A SINGLE ELEMENT WATTMETER OR WATT TRANSDUCER ON A BALANCED THREE-PHASE THREE-WIRE LOAD WILL NOT WORK. HERE'S WHY.

Laboratory 3 Learning C and Sinewave Generation

Using the RDTSC Instruction for Performance Monitoring

Scan Conversion of Filled Primitives Rectangles Polygons. Many concepts are easy in continuous space - Difficult in discrete space

JDK 1.5 Updates for Introduction to Java Programming with SUN ONE Studio 4

CD-ROM Appendix E: Matlab

Exact Values of the Sine and Cosine Functions in Increments of 3 degrees

Simple C++ Programs. Engineering Problem Solving with C++, Etter/Ingber. Dev-C++ Dev-C++ Windows Friendly Exit. The C++ Programming Language

#820 Computer Programming 1A

fx-92b Collège 2D+ User s Guide CASIO Worldwide Education Website CASIO EDUCATIONAL FORUM

Thnkwell s Homeschool Precalculus Course Lesson Plan: 36 weeks

Writing Applications for the GPU Using the RapidMind Development Platform

Arrays in Java. Working with Arrays

South Carolina College- and Career-Ready (SCCCR) Pre-Calculus

fx-83gt PLUS fx-85gt PLUS User s Guide

ADVANCED SCHOOL OF SYSTEMS AND DATA STUDIES (ASSDAS) PROGRAM: CTech in Computer Science

COMPUTER SCIENCE 1999 (Delhi Board)

Common Core Standards Practice Week 8

ERDAS Spatial Modeler Language Reference Manual

CS 241 Data Organization Coding Standards

Precalculus REVERSE CORRELATION. Content Expectations for. Precalculus. Michigan CONTENT EXPECTATIONS FOR PRECALCULUS CHAPTER/LESSON TITLES

Name: Class: Date: 9. The compiler ignores all comments they are there strictly for the convenience of anyone reading the program.

Introduction to Python

Data Structures and Algorithms

Efficiency of algorithms. Algorithms. Efficiency of algorithms. Binary search and linear search. Best, worst and average case.

Section 1.1. Introduction to R n

Chapter 7 Outline Math 236 Spring 2001

Computer Science 2nd Year Solved Exercise. Programs 3/4/2013. Aumir Shabbir Pakistan School & College Muscat. Important. Chapter # 3.

CP Lab 2: Writing programs for simple arithmetic problems

Chapter. Numerical Calculations

CORDIC: How Hand Calculators Calculate

Classroom Tips and Techniques: The Student Precalculus Package - Commands and Tutors. Content of the Precalculus Subpackage

JAVA ARRAY EXAMPLE PDF

CUDA Programming. Week 4. Shared memory and register

MULTIPLE CHOICE. Choose the one alternative that best completes the statement or answers the question.

2009 Chicago Area All-Star Math Team Tryouts Solutions

6.1 Add & Subtract Polynomial Expression & Functions

Programming Exercise 3: Multi-class Classification and Neural Networks

9 Control Statements. 9.1 Introduction. 9.2 Objectives. 9.3 Statements

Rigorous Software Development CSCI-GA

An Incomplete C++ Primer. University of Wyoming MA 5310

Recursive Algorithms. Recursion. Motivating Example Factorial Recall the factorial function. { 1 if n = 1 n! = n (n 1)! if n > 1

FX 260 Training guide. FX 260 Solar Scientific Calculator Overhead OH 260. Applicable activities

Computers. An Introduction to Programming with Python. Programming Languages. Programs and Programming. CCHSG Visit June Dr.-Ing.

With the Tan function, you can calculate the angle of a triangle with one corner of 90 degrees, when the smallest sides of the triangle are given:

Phys4051: C Lecture 2 & 3. Comment Statements. C Data Types. Functions (Review) Comment Statements Variables & Operators Branching Instructions

Lectures 5-6: Taylor Series

Chapter 6: Programming Languages

Crash Course in Java

Programming Languages & Tools

TivaWare Utilities Library

Debugging. Common Semantic Errors ESE112. Java Library. It is highly unlikely that you will write code that will work on the first go

Accredited Specimen Mark Scheme

13 File Output and Input

Boolean Expressions, Conditions, Loops, and Enumerations. Precedence Rules (from highest to lowest priority)

Finding Solutions of Polynomial Equations

Computer Programming I

Mathematics Course 111: Algebra I Part IV: Vector Spaces

Operating Systems and Programming Languages

Introduction to Object-Oriented Programming

Transcription:

C Programming Charudatt Kadolkar IIT Guwahati C Programming p.1/34

We want to print a table of sine function. The output should look like: 0 0.0000 20 0.3420 40 0.6428 60 0.8660 80 0.9848 100 0.9848 120 0.8660 140 0.6428 160 0.3420 180-0.0000 C Programming p.2/34

main() int float degrees; radians; degrees = 0; while ( degrees <= 180 ) radians = degrees * 3.141592654 / 180.0; printf("%3d %8.4f\n",degrees,sin(radians) ); degrees = degrees + 20; C Programming p.3/34

return statement return statement returns the control to operating system, or in other words, terminates the program C Programming p.4/34

While Loop Syntax for while loop is while ( condition ) statement1; or while ( condition ) statement1; statement2; : : C Programming p.5/34

main() int degrees; degrees :? float radians; radians :? degrees = 0; while ( degrees <= 180 ) radians = degrees * 3.141592654 / 180.0; printf("%3d %8.4f\n",degrees,sin(radians) ); degrees = degrees + 20; C Programming p.6/34

main() int degrees; degrees : 0 float radians; radians :? degrees = 0; while ( degrees <= 180 ) radians = degrees * 3.141592654 / 180.0; printf("%3d %8.4f\n",degrees,sin(radians) ); degrees = degrees + 20; C Programming p.7/34

main() int degrees; degrees : 0 float radians; radians :? degrees = 0; while ( degrees <= 180 ) radians = degrees * 3.141592654 / 180.0; printf("%3d %8.4f\n",degrees,sin(radians) ); degrees = degrees + 20; C Programming p.8/34

main() int degrees; degrees : 0 float radians; radians : 0.0000 degrees = 0; while ( degrees <= 180 ) radians = degrees * 3.141592654 / 180.0; printf("%3d %8.4f\n",degrees,sin(radians) ); degrees = degrees + 20; C Programming p.9/34

main() int degrees; degrees : 0 float radians; radians : 0.0000 degrees = 0; while ( degrees <= 180 ) radians = degrees * 3.141592654 / 180.0; printf("%3d %8.4f\n",degrees,sin(radians) ); degrees = degrees + 20; C Programming p.10/34

main() int degrees; degrees : 20 float radians; radians : 0.0000 degrees = 0; while ( degrees <= 180 ) radians = degrees * 3.141592654 / 180.0; printf("%3d %8.4f\n",degrees,sin(radians) ); degrees = degrees + 20; C Programming p.11/34

main() int degrees; degrees : 20 float radians; radians : 0.0000 degrees = 0; while ( degrees <= 180 ) radians = degrees * 3.141592654 / 180.0; printf("%3d %8.4f\n",degrees,sin(radians) ); degrees = degrees + 20; C Programming p.12/34

main() int degrees; degrees : 20 float radians; radians : 0.3491 degrees = 0; while ( degrees <= 180 ) radians = degrees * 3.141592654 / 180.0; printf("%3d %8.4f\n",degrees,sin(radians) ); degrees = degrees + 20; C Programming p.13/34

main() int degrees; degrees : 20 float radians; radians : 0.3491 degrees = 0; while ( degrees <= 180 ) radians = degrees * 3.141592654 / 180.0; printf("%3d %8.4f\n",degrees,sin(radians) ); degrees = degrees + 20; C Programming p.14/34

main() int degrees; degrees : 40 float radians; radians : 0.3491 degrees = 0; while ( degrees <= 180 ) radians = degrees * 3.141592654 / 180.0; printf("%3d %8.4f\n",degrees,sin(radians) ); degrees = degrees + 20; C Programming p.15/34

main() int degrees; degrees : 180 float radians; radians : 2.7925 degrees = 0; while ( degrees <= 180 ) radians = degrees * 3.141592654 / 180.0; printf("%3d %8.4f\n",degrees,sin(radians) ); degrees = degrees + 20; C Programming p.16/34

main() int degrees; degrees : 180 float radians; radians : 2.7925 degrees = 0; while ( degrees <= 180 ) radians = degrees * 3.141592654 / 180.0; printf("%3d %8.4f\n",degrees,sin(radians) ); degrees = degrees + 20; C Programming p.17/34

main() int degrees; degrees : 180 float radians; radians : 3.1416 degrees = 0; while ( degrees <= 180 ) radians = degrees * 3.141592654 / 180.0; printf("%3d %8.4f\n",degrees,sin(radians) ); degrees = degrees + 20; C Programming p.18/34

main() int degrees; degrees : 180 float radians; radians : 3.1416 degrees = 0; while ( degrees <= 180 ) radians = degrees * 3.141592654 / 180.0; printf("%3d %8.4f\n",degrees,sin(radians) ); degrees = degrees + 20; C Programming p.19/34

main() int degrees; degrees : 200 float radians; radians : 3.1416 degrees = 0; while ( degrees <= 180 ) radians = degrees * 3.141592654 / 180.0; printf("%3d %8.4f\n",degrees,sin(radians) ); degrees = degrees + 20; C Programming p.20/34

main() int degrees; degrees : 200 float radians; radians : 3.1416 degrees = 0; while ( degrees <= 180 ) radians = degrees * 3.141592654 / 180.0; printf("%3d %8.4f\n",degrees,sin(radians) ); degrees = degrees + 20; C Programming p.21/34

Formatting the Output degrees = 1234; printf("%d\n",degrees); 1234 printf("%3d\n",degrees); 1234 printf("%4d\n",degrees); 1234 printf("%5d\n",degrees); 1234 printf("%6d\n",degrees); 1234 radians = 123.456789 printf("%f\n",radians); 123.456789 printf("%4.4f\n",radians); 123.4568 printf("%6.4f\n",radians); 123.4568 printf("%8.4f\n",radians); 123.4567 printf("%10.4f\n",radians); 123.456789 C Programming p.22/34

Integer Sum For a positive integer n, let s = n + (n 1) + + 2 + 1 We want to write a program which calculates this sum. C Programming p.23/34

Integer Sum main() int sum; int i; int n; scanf("%d" &n); sum = 0; i = 1; while ( i <= n ) sum = sum + i; i = i + 1; printf("sum of first %d integers = %d\n",n,sum); C Programming p.24/34

Factorial Function For a positive integer n, n! = n (n 1) 2 1 We want to write a program which calculates factorial of n. C Programming p.25/34

Factorial Functions main() int factorial; int i; int n; scanf("%d" &n); factorial = i = 1; while ( i <= n ) factorial = factorial * i; i = i + 1; printf("factorial of %d = %d\n",n,factorial); C Programming p.26/34

Average Heights A class has n students. We want a program that reads the heights in cms and prints the average. C Programming p.27/34

Integer Sum main() int sum; int i; int n; int height; scanf("%d" &n); sum = 0; i = 1; while ( i <= n ) scanf("%d", &height); sum = sum + height; i = i + 1; printf("average Height = %d\n",n,sum/n); C Programming p.28/34

Exponential Function For any x R, e x = 1 + x + x2 2! + + xn n! + R n(x) where R n (x) = x n+1 e ξ /(n + 1)! for some 0 < c < x. If x is sufficiently small, we can approximate exponential function by a polynomial of degree n. We can do this in two ways. 1. Add first n terms of the series, for a given n. 2. Add first n terms of the series, such that R n (x) < ɛ for some given ɛ > 0. Also, R n (x) < ex n+1 /(n + 1)! if 0 < x < 1. C Programming p.29/34

Exponential Function float x, ex, t; int i, n; scanf("%d", scanf("%f", &n); &x); ex = 1; t = 1; i = 1; while ( i <= n ) t = t * x / i; ex = ex + t; i = i + 1; printf("exp(%f) = %f\n", x, ex); C Programming p.30/34

Exponential Function float x, ex, t, eps; int i; scanf("%f", scanf("%f", &eps); &x); ex = 1; t = 1; i = 1; while ( 1 > 0 ) t = t * x / i; if ( 2.718282 * t < eps ) break; ex = ex + t; i = i + 1; printf("exp(%f) = %f\n", x, ex); C Programming p.31/34

Arrays Array is a regular arrangement of objects. In C programming, int marks[10]; declaration, creates 10 int type variables, puts them in adjacent memories, and calls this array of variables by name marks. These are numbered from 0 to 9. We can access the 6 th memory location of marks array by typing marks[5]. marks[5] = 96; marks[7] = 27; total = marks[5] + marks[7]; We can enclose any integer expression i = 5; marks[i] = 96; marks[i+2] = 27; C Programming p.32/34

Arrays If x = (x 1, x 2, x 3 ) and y = (y 1, y 2, y 3 ) are two vectors in 3D, the angle between these is given by x «y cos 1 x y C Programming p.33/34

Angle between vectors float x[3]; float y[3]; float sx, sy, sxy; int i; scanf("%f%f%f", &x[0], &x[1], &x[2]); scanf("%f%f%f", &y[0], &y[1], &y[2]); sx = 0; sy = 0; sxy = 0; i = 0; while ( i < 3 ) sx = sx + x[i]*x[i]; sy = sy + y[i]*y[i]; sxy = sxy + x[i]*y[i]; angle = sxy / sqrt(sx*sy); C Programming p.34/34