1 بنام خدا زبان برنامه نویسی C (21814( Lecture 9 Selected Topics.

Slides:



Advertisements
Similar presentations
Character Arrays (Single-Dimensional Arrays) A char data type is needed to hold a single character. To store a string we have to use a single-dimensional.
Advertisements

Computer Programming w/ Eng. Applications
Files in C Rohit Khokher. Files in C Real life situations involve large volume of data and in such cases, the console oriented I/O operations pose two.
11-2 Identify the parts of the “main” function, which include Preprocessor Directives main function header main function body which includes Declaration.
Introduction to C Programming
1 Lecture 2  Input-Process-Output  The Hello-world program  A Feet-to-inches program  Variables, expressions, assignments & initialization  printf()
Character I/O. COMP104 Character I/O Slide 2 Data Type: char * Constant declaration const char star = '*'; * Variable declaration char resp; * Variable.
Characters. COMP104 Lecture 21 / Slide 2 Data Type: char * Constant declaration const char star = '*'; * Variable declaration char resp; * Variable assignment.
The scanf Function The scanf function reads input from the standard input device into one or more variables Example: scanf(“%lf”, &miles); Reads a real.
 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.
فایل پردازی در C File based Programming in C. انواع فایل متنی –سرعت بالا –حجم کمتر –امکان دسترسی تصادفی –حفظ امنیت داده ها دودویی (باینری) –امکان باز.
Input/Output  Input/Output operations are performed using input/output functions  Common input/output functions are provided as part of C’s standard.
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.
1 Lab Session-III CSIT-120 Fall 2000 Revising Previous session Data input and output While loop Exercise Limits and Bounds Session III-B (starts on slide.
1 بنام خدا زبان برنامه نویسی C (21814( Lecture 13 Chapter 13 Strings.
Chapter 9 Formatted Input/Output. Objectives In this chapter, you will learn: –To understand input and output streams. –To be able to use all print formatting.
13&14-2 Know the forms of loop statements in C (while,do/while,for). Understanding how data conversion occurs. Read/Write data in files using Unix redirection.
Chapter 2 Overview of C Part I J. H. Wang ( 王正豪 ), Ph. D. Assistant Professor Dept. Computer Science and Information Engineering National Taipei University.
CNG 140 C Programming (Lecture set 9) Spring Chapter 9 Character Strings.
STRING Dong-Chul Kim BioMeCIS UTA 10/7/
Programming I Introduction Introduction The only way to learn a new programming language is by writing programs in it. The first program to.
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 Lecture 10 Instructor: Wen, Chih-Yu Department of Electrical Engineering National Chung Hsing University.
Constants Numeric Constants Integer Constants Floating Point Constants Character Constants Expressions Arithmetic Operators Assignment Operators Relational.
1 بنام خدا زبان برنامه نویسی C (21814( Lecture 14 Structures.
به نام خدا برنامه سازی سمت سرور (php)
File IO and command line input CSE 2451 Rong Shi.
برنامه نویسی کامپیوتر بخش ششم مدرس: دکتر خالدیان.
Digital Computer Concept and Practice Copyright ©2012 by Jaejin Lee C Language Part 4.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 3: Introduction to C: Input & Output, Assignments, Math functions.
فصل سوم. 2 Phases of C++ Programs: 1.Edit 2.Preprocess 3.Compile 4.Link 5.Load 6.Execute Program is created in the editor and stored on disk. Preprocessor.
Chapter 3 Input and Output
1 بنام خدا زبان برنامه نویسی C (21814( Lecture 8 Chapters 8 & 9.
Chapter-4 Managing input and Output operation.  Reading, processing and writing of data are three essential functions of a computer program.  Most programs.
Data Types Modifiers Base data type Size Sign signed int 2 B unsigned float 4 B short double 8 B long char 1 B.
Agenda  Take up homework  Loops - Continued –For loops Structure / Example involving a for loop  Storing Characters in variables  Introduction to Functions.
Operating System Discussion Section. The Basics of C Reference: Lecture note 2 and 3 notes.html.
1 Lexical Elements, Operators, and the C System. 2 Outline Characters and Lexical Elements Syntax Rules Comments Keywords Identifiers Constants String.
Computer Science 1620 Sorting. cases exist where we would like our data to be in ascending (descending order) binary searching printing purposes selection.
CS 1704 Introduction to Data Structures and Software Engineering.
Variables in C Topics  Naming Variables  Declaring Variables  Using Variables  The Assignment Statement Reading  Sections
CMSC 104, Version 8/061L09VariablesInC.ppt Variables in C Topics Naming Variables Declaring Variables Using Variables The Assignment Statement Reading.
Files A collection of related data treated as a unit. Two types Text
CPT: Chars/ Computer Programming Techniques Semester 1, 1998 Objectives of these slides: –to look at how C processes characters 4. Character.
CCSA 221 Programming in C INPUT AND OUTPUT OPERATIONS IN C – PART 1 1.
Chapter 3: Formatted Input/Output 1 Chapter 3 Formatted Input/Output.
Lecture 3.1 Operators and Expressions Structured Programming Instructor: Prof. K. T. Tsang 1.
OPERATORS IN C CHAPTER 3. Expressions can be built up from literals, variables and operators. The operators define how the variables and literals in the.
Revision Lecture
Unit-4, Chapter-2 Managing Input and Output Operations
Introduction to C CSE 2031 Fall /3/ :33 AM.
Lecture 13 & 14.
Input/Output Input/Output operations are performed using input/output functions Common input/output functions are provided as part of C’s standard input/output.
بنام خدا زبان برنامه نویسی C (21814( Lecture 12 Selected Topics
Variables In programming, we often need to have places to store data. These receptacles are called variables. They are called that because they can change.
بنام خدا زبان برنامه نویسی C (21814( Lecture 10 Chapter 11
A First Book of ANSI C Fourth Edition
بنام خدا زبان برنامه نویسی C (21814( Lecture 3 Chapter 4
Your questions from last session
Homework Applied for cs240? (If not, keep at it!) 8/10 Done with HW1?
Module 12 Input and Output
Introduction to C EECS May 2019.
Variables in C Topics Naming Variables Declaring Variables
DATA TYPES There are four basic data types associated with variables:
Professor Jodi Neely-Ritz University of Florida
Presentation transcript:

1 بنام خدا زبان برنامه نویسی C (21814( Lecture 9 Selected Topics

2 زبان برنامه نویسی C (21814 ( نکاتی برگزیده از زبان برنامه نویسی C: روشی دیگر برای ارسال متن به روی خروجی: printf ("%s \n %s", "one", "two"); which prints: one two

3 زبان برنامه نویسی C (21814 ( Integers and strings can also use field width %nd, %ns where n is again min field width. Example: printf("%-10d%10s", 21,"abcde") 21 abcde 10 wide 8 wide5 wide

4 زبان برنامه نویسی C (21814 ( مثال: /* * Name: Nasser Salmasi * Date: 10/3/06 * Assignment: Term Project1 * Description: Johnson’s Algorithm. */ #include int main(void) { /* print the message */ printf(“Whatever you want!"); return 0; } File Header Comment Blank Lines (readability) Inline Comment Indenting – Use Tab Key!

5 زبان برنامه نویسی C (21814 ( مثال: Print the squares and cubes of integers from 1 through 3. Declare variables needed. Calculate the numbers. Print the results.

6 The Complete Program #include int main(void) { int n, square, cube;/* n is input, square & cube are output */ printf("Table of squares and cubes\n"); n = 1; square = n * n; cube = n * square; /* first line of output */ printf("%d %d %d\n", n, square, cube); n = 2; square = n * n; cube = n * square; /* second line of output */ printf("%d %d %d\n", n, square, cube); n = 3; square = n * n; cube = n * square; /* third line of output */ printf("%d %d %d\n", n, square, cube); return 0; } note

7 /* * Name: Nasser Salmasi * * Date: Oct 6, 2006 * Assignment: Squares Cubes * Description: Make a table of the integers from 1 – 3 and their squares and cubes. */ #include int main(void) { int n, square, cube;// n is input, square & cube are output printf("Table of squares and cubes\n"); n = 1; square = n * n; cube = n * square; // first line of output printf("%d %d %d\n", n, square, cube); n = 2; square = n * n; cube = n * square; // second line of output printf("%d %d %d\n", n, square, cube); n = 3; square = n * n; cube = n * square; // third line of output printf("%d %d %d\n", n, square, cube); return 0; } header comment inline comments indentation -- Use Tab Key! readability (blank lines)

8 زبان برنامه نویسی C (21814 ( Same Program in Unreadable Form #include int main(void) {int n, square, cube; printf("Table of squares and cubes\n"); n = 1; square = n * n; cube = n * square; printf("%d %d %d\n", n, square, cube); n = 2; square = n * n; cube = n * square; printf("%d %d %d\n", n, square, cube); n = 3; square = n * n; cube = n * square; printf("%d %d %d\n", n, square, cube); return 0; }

9 زبان برنامه نویسی C (21814 ( Precedence Precedence = rank or priority. Some operators have priority over others (i.e should be done first) * 3 same as 1 + (2 * 3) (i.e. = 7) i.e. * has higher precedence than binary + or - Parentheses force precedence. (1 + 2) * 3 is equal to 9

10 زبان برنامه نویسی C (21814 ( Associativity When operators have same precedence we must use rules of associativity (left to right or right to left) + - same precedence, left to right assoc means (((1+2)+3)-4)+5 = 7 * / % same precedence, left to right assoc. 5 / 2 * 2 means (5/2)*2 = 4 not 5/(2*2) = 1

11 زبان برنامه نویسی C (21814 ( Precedence /Associatively Table operationprecedenceassociatively ( ) 1 (highest)L to R +(plus) -(minus)2R to L * /%3L to R + -4L to R =(assignment)14 (lowest)R to L

12 زبان برنامه نویسی C (21814 ( Use Parentheses!!!!!!!! Best to force precedence and associatively with parentheses. Easier to read Everyone gets confused about these rules!

13 زبان برنامه نویسی C (21814 ( Increment, Decrement Examples Let b = 2 and c = 3. After assignment a = (++b) + (++c); the values will be a=7, b=3, c=4 After the assignment a = (b++) + (c++); the values will be a=5,b=3,c=4 --i; is a perfectly good statement. Same as i = i - 1;

14 زبان برنامه نویسی C (21814 ( Examples - Expressions If c = 0, then what are values of a, b, c after these two statements are executed. a = ++c; b = a++; a =, b =, c = Avoid expressions like a = c + ++c; where a variable and it's increment or decrement occur in same expression (see the code).

15 زبان برنامه نویسی C (21814 ( Assignment Operator An assignment has a value! a = (b = 0); same as a=0; b=0; or a = b = 0; (because of right to left associatively).

16 زبان برنامه نویسی C (21814 ( int a = -2, b = 3, c = 4, d = 5; d *= a b * c - c % 2; What is d after executing this statement? But, please don’t write like this!

17 زبان برنامه نویسی C (21814 ( Find the 8 Programming Errors #include ; #define TOPBOTTOM "III"; # define MIDDLE "\nI" void main(void) { printf("/n/n/n"); printf(TOPBOTTON) printf(MIDDLE); printf(\n); prinf(TOPBOTTOM); }

18 زبان برنامه نویسی C (21814 ( نحوه تعریف متغیر برای ذخیره کاراکتر برای معرفی متغیر به منظور ذخیره کاراکتر می توان بصورت زیر عمل نمود: char b = 'z'; در این حالت متغیر b می تواند یک کاراکتر A-Z, a-z, 0-9 و غیره را ذخیره کند. در واقع در این حالت فقط یک بایت برای کاراکترb اختصاص می یابد. می توان آرایه برای ذخیره کاراکتر نیز تعریف نمود )مثال) : char Name[20];

19 زبان برنامه نویسی C (21814 ( نحوه تعریف متغیر برای ذخیره کاراکتر و ارسال به خروجی: نحوه گرفتن کاراکتر از کاربر و همچنین ارسال آن به خروجی بصورت زیر است: char b = 'z'; b = getchar(); printf( “ %c”, b); putchar (b);

20 Some other useful functions These all require #include –isalpha(c) - returns nonzero if c is letter –isdigit(c) - returns nonzero if c is a digit (0-9) –isspace(c) - returns nonzero if c is space, tab or newline. –islower(c) - returns nonzero if c is lower case. –isupper(c) - returns nonzero if c is upper case. –toupper(c) – returns upper case char. –tolower(c) – returns lower case char.

21 زبان برنامه نویسی C (21814 ( برنامه زیر را در نظر بگیرید: int i; i = ; printf("%d %d %d", i, i+1, i*i); فکر می کنید نتیجه چه باشد؟

22 What’s going on here? Computers store only so many digits. When an operation creates more than they have, the higher digits are lost. Just like 5-digit odometer on a car. –What comes after 99,999? -- 00,000 right? Same in computers -- next integer after the largest integer is the smallest integer

23 زبان برنامه نویسی C (21814 ( نحوه بازکردن یک فایل در زبان برنامه نویسی :C خصوصیت های یک فایل: هر فایل دارای یک نام می باشد. هر فایل دارای اندازه (Size) می باشد (بر حسب کیلو بایت). هر فایل می تواند شامل انواع مختلفی از داده ها باشد. هر فایل را می توان بصورت اطلاعاتی که در یک دیسک جانبی ذخیره شده اند تعبیر نمود.

24 زبان برنامه نویسی C (21814 ( FILE* INDATA; // *fp means fp points to a FILE structure INDATA=fopen(“Data1.txt","r"); fscanf(INDATA," %i\n",&Machine_Number); for(f1=1;f1<=group_number;f1++){ for(f2=1;f2<=job_number[f1];f2++){ for(f3=1;f3<=Machine_Number;f3++){ fscanf(INDATA," %i",&time_M[f1][f2][f3]); } fscanf(INDATA,"\n"); } fclose(INDATA);

25 زبان برنامه نویسی C (21814 (نحوه خواندن یک فایل fopen(filename, mode) int a, b, c; FILE *fp; // *fp means fp points to a FILE structure fp = fopen("data.txt", "r"); fscanf(filepointer, string, &var1, &var2, …) // read three integers from the file fscanf(fp, "%d %d %d", &a, &b, &c); fclose(filepointer) fclose(fp);// close the file

26 زبان برنامه نویسی C (21814 (نحوه خواندن یک فایل Open Errors If fp == NULL, there was an error and your program cannot proceed! Possible causes: –filename doesn’t exist in current folder. –file is being used (i.e. is opened) by another application. –file is corrupted (i.e has been damaged). test for equality

27 زبان برنامه نویسی C (21814 (نحوه خواندن یک فایل File Mode - text files mode can be "r" "w", "a" "r" open a text file for reading from the beginning (fscanf) "w" open a text file for writing from the beginning (fprintf). Replaces any existing file or creates new one. "a" open a text file for writing starting at the end (fprintf).

28 زبان برنامه نویسی C (21814 (نحوه خواندن یک فایل In text files, numbers, characters, and strings are all stored as text characters. –'a' stored as the character a. –"a string" stored as the characters a string. – (a 4 byte int) stored as the 10 ASCII characters – (a 4 byte float) stored as the 7 ASCII characters White space is used to separate.

29 زبان برنامه نویسی C (21814 (نحوه خواندن یک فایل fscanf(filepointer, formatString, var, var,…) FILE *fp; int a, c; double b; fp = fopen("input.txt", "r"); fscanf(fp, "%d %lf %d", &a, &b, &c); printf("%d\n%f\n%d", a, b, c); fclose(fp); 234, 5.345, and 567 will be assigned to a, b, and c respectively input.txt white space

30 How scanf (or fscanf) Reads When using %d, %f, or %lf, scanf: 1.skips white space 2.reads the ASCII digits (and/or '+', '-', '.') until it encounters white space. 3.converts the digits to an integer, float, or double Returns number of successful conversions or EOF (if encountered an error).

31 زبان برنامه نویسی C (21814 ( نحوه نوشتن به روی فایل: برای نوشتن داخل یک فایل از دستور fprintf استفاده می کنیم. نحوه استفاده از این دستور همانند دستور printf است. مراجعه به مثال

32 Foxtrot Version

33 زبان برنامه نویسی C (21814 ( تمرین: برنامه ای بنویسید که تعداد دانشجویان ،نام دانشجو، شماره دانشجویی، نمرات میان ترم و پایان ترم دانشجویان کلاس را از فایل HW.txt گرفته، میانگین و واریانس نمرات میان ترم و پایان ترم را محاسبه کند. همچنین برای هر دانشجو اختلاف نمرات میان ترم و پایان ترم وی را با میانگین گزارش دهد و نتایج را در فایلی به نام Report.txt گزارش کند.

34 زبان برنامه نویسی C (21814 ( Bubble sort Sort an array of numbers or characters into numeric or alphabetic order. 1) Compare last two -- swap, if out of order 2)Compare next to last two -- etc. 3) Start again at end but stop one shy of beginning 4) Continue

35 زبان برنامه نویسی C (21814 ( Bubble sort first pass start loop loop loop loop end pass 1

36 زبان برنامه نویسی C (21814 ( Bubble sort -- second pass start pass two loop loop loop end of pass two

37 زبان برنامه نویسی C (21814 ( Bubble sort - 3rd pass start pass three loop loop end of pass three (Note: nothing actually done)

38 زبان برنامه نویسی C (21814 ( Bubble sort - 4rd pass start pass four loop end of pass four

39 زبان برنامه نویسی C (21814 ( Bubble sort - 5th pass start of pass done Note that this computer algorithm is quite different from the way we do this by hand. Another example of the difficulty of doing something with a computer that is easy with human brain.

40 زبان برنامه نویسی C (21814 ( Bubble sort - code void bubble(int a[], int n) { int i, j; // i counts passes, j cycles thru swap positions // numbers are: a[0], a[1], … a[n-2], a[n-1] for(i=0; i < n-1; ++i) for(j=n-1; j > i; --j) if( a[j-1] > a[j]) // if out of order, swap swap(&a[j-1], &a[j]); return; }

41 زبان برنامه نویسی C (21814 ( Bubble Sort Analysis Assume N items to sort. 1st pass N-1 loops. 2nd pass N-2 loops. Last pass 1 loop. (N-1) + (N-2) + … + 1 = N*(N-1)/2 Execution time increases as square of size, which is not as good as QuickSort, N*ln(N).