CS50 Week 2. RESOURCES Office hours (https://www.cs50.net/ohs)https://www.cs50.net/ohs Lecture videos, slides, source code, and notes (https://www.cs50.net/lectures.

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
C Programming lecture 2 Beautiful programs Greek algorithms
C Language.
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.
Functions in C++. Functions  Groups a number of program statements into a unit & gives it a name.  Is a complete and independent program.  Divides.
Loops (Part 1) Computer Science Erwin High School Fall 2014.
Python Magic Select a Lesson: Why Learn to Code? Basic Python Syntax
Week 9: Methods 1.  We have written lots of code so far  It has all been inside of the main() method  What about a big program?  The main() method.
©2004 Brooks/Cole Chapter 8 Arrays. Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Sometimes we have lists of data values that all need to be.
Loops – While, Do, For Repetition Statements Introduction to Arrays
ECE122 L11: For loops and Arrays March 8, 2007 ECE 122 Engineering Problem Solving with Java Lecture 11 For Loops and Arrays.
C. About the Crash Course Cover sufficient C for simple programs: variables and statements control functions arrays and strings pointers Slides and captured.
CS1061 C Programmuing Lecture 12 Arrays A. O’Riordan, 2004.
CS 106 Introduction to Computer Science I 10 / 15 / 2007 Instructor: Michael Eckmann.
Assignment #2, 12- month Calendar CS-2301, B-Term Programming Assignment #2 12-Month Calendar CS-2301, System Programming for Non-Majors (Slides.
Introduction to Methods
The switch Statement, DecimalFormat, and Introduction to Looping
TODAY’S LECTURE Review Chapter 2 Go over exercises.
C++ Functions. 2 Agenda What is a function? What is a function? Types of C++ functions: Types of C++ functions: Standard functions Standard functions.
CS 31 Discussion, Week 6 Faisal Alquaddoomi, Office Hours: BH 2432, W 4:30-6:30pm, F 12:30-1:30pm.
Programming Languages -1 (Introduction to C) arrays Instructor: M.Fatih AMASYALI
Beginning C++ Through Game Programming, Second Edition by Michael Dawson.
18-2 Understand “Scope” of an Identifier Know the Storage Classes of variables and functions Related Chapter: ABC 5.10, 5.11.
Variables, Functions & Parameter Passing CSci 588 Fall 2013 All material not from online sources copyright © Travis Desell, 2011.
NSIT,Jetalpur CORE JAVA CONCEPTS SURABHI MISHRA (LCE)NSIT.
Loops: Handling Infinite Processes CS 21a: Introduction to Computing I First Semester,
Oct 15, 2007Sprenkle - CS1111 Objectives Creating your own functions.
Web Database Programming Week 3 PHP (2). Functions Group related statements together to serve “a” specific purpose –Avoid duplicated code –Easy maintenance.
CS50 Section 2 21 September 2015 Sam Green ’17 (646)
Variables and Functions. Open your Encoder program Let’s begin by opening the “Labyrinth Auto Straight” code. Save this file as Labyrinth with variables.
CPS120: Introduction to Computer Science Decision Making in Programs.
Engineering Computing I Chapter 4 Functions and Program Structure.
Current Assignments Start Reading Chapter 6 Project 3 – Due Thursday, July 24 Contact List Program Homework 6 – Due Sunday, July 20 First part easy true/false.
CS161 Topic #16 1 Today in CS161 Lecture #16 Prepare for the Final Reviewing all Topics this term Variables If Statements Loops (do while, while, for)
Lecture 22: Reviews for Exam 2. Functions Arrays Pointers Strings C Files.
Lecture 13: Working with Multiple Programmers. Headers Header files: Each standard library has a corresponding header. The function prototype for all.
1 Basic Java Constructs and Data Types – Nuts and Bolts Looking into Specific Differences and Enhancements in Java compared to C.
Computer Organization and Design Pointers, Arrays and Strings in C Montek Singh Sep 18, 2015 Lab 5 supplement.
Cryptography.
Programming Fundamentals. Topics to be covered Today Recursion Inline Functions Scope and Storage Class A simple class Constructor Destructor.
Fall 2004CS-183 Dr. Mark L. Hornick 1 C++ Arrays C++ (like Java) supports the concept of collections – mechanisms to sort and manipulate many instances.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Summary and Exam COMP 102.
Arrays-. An array is a way to hold more than one value at a time. It's like a list of items.
CS 31 Discussion, Week 5 Faisal Alquaddoomi, Office Hours: BH 2432, MW 4:30-6:30pm, F 12:00-1:00pm (today)
Functions Structured Programming. Topics to be covered Introduction to Functions Defining a function Calling a function Arguments, local variables and.
Lecture 7: Arrays Tami Meredith. Roadmap Variables Arrays Array indices & Using arrays Array size versus length Two dimensional arrays Sorting an array.
Methods Awesomeness!!!. Methods Methods give a name to a section of code Methods give a name to a section of code Methods have a number of important uses.
Computer Programming for Engineers
Arrays Chapter 12. Overview Arrays and their properties Creating arrays Accessing array elements Modifying array elements Loops and arrays.
CPS120 Introduction to Computer Science Exam Review Lecture 18.
Passing Function Arguments by Reference : A Sample Lesson for CS 1 using the C Programming Language Andy D. Digh Friday, May 29th, 1998.
ECE 103 Engineering Programming Chapter 30 C Functions Herbert G. Mayer, PSU CS Status 8/9/2014 Initial content copied verbatim from ECE 103 material developed.
FUNCTIONS (C) KHAERONI, M.SI. OBJECTIVE After this topic, students will be able to understand basic concept of user defined function in C++ to declare.
C Tutorial - Pointers CS 537 – Introduction to Operating Systems.
ITM 3521 ITM 352 Functions. ITM 3522 Functions  A function is a named block of code (i.e. within {}'s) that performs a specific set of statements  It.
Beginning C for Engineers Fall 2005 Arrays, 2-D arrays, character strings Bettina Schimanski Lecture 5: Section 2 (9/28/05) Section 4 (9/29/05)
User-Written Functions
CMSC201 Computer Science I for Majors Lecture 12 – Lists (cont)
Programming Fundamentals Lecture #7 Functions
Loops The loop blocks you've used in Scratch can all be recreated in C! Loops allow for the repetition of code until a condition is met. There are three.
Functions Inputs Output
Switch Statements, Do While, Break, Continue, Goto, Comma Operator
Functions BIS1523 – Lecture 17.
IPC144 Introduction to Programming Using C Week 4 – Lesson 2
Arrays.
Methods and Data Passing
Presentation transcript:

CS50 Week 2

RESOURCES Office hours ( Lecture videos, slides, source code, and notes ( ) A growing corpus of course material ( Walkthroughs Sundays 7-8:30 in Northwest Science B103 (videos at Bulletin board ( Us! (

FUNCTIONS f xf(x) A function is a black box – it takes some input(s), does something to them, and produces some output.

What's the use? Organization – we can group ('encapsulate') lines of related code into a function, and give it some descriptive name. This helps us break a big problem into small chunks Reusability – we can write a function once, and use it as many times as we'd like. Imagine if you had to copy and paste the code that implements “printf” every time you wanted to print something!

Anatomy of a function ( ) { } int main (int argc, char *argv[]) { // code }

Anatomy of a function Return type: int, void, char, float, etc... Name: main, printf, GetInt, do_something3, etc... Arguments: “int x”, “char *argv[]”, “float max”, etc. These 3 things make up the “header” of the function. The implementation of the function is all that's left, and it is called the “body”

SCOPE All variables are contained within a certain scope, and can only be used within that scope. Anything within the same set of curly braces is within the same scope. So functions, loops, and if/else blocks, which always have explicit or implied curly braces, always set up a new scope If a variable is declared within a scope with the same name as a variable declared in a broader scope that contains the inner scope (for example, an if condition inside of a while loop), then it is impossible to access the first-declared variable until the inner scope is exited. Global (as opposed to local) variables are declared outside all curly braces, and thus outside of all functions. There scope is thus the program itself!

MAGIC NUMBERS Image: Values hard-coded in a program that were seemingly “pulled out of a hat.” Magic numbers are bad style! Instead, use #define to give the number a name, e.g. NUM_INPUTS, and replace all instances of the magic number with that constant. This allows us to, for example, change the number of inputs by just changing what that constant is defined as! #define NUM_INPUTS 4

ARRAYS A data structure that groups together a certain number of values, all of the same type. Thanks to arrays, if we want 100 different variables that store user input, we don't need to declare 100 variables, we can just use a single array of size 100. Allow for easy iteration over a set of data.

ARRAYS int numbers[4]; numbers[0] = 7; numbers[1] = 8; numbers[3] = 2; numbers[2] = 5; ???? numbers ? Alternative: int numbers[4] = {7,8,5,2};

ARRAYS for(int i = 0; i < 4; i++) { printf(“%d\n”, numbers[i]); } Easy iteration:

MULTI-DIMENSIONAL ARRAYS A.K.A., “arrays of arrays” Useful when it makes more sense to think of an array in terms of having both rows and columns, or more! char tictactoe[3][3]; tictactoe[0][0] = 'X'; tictactoe[0][2] = 'X'; tictactoe[1][1] = 'O';...

Passing Arrays to Functions Remember from lecture that the “swap” function didn't work so well, since the variables in the function are just copies, and so changing them doesn't affect the original variables. Arrays aren't primitive data types, so they don't work in the same way. Instead, they are passed by 'reference', as opposed to the way we have seen where primitives are passed by 'value' So if you pass an array to a function, and that function changes the array, the change remains after the function returns!

CAESAR CIPHER ABCDEFGHIJKLMNOPQRSTUVWXYZ GHIJKLMNOPQRSTUVWXYZABCDEF Example: Rot 6 Rotate each character in a string by some number n characters. Effectively, a new “starting point” of the alphabet is created

VIGENERE CIPHER Similar to Caesar, except now “n” isn't constant over the entire string Given the key 'abf': To encode: rotate the 1 st character in our secret string by 'a' (i.e. 0), the 2 nd by 'b' (1), the 3 rd by 'f' (5), the 4 th by 'a' again, etc. To decode: rotate letters in the opposite direction! Instead of taking in just the string that we want to encode as input, we take an additional string, the “key”, which will be used to encode the first string

NOW IT’S TIME FOR SOME CODING!!!