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?

Slides:



Advertisements
Similar presentations
AP Computer Science Anthony Keen. Computer 101 What happens when you turn a computer on? –BIOS tries to start a system loader –A system loader tries to.
Advertisements

Overview of programming in C C is a fast, efficient, flexible programming language Paradigm: C is procedural (like Fortran, Pascal), not object oriented.
Lectures 10 & 11.
Programming Languages and Paradigms The C Programming Language.
 2003 Prentice Hall, Inc. All rights reserved Fundamentals of Characters and Strings Character constant –Integer value represented as character.
Strings.
1 Chapter 10 Strings and Pointers. 2 Introduction  String Constant  Example: printf(“Hello”); “Hello” : a string constant oA string constant is a series.
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.
Engineering Computing I Chapter 1 – Part B A Tutorial Introduction continued.
Homework Any Questions?. Statements / Blocks, Section 3.1 An expression becomes a statement when it is followed by a semicolon x = 0; Braces are used.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Fundamentals of Strings and Characters Characters.
Loops – While, Do, For Repetition Statements Introduction to Arrays
流程控制: while loop 迴圈 Test condition Enter loop Yes (non-0) Execute Loop body no exit F=0 F=F+20 … F=F
Imperative Programming Prof. Béat Hirsbrunner Amine Tafat, PhD Student Matthias Buchs and Raphaël Lesceux, Graduate Students Department of Informatics.
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
Please pick up an attendance question and submit in 5 minutes CS 1003 Lecture #3 Sept 12, 2006 Knarig Arabshian.
Functions / Procedures
C. About the Crash Course Cover sufficient C for simple programs: variables and statements control functions arrays and strings pointers Slides and captured.
Fundamental Programming Structures in Java: Comments, Data Types, Variables, Assignments, Operators.
COMP1170 Midterm Preparation (March 17 th 2009) Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education.
C Programming Day 2 based upon Practical C Programming by Steve Oualline CS550 Operating Systems.
An Introduction to C Programming Geb Thomas. Learning Objectives Learn how to write and compile a C program Learn what C libraries are Understand the.
 2007 Pearson Education, Inc. All rights reserved C Characters and Strings.
CSCI 130 for Loops Chapter 7 - A. Execution of a C Program Execution starts in main( ) Top down style –sequential flow Unrealistic to expect sequence.
C Programming A Modern Approach
Homework –Continue Reading K&R Chapter 2 –We’ll go over HW2 –HW3 is posted Questions?
Lecture 3.1 Operators and Expressions Structured Programming Instructor: Prof. K. T. Tsang 1.
Outline Symbolic Constants Character Input and Output if … else switch…case.
Programming Language  C Tutorial Introduction 主講人:虞台文.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Tevfik Bultan Lecture 12: Pointers continued, C strings.
Arrays An array is a data structure that consists of an ordered collection of similar items (where “similar items” means items of the same type.) An array.
1/16 Programski jezik C Vladimir Filipović
1 Homework / Exam Turn in HW3 today Exam 1 next class –Open Book / Open Notes –Recommended Use of Book / Notes in Exam: Avoids reliance on “rote memorization”
C STRUCTURES. A FIRST C PROGRAM  #include  void main ( void )  { float height, width, area, wood_length ;  scanf ( "%f", &height ) ;  scanf ( "%f",
Sections 5.1 – 5.4 © Copyright by Pearson Education, Inc. All Rights Reserved.
Strings Programming Applications. Strings in C C stores a string in a block of memory. The string is terminated by the \0 character:
1 Homework –Continue Reading K&R Chapter 2 –We’ll go over HW2 at end of class today –Continue working on HW3 Questions?
Computer Organization and Design Pointers, Arrays and Strings in C Montek Singh Sep 18, 2015 Lab 5 supplement.
Lecture 13: Arrays, Pointers, Code examples B Burlingame 2 Dec 2015.
Prof. Béat Hirsbrunner Ammar Halabi, PhD student (exercises) Dani Rotzetter, Master student (exercises) Bachelor students : Major in computer science (3rd.
1 Homework Done the reading? –K&R –Glass Chapters 1 and 2 Applied for cs240? (If not, keep at it!) Gotten a UNIX account? (If not, keep at it!)
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.
Chapter 8 Characters and Strings. Objectives In this chapter, you will learn: –To be able to use the functions of the character handling library ( ctype).
C++ Programming Lecture 19 Strings The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
Lecture 15: Course Review BJ Furman ME 30 16MAY2011.
0 Chap.2. Types, Operators, and Expressions 2.1Variable Names 2.2Data Types and Sizes 2.3Constants 2.4Declarations 2.5Arithmetic Operators 2.6Relational.
Chapter 1 Basic C Programming
CCSA 221 Programming in C CHAPTER 11 POINTERS ALHANOUF ALAMR 1.
C Language 1 Program Looping. C Language2 Topics Program looping Program looping Relational operators / expressions Relational operators / expressions.
Java Basics. Tokens: 1.Keywords int test12 = 10, i; int TEst12 = 20; Int keyword is used to declare integer variables All Key words are lower case java.
Sudeshna Sarkar, IIT Kharagpur 1 Programming and Data Structure Sudeshna Sarkar Lecture 3.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 1 Chapter 7 Pointers and C-Strings.
1 Midterm 1 Review. 2 Midterm 1 on Friday February 27 Closed book, closed notes No computer can be used 50 minutes 4 or 5 questions write full programs.
1 CSC103: Introduction to Computer and Programming Lecture No 9.
Lecture 3.1 Operators and Expressions Structured Programming Instructor: Prof. K. T. Tsang 1.
L131 Assignment Operators Topics Increment and Decrement Operators Assignment Operators Debugging Tips rand( ) math library functions Reading Sections.
Programming Languages and Paradigms
Review of C… The basics of C scanf/printf if/elseif statements
Programming Paradigms
Arrays in C.
Assignment Operators Topics Increment and Decrement Operators
Assignment Operators Topics Increment and Decrement Operators
Your questions from last session
Homework Any Questions?.
Homework Applied for cs240? (If not, keep at it!) 8/10 Done with HW1?
C Programming Getting started Variables Basic C operators Conditionals
Programming Languages and Paradigms
Assignment Operators Topics Increment and Decrement Operators
Homework Homework Questions? Continue Reading K&R Chapter 2
Assignment Operators Topics Increment and Decrement Operators
Presentation transcript:

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? HW2 (due class 6) on line now DON’T USE and string library functions, e.g. strlen ( ) – Write your own!

2 Counting Lines #include int main ( ) { int c, m; m = 0; while ((c=getchar( )) != EOF) if (c== ‘\n’) ++m; printf(“Number of lines = %d\n”, m); return 0; }

3 What’s new? if (c == ‘\n’) –If statement with logical expression in parentheses Result of comparison equal to 0 is treated as False Result of comparison not equal to 0 is treated as True –The expression is a check for int c equal to ‘\n’ or not –Use double equals (==) for checking “equals” condition if (c = ‘\n’) –If int c wasn’t equal to ‘\n’ before, it is now! –And the expression is treated as true (‘\n’ is not = 0)

4 What’s new? Incrementing a variable Shorthand ++m; Shorthandm++; Equivalent to m = m + 1; Decrementing a variable Shorthand--m Shorthandm-- Equivalent tom = m - 1

5 Review of Control Statements While Statement while (logical expression) { statements while expression is true; } While does not execute any statements if the logical expression is false upon entry!

6 Review of Control Statements For Statement for (initialize; loop test; increment) { statements for expression is true); } For does not execute any statements if the loop test is false after initialization!

