0 Arrays (1/2) #include /* count digits, white space, others */ main() { int c, i, nwhite, nother; int ndigit[10]; nwhite = nother = 0; for (i = 0; i <

Slides:



Advertisements
Similar presentations
COMP 2130 Intro Computer Systems Thompson Rivers University
Advertisements

Lectures 10 & 11.
LCS Non-Dynamic Version int function lcs (x, y, i, j) begin if (i = 0) or (j = 0) return 0; else if (x[i] = y[j]) return lcs(x, y, i-1, j-1)+1; else return.
1 Chapter 10 Strings and Pointers. 2 Introduction  String Constant  Example: printf(“Hello”); “Hello” : a string constant oA string constant is a series.
Lecture 20 Arrays and Strings
What is a pointer? First of all, it is a variable, just like other variables you studied So it has type, storage etc. Difference: it can only store the.
ECE Application Programming Instructor: Dr. Michael Geiger Spring 2012 Lecture 31: PE5.
Engineering Computing I Chapter 1 – Part B A Tutorial Introduction continued.
1 Homework Assignments Turn in HW1 (If not done yet, catch up!) Questions about HW1? Anyone still stuck on apply / UNIX account? Everyone have the books?
COMP1180 Review Date: 4 March, 2009 Time: 10:30am - 12:20pm Venue: –CS students -- FSC801C and FSC801D –IS and other students -- OEE1017 Remarks: – 1)
The C programming language: Introduction Fall 2003, Jen-Chang Liu.
Character Input and Output C and Data Structures Baojian Hua
Introduction to Computers and Programming Class 15 Introduction to C Professor Avi Rosenfeld.
0 Chap. 4 Functions and Program Structure 4.1 Basics of Functions 4.2 Functions Returning Non-integers 4.3 External Variables 4.4 Scope Rules 4.5 Header.
流程控制: while loop 迴圈 Test condition Enter loop Yes (non-0) Execute Loop body no exit F=0 F=F+20 … F=F
To remind us We finished the last day by introducing If statements Their structure was:::::::::
Imperative Programming Prof. Béat Hirsbrunner Amine Tafat, PhD Student Matthias Buchs and Raphaël Lesceux, Graduate Students Department of Informatics.
Functions / Procedures
0 Chap. 5 Pointers and Arrays 5.1Pointers and Adresses 5.2Pointers and Function Arguments 5.3Pointers and Arrays 5.4Address Arithmetic 5.5Character Pointers.
0 Chap. 2. Types, Operators, and Expressions 2.1Variable Names 2.2Data Types and Sizes 2.3Constants 2.4Declarations Imperative Programming, B. Hirsbrunner,
Prof. Béat Hirsbrunner Fulvio Frapolli, PhD Student (exercises) Bachelor students : - Major in computer science (first year, 2nd term) - Major in information.
Character Input and Output
Review: midterm #9 #include void main(void) { int c; c = getchar(); if(c>=48){ if(c
C Programming Strings. Array of characters – most common type of array in C  Let’s make them easier for use Denote the end of array using a special character.
0 5.6 Pointer Arrays; Pointers to Pointers Session May 2007 Since pointers are variables themselves, they can be stored in arrays just as other variables.
CMSC 104, Version 8/061L22Arrays1.ppt Arrays, Part 1 of 2 Topics Definition of a Data Structure Definition of an Array Array Declaration, Initialization,
C Programming A Modern Approach
Homework –Continue Reading K&R Chapter 2 –We’ll go over HW2 –HW3 is posted Questions?
1 COMP 2130 Introduction to Computer Systems Computing Science Thompson Rivers University.
Introduction to C Programming Summer 2014 COMP 2130 Intro Computer Systems Computing Science Thompson Rivers University.
Chapter 4 Functions and Program Structure Ku-Yaw Chang Assistant Professor, Department of Computer Science and Information Engineering.
Programming I Introduction Introduction The only way to learn a new programming language is by writing programs in it. The first program to.
CSC 270 – Survey of Programming Languages C Lecture 4 – Strings Heavily borrowed from Dr. Robert Siegfried and Dietel How to Program in C Powerpoint Presentations.
Outline Symbolic Constants Character Input and Output if … else switch…case.
C Program Design Functions and Program Structure 主講人:虞台文.
Programming Language  C Tutorial Introduction 主講人:虞台文.
Introduction to Computer Algorithmics and Programming Ceng 113 Program Control Statements.
#include int main(void) { printf("Hello, world!\n"); return 0; } entry point called on program start only one main( ) in any program # for preprocessor.
1/16 Programski jezik C Vladimir Filipović
5/27/2016 1R. Smith - University of St Thomas - Minnesota CISC 130: Today’s Class RecapRecap Drawing in 2 dimensionsDrawing in 2 dimensions Vertical HistogramVertical.
1 CSE1301 Computer Programming Lecture 12 Functions (Part 1)
File IO and command line input CSE 2451 Rong Shi.
Chapter 8: Arrays and Functions Department of Computer Science Foundation Year Program Umm Alqura University, Makkah Computer Programming Skills
Functions #include int sum(int i, int j); main() { int a, b, res; a = 1; b = 2; res = sum(a, b); printf("%d + %d = %d\n", a, b, res); } int sum(int i,
0 4.1 Basics of Functions + Execution Diagram + make 4.2 Functions Returning Non-integers + Prototype Function 4.3 External Variables 4.4 Scope Rules 4.5.
Computer programming Outline Functions [chap 8 – Kochan] –Defining a Function –Arguments and Local Variables Automatic Local.
CCSA 221 Programming in C CHAPTER 8 – PART 1 WORKING WITH FUNCTIONS 1.
1 CSE1301 Computer Programming Lecture 13 Functions (Part 1)
USER DEFINED FUNCTIONS Computer Programming Asst. Prof. Dr. Choopan Rattanapoka and Asst. Prof. Dr. Suphot Chunwiphat.
CECS 130 EXAM 1.  int main() { printf (“%c %c \n", 'a', 65); printf ("%d %ld\n", 1977, L); printf (" %10d \n", 1977); printf ("%010d \n", 1977);
Structured Programming Instructor: Prof. K. T. Tsang Lecture 6: Arrays 数据序列, 数组 1.
1 Homework –Continue Reading K&R Chapter 2 –We’ll go over HW2 at end of class today –Continue working on HW3 Questions?
Programming Language  C Functions and Program Structure 主講人:虞台文.
Prof. Béat Hirsbrunner Ammar Halabi, PhD student (exercises) Dani Rotzetter, Master student (exercises) Bachelor students : Major in computer science (3rd.
CS 261 C Basics Page 2 1/14/2016 CS 261, WSU Vancouver Primitive Types Notes: 4 A is a constant; B is a variable.
An Array Type For Strings. Two ways to represent strings – i.e. “Hello” cstring An array with base type char Older way of processing strings Null character.
2/22/2016 1R. Smith - University of St Thomas - Minnesota CISC 130: Today’s Class History Paper scheduleHistory Paper schedule RecapRecap Plus PlusPlus.
CS 1704 Introduction to Data Structures and Software Engineering.
19-Feb-02 Sudeshna Sarkar, CSE, IIT Kharagpur1 Arrays, Pointers, Strings Lecture 18 19/2/2002.
CPT: Chars/ Computer Programming Techniques Semester 1, 1998 Objectives of these slides: –to look at how C processes characters 4. Character.
Chap. 2. Types, Operators, and Expressions
Control Flow (Chapter 3)
Lecture 8 String 1. Concept of strings String and pointers
Kontrola toka izvršenja
read the next character into ch using getchar();
CS 1430: Programming in C++.
Structured Programming (Top Down Step Refinement)
Your questions from last session
Homework Applied for cs240? (If not, keep at it!) 8/10 Done with HW1?
Homework Homework Questions? Continue Reading K&R Chapter 2
Presentation transcript:

0 Arrays (1/2) #include /* count digits, white space, others */ main() { int c, i, nwhite, nother; int ndigit[10]; nwhite = nother = 0; for (i = 0; i < 10; ++i) ndigit[i] = 0; (KR p22) index ndigit ndigit[2] Imperative Programming, B. Hirsbrunner, diuf.unifr.ch/pai/ip Session March 2007

1 Arrays (2/2) while ((c = getchar()) != EOF) if (c >= '0' && c <= '9') ++ndigit[c - '0']; else if (c == ' ' || c == '\n' || c == '\t') ++nwhite; else ++nother; printf("digit ="); for (i = 0; i < 10; ++i) printf(" %d", ndigit[i]); printf(", white space = %d, other = %d\n", nwhite, nother); } (KR p22)

2 Functions (1/2) #include int power(int m, int n); /* test power function */ main() { int i; for (i = 0; i < 10; ++i) printf("%d %d %d\n", i, power(2, i), power(-3, i)); return 0; } (KR p24)

3 Functions (2/2) /* power: raise base to n-th power; n >= 0 */ int power(int base, int n) { int i, p; p = 1; for (i = 1; i <= n; ++i) p = p * base; return p; } (KR p24) power (base, n) = base n

4 /* power: raise base to n-th power; n >= 0; */ /* version 2 */ int power(int base, int n) { int p; for (p = 1; n > 0; - - n) p = p * base; return p; } Arguments – Call by Value (KR p27)

5 Character Arrays (1/2) #include #define MAXLINE 1000/* maximum input line size */ int getline(char line[], int maxline); void copy(char to[], char from[]); /* print longest input line */ main() { int len; /* current line length */ int max; /* maximum length seen so far */ char line[MAXLINE]; /* current input line */ char longest[MAXLINE]; /* longest line saved here */ max = 0; while ((len = getline(line, MAXLINE)) > 0) if (len > max) { max = len; copy(longest, line); } if (max > 0) /* there was a line */ printf("%s", longest); return 0; } (KR p29)

6 Character Arrays (2/2) /* getline: read a line into s, return length */ int getline(char s[], int lim) { int c, i; for (i = 0; i < lim-1 && (c = getchar())!= EOF && c!='\n'; ++i) s[i] = c; if (c == '\n') { s[i] = c; ++i; } s[i] = '\0'; return i; } /* copy: copy 'from' into 'to'; assume 'to' is big enough */ void copy(char to[], char from[]) { int i; i = 0; while ((to[i] = from[i]) != '\0') ++i; } (KR p29)

7 Summary #include name #define name replacement text main() { } while (expression) statement for (expr1; expr2; expr3) statement if (expr1) statement1 else if (expr2) statement2 … else statement_n Return-type function-name (parameter declarations, if any) { declarations statements }