Gator Engineering Google Code Jam 2015 Copyright © 2008 W. W. Norton & Company. All rights reserved. 1.

Slides:



Advertisements
Similar presentations
C Language.
Advertisements

Pointers and Arrays Chapter 12
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.
1 1-d Arrays. 2 Array Many applications require multiple data items that have common characteristics  In mathematics, we often express such groups of.
Programming in C Pointers and Arrays. 1/14/102 Pointers and Arrays In C, there is a strong relationship between pointers and arrays.In C, there is a strong.
Lecture 2 Introduction to C Programming
Introduction to C Programming
 2000 Prentice Hall, Inc. All rights reserved. Chapter 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line.
Introduction to C Programming
Chapter 10.
Enumerated Types 4 Besides the built-in types, ANSI C allows the definition of user-defined enumerated types –To define a user-define type, you must give.
Computer Science 1620 Multi-Dimensional Arrays. we used arrays to store a set of data of the same type e.g. store the assignment grades for a particular.
Pointers Pointer - A pointer is a derived data type; that is it is a data type built from one of the standard types. Its value is any of the addresses.
Arrays Hanly - Chapter 7 Friedman-Koffman - Chapter 9.
 2007 Pearson Education, Inc. All rights reserved Introduction to C Programming.
Chapter 8. 2 Objectives You should be able to describe: One-Dimensional Arrays Array Initialization Arrays as Arguments Two-Dimensional Arrays Common.
C. About the Crash Course Cover sufficient C for simple programs: variables and statements control functions arrays and strings pointers Slides and captured.
1 CS 201 Array Debzani Deb. 2 Having trouble linking math.h? Link with the following option gcc –lm –o test test.o.
1 The first step in understanding pointers is visualizing what they represent at the machine level. In most modern computers, main memory is divided into.
Introduction to C Programming
Programming in C Pointers and Arrays. 1/14/102 Pointers and Arrays In C, there is a strong relationship between pointers and arrays.In C, there is a strong.
1 Arrays Chapter 13 Especially read 13.1 – 13.2 Text introduces vectors, which we will not cover, in 13.3.
Palindromes revisited Here's a simpler program for checking palindromes: int nums[100]; int i = 0, a; cin >> a; while(a > 0) { nums[i++] = a; cin >> a;
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 2 Chapter 2 - Introduction to C Programming.
Chapter 11 introduced pointers and showed how they're used as function arguments and as values returned by functions. This chapter covers another application.
Lecture 2: Introduction to C Programming. OBJECTIVES In this lecture you will learn:  To use simple input and output statements.  The fundamental data.
Chapter 11: Pointers Copyright © 2008 W. W. Norton & Company. All rights reserved. 1 Chapter 11 Pointers.
Chapter 3: Formatted Input/Output Copyright © 2008 W. W. Norton & Company. All rights reserved. 1 Chapter 3 Formatted Input/Output.
Digital Computer Concept and Practice Copyright ©2012 by Jaejin Lee C Language Part 4.
Chapter 3 Top-Down Design with Functions Part II J. H. Wang ( 王正豪 ), Ph. D. Assistant Professor Dept. Computer Science and Information Engineering National.
A First Book of C++: From Here To There, Third Edition2 Objectives You should be able to describe: One-Dimensional Arrays Array Initialization Arrays.
Copyright © 2012 Pearson Addison-Wesley. All rights reserved. Chapter 7 Arrays.
Copyright © 2002 W. A. Tucker1 Chapter 9 Lecture Notes Bill Tucker Austin Community College COSC 1315.
 2008 Pearson Education, Inc. All rights reserved. 1 Arrays and Vectors.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 1 Chapter Array Basics.
