C Program Structures

Similar documents
Computer Science & Engineering 150A Problem Solving Using Computers

Computer Science & Engineering 150A Problem Solving Using Computers. Chapter 3. Existing Information. Notes. Notes. Notes. Lecture 03 - Functions

ET156 Introduction to C Programming

ANSI C Programming Simple Programs

Chapter 3. Computer Science & Engineering 155E Computer Science I: Systems Engineering Focus. Existing Information.

6-1 (Function). (Function) !*+!"#!, Function Description Example. natural logarithm of x (base e) rounds x to smallest integer not less than x

Operators and Expression. Dr Muhamad Zaini Yunos JKBR, FKMP

Structured Programming. Dr. Mohamed Khedr Lecture 4

Introduction to Computers II Lecture 4. Dr Ali Ziya Alkar Dr Mehmet Demirer

Function I/O. Function Input and Output. Input through actual parameters. Output through return value. Input/Output through side effects

Function I/O. Last Updated 10/30/18

Lesson #3. Variables, Operators, and Expressions. 3. Variables, Operators and Expressions - Copyright Denis Hamelin - Ryerson University

Chapter 4: Basic C Operators

CC112 Structured Programming

Laboratory 2: Programming Basics and Variables. Lecture notes: 1. A quick review of hello_comment.c 2. Some useful information

C++ Programming Lecture 11 Functions Part I

ET156 Introduction to C Programming

ME 172. Lecture 2. Data Types and Modifier 3/7/2011. variables scanf() printf() Basic data types are. Modifiers. char int float double

Lecture 14. Daily Puzzle. Math in C. Rearrange the letters of eleven plus two to make this mathematical statement true. Eleven plus two =?

Programming Language A

LESSON 5 FUNDAMENTAL DATA TYPES. char short int long unsigned char unsigned short unsigned unsigned long

Chapter 2: Overview of C. Problem Solving & Program Design in C

C Functions. CS 2060 Week 4. Prof. Jonathan Ventura

Fundamentals of Programming & Procedural Programming

A) Central Processing Unit (CPU = Arithmetic-Logic Unit (ALU) + Control Unit (CU))

Chapter 5 C Functions

CpSc 111 Lab 3 Integer Variables, Mathematical Operations, & Redirection

CpSc 1111 Lab 6 Conditional Statements, Loops, the Math Library, and Random Numbers What s the Point?

1 Introduction to C part I

Bil 104 Intiroduction To Scientific And Engineering Computing. Lecture 5. Playing with Data Modifiers and Math Functions Getting Controls

Integer Representation. Variables. Real Representation. Integer Overflow/Underflow

Chapter 4 Expression & Operators

CT 229 Java Syntax Continued

BIL 104E Introduction to Scientific and Engineering Computing. Lecture 4

Introduction to Engineering gii

C Syntax Arrays and Loops Math Strings Structures Pointers File I/O. Final Review CS Prof. Jonathan Ventura. Prof. Jonathan Ventura Final Review

Functions. Systems Programming Concepts

CpSc 1011 Lab 3 Integer Variables, Mathematical Operations, & Redirection

C Programming Language

C Functions. 5.2 Program Modules in C

ECE15: Lab #3. Problem 1. University of California San Diego ( 1) + x4. + x8 + (1)

Lecture 16. Daily Puzzle. Functions II they re back and they re not happy. If it is raining at midnight - will we have sunny weather in 72 hours?

3.1. Chapter 3: The cin Object. Expressions and Interactivity

Introduction to Functions in C. Dr. Ahmed Telba King Saud University College of Engineering Electrical Engineering Department

Arithmetic Expressions in C

ECET 264 C Programming Language with Applications

sends the formatted data to the standard output stream (stdout) int printf ( format_string, argument_1, argument_2,... ) ;

Beginning C Programming for Engineers

Introduction to Programming

Dr M Kasim A Jalil. Faculty of Mechanical Engineering UTM (source: Deitel Associates & Pearson)

C Programs: Simple Statements and Expressions

1001ICT Introduction To Programming Lecture Notes

Summary of basic C++-commands

Chapter 6 - Notes User-Defined Functions I

2. Numbers In, Numbers Out

Functions. Functions are everywhere in C. Pallab Dasgupta Professor, Dept. of Computer Sc & Engg INDIAN INSTITUTE OF TECHNOLOGY KHARAGPUR

Applied Programming and Computer Science, DD2325/appcs15 PODF, Programmering och datalogi för fysiker, DA7011

Lecture 3. Review. CS 141 Lecture 3 By Ziad Kobti -Control Structures Examples -Built-in functions. Conditions: Loops: if( ) / else switch

Data Type Fall 2014 Jinkyu Jeong

CSI31 Lecture 5. Topics: 3.1 Numeric Data Types 3.2 Using the Math Library 3.3 Accumulating Results: Factorial

Computer Programming

Programming in C Quick Start! Biostatistics 615 Lecture 4

Computer Programming 5th Week loops (do-while, for), Arrays, array operations, C libraries

OUTLINE. Review Functions Pointers Function calls Array Pointers Pointer Arrays Data Structures and dynamic Memory Function Pointers quicksort example

