1 Programming a Computer Lecture Ten. 2 Outline  A quick introduction to the programming language C  Introduction to software packages: Matlab for numerical.

Slides:



Advertisements
Similar presentations
Overview of programming in C C is a fast, efficient, flexible programming language Paradigm: C is procedural (like Fortran, Pascal), not object oriented.
Advertisements

CS1010 Programming Methodology
Lecture 2 Introduction to C Programming
Introduction to C Programming
1 Chapter 2 Introduction to Java Applications Introduction Java application programming Display ____________________ Obtain information from the.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line.
Introduction to C Programming
Primitive Data Types There are a number of common objects we encounter and are treated specially by almost any programming language These are called basic.
Basic C Programming Data Types and Arithmetic Operations 01/30/15.
 2007 Pearson Education, Inc. All rights reserved Introduction to C Programming.
More on Numerical Computation CS-2301 B-term More on Numerical Computation CS-2301, System Programming for Non-majors (Slides include materials from.
Introduction to C Programming Overview of C Hello World program Unix environment C programming basics.
1 Key Concepts:  Data types in C.  What is a variable?  Variable Declaration  Variable Initialization  Printf()  Scanf()  Working with numbers in.
1 Introduction to Computers and Programming Class 3 Introduction to C Professor Avi Rosenfeld.
C programming an Introduction. Types There are only a few basic data types in C. char a character int an integer, in the range -32,767 to 32,767 long.
Introduction to C Programming
1 Agenda Variables (Review) Example Input / Output Arithmetic Operations Casting Char as a Number (if time allow)
Basic Input/Output and Variables Ethan Cerami New York
Programming Variables. Named area in the computer memory, intended to contain values of a certain kind (integers, real numbers, characters etc.) They.
Computer Science 210 Computer Organization Introduction to C.
01- Intro-Java-part1 1 Introduction to Java, and DrJava Barb Ericson Georgia Institute of Technology June 2008.
EG280 - CS for Engineers Chapter 2, Introduction to C Part I Topics: Program structure Constants and variables Assignment Statements Standard input and.
 2003 Prentice Hall, Inc. All rights reserved. 1 Introduction to C++ Programming Outline Introduction to C++ Programming A Simple Program: Printing a.
C Programming Lecture 3. The Three Stages of Compiling a Program b The preprocessor is invoked The source code is modified b The compiler itself is invoked.
Georgia Institute of Technology Introduction to Java, and DrJava Barb Ericson Georgia Institute of Technology Aug 2005.
Goals of Course Introduction to the programming language C Learn how to program Learn ‘good’ programming practices.
C programming for Engineers Lcc compiler – a free C compiler available on the web. Some instructions.
Chapter 3 Processing and Interactive Input. 2 Assignment  The general syntax for an assignment statement is variable = operand; The operand to the right.
CNG 140 C Programming Lecture Notes 2 Processing and Interactive Input Spring 2007.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
C Programming n General Information on C n Data Types n Arithmetic Operators n Relational Operators n if, if-else, for, while by Kulapan Waranyuwat.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 2 Chapter 2 - Introduction to C Programming.
Constants Numeric Constants Integer Constants Floating Point Constants Character Constants Expressions Arithmetic Operators Assignment Operators Relational.
Introduction to C Programming Chapter 2 : Data Input, Processing and Output.
Lecture 2: Introduction to C Programming. OBJECTIVES In this lecture you will learn:  To use simple input and output statements.  The fundamental data.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 3: Introduction to C: Input & Output, Assignments, Math functions.
Ch13-1 Chap 13 Introduction to Matlab 13.1 Introduction MATLAB : The MATrix LABoratory program Not only is the MATLAB programming language exceptionally.
School of Computer Science & Information Technology G6DICP - Lecture 4 Variables, data types & decision making.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 2 - Introduction to C Programming Outline.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
CMPE13Cyrus Bazeghi 1 Chapter 11 Introduction to Programming in C.
Sudeshna Sarkar, IIT Kharagpur 1 Programming and Data Structure Sudeshna Sarkar Lecture 3.
1 Types of Programming Language (1) Three types of programming languages 1.Machine languages Strings of numbers giving machine specific instructions Example:
Sesi 0607EKT120/4 Computer Programming Week 5 – Repetition / Loops.
+ Note On the Use of Different Data Types Use the data type that conserves memory and still accomplishes the desired purpose. For example, depending on.
Lecture 3: More Java Basics Michael Hsu CSULA. Recall From Lecture Two  Write a basic program in Java  The process of writing, compiling, and running.
1 Lecture 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line of Text 2.3Another Simple C Program: Adding.
Simple C Programs.
Computer Science 210 Computer Organization
Chapter 2 - Introduction to C Programming
Revision Lecture
INC 161 , CPE 100 Computer Programming
Chapter 2 - Introduction to C Programming
Computer Science 210 Computer Organization
IPC144 Introduction to Programming Using C Week 2 – Lesson 1
Chapter 2 - Introduction to C Programming
Chapter 11 Introduction to Programming in C
INPUT & OUTPUT scanf & printf.
Introduction to Java, and DrJava part 1
Chapter 2 - Introduction to C Programming
Chapter 11 Introduction to Programming in C
Chapter 2 - Introduction to C Programming
Introduction to Java, and DrJava
Chapter 11 Introduction to Programming in C
EECE.2160 ECE Application Programming
Chapter 2 - Introduction to C Programming
Introduction to Java, and DrJava part 1
DATA TYPES There are four basic data types associated with variables:
Introduction to C Programming
C Programming Mr. KAJAL MAJI ASSISTANT PROFESSOR DEPARTMENT OF PHYSICS
Presentation transcript:

1 Programming a Computer Lecture Ten

2 Outline  A quick introduction to the programming language C  Introduction to software packages: Matlab for numerical and matrix computation Mathematica for symbolic computation

3 Why C?  Standard and portable  Simple, concise, and fast  All programming languages are conceptually similar; C is a good example  Other programming languages: Fortran, C++, Java, Pascal, Basic, Lisp, etc

4 Conceptual Frame of a Program Memory Data of various types Machine instructions to manipulate the data C program main() { int i, j, k; float a, b; char c; i = 1; j = 2; k = i+j; } Declaration of data types and given names Operations on variables

5 Data Types in C  int : typically 32-bit signed integers from to  float : 32-bit floating-point numbers  char : a byte for small integers, or for the ASCII character set  Declaration of these variables makes a reservation of memory space in computer

6 Conceptual Frame of a Program C program #include main() { int i; scanf(“%d”, &i); i = i + 1; printf(“%d”, i); } Input or read from key board Output to screen

7 I/O in C  printf(….) is used to report results or write a message to the user. E.g. printf(“Hello\n”); (print Hello & end of line)  printf(“result is %d”, i); (print result is X, where X is the value of i)  printf(“i=%d, f=%f, c=%c\n”, i, f, c); (print respectively the int, float, and char values, in the form i=X, f=Y, x=Z) The percent sign % followed by d, f, or c is formatting string for integer, float, or char type.

8 I/O in C  scanf(….) is opposite to printf(). It is used to read (scan) the input from keyboard. E.g.  scanf(“%d”, &i); (read an int)  scanf(“%f”, &a); (read into float variable a)  scanf(“%c”, &c); (read a char or byte into variable c) The ampersand sign & is called address operator, required for scanf() but not printf().

9 I/O Example #include main() { int i, j, k; printf(“enter two numbers: ”); scanf(“%d%d”, &i, &j); k = i+j; printf(“the sum equals %d\n”, k); } Needed for use I/O & required for read, but not print

10 Arithmetic in C main() { int i, j, k; float a, b, c; k = i + j; c = a + b; k = i – j; c = a – b; k = i * j; c = a * b; k = i / j; c = a / b; c = i*j + a/b; }

11 Math Functions in C #include main() { double a, b, c, d, f, x; x = 1.0; a = sin(x); b = cos(x); c = sqrt(x); d = log(x); f = pow(x, a); } square root function x raised to power a, x a double type is double precision floating point number, 14-decimal digit accuracy. required for using mathematical functions

12 Equal is not “equal”  In C or another programming languages, the equal sign “=” differs from ordinary math – the equal sign stands for assignment!  a = b+c; (assign the sum to a)  b+c = a; (this is meaningless in C)  To compare whether two numbers are equal or not, one uses “==”, the result is true (1) or false (0). E.g.:  K = (i==j) (K will be 1 if i and j are equal and 0 otherwise)

13 Control of the Program Execution  Suppose we want to compute the sum from 1 to 100, S= … We could write a program like in the next page:  But much better method is to use the control structure in C.

14 Sum from 1 to 100 #include main() { int S; S = ; printf(“sum from 1 to 100 is %d\n”, S); }

15 Sum from 1 to 100, using for(…) #include main() { int i, S; S = 0; for(i = 1; i <= 100; ++i) { S = S + i; } printf(“sum from 1 to 100 is %d\n”, S); } Starting with S = 0, run a for-loop, beginning with i equal to 1, increment i in steps of 1 (++i), adding i to S, until adding for the last time when i equals 100. Final S contains the answer.

16 The For-Loop  for(initialization; condition; increment) { body; }  E.g.: for(i = 0; i < n; ++i) { printf(“i=%d\n”, i); } If n = 4; the print-out will be i=0 i=1 i=2 i=3 ++i means adding one to i.

17 S = S + i ?  Mathematically, S ≠ S + i, unless i = 0.  Remember that “=” is not equality, by assignment. The meaning of above is to say, take the value of S and add it with i; the new value S + i replaces the old value in variable S.

18 While-Loop #include main() { int i, S; S = 0; i = 1; while (i <= 100) { S = S + i; } printf(“sum from 1 to 100 is %d\n”, S); } Keep adding while i is less than or equal to 100 [Stop otherwise].

19 Conditional Execution  We can make choices out of two with if statement: if (a < b) { do this; } else { do that; }

20 Compute  /* Compute the value of Pi, using the formula Pi/4 = 1 - 1/3 + 1/5 - 1/ */ #include main() { int i, N; double S, pi, term; printf("enter number of terms N:\n"); scanf("%d", &N); S = 0; for(i = 0; i <= N; ++i) { term = 1.0/(2*i+1); if((i % 2) == 0) { S = S + term; } else { S = S - term; } pi = 4.0*S; printf("pi approx = %f\n", pi); } (i % m) means i modulo m, i.e., the remainder of integer division, i/m. (i%2)==0 determines if i is even or odd. In C, an integer divided by an integer results an integer, e.g., 1/3 is 0. Thus we must write 1.0/(2*i+1), for the intended floating point division. To get 6-digit accuracy for , one need more than N=10 6 terms in the summation.

21 Euclidean Algorithm for Greatest Common Divisor 1.Let x takes the value of a, d value of b 2.Compute q and r in x = q*d + r 3.If r = 0, then gcd = d; stop 4.Let x takes the current value of d, replacing current value of d with value of r, go to step 2.

22 A GCD Program #include main() { int a, b, r, q, d, x; printf("enter two integers\n"); scanf("%d%d", &a, &b); x = a; d = b; q = x/d; r = x - q*d; while(r != 0) { x = d; d = r; q = x/d; r = x - q*d; } printf("GCD(%d, %d) = %d\n", a, b, d); } /* “!=” for not equal */ /* x/d is an integer */

23 How to Make a Running Program?  You need a computer  You need a compiler (Visual C++, or GCC, or ?)  Exactly what to do depends on your compiler; the compiler produces an executable file (with extension.exe on PCs)  gcc myprogram.c [with GCC] The name of a C program ends with.c

24 Demonstration of Matlab and Mathematica Software  Interactive system need much less programming. The user keys in a question or expression, the system gives you answer immediately, like a desk calculator. No compilation process is needed.

25 MATLAB (MATrix LABoratory) >>2 + 2 Ans = 4 >>x = x = 4 >>y = 2^2 + log(pi)*sin(x); y = Black type: user input Blue italic: system response Set value into variable x, and use it later, such as sin(x)

26 Matrix in MATLAB >>A=[1 2 3; 4 5 6; 7 8 9] A = >>A * A Matrices are square or rectangular rows of numbers. The concept is useful in solving equations. We can add, multiply, and take inverse of a matrix. MATLAB is designed for efficient matrix computations.

27 Mathematica In[1]:= Out[1]= 4 In[2]:= 2^100 Out[2]= In[3]:= N[Pi, 50] Out[3]= In[4]:= 2x + 5x + y Out[4]= 7x + y Black: user input, blue: machine output Mathematica can work with symbols Mathematica’s integers are not limited in sizes

28 Symbolic Computation In[5]:= Expand[(1+x)^2] Out[5]= 1 + 2x + x 2 In[6]:= D[x^2,x] Out[6]= 2x In[7]:= Integrate[Sin[x],x] Out[6]= –Cos[x] Expand the formula: Compute derivative Do integral

29 Summary  We learned data types (int, char, float and double), simple expressions like add, “=” for assignment, not equal, and flow control of programs, such as for, while, and if.  Interactive systems (such as Mathematica) can be more convenient to use. Calculation with formula is possible in symbolic systems.