Copyright © 2000, Department of Systems and Computer Engineering, Carleton University 1 Introduction An array is a collection of identical boxes.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 2 - Introduction to C Programming Outline.
Gator Engineering Copyright © 2008 W. W. Norton & Company. All rights reserved. 1 Chapter 3 Formatted Input/Output.
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.
MORE POINTERS Plus: Memory Allocation Heap versus Stack.
CCSA 221 Programming in C CHAPTER 11 POINTERS ALHANOUF ALAMR 1.
Using a Pointer as an Array Name C allows us to subscript a pointer as though it were an array name Here's an example: #define N 100 int a[N], i, sum =0,
CS162 - Topic #12 Lecture: –Arrays with Structured Elements defining and using arrays of arrays remember pointer arithmetic Programming Project –Any questions?
SEQUENTIAL AND OBJECT ORIENTED PROGRAMMING Arrays.
1 ENERGY 211 / CME 211 Lecture 4 September 29, 2008.
1 Lecture 4: Part1 Arrays Introduction Arrays  Structures of related data items  Static entity (same size throughout program)
Chapter 12: Pointers and Arrays Copyright © 2008 W. W. Norton & Company. All rights reserved. 1 Chapter 9 Functions.
Introduction C# program is collection of classes Classes are collection of methods and some statements That statements contains tokens C# includes five.
CSCI 125 & 161 / ENGR 144 Lecture 16 Martin van Bommel.
1 Lecture 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line of Text 2.3Another Simple C Program: Adding.
Gator Engineering Copyright © 2008 W. W. Norton & Company. All rights reserved. 1 Chapter 9 Functions.
A FIRST BOOK OF C++ CHAPTER 7 ARRAYS. OBJECTIVES In this chapter, you will learn about: One-Dimensional Arrays Array Initialization Arrays as Arguments.
Strings (Continued) Chapter 13
Chapter 7 Top-Down Development
Lecture 6 C++ Programming
Pointers and Arrays Chapter 12
Functions Chapter 9 Copyright © 2008 W. W. Norton & Company. 1
Arrays We often want to organize objects or primitive data in a way that makes them easy to access and change. An array is simple but powerful way to.
Lecture 18 Arrays and Pointer Arithmetic
Functions Chapter 9 Copyright © 2008 W. W. Norton & Company.
Pointers and Arrays Chapter 12
Pointers (continued) Chapter 11
Regrading Project 2 Exam 2 One week period TA: Huiyuan TA: Fardad
Pointers and Arrays Chapter 12
Arrays Chapter 8 Copyright © 2008 W. W. Norton & Company.
Pointers Chapter 11 Copyright © 2008 W. W. Norton & Company.
Pointers Chapter 11 Copyright © 2008 W. W. Norton & Company.
Pointers Chapter 11 Copyright © 2008 W. W. Norton & Company.
Programming in C Pointers and Arrays.
4.1 Introduction Arrays A few types Structures of related data items
Presentation transcript:

Gator Engineering Google Code Jam 2015 Copyright © 2008 W. W. Norton & Company. All rights reserved. 1

Gator Engineering Rules Copyright © 2008 W. W. Norton & Company. All rights reserved. 2

Gator Engineering Sample questions P1: Reverse words –Given a list of space separated words, reverse the order of the words. Each line of text contains L letters and W words. A line will only consist of letters and space characters. There will be exactly one space character between each pair of consecutive words. Copyright © 2008 W. W. Norton & Company. All rights reserved. 3

Gator Engineering P1: Reverse Words Input –The first line of input gives the number of cases, N. N test cases follow. For each test case there will a line of letters and space characters indicating a list of space separated words. Spaces will not appear at the start or end of a line. Output –For each test case, output one line containing "Case #x: " followed by the list of words in reverse order. Copyright © 2008 W. W. Norton & Company. All rights reserved. 4

Gator Engineering P1: Reverse Words Limits –Small dataset –N = 5 1 ≤ L ≤ 25 Large dataset –N = ≤ L ≤ 1000 Copyright © 2008 W. W. Norton & Company. All rights reserved. 5

Gator Engineering P1: Reverse Words Sample Copyright © 2008 W. W. Norton & Company. All rights reserved. 6 Input Output 3 this is a test foobar all your base Case #1: test a is this Case #2: foobar Case #3: base your all

Gator Engineering P2: T9 Spelling Problem –The Latin alphabet contains 26 characters and telephones only have ten digits on the keypad. We would like to make it easier to write a message to your friend using a sequence of keypresses to indicate the desired characters. The letters are mapped onto the digits as shown below. To insert the character B for instance, the program would press 22. In order to insert two characters in sequence from the same key, the user must pause before pressing the key a second time. The space character ' ' should be printed to indicate a pause. For example, 2 2 indicates AA whereas 22 indicates B. Copyright © 2008 W. W. Norton & Company. All rights reserved. 7

Gator Engineering P2: T9 Spelling Input –The first line of input gives the number of cases, N. N test cases follow. Each case is a line of text formatted as desired_message –Each message will consist of only lowercase characters a-z and space characters ' '. Pressing zero emits a space. Output –For each test case, output one line containing "Case #x: " followed by the message translated into the sequence of keypresses. Copyright © 2008 W. W. Norton & Company. All rights reserved. 8

Gator Engineering P2: T9 Spelling Limits –1 ≤ N ≤ 100. Small dataset –1 ≤ length of message in characters ≤ 15. Large dataset –1 ≤ length of message in characters ≤ Copyright © 2008 W. W. Norton & Company. All rights reserved. 9

Gator Engineering P2: T9 Spelling Sample Copyright © 2008 W. W. Norton & Company. All rights reserved. 10 Input Output 4 hi yes foo bar hello world Case #1: Case #2: Case #3: Case #4:

Gator Engineering Copyright © 2008 W. W. Norton & Company. All rights reserved. 11 Chapter 12 Pointers and Arrays (Continued)