Variable and Data Type 2

WARM UP LESSONS BARE BASICS

INTRODUCTION TO C++ FUNCTIONS. Dept. of Electronic Engineering, NCHU. Original slides are from

Programming Fundamentals (CS 302 ) Dr. Ihsan Ullah. Lecturer Department of Computer Science & IT University of Balochistan

Methods CSC 121 Fall 2014 Howard Rosenthal

Python Lists: Example 1: >>> items=["apple", "orange",100,25.5] >>> items[0] 'apple' >>> 3*items[:2]

Technical Questions. Q 1) What are the key features in C programming language?

Expressions. Arithmetic expressions. Logical expressions. Assignment expression. n Variables and constants linked with operators

Methods CSC 121 Fall 2016 Howard Rosenthal

C Overview Fall 2015 Jinkyu Jeong

Lecture 2: C Programming Basic

Object-Based Programming. Programming with Objects

Computer Programming: Skills & Concepts (CP) arithmetic, if and booleans (cont)

2. Numbers In, Numbers Out

A function is a named group of statements developed to solve a sub-problem and returns a value to other functions when it is called.

Introduction to Computer Programming in Python Dr. William C. Bulko. Data Types

.. Cal Poly CPE 101: Fundamentals of Computer Science I Alexander Dekhtyar..

CS1010E Lecture 3 Simple C Programs Part 2

PROGRAMMING IN C AND C++:

CPE 101, reusing/mod slides from a UW course (used by permission) Lecture 5: Input and Output (I/O)

Fundamental of Programming (C)

Fundamental Data Types. CSE 130: Introduction to Programming in C Stony Brook University

CpSc 111 Lab 5 Conditional Statements, Loops, the Math Library, and Redirecting Input

