Your questions from last session

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

Introduction to C Programming
Overview of programming in C C is a fast, efficient, flexible programming language Paradigm: C is procedural (like Fortran, Pascal), not object oriented.
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.
Computer programming Lecture 3. Lecture 3: Outline Program Looping [Kochan – chap.5] –The for Statement –Relational Operators –Nested for Loops –Increment.
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?
流程控制: 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
Functions / Procedures
Introduction to C Programming Overview of C Hello World program Unix environment C programming basics.
Chapter 3: Introduction to C Programming Language C development environment A simple program example Characters and tokens Structure of a C program –comment.
Homework Reading –Finish K&R Chapter 1 (if not done yet) –Start K&R Chapter 2 for next time. Programming Assignments –DON’T USE and string library functions,
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.
C Programming A Modern Approach
Homework –Continue Reading K&R Chapter 2 –We’ll go over HW2 –HW3 is posted Questions?
Homework Reading Programming Assignments
CECS 121 EXAM 1. /* C Programming for the Absolute Beginner */ // by Michael Vine #include main() { printf(“\nC you later\n”); system(“pause”); }
CHAPTER 8 CHARACTER AND STRINGS
STRING Dong-Chul Kim BioMeCIS UTA 10/7/
Outline Symbolic Constants Character Input and Output if … else switch…case.
Programming Language  C Tutorial Introduction 主講人:虞台文.
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 Programming for the Absolute Beginner */ // by Michael Vine #include main() { printf(“\nC you later\n”); system(“pause”); }
CECS 121 Test 1. Functions allow you to group program statements under one name C and C++ are case-sensitive so main(), MAIN(), and Main() are all different.
VARIABLES, CONSTANTS, OPERATORS ANS EXPRESSION
/* C Programming for the Absolute Beginner */ // by Michael Vine #include main() { printf(“\nC you later\n”); system(“pause”); }
1 Homework –Continue Reading K&R Chapter 2 –We’ll go over HW2 at end of class today –Continue working on HW3 Questions?
Pointers *, &, array similarities, functions, sizeof.
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.
Lecture 15: Course Review BJ Furman ME 30 16MAY2011.
Characters and Strings
FUNCTIONS. Midterm questions (1-10) review 1. Every line in a C program should end with a semicolon. 2. In C language lowercase letters are significant.
C Language 1 Program Looping. C Language2 Topics Program looping Program looping Relational operators / expressions Relational operators / expressions.
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.
Principles of Programming - NI Chapter 10: Character & String : In this chapter, you’ll learn about; Fundamentals of Strings and Characters The difference.
Lecture 3.1 Operators and Expressions Structured Programming Instructor: Prof. K. T. Tsang 1.
The Machine Model Memory
UMBC CMSC 104 – Section 01, Fall 2016
C Characters and Strings
Chap. 2. Types, Operators, and Expressions
© 2016 Pearson Education, Ltd. All rights reserved.
CSE1320 Loop Dr. Sajib Datta
Arrays in C.
Programming in C Input / Output.
Lexical Elements, Operators, and the C Cystem
Strings, Line-by-line I/O, Functions, Call-by-Reference, Call-by-Value
Arrays, For loop While loop Do while loop
Bases and Representations, Memory, Pointers, Arrays, For-Loops
7 Arrays.
Lexical Elements, Operators, and the C Cystem
Outline Defining and using Pointers Operations on pointers
Computing Fundamentals
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
Computer programming Lecture 3.
7 Arrays.
Homework Reading Programming Assignments Finish K&R Chapter 1
ECE 103 Engineering Programming Chapter 8 Data Types and Constants
Chapter 11 Programming in C
Programming Languages and Paradigms
Homework Homework Questions? Continue Reading K&R Chapter 2
ECE 120 Midterm 1 HKN Review Session.
Presentation transcript:

Your questions from last session If your computer has problem with “–m32”. Your problem will be probably fixed by typing: sudo apt-get install libx32gcc-4.8-dev sudo apt-get install libc6-dev-i386 To show exponent numbers in printf, you should use %e instead of %d

Counting Lines #include <stdio.h> int main (void) { int c, m; m = 0; /* a counter */ while ((c=getchar( )) != EOF) if (c= = ‘\n’) ++m; printf(“%d\n”, m); return 0; } Memory allocation for int -> 4Bytes Draw the memory chip diagram again Compiler saves the address of variables Differennce of = and == ‘\n’ ascii code

What’s new? if (c = = ‘\n’) 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! if(c = ‘\n’) is the same as if(c) And the expression is treated as true (‘\n’ is not = 0) 0 -> false. Anything else -> true

What’s new? Incrementing a variable Decrementing a variable Shorthand ++m; Shorthand m++; Equivalent to m = m + 1; Decrementing a variable Shorthand --m; Shorthand m--; Equivalent to m = m – 1; Prefix: increment m before m is used Postfix: increment m after m is used m = 3; c = m++; // c = ? m = ? c = ++m; // c = ? m = ?

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!

Review of Control Statements do while Statement do { statements; } while (logical expression); Do while does execute the statements at least once even if the logical expression is false!

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! Generally use for when you have an index that changes each iteration. Use while when you don’t.

Review of Control Statements if-else Statement if (logical expression) { statements when expression is true; } else { statements when expression is false; } More specific! An example

Review of Control Statements if-else-if Statement if (logical expression 1) { statements when expression 1 is true; } else if (logical expression 2) { statements when expression 1 is false, 2 is true; } else if (logical expression 3) { statements when expression 1,2 are false, 3 is true; } else { statements when expression 1,2,3 are false; } Only one of the blocks of statements will run!

