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