Gator Engineering Using an Array Name as a Pointer Pointer arithmetic is one way in which arrays and pointers are related. Another key relationship: The name of an array can be used as a pointer to the first element in the array. This relationship simplifies pointer arithmetic and makes both arrays and pointers more versatile. Copyright © 2008 W. W. Norton & Company. All rights reserved. 12

Gator Engineering Using an Array Name as a Pointer Suppose that a is declared as follows: int a[10]; Examples of using a as a pointer: *a = 7; /* stores 7 in a[0] */ *(a+1) = 12; /* stores 12 in a[1] */ In general, a + i is the same as &a[i]. –Both represent a pointer to element i of a. Also, *(a+i) is equivalent to a[i]. –Both represent element i itself. Copyright © 2008 W. W. Norton & Company. All rights reserved. 13

Gator Engineering Using an Array Name as a Pointer The fact that an array name can serve as a pointer makes it easier to write loops that step through an array. Original loop: for (p = &a[0]; p < &a[N]; p++) sum += *p; Simplified version: for (p = a; p < a + N; p++) sum += *p; Copyright © 2008 W. W. Norton & Company. All rights reserved. 14

Gator Engineering Using an Array Name as a Pointer Although an array name can be used as a pointer, it’s not possible to assign it a new value. Attempting to make it point elsewhere is an error: while (*a != 0) a++; /*** WRONG ***/ This is no great loss; we can always copy a into a pointer variable, then change the pointer variable: p = a; while (*p != 0) p++; Copyright © 2008 W. W. Norton & Company. All rights reserved. 15

Gator Engineering Array Arguments (Revisited) When passed to a function, an array name is treated as a pointer. Example: int find_largest(int a[], int n) { int i, max; max = a[0]; for (i = 1; i < n; i++) if (a[i] > max) max = a[i]; return max; } A call of find_largest : largest = find_largest(b, N); This call causes a pointer to the first element of b to be assigned to a ; the array itself isn’t copied. Copyright © 2008 W. W. Norton & Company. All rights reserved. 16

Gator Engineering Array Arguments (Revisited) The fact that an array argument is treated as a pointer has some important consequences. Consequence 1: When an ordinary variable is passed to a function, its value is copied; any changes to the corresponding parameter don’t affect the variable. In contrast, an array used as an argument isn’t protected against change. Copyright © 2008 W. W. Norton & Company. All rights reserved. 17

Gator Engineering Array Arguments (Revisited) For example, the following function modifies an array by storing zero into each of its elements: void store_zeros(int a[], int n) { int i; for (i = 0; i < n; i++) a[i] = 0; } Copyright © 2008 W. W. Norton & Company. All rights reserved. 18

Gator Engineering Array Arguments (Revisited) To indicate that an array parameter won’t be changed, we can include the word const in its declaration: int find_largest(const int a[], int n) { … } If const is present, the compiler will check that no assignment to an element of a appears in the body of find_largest. Copyright © 2008 W. W. Norton & Company. All rights reserved. 19

Gator Engineering Array Arguments (Revisited) Consequence 2: The time required to pass an array to a function doesn’t depend on the size of the array. There’s no penalty for passing a large array, since no copy of the array is made. Copyright © 2008 W. W. Norton & Company. All rights reserved. 20

Gator Engineering Array Arguments (Revisited) Consequence 3: An array parameter can be declared as a pointer if desired. find_largest could be defined as follows: int find_largest(int *a, int n) { … } Declaring a to be a pointer is equivalent to declaring it to be an array; the compiler treats the declarations as though they were identical. Copyright © 2008 W. W. Norton & Company. All rights reserved. 21

Gator Engineering Array Arguments (Revisited) Although declaring a parameter to be an array is the same as declaring it to be a pointer, the same isn’t true for a variable. The following declaration causes the compiler to set aside space for 10 integers: int a[10]; The following declaration causes the compiler to allocate space for a pointer variable: int *a; Copyright © 2008 W. W. Norton & Company. All rights reserved. 22

Gator Engineering Array Arguments (Revisited) In the latter case, a is not an array; attempting to use it as an array can have disastrous results. For example, the assignment *a = 0; /*** WRONG ***/ will store 0 where a is pointing. Since we don’t know where a is pointing, the effect on the program is undefined. Copyright © 2008 W. W. Norton & Company. All rights reserved. 23

Gator Engineering Array Arguments (Revisited) Consequence 4: A function with an array parameter can be passed an array “slice”—a sequence of consecutive elements. An example that applies find_largest to elements 5 through 14 of an array b : largest = find_largest(&b[5], 10); Copyright © 2008 W. W. Norton & Company. All rights reserved. 24

Gator Engineering Using a Pointer as an Array Name C allows us to subscript a pointer as though it were an array name: #define N 10 … int a[N], i, sum = 0, *p = a; … for (i = 0; i < N; i++) sum += p[i]; The compiler treats p[i] as *(p+i). Copyright © 2008 W. W. Norton & Company. All rights reserved. 25