No brace case While(c != 10) for(i= 0; i < 5; i++) if(j == 3) printf(……); Single statement Single statement Single statement Printf(…); Indentation is important, making your program easy to read!

Arrays / Character Strings Character string is an array of character type values (integers) ending with a null character (\0) “array[]” is “a pointer” to sequential memory locations containing elements of defined type Individual element n is accessed as “array[n]” If you don’t have a null at the end of the string and you want to print the string, the printf will continue to print until to find a null character!

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: We need at least 7 here Ascii value of h e l l … Start index -> 0 array[0] array[1] array[2] array[3] array[4] array[5] array[6] ‘h’ ‘e’ ‘l’ ‘l’ ‘o’ ‘\n’ ‘\0’

Numbering systems Binary: 0s and 1s Octal: 0, 1, 2, …, 7 The only thing computers understand is the numbers. The getchar() reads the numbers representing the characters (ASCII value representation) Binary: 0s and 1s Octal: 0, 1, 2, …, 7 Hexadecimal: 0, 1, 2, …, 9, A, B, C, D, E, F Octal -> 3 5 3 Binary -> 0 1 1 1 0 1 0 1 1 Hexa -> 0 E B Show the ASCII table Some weird characters in ascii. They are special characters used by IBM in the past You manipulate the characters by their ascii number Octal numbers -> grouping the binary numbers by 3. but 8 16 32 is not devisable by 3 So group them by 4 -> hexadecimal Show these numbers in decimal Char Decimal ‘0’ 48 Octal Hexadecimal 30 060

Arrays / Character Strings #include <stdio.h> /* count digit characters 0-9 coming from stdin */ int main(void) { int c, i; /* c for getchar - ASCII code for integers */ int ndigit[10]; /* subscripts 0 through 9 */ for (i = 0; i <= 9; ++i) /* Set all array value = 0 */ ndigit[i] = 0; 0 to 9 means 10 times

Arrays / Character Strings (cont’d) while ((c = getchar()) != EOF) if(c >= '0' && c <= '9') /* if c is a digit */ ++ndigit[c-'0']; /* increment 1 array element */ printf("digits = "); for (i = 0; i <= 9; ++i) printf("%d ", ndigit[i]); printf("\n"); return 0; } Skip other characters than numbers Show the ascii code chart again Figure it out where that character is and then increment that Subtract the value from ascii value of 0

Arrays / Character Strings % gcc count.c % a.out 123456789011222333344444555555677888999000 fgfgfgfg (Note: These won’t be counted as digits) ^D (Control-D is End of File – EOF) digits = 4 3 4 5 6 7 2 3 4 4 %

Program: maxline Program to figure out which line is the longest, print it. 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” Hw2 begins from here Explain trimming the end of the lines with an example. Vertical tab horizontal tab space, … Talk about pseudo code. You should not start to code after you see a question. You should think about it and try to write a pseudo code first to get idea. You write down pseudo English and avoid using C syntax. By reading it you can understand what the problem supposed to do For each function write a pseudo code in comment. Use comments in your code. Read from end of each line after saving and then ignore non characters Talk about second part of the hw2. Reverse line by line. -> change the indexes -> reversing the indexes

There is another line Put this as a new “function” Function name: getline Function input: a character array to save the next line Function output: the length of the next line

Copy one character array to another (save the longest line) Put this as a new “function” Function name: copy Function input: two character arrays Function output: void

Program: maxline #include <stdio.h> /* define maximum length of lines */ #define MAXLINE 1000 /* define function prototypes */ int getline(char line[], int maxline); void copy(char to[], char from[]); Declare function prototypes such that main function knows their existence Tell the compiler to know the function. line -> starting position address of the array. You don’t need to define main()

Program: maxline (cont’d) /* print longest input line */ int main (void){ int len, max; /* initialization */ char line[MAXLINE], longest[MAXLINE]; max = 0; while ((len = getline(line, MAXLINE)) >0) if (len > max) { /*You found a longer line*/ max = len; copy(longest, line); } if (max > 0) /* there was a line */ printf (“%s”, longest); Defining 2 character arrays each element 1 Byte. First input of getline -> address of the array %s means writing the string from the beginning of the array to terminating character \0 It doesn’t matter where to define functions.

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’) { ++i; } s[i] = ‘\0’; return i; K&R p.30 -an array of characters; length unspecified The variable name could be different, but the position matters Compiler checks the prototype and the actual function and they should be same Explain the loop test Read the characters until it sees a new line character Change the name of this function because stdio.h has a function with this name …… array[0] array[1] array[2] array[3] array[4] array[5] array[6] …… ‘h’ ‘e’ ‘l’ ‘l’ ‘o’ ‘\n’ ‘\0’

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; } an array of characters; length unspecified

Notes on the Details Precedence of operators in getline( ) - (expression1 && expression2 && expression3) i < lim-1; ((c = getchar()) != EOF); ++i Pass by address arguments for copy (pointers) void copy(char to[], char from[]) while ((to[i] = from [i]) != ‘\0’)

“Octal” Dump Use “od –xc” to see hex dump of a file Show Character Show Hex Show Character Use “od –xc” to see hex dump of a file Use od –xc to see the ascii codes of the input So for the hw you can do octal dump for input and again do it for the output and see the result This flips the letters in the worlds! Because of different CPUs The hardware flips it again when it wants to use it Character ASCII value in Hex Byte offset