Extra Recitations Wednesday 19:40-22:30 FENS L055 (tomorrow!) Friday 13:40-16:30 FENS L063 Friday 17:40-2 0 :30 FENS L045 Friday 19:40-22:30 FENS G032.

Slides:



Advertisements
Similar presentations
Introduction to C Programming
Advertisements

C Language.
CS201 – Introduction to Computing – Sabancı University 1 First Midterm Exam l November 22, 2008, Saturday, 10:40 – 12:20, max 100 minutes l One A4 size.
1 CSC 222: Computer Programming II Spring 2004  vectors  library, templated  advantages over arrays:  const-reference parameters  growable vectors.
Chapter 10.
Computer Science 1620 Loops.
Arrays Hanly - Chapter 7 Friedman-Koffman - Chapter 9.
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.
Chapter 8. 2 Objectives You should be able to describe: One-Dimensional Arrays Array Initialization Arrays as Arguments Two-Dimensional Arrays Common.
Chapter 9: Arrays and Strings
Computer Science II Exam I Review Monday, February 6, 2006.
How to Program in C++ CHAPTER 3: INPUT & OUTPUT INSTRUCTOR: MOHAMMAD MOJADDAM.
Introduction to Array The fundamental unit of data in any MATLAB program is the array. 1. An array is a collection of data values organized into rows and.
Program Input and the Software Design Process ROBERT REAVES.
Chapter 11: Structured Data. Slide Introduction An array makes it possible to access a list or table of data of the same data type by using a single.
 2006 Pearson Education, Inc. All rights reserved Arrays.
