ACM notes Can work on the problems anytime throughout the term Contest conflict -- The GRE subject test is Nov. 10th! int x; while (cin >> x) { cout <<

Slides:



Advertisements
Similar presentations
A C++ Crash Course Part II UW Association for Computing Machinery Questions & Feedback.
Advertisements

Strings Input/Output scanf and printf sscanf and sprintf gets and puts.
Character String Manipulation. Overview Character string functions sscanf() function sprintf() function.
Chapter 3. Expressions and Interactivity CSC125 Introduction to C++
Dale Roberts Basic I/O – printf() CSCI 230 Department of Computer and Information Science, School of Science, IUPUI Dale Roberts, Lecturer Department of.
One-dimensional character Arrays C does not have string data type. So strings are represented as arrays of characters. The end of the string is marked.
CS1061 C Programming Lecture 16: Formatted I/0 A. O’Riordan, 2004.
Primitive Variable types Basic types –char (single character or ASCII integer value) –int (integer) –short (not longer than int) –long (longer than int)
1 Chapter 9 - Formatted Input/Output Outline 9.1Introduction 9.2Streams 9.3Formatting Output with printf 9.4Printing Integers 9.5Printing Floating-Point.
Differences between Java and C CS-2303, C-Term Differences between Java and C CS-2303, System Programming Concepts (Slides include materials from.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 2: Introduction to C++ Starting Out with C++ Early Objects Sixth.
More on Numerical Computation CS-2301 B-term More on Numerical Computation CS-2301, System Programming for Non-majors (Slides include materials from.
Chapter 9 Formatted Input/Output Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 9 - Formatted Input/Output Outline 9.1Introduction.
 2007 Pearson Education, Inc. All rights reserved C Formatted Input/Output.
C Formatted Input/Output /* Using Integer Conversion Specifiers */ #include int main ( ) { printf( "%d\n", 455 ); printf( "%i\n", 455 ); printf( "%d\n",
CS 161 Introduction to Programming and Problem Solving Chapter 13 Console IO Herbert G. Mayer, PSU Status 9/8/2014 Initial content copied verbatim from.
The printf Method The printf method is another way to format output. It is based on the printf function of the C language. System.out.printf(,,,..., );
 2007 Pearson Education, Inc. All rights reserved C Formatted Input/Output.
Lecture 11 Standard Template Library Stacks, Queue, and Deque Lists Iterators Sets Maps.
Simple Data Type Representation and conversion of numbers
Strings in C. Strings are Character Arrays Strings in C are simply arrays of characters. – Example:char s [10]; This is a ten (10) element array that.
CSC 125 Introduction to C++ Programming Chapter 2 Introduction to C++
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 2: Introduction to C++ Starting Out with C++ Early Objects Seventh.
CSIS 123A Lecture 5 Friend Functions Glenn Stevenson CSIS 113A MSJC.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Streams Streams –Sequences of characters organized.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 9 - Formatted Input/Output Outline 9.1Introduction.
Dr. Yingwu Zhu STL Vector and Iterators. STL (Standard Template Library) 6:14:43 AM 2 A library of class and function templates Components: 1. Containers:
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.
EPSII 59:006 Spring Introduction In this lecture  Formatted Input/Output scanf and printf  Streams (input and output) gets, puts, getchar, putchar.
C Tokens Identifiers Keywords Constants Operators Special symbols.
You gotta be cool. Stream Stream Output Stream Input Unformatted I/O with read, gcount and write Stream Manipulators Stream Format States Stream Error.
CS1 Lesson 2 Introduction to C++ CS1 Lesson 2 -- John Cole1.
Sales person receive RM200/week plus 9% of their gross sales for that week. Write an algorithms to calculate the sales person’s earning from the input.
Introduction to STL and the vector container class CS342 Data Structures Based on Ford & Topp.
Chapter 3 Processing and Interactive Input. 2 Assignment  The general syntax for an assignment statement is variable = operand; The operand to the right.
Adv. UNIX:io/91 Advanced UNIX v Objectives of these slides: –look in some detail at standard input and output in C Special Topics in Comp.
Welcome to Programming Practicum “Putting the C into CS” You aren’t here writing clinic reports Lecture clinic liaison phone call coding chunky strings.
CSE1222: Lecture 2The Ohio State University1. mathExample2.cpp // math example #include using namespace std; int main() { cout
Data TypestMyn1 Data Types The type of a variable is not set by the programmer; rather, it is decided at runtime by PHP depending on the context in which.
A chance to “improve” your C/C++ … Preparation for the ACM competition... Problem Insight and Execution... Get into the minds of the judges Anxiety! 1.
Chapter 3 – Variables and Arithmetic Operations. Variable Rules u Must declare all variable names –List name and type u Keep length to 31 characters –Older.
Basic I/O in C Computer Organization I 1 August 2009 © McQuain, Feng & Ribbens Stream Model of I/O header file: A stream provides a connection.
5-1 Embedded Systems C Programming Language Review and Dissection III Lecture 5.
1 Homework –Continue Reading K&R Chapter 2 –We’ll go over HW2 at end of class today –Continue working on HW3 Questions?
ACM notes Why… ? Not always an easy question! Entropy problem -- Huffman Codes InputOutput A..AB..BC..CD..DE..EF compression ratio, to 1 place.
Programming Fundamentals. Summary of previous lectures Programming Language Phases of C++ Environment Variables and Data Types.
1 Comments Allow prose or commentary to be included in program Importance Programs are read far more often than they are written Programs need to be understood.
Introduction to Computer Organization & Systems Topics: Types in C: floating point COMP C Part III.
© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming Input and Output.
1 Queues Queue API Application: Radix Sort Implementation: Using Deque Using Deque Circular Array Circular Array Priority Queue Priority Queue API Implementation.
Lecture 5 Computer programming -1-. Input \ Output statement 1- Input (cin) : Use to input data from keyboard. Example : cin >> age; 2- Output (cout):
Characters and Strings
Literals A literal (sometimes called a constant) is a symbol which evaluates to itself, i.e., it is what it appears to be. Examples: 5 int literal
ACM notes Sometimes a problem is much more difficult than expected! Basic approach:
C is a high level language (HLL)
1 The Standard Template Library The STL is a collection of Container classes These are class templates for containers. A container is an object that stores.
1 float Data Type Data type that can hold numbers with decimal values – e.g. 3.14, 98.6 Floats can be used to represent many values: –Money (but see warning.
1 ENERGY 211 / CME 211 Lecture 3 September 26, 2008.
28 Formatted Output.
C Formatted Input/Output
Chapter 2: Introduction to C++
A bit of C programming Lecture 3 Uli Raich.
Programming Fundamentals
TMF1414 Introduction to Programming
Formatting Output.
Lecture 13 Input/Output Files.
Differences between Java and C
Binary multiplication
An Introduction to STL.
Presentation transcript:

ACM notes Can work on the problems anytime throughout the term Contest conflict -- The GRE subject test is Nov. 10th! int x; while (cin >> x) { cout << ( (x%9) ? (x%9) : 9 ); } Digroot “problem”

ACM “style” while (nlines-- > 0) { queue q; string s; getline(cin, s); for (int i = 0; s[i]; i++) { if (!q.empty() && s[i] == q.front()) { // found q.pop(); } else { q.push(s[i]); if (q.size() > 10) { cout << "Not consistent with the theory\n"; goto done; } if (q.empty()) cout << "An echo string with buffer size ten\n"; else cout << "Not an echo string, but still consistent with the theory\n"; done: ; } Echo problem

Storing results in tables “dynamic programming”... Find the number of prime factors in N! (1 is not prime.)

Other problems Change counting input: output: There are 292 ways to make $1.00 There are 2 ways to make $0.06 Sigma series input: output: What?

Other problems Change counting input: output: There are 292 ways to make $1.00 There are 2 ways to make $0.06 Sigma series input: output: Shortest sequences from 1 to N such that each element is the sum of two previous elements.

C++ STL vector v; // basically an int array v.reserve(10); // assure 10 spots v.push_back(42); // adds 42 to the end v.back(); // returns 42 v.pop_back(); // removes 42 v.size(); // # of elements v[i]; // ith element sort( v.begin(), v.end() ); // default sort sort( v.begin(), v.end(), mycompare ); deque d; // double-ended queue d.push_front(42); // add to front d.front(42); // return front element d.push_front(42); // remove from front sort #include vector #include deque #include last time

Useful C functions int atoi(char* s); double atof(char* s); int strcasecomp(char* s1, char* s2); long strtol(char* s, NULL, int base) strtol(“Charlie”, NULL, 36) == L converts C strings to ints atoi(“100”) == 100 converts C strings to doubles atoi(“100.0”) == case-insensitive C string comparison strcasecmp(“aCm”,“ACm”) == 0 arbitrary conversion from a string in bases (2-36) to a long int use man for more...

sprintf int sprintf(char* str, char* format,...); prints anything to the string str char str[100]; sprintf(str,“%d”,42); // str is “42” sprintf(str,“%f”,42.0); // str is “42.0” sprintf(str,“%10d”,42); // str is “ 42” sprintf(str,“%-10d”,42); // str is “42 ” flexible formatting: right/left justify:

A chance to “improve” your C/C++ … Preparation for the ACM competition... Problem Insight and Execution... Get into the minds of the judges Anxiety! 1 2 Two ACM programming skills

Get into the minds of the judges Key Skill #1: mindreading “What cases should I handle?” spectrum 100% 0%

Key Skill #2: anxiety Anxiety!

Dynamic Programming Strategy: create a table of partial results & build on it. divis.cc T(n) = T(3n+1) + 1 if n odd T(n) = number of steps yet to go T(n) = T(n/2) + 1 if n even

Dynamic Programming Keys: create a table of partial results, articulate what each table cell means, then build it up... divis.cc T[i][j] is 1 if i is a possible remainder using the first j items in the list. Table T 3 j = items considered so far i = possible remainder the list the divisor 4

Dynamic programs can be short #include vector v(10000); vector m(100); // old mods vector m2(100); // new mods int n, k; bool divisible() { fill(m.begin(),m.end(),false); m[0] = true; for (int i=0; i<n; i++) { /* not giving away all of the code */ /* here the table is built (6 lines) */ } return m[0]; } int main() { cin >> n; // garbage while (cin >> n) { cin >> k; for (int i=0; i<n; i++) { cin >> v[i]; v[i] = abs(v[i]); v[i] %= k; } cout << (divisible() ? "D" : "Not d") << "ivisible\n"; } cout << endl; } acknowledgment: Matt Brubeck STL:

General ACM Programming Try brute force first (or at least consider it) -- sometimes it will work fine… -- sometimes it will take a _bit_ too long -- sometimes it will take _way_ too long for (int j=1 ; j<N ; ++j) { cin >> Array[i]; } Table[i + n % k] = 1; Table[i - n % k] = 1; filling in the table in the “divis” problem:getting the input in the “pea” problem: Best bugs from last week:

New Problem Input A list of words Word Chains Output yes or no -- can these words be chained together such that the last letter of one is the first letter of the next… ? doze aplomb ceded dozen envy ballistic yearn hertz jazz hajj zeroth

Knapsack Problem V(n,w) = max value stealable w/ ‘n’ objects & ‘w’ weight V(n,w) = object wt. val Maximize loot w/ weight limit of 4. 4 Number of objects considered Weight available for use n w

C Output printf, fprintf, sprintf(char* s, const char* format, …) the destinationthe format string the values h.412-#0%d start character flags - left-justify 0 pad w/ zeros + use sign (+ or -) (space) use sign ( or -) # deviant operation minimum field width precision size modifier h short l long (lowercase L) L long double type d decimal integers u unsigned (decimal) ints o octal integers x hexadecimal integers f doubles (floats are cast) e doubles (exp. notation) g f or e, if exp < -3 or -4 c character s string n outputs # of chars written !! % two of these print a ‘%’ allowed size modifiers possible format strings

C Output %10.4d value = 42value = %-#12x 0x2a0xffffffd6 %+10.4g value = 42value = %- 10.4g %-#10.4g value = “forty-two” %10.5s forty