Week 2: Console I/O and Operators Arithmetic Operators. Integer Division. Arithmetic Operators. Gaddis: Chapter 3 (2.14,3.1-6,3.9-10,5.

Expressions. Eric McCreath

Function Example. Function Definition. C Programming. Syntax. A small program(subroutine) that performs a particular task. Modular programming design

Outline. Functions. Functions. Predefined Functions. Example. Example. Predefined functions User-defined functions Actual parameters Formal parameters

CS16 Exam #1 7/17/ Minutes 100 Points total

Question 2. [5 points] Given the following symbolic constant definition

Chapter 2. Outline. Simple C++ Programs

CSE123. Program Design and Modular Programming Functions 1-1

Program Organization and Comments

Functions in C. Lecture Topics. Lecture materials. Homework. Machine problem. Announcements. ECE 190 Lecture 16 March 9, 2011

Methods CSC 121 Spring 2017 Howard Rosenthal

CpSc 1011 Lab 4 Formatting and Flow Control Windchill Temps

Numerical Analysis First Term Dr. Selcuk CANKURT

Transcription:

Review-1 Structure of C program Variable types and Naming Input and output Arithmetic operation 85-132 Introduction to C-Programming 9-1 C Program Structures #include <stdio.h> void main (void) { } declaration 1; declaration 2; statement 1; statement 2; 85-132 Introduction to C-Programming 9-2 1

C Program Structures /* C is case sensitive Must have a function called main Pre-made functions in libraries Have to #include (use) libraries to get functions (books) Body of a function in { } Have to declare all variables printf ( twice per day ), // controls the printer C statements end with a ; Comments always welcome Edit, compile & execute */ 85-132 Introduction to C-Programming 9-3 Variables Variable types: real, integer, and character different range, precision, and operation totally different presentation in computers: complier needs to know Naming: Meaningful but not too long, not to use reserved words /* more about comments which are welcome */ /* try to be informative */ Example: float tax; int year, i; income; //another way to have comments Introduction to C-Programming 9-4 2

Example-1. Integer or real Population, age, number of buildings Have to be real: if too large. e.g. world population Better be integer: if people don t feel comfortable seeing real numbers. e.g. 600.1 MDs serving Windsor Remember: round-off errors in presenting real numbers Be careful: calculation mixing integer and real 85-132 Introduction to C-Programming 9-5 Example-2. Character or Integer Phone number, SIN, income (round-up to dollar) Have to be integer: if will be in arithmetic operation by any chance Better be character: never be in arithmetic operation more flexible. e.g. ext or or space in phone # 85-132 Introduction to C-Programming 9-6 3

Quiz time: What to use, and why Bacteria population Saving account number Saving account balance Saving account holder s name Number of universities in a country Marks-lab assignments 85-132 Introduction to C-Programming 9-7 Quiz time: right/wrong, good/bad int union, int; int i; int j; int number_of_university_in_a_country; int no_of_univ; int phone#; char phone_no float distance_1; distance_2; float distance_a&b; distance-ab; 85-132 Introduction to C-Programming 9-8 4

Input/output: format Common component: ( format string, list of variables) Difference: &with input variable name: address of File pointer for files Suggestion: use printf to check input data and calculation 85-132 Introduction to C-Programming 9-9 Input and output Input Output Keyboard: scanf Screen/printer: printf File: fscanf file: fprintf Variable name with & without Both need format according to variable type Files: pointer, open file&indicate read or write, close file 85-132 Introduction to C-Programming 9-10 5

Input %f different from %lf; go with declared type %5.2f may cause you trouble; use %f for now one data line per fscanf; data file has to be text #of format=# of available, will not recycle format Don t use \n at the end of format pay attention to divider e.g. %f, &a 1.56 %f %f, &a, &b 1.56 2.47 %f,%f, &a, &b 1.56, 2.47 %f %f, &a, &b 1.56 2.47 85-132 Introduction to C-Programming 9-11 Scientific notation Format: %m.ne [sign]d.ddd[e][sign]dd Example: n=2 1.23e+07 or 1.23e+007 1.23E+00-1.23E-02 Display may lose accuracy, numbers in memory will not! 85-132 Introduction to C-Programming 9-12 6

Format: most often used int: %md %5d 1234 float: %m.nf %5.2f 12.34 float: %m.ne %6.1e -1.2e+01 : %m.nlf %10.6lf 1.234567 What are counted in m: +or-,.,e, and n What if field too small? Number too large? smart computer programmer Know your variable type! Be careful with small numbers using %f Safe to use %d, %f, %e 85-132 Introduction to C-Programming 9-13 Quiz time: Right or wrong FILE inp, outp; FILE *inp, *outp; float mark; scanf ( input.dat, mark); inp=fopen ( c:/myproject/input.dat ); inp=fopen ( c:/myproject/input.dat, r ); fscanf (inp, %d, mark); fscanf (inp, %f, &mark); Do it yourself: write a section to write a mark to file output.dat 85-132 Introduction to C-Programming 9-14 7

Assignment What is the meaning of = a=a+b a a+b Be careful with calculation involves integer One real, one integer, result real, but save a (real) result in an integer will lose.xxx float a; integer a; a=6/4.; a=6/4.; a=6./4; a=6./4; a=6./4.; a=6./4.; a=6/4; a=6/4; 85-132 Introduction to C-Programming 9-15 Combined assignment omit first operand: a+=4 i.e. a=a+4, try not to use them for now increment/decrement: often in loops, have to master omit first operand, omit 1 as the second, +- only rule: first come, first server e.g. j++: use j; then j=j+1 -- j: j=j-1; then use j 85-132 Introduction to C-Programming 9-16 8

Quiz time: what is printed int i=1,j=2; printf("\n value of j++ = %d",j++); printf("\n value of ++i = %d ",++i); printf("\n value of i,j,i/j= %d, %d, %d ",i,j,i/j); printf("\n value of j%%i = %d, ",j%i); printf("\n value of float i / float j= %f ",(float) i/(float)j); printf("\n value of i / float j= %f ", i/(float)j); printf("\n value of float i / j= %f ",(float)i/j); value of j++ = 2 value of ++i = 2 value of i,j,i/j= 2, 3, 0 value of j%i = 1, value of float i / float j= 0.666667 value of i / float j= 0.666667 value of float i / j= 0.666667 85-132 Introduction to C-Programming 9-17 Mathematical functions in C Mathematical functions offers capabilities not offered by arithmetic operators To use mathematical functions the headers <math.h> and/or <stdlib.h> have to be included /* Illustrate the use of math functions */ #include <stdio.h> #include <math.h> void main(void) { Pi, Radius, Surface; printf(" Enter the radius ---> " ); scanf("%lf", &Radius); Pi = 4.0*atan(1.0); /* calculate Pi */ Surface = Pi*pow(Radius, 2.0); /* calculate Pi */ printf("the surface of a circle of radius %10.3e is %10.4e %10.3e\n\n", Radius, Surface, Pi); } 85-132 Introduction to C-Programming 9-18 9

Mathematical functions in C List of some mathematical function in C (see table 3.10 pp 131) Name Purpose Argument( s) Return type cos(x), sin(x) tan(x) Returns cos, sin or tan of x (radians) exp(x) Returns e raised to the power x sprt(x) Returns the nonnegative square root of x pow(x,y) x is raised to the power y ceil(x) Returns smallest whole number not less than x abs(x) * Returns the absolute value of an integer x int int fabs(x) * Returns the absolute value of a float x *these function are included in the header stdlib.h 85-132 Introduction to C-Programming 9-19 Summary Language: rules to follow How to learn: mimic, read, write, rewrite, and rewrite Write a C program to convert kilometers to miles. 85-132 Introduction to C-Programming 9-20 10

Implementation: C program /* Converts distance in miles to kilometers */ # include <stdio.h> /* define printf, scanf */ # define KMS_PER_MILE 1.609 /* define constant */ main (void) { miles, kms; /* define variables */ printf ( Enter the distance in miles> ); /* get the distance in miles*/ scanf ( %lf, &miles); kms= KMS_PER_MILE * miles; /* convert to km */ } printf ( That equals %lf kilometers. \n, kms); /* display in km */ 85-132 Introduction to C-Programming 9-21 Your tasks Do your own review Print out Lecture 10 and preview 85-132 Introduction to C-Programming 9-22 11