7 Review of Control Statements If-else Statement if (logical expression) { statements when expression is true; } else { statements when expression is false; } “Else” portion of statement is optional!

8 Review of Control Statements If-else-if Statement if (logical expression) { statements when expression is true; } else if (logical expression) { statements when expression is false; } else if (logical expression) …. Only one of the blocks of statements will run!

9 Arrays / Character Strings Unlike Java, C has no “special” string type Character string is an array of character type values (integers) ending with a 0 (‘\0’) “array[]” is “a pointer” to sequential memory locations containing elements of defined type Individual element n accessed as “array[n]”

10 Arrays / Character Strings Defining/initializing an array to contain string “hello” plus an end of line character: char array[7] = “hello\n”; Sets up memory locations as follows: array[0]array[1]array[2]array[3]array[4]array[5]array[6] ‘h’‘e’‘l’ ‘o’‘\n’‘\0’

11 Arrays / Character Strings /* count.c: count digit s 0-9 coming from stdin */ #include int main( ) { int c,i; /* c for char - ASCII code for integers */ char ndigit[10]; /* subscripts 0 through 9 */ for (i=0;i<=9;++i) /* clear the count array */ ndigit[i]=0;

12 Arrays / Character Strings while ((c=getchar())!=EOF) if(c>='0' && c<='9') /* if c is a digit */ ++ndigit[c-'0']; /* add one to its counter */ printf("digit count\n"); /* print heading */ for (i=0;i<=9;++i) /* print counts */ printf("%5d %5d\n",i, ndigit[i]); return 0; }

13 Arrays / Character Strings u18(14)% gcc -Wall -o count count.c u18(15)% count fgf digit count u18(16)%

14 Program: maxline Outline of maxline program (“Pseudocode”) while (there’s another line) if (it’s longer than the previous longest) save it save its length print longest line Large enough to break up into “functions”

15 Program: maxline #include /* define maximum length of lines */ #define MAXLINE 1000 /* define our function prototypes */ int getline(char line[], int maxline); void copy(char to[], char from[]);

16 Program: maxline /* print longest input line */ int main ( ) { /* initialization */ int len,max; char line[MAXLINE], longest[MAXLINE]; max = 0;

17 Program: maxline while ((len = getline(line, MAXLINE)) >0) if (len > max) { max = len; copy(longest, line); } if (max > 0) /* there was a line */ printf (“%s”, longest); }

18 Function: getline( ) /* 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; }

19 Function: copy ( ) /* copy: copy ‘from’ into ‘to’ assume size of array ‘to’ is large enough */ void copy (char to[], char from[]) { int i; i = 0; while ((to[i] = from[i]) != ‘\0’) ++i; }

20 Notes on Details Precedence of Operators in getline( ) –i < lim-1 –(c = getchar()) != EOF –(expression && expression && expression) Pass by value arguments for copy (pointers) –void copy(char to[], char from[]) –while ((to[i] = from [i]) != ‘\0’)