CHAPTER 07 Arrays and Vectors (part I). OBJECTIVES 2 In this part you will learn:  To use the array data structure to represent a set of related data.
Arrays Module 6. Objectives Nature and purpose of an array Using arrays in Java programs Methods with array parameter Methods that return an array Array.
Announcements HW3 grades will be announced this week HW4 is due this week Final exam on August 25, 2010, Wednesday at 09:00 Duration is about 140 minutes.
File I/O ifstreams and ofstreams Sections 11.1 &
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 11 Structured Data.
DATA STRUCTURES LAB 1 TA: Nouf Al-harbi
Introduction to C++ Version 1.1. Topics C++ Structure Primitive Data Types I/O Casting Strings Control Flow.
CHAPTER 7 arrays I NTRODUCTION T O C OMPUTER P ROGRAMMING (CSC425)
File I/O 1 ifstreams and ofstreams Sections 11.1 & 11.2.
1 Chapter 11 Structured Data. 2 Topics 10.1 Abstract Data Types 10.2 Combining Data into Structures 10.3 Accessing Structure Members 10.4 Initializing.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 11: Structured Data.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 11: Structured Data.
2nd Midterm Exam December 19th, 2009, Saturday 09:00 – 11:00 Places: if (LastName
Reference parameters (6.2.3) We might need a function to return more than one value Find roots of a quadratic 2 return values. What are they? Get first.
Copyright © 2012 Pearson Education, Inc. Chapter 11: Structured Data.
Structured Data Types struct class Structured Data Types array – homogeneous container collections of only one type struct – heterogeneous data type.
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 Curt Hill Arrays in C/C++ What? Why? How?
2nd Midterm Exam December 26th, 2008, Friday 17:40 – 19:20 Places: FENS G032 Acar - Avcı FENS G035 Aydeniz - Büyükkıdık FASS G022 Büyüknacar - Demir FASS.
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.
Structuring Data: Arrays ANSI-C. Representing multiple homogenous data Problem: Input: Desired output:
CS201 – Introduction to Computing – Sabancı University 1 Built-in Arrays l C++ native array type (not the class version) l Two versions ä fixed size arrays.
Arrays.
1 CS161 Introduction to Computer Science Topic #15.
CSC 221: Computer Programming I Fall 2001  arrays  homogeneous collection of items, accessible via an index  initializing/traversing/displaying arrays.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 11: Structured Data.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 11: Structured Data.
Manipulator example #include int main (void) { double x = ; streamsize prec = cout.precision(); cout
Struct s (7.4) Used as data aggregates for an entity can be different types of data e.g. for student id, name, GPA, address,... Similar to classes, but.
Array Size Arrays use static allocation of space. That is, when the array is created, we must specify the size of the array, e.g., int[] grades = new int[100];
Announcements Midterm 2 is on TODAY, December 19 th at 19:40. Extra Recitation TODAY at 15:40-18:30 in FENS L062 Midterm Places: if (LastName
CPS120 Introduction to Computer Science Exam Review Lecture 18.
Programming Fundamentals. Today’s Lecture Array Fundamentals Arrays as Class Member Data Arrays of Objects C-Strings The Standard C++ string Class.
1 1  Lecture 11 – Structured Data FTMK, UTeM – Sem /2014.
1 Lecture 4: Part1 Arrays Introduction Arrays  Structures of related data items  Static entity (same size throughout program)
1 Chapter 7 Pointers and C-Strings. 2 Objectives  To describe what a pointer is (§7.1).  To learn how to declare a pointer and assign a value to it.
Announcements Assignment 2 Out Today Quiz today - so I need to shut up at 4:25 1.
Chapter Structured Data 11. Combining Data into Structures 11.2.
A Computer Science Tapestry 8.1 Vectors l Vectors are homogeneous collections with random access  Store the same type/class of object, e.g., int, string,
Topic 5 Addresses, Pointers and Arrays. 2 Objectives (Textbook Chapter 14) You should be able to describe: Addresses and Pointers Pointer Operators Pointer.
A FIRST BOOK OF C++ CHAPTER 7 ARRAYS. OBJECTIVES In this chapter, you will learn about: One-Dimensional Arrays Array Initialization Arrays as Arguments.
Announcements HW6 due this Wednesday
Chapter 7 Part 1 Edited by JJ Shepherd
What’s Left in the Course
Chapter 11: Structured Data.
Arrays An array is a collection of variables that all have the same name and the same data type. Each member of the array is known as an element of the.
Announcements HW6 due this Wednesday
7 Arrays.
Java Programming Language
Standard Version of Starting Out with C++, 4th Edition
Presentation transcript:

Extra Recitations Wednesday 19:40-22:30 FENS L055 (tomorrow!) Friday 13:40-16:30 FENS L063 Friday 17: :30 FENS L045 Friday 19:40-22:30 FENS G032

Announcements about HW6 You may need to use clear() member function before you try to reopen an input file stream object that you failed to open previously (for example due to wrong file name), or that you opened and processed but for some reason if the error flags are set (for example due to reaching the end of the file). Possible reasons for run time errors in this homework Attempting to read from a file that has not been opened yet Attempting to write to a file that has not been opened yet Range and index problems while trying to access characters of a string using find, substr and at member functions.

Announcements about HW6 “Should we check if the output file is opened successfully or not?” Not required, but advised There might be some cases that the output files are not opened successfully If you check and the output file is not opened, then do not continue with the program. What happens if the files are opened but the content is irrelevant? What happens if the file names are entered in the wrong order? What happens if both file names are the same? All of these questions are reduced into the same main question: should we make the content check for the files that are opened successfully? NO. As mentioned in the HW document, the content of the files are assumed to be correct. What you have to do is only to check if the files are opened successfully or not and continue to read file names until opened. Once opened, we assume that the first file is the business database, and the second one is the distance database files.

struct s (7.4) Used as data aggregates for an entity can be different types of data e.g. for student id, name, GPA, address,... Similar to classes, but everything is public structs can have constructors structs can have member functions we will not deal with constructors and member functions for structs unless they are necessary mostly we will use structs for combining data for an entity into a single structure

Structs Example: struct for student First struct must be defined by giving it a name and its fields (data) struct student // student is struct name { unsigned int id; // fields of student struct string name, lastname; double gpa; }; // dont forget ; at the end Then variables of that type are declared and used. dot operator is used to refer fields of a struct variable student stu; stu.name = "Ali"; cout << stu.gpa; See structdemo.cpp (not in book)

What can and can’t be done with structs Structs can be passed to functions as parameters use const-reference if not changing (using value parameter is syntactically OK, but not preferred due to performance reasons) use reference parameter if changing struct fields behave as variables/objects of field type id is an integer name is a string You may read, write, use as operands in operations, etc. However, processing the entire struct variable is restrictive cannot read or write (using >> and <<) structs unless those operators are specially defined for that struct cannot use operators between two structs unless those operators are specially defined for that struct see for operator definitions for structs, but not responsible structs are useful mostly in vectors (arrays)

Vectors and Arrays Arrays are collections of several elements of the same type E.g. 100 integers, 20 strings, 125 students, 12 dates, etc. Single name is given to the entire array But each element is accessed separately Any element of an array can be accessed just as quickly as any other element (this is called “random access” but do not get confused with RandGen type of randomness) In C/C++ there is a built-in array type We will see it, but later Vectors are a class-based version of arrays First we will see vectors.

Vectors We’re using the class tvector Copy tvector.h and tvector.cpp to your project folder need #include "tvector.h" But do NOT add tvector.cpp to your project It is already included within tvector.h If you mistakenly add tvector.cpp to your project, then you get tons of errors. tvector is a tapestry class that has the same functionality and based on the standard C++ class vector, but safer “Safe” means programming errors are caught rather than ignored

Why do we need arrays/vectors? Consider the following example (not in the book): pick n random numbers between 0 and 6 and count total number of occurrences of all outcomes (0, 1, 2, 3, 4, 5, 6) n is an input we need 7 counters 7 declarations 7 initializations 7 conditions to increment after each occurrence 7 cout statements to display the result Fortunately, we have shorter way: ARRAYS/VECTORS We can use vectors to store counters for all possible outcomes of the random numbers under a single name easier processing in loops see next slide for the program

Example Previous example using vectors - see randnums.cpp int num; int k; RandGen random; tvector randStats(7); // vector for counters int n = PromptRange("how many random numbers",1,20000); for(k=0; k <= 6; k++) // initialize counters to zero { randStats[k] = 0; } for(k=0; k < n; k++) // pick all random numbers { num = random.RandInt(7); // between 0 and 6 randStats[num] = randStats[num] + 1; // and increment // corresponding counter } cout << "number\t\t# of occurrences" << endl; for(k=0; k <= 6; k++) { cout << k << "\t\t" << randStats[k] << endl; } randStats

Vector/Array basics Vectors/Arrays are homogeneous each item (sometimes called element) has the same type that type must be specified at declaration Items in a vector/array are numbered (e.g. 1 st, 3 rd, or 105 th ) those are called index or subscript numbering starts with 0 we have to use the index value to refer an element in a vector/array Example definition and use of vectors (array definition is a bit different) tvector ivals(10); // ivals can store 10 ints ivals[0] = 3; // 0 th element becomes 3 tvector svals(20); // svals can store 20 strings svals[4] = "cs201"; // 4 th element contains "cs201"

Vector basics Syntax of vector declaration tvector is a class, its declaration is construction 3 different methods tvector variable_name; empty vector (will see later) tvector variable_name (size_expression); vector with size_expression elements in it tvector variable_name (size_expression, init_value); vector with all size_expression elements initialized to init_value

Vector basics size_expression can be any expression of type integer (or cast into integer) not necessarily a constant value (this is actually a very important flexibility as compared to built-in arrays) examples: tvector letters (int('Z')-int('A') + 1); creates a vector of 26 integer elements and name it letters cin >> num; tvector counters (num); creates a vector of doubles; total number of elements is input Index value starts with 0 and ends with size-1 type is type of the vector elements can be built-in types (int, double,...) or user defined types or classes or structs (string and date are class examples; student is struct example) classes must have default constructors to be used in vector definition as element type

Defining tvector objects Can specify # elements in a vector, optionally an initial value tvector counts(300); //300 ints, values not initialized tvector nums(200, 0); // 200 ints, all zero tvector d(10, 3.14); // 10 doubles, all pi tvector w(10, "cs"); // 10 strings, all "cs" tvector words(10); // 10 strings, all "" If the vector type is a class, then this class must have a default constructor Default constructor is the one without parameters Cannot define tvector cubes(10); since Dice doesn’t have default constructor Vectors of classes are initialized with the default constructor that is why all words are "" (empty string) Vectors with built-in types are not initialized (unless explicitly initialized with the second argument of tvector definition)

Example tvector definitions tvector counter(9, 0); each element is an integer (all initialized to 0) counter letters tvector letters(18); each element is a char (not initialized yet) holidays tvector holidays(6); each element is a date object that contains todays date

How to reach a single vector/array element specify the index value within square brackets after the vector/array name var_name [index_expr] the value of index expression must be between 0 and (vector size – 1) Examples tvector nums(9); nums[5] = 102; nums[0] = nums[5]*2-1; nums[nums[5]/20-3] = 55; nums[10] = 5; // error nums

Passing vectors to functions as parameters Vectors can be passed as parameters to functions Pass by reference (if function changes the vector) void Count (tvector & counts); Pass by const-reference (if no changes made). void Print(const tvector & counts); Passing by value makes a copy, requires time and space, so not preferred IMPORTANT!!! Vector size cannot be given in parameter definition. Three solutions to this problem: the size may be passed as another parameter the size may be fixed and known tvector has a member function, size, to return the size of a vector

Example Counting letters of a file display number of occurrences of each letter at the end counting is case insensitive see letters.cpp (the one in book is a bit different)