Scope When we create variables and functions, they are limited in where they are visible and where they can be referenced For the most part, the identifiers.

Slides:



Advertisements
Similar presentations
Introduction to C Programming
Advertisements

08/2012Tanya Mishra1 EASYC for VEX Cortex Llano Estacado RoboRaiders FRC Team 1817.
One Dimensional Arrays
Programming Languages and Paradigms The C Programming Language.
General Computer Science for Engineers CISC 106 Lecture 28 Dr. John Cavazos Computer and Information Sciences 04/29/2009.
Tutorial 12 Working with Arrays, Loops, and Conditional Statements
 Monday, 9/30/02, Slide #1 CS106 Introduction to CS1 Monday, 9/30/02  QUESTIONS (on HW02, etc.)??  Today: Libraries, program design  More on Functions!
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.
CMPUT 101 Lab #6 October 29, :00 – 17:00. Array in C/C++ Array is a structure type variable. One dimension of array int: int num[3]; There are.
Java: How to Program Arrays and ArrayLists Summary Yingcai Xiao.
1 Arrays In many cases we need a group of nearly identical variables. Example: make one variable for the grade of each student in the class This results.
ECE122 L11: For loops and Arrays March 8, 2007 ECE 122 Engineering Problem Solving with Java Lecture 11 For Loops and Arrays.
Multiple-Subscripted Array
Chapter 8 Arrays and Strings
Guide To UNIX Using Linux Third Edition
1 Arrays & functions Each element of an array acts just like an ordinary variable: Like any ordinary variable, you can pass a single array element to a.
Arrays.
Computer Science 1620 Lifetime & Scope. Variable Lifetime a variable's lifetime is finite Variable creation: memory is allocated to the variable occurs.
Java Unit 9: Arrays Declaring and Processing Arrays.
Overview of Previous Lesson(s) Over View  OOP  A class is a data type that you define to suit customized application requirements.  A class can be.
Scott Marino MSMIS Kean University MSAS5104 Programming with Data Structures and Algorithms Week 10 Scott Marino.
 For Loops › for (variable set; condition; incremental or decrement){ // loop beginning › } // loop end  While loops › while (condition) { // beginning.
CSE 2341 Honors Professor Mark Fontenot Southern Methodist University Note Set 04.
C Programming Tutorial – Part I CS Introduction to Operating Systems.
Arrays (Part 1) Computer Science Erwin High School Fall 2014.
Chapter 8 Arrays and Strings
ASP.NET Programming with C# and SQL Server First Edition Chapter 3 Using Functions, Methods, and Control Structures.
Chapter 13 Recursion. Learning Objectives Recursive void Functions – Tracing recursive calls – Infinite recursion, overflows Recursive Functions that.
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.
DATA STRUCTURES LAB 1 TA: Nouf Al-harbi
1 Chapter 9 Additional Control Structures Dale/Weems.
Vectors and Grids Eric Roberts CS 106B April 8, 2009.
Lecture 4 Looping. Building on the foundation Now that we know a little about  cout  cin  math operators  boolean operators  making decisions using.
Chapter 8 Scope of variables Name reuse. Scope The region of program code where it is legal to reference (use) a variable The scope of a variable depends.
DT249-Information Systems Research Practice Programming Revision Lecture 2 Lecturer: Patrick Browne.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 5 Looping.
CSE 232: C++ memory management Overview of Arrays Arrays are the simplest kind of data structure –One item right after another in memory (“contiguous range”
Arrays.
CHAPTER 10 ARRAYS AND FUNCTIONS Prepared by: Lec. Ghader Kurdi.
Arrays An array is a data object that can hold multiple objects, all of the same type. We can think of an array as a storage box which has multiple compartments.
1 Standard Version of Starting Out with C++, 4th Brief Edition Chapter 5 Looping.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5: Looping.
Arrays. Collections We would like to be able to keep lots of information at once Example: Keep all the students in the class Grade each one without writing.
General Computer Science for Engineers CISC 106 Lecture 12 James Atlas Computer and Information Sciences 08/03/2009.
CS Class 15 Today  More practice with arrays  Introduction to Multi-dimensional arrays Announcements  Programming project #4 due 10/23 by midnight.
Repetition Statements (Loops). 2 Introduction to Loops We all know that much of the work a computer does is repeated many times. When a program repeats.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
C++ Array 1. C++ provides a data structure, the array, which stores a fixed-size sequential collection of elements of the same type. An array is used.
Arrays What is an array… –A data structure that holds a set of homogenous elements (of the same type) –Associate a set of numbers with a single variable.
Arrays Chapter 12. One-Dimensional Arrays If you wanted to read in 1000 ints and print them in reverse order, it would take a program that’s over 3000.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
Lecture 7 – Repetition (Loop) FTMK, UTeM – Sem /2014.
Chapter 5 Arrays Copyright © 2016 Pearson, Inc. All rights reserved.
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 7A Arrays (Concepts)
Chapter 9 Introduction to Arrays Fundamentals of Java.
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.
Introduction to Programming Lecture 12. Today’s Lecture Includes Strings ( character arrays ) Strings ( character arrays ) Algorithms using arrays Algorithms.
Test 2 Review Outline.
Two Dimensional Array Mr. Jacobs.
Suppose we want to print out the word MISSISSIPPI in big letters.
Chapter 5: Looping Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
11/10/2018.
Engineering Problem Solving with C++, Etter/Ingber
Value returning Functions
1020: Introduction to Programming Mohamed Shehata November 22, 2017
1020: Introduction to Programming Mohamed Shehata November 7, 2016
Multidimensional Arrays
The Function Prototype
Based on slides created by Bjarne Stroustrup & Tony Gaddis
Presentation transcript:

Scope When we create variables and functions, they are limited in where they are visible and where they can be referenced For the most part, the identifiers we create are visible in the block they are created, and in any sub-blocks What blocks are we talking about The global block – the entire program Function blocks – the code following a function definition, contained in {} Condition blocks – the code inside the {} of an if or else Loop blocks – the code inside the {} of a for or while loop Generic blocks – any code contained within {}

Scope... int main(){ int x = 3; fun();... } void fun(){ cout << x; //This will cause problems }

Scope … int x = 0; //This is global void fun(); int main(){ int x = 1;//this is local to main cout << x << endl; fun(); } void fun(){ cout <<x << endl;//what do we see here? }

Design We can generally break big problems down into smaller ones Sometimes the smaller ones can be broken down again Often we break down to the point where the small problems are handled by a single function This allows us to design a program to solve larger problems through the use of simple to write functions

Design Example “Write a program to read a text file containing names and output a new file with the names sorted and formatted” What are the small problems (look for the verbs)? - read- output- sort- format

Design So we’ll probably want functions that can void readFile(string names[]); void sortNames(string names[]); void formatNames(string names[]); void outputFile(string names[]);

Design Our main then simple can call the names in the order described by the problem int main(){ string names[100]; readFile(names); formatNames(names); sortNames(names); outputFile(names); }

Arrays Arrays allow us to make a collection of variables, each identified with the same label Each element is of the same type We refer to a specific element using an index Index are from 0 to NUM-1, where NUM is the number of elements in the array int x[5]; // an array of 5 int X[3] = 2; //sets the 4 th element to 2

Arrays We can create multi-dimensional arrays double y[3][7]; // a 3x7 matrix of doubles again, elements are indexed in the same manner y[2][5] = 1.5; //The element at 2,5, or row 2, col 5

Arrays Working with Most commonly problems deal with moving through the array - output every 2 nd element - add all elements - find the maximum element

Arrays Arrays are special in that they are always passed by reference into functions (they just are) This is true for 2-d arrays, but in this case we must always indicate the size of the 2 nd dimension( the columns) int fun(int a[], fload b[][5]) Care must always be taken to keep track of the size of the arrays

For Loops The other main form of looping (and often used when dealing with arrays and indexing) are for loops For loops allow us to declare code that is executed before the loop, a condition, and code that is executed at the end of each loop for (initialization code; condition; end of loop code) { code block; }

For Loops The most common form of for loops utilizes a counting variable, a condition to stop the loop, and a count update for(int i = 0; i < 5; i++) { cout << i << ‘,’; //We get 0,1,2,3,4, }

Strings String is a class (kind of like a type) which not only holds data, but also provides built in functions We declare objects (kinda like variables) which holds data strings, and allows us to call the built in functions to operate on the data Strings hold characters string s = “I’m a string”;

Strings We can do all kinds of fun things with Strings string s = “I’m a string”; cout << s; // output them s.length(); // find out how many characters int p = s.find(‘a’); //find the index of a character s.erase(p,2); // remove characters cout <<s; // What do we output now “I’m string”