Data Types Storage Size Domain of all possible values Operations 1.

Slides:



Advertisements
Similar presentations
Array. Convert Numbers in Different Base Systems Generate values to a series of numbers in different base systems: Base is between 2 and 9; Maximum number.
Advertisements

Operator Overloading. Introduction Operator overloading –Enabling C++’s operators to work with class objects –Using traditional operators with user-defined.
Templated Functions. Overloading vs Templating  Overloaded functions allow multiple functions with the same name.
Student Data Score First Name Last Name ID GPA DOB Phone... How to store student data in our programs? 1.
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.
Current Assignments Homework 5 will be available tomorrow and is due on Sunday. Arrays and Pointers Project 2 due tonight by midnight. Exam 2 on Monday.
File streams Chapter , ,
C Strings. The char Data Type for Storing Characters The char data type can is used to declare a variable that can hold a single character. Examples:
Pointers and Strings. Introduction Pointers –Powerful, but difficult to master –Simulate call-by-reference –Close relationship with arrays and strings.
Chapter 10.
1 String Library and Stream I/O Ying Wu Electrical Engineering & Computer Science Northwestern University ECE230 Lectures Series.
Arrays Hanly - Chapter 7 Friedman-Koffman - Chapter 9.
Chapter 9: Arrays and Strings
CS Nov 2006 C-strings.
Chapter 8 Arrays and Strings
String What it is Why it’s useful library routines for handling strings how to input a string from the keyboard.
CS 117 Spring 2002 Review for Exam 3 arrays strings files classes.
Modular Programming Chapter Value and Reference Parameters t Function declaration: void computesumave(float num1, float num2, float& sum, float&
Elements of a C++ program 1. Review Algorithms describe how to solve a problem Structured English (pseudo-code) Programs form that can be translated into.
In Addition... To the string class from the standard library accessed by #include C++ also has another library of string functions for C strings that can.
Pointers CS362. Pointers A Pointer is a variable that can hold a memory address Pointers can be used to: Indirectly reference existing variables (sometimes.
Data Type string #include // C++ String class string str1, str2; // Default constructor cin >> str1 >> str2; cout
Modular Programming Chapter Value and Reference Parameters computeSumAve (x, y, sum, mean) ACTUALFORMAL xnum1(input) ynum2(input) sumsum(output)
Chapter 8 Arrays and Strings
M. Taimoor Khan #include void main() { //This is my first C++ Program /* This program will display a string message on.
EGR 2261 Unit 9 Strings and C-Strings  Read Malik, pages in Chapter 7, and pages in Chapter 8.  Homework #9 and Lab #9 due next week.
C++ PROGRAMMING: PROGRAM DESIGN INCLUDING DATA STRUCTURES, FIFTH EDITION Chapter 10: Strings and string type.
Character Arrays Based on the original work by Dr. Roger deBry Version 1.0.
Chapter 7 Additional Control Structures. 2 2 void GetYesOrNo (/* out */ char& response) // Inputs a character from the user // Postcondition: response.
CS Midterm Study Guide Fall General topics Definitions and rules Technical names of things Syntax of C++ constructs Meaning of C++ constructs.
Pointers, Variables, and Memory. Variables and Pointers When you declare a variable, memory is allocated to store a value. A pointer can be used to hold.
Define our own data types We can define a new data type by defining a new class: class Student {...}; Class is a structured data type. Can we define our.
Chapter 13 – C++ String Class. String objects u Do not need to specify size of string object –C++ keeps track of size of text –C++ expands memory region.
Review Binary Numbers Bit : 0 or 1 Byte: 8 bites 256 different values 2 8 KB : 1024 bytes 2 10 bytes MB : 1024 * 1024 bytes 2 10 * 2 10 (2 20 ) bytes GB.
 2000 Deitel & Associates, Inc. All rights reserved Introduction Pointers –Powerful, but difficult to master –Simulate call-by-reference –Close.
1 Cannon_Chapter9 Strings and the string Class. 2 Overview  Standards for Strings  String Declarations and Assignment  I/O with string Variables 
12/15/2015Engineering Problem Solving with C++, Second Edition, J. Ingber 1 Engineering Problem Solving with C++, Etter Chapter 6 One-Dimensional Arrays.
1 Character Strings (Cstrings) Reference: CS215 textbook pages
COIT29222-Structured Programming Lecture Week 08  Reading: Textbook (4 th Ed.), Chapter 4 Textbook (6 th Ed.), Chapter 7 Study Guide Book 2, Module 11.
Quiz // // The function exchanges the two parameters. // Param: ( ) // Param:
Ch6 & 12 Note Dr. Wang. Strings for C, C++, Java C++ string – string str = “VWC”; C- string – char cstr[21] = {‘V’, ‘W’, ‘C’, ‘\0’}; char cstr2[21] =
CS 1430: Programming in C++ 1. Data Type string #include // C++ String class string str1, str2; // Default constructor cin >> str1 >> str2; cout
Chapter 15 Strings as Character Arrays
Sahar Mosleh California State University San MarcosPage 1 Character String.
 2003 Prentice Hall, Inc. All rights reserved. 5.11Function Pointers Pointers to functions –Contain address of function –Similar to how array name is.
Review Pointer Pointer Variables Dynamic Memory Allocation Functions.
1 For Loops l From Chapter 9 l A shorthand way of coding count loops.
CS 1430: Programming in C++ 1. Class StudentList class StudentList { private: int numStudents; Student students[MAX_SIZE]; int find(const Student& s)
Strings, Slide Fundamental Programming Strings.
Class Method Read class Student { private: string id; string firstName, lastName; float gpa; public: // Will the method change any data members? // Yes!
CS 1430: Programming in C++ 1. Test 2 Friday Functions Arrays For Loops Understand Concepts and Rules Memorize Concepts and Rules Apply Concepts and Rules.
13/15/2016CS150 Introduction to Computer Science 1 Summary  Assignment due on Wednesday, October 29,  Tutor will be in the lab on Tuesday evening,
CS 1430: Programming in C++.
CS 1430: Programming in C++ 1. File Input in VS Project Properties Debugging Command Arguments quiz8-1.out We want to know how to do it ourselves, right?
Array. Array is a group of data of the same type. Array elements have a common name –The array as a whole is referenced through the common name Individual.
Arrays float Scores[9]; ? index: element // one dimensional array 2.
Nested Structures struct TDate { int year, month, day; }; struct StudentType { string id, firstName, lastName; float gpa; TDate DOB; }; struct SectionType.
CS 1430: Programming in C++.
CS 1430: Programming in C++.
CS 1430: Programming in C++.
CS 1430: Programming in C++ Turn in your Quiz1-2 No time to cover HiC.
C Stuff CS 2308.
Enumeration Data Type enum Day {SUN, MON, TUE, WED, THU, FRI, SAT};
CS1430: Programming in C++ Section 2 Instructor: Qi Yang 213 Ullrich
Counting Loops.
CS150 Introduction to Computer Science 1
Engineering Problem Solving with C++, Etter
CS150 Introduction to Computer Science 1
What Actions Do We Have Part 1
Fundamental Programming
Presentation transcript:

Data Types Storage Size Domain of all possible values Operations 1

C++ Data Types 2

Storage Size System dependent char: 1 byte int: 2 bytes float: 4 bytes bool: 1 or 2 bytes C++ string: Reference (num of chars + 1) Array: max number of elements type of elements C string (array of char as string) max num of chars + 1 Structure Sum of all member fields 3

Domain of Possible Values char ASCII code table int float Much larger range string (both C string and C++ string) Any thing within “a string” Array All elements are of the same type char, int, float, string, bool, structure… Struct 4

C++ Operations Input Output Assignment Comparison Arithmetic operations Function return value Function parameter Others? 5

Operations on int int x, y; // Input cin >> x; inFile >> x; // Output cout << x; outFile << x; cout << setw(9) << x << endl; // Assignment x = 10; y = x; // Comparison if (x <= y) cout << “x is no more than y.”; if (x == y) cout << “x is the same as y.”; // Syntax and Style // Arithmetic operations x += y; y = x % 5; 6

Operations on int // Function return value int ReturnInt(); // Function parameters // Params: (in, out, inout) void IntParameters(int IntIn, int& IntOut, int& IntInOut); 7

Operations on char char x, y; // Input cin >> x; // skip white spaces cin.get(x); // read in white spaces inFile.get(x); // Output cout << x; cout << setw(9) << x << endl; // Assignment x = ‘0’; // not null char x = ‘\0’; // null char y = x; // Comparison: ASCII code value if (x <= y) cout << “x is no more than y.”; if (x == ‘A’) cout << “x is a ‘A’.”; // Arithmetic operations x ++; y = x % 5; 8

Operations on char // Function return value char ReturnChar(); // Function parameters // Params: (in, out, inout) void charParameters(char charIn, char& charOut, char &charInOut); 9

Operations on float Same as int, except % float x, y; // Input // Output // Assignment // Comparison // Arithmetic operations y = x % 5; // NO! // Function return value // Function parameters // Params: (in, out, inout) 10

Operations on string (C++ string) string x, y; // Input cin >> x; inFile >> x; // Output cout << x; cout << setw(9) << x; // Assignment x = “My String.”; y = x; // Comparison if (x > “CS1430”) cout << “x is larger than CS1430.”; if (x > “CS1430”) cout << "x is larger than " << char(34) << "CS1430" << char(34); if (x == y) cout << “x is the same as y.”; // Arithmetic operations // NO! 11

Operations on string (C++ string) // Function return value string ReturnString(); // Function parameters // Params: (in, out, inout) void stringParameters(string stringIn, string& stringOut, string& stringInOut); 12

Operations on Array const int MAX_SIZE = 100; int size = 100; int GoodIntArray[MAX_SIZE * ]; int badIntArray[size]; float floatArray[100]; string stringArray[MAX_SIZE]; char charArray[MAX_SIZE]; // Array of char or C String StudentType CS143[25]; 13

Operations on Array int GoodIntArray[MAX_SIZE * ]; float floatArray[100]; char charArray[MAX_SIZE + 1]; // Input cin >> GoodIntArray; // NO! cin >> floatArray; // NO! cin >> charArray; // Yes! Treated as a String (null char inserted) cout << GoodIntArray; // NO! cout << floatArray; // NO! cout << charArray; // Yes if treated as a string (must be null terminated) 14

Operations on Array int GoodIntArray[100]; float floatArray[100]; char charArray[101]; int A[100]; A = GoodIntArray; // NO! float B[100]; B = floatArray; // NO! char C[101]; C = charArray; // NO! 15

Operations on Array // Comparison int A[100]; if (A == GoodIntArray) cout << “Same array!”; // NO! float B[100]; if (B <= floatArray) cout << “B is smaller than floatArray.”; // NO! char C[101]; if (C > charArray) cout << “C is larger than charArray.”; // NO! 16

Operations on Array // Function return value int A[] CannotReturnArray(); // NO! // Function parameters // Params: (in, out, inout, in) void arrayParameters(const int arrayIn[], float arrayOut[], char arrayInOut[], int size); 17

C String (Array of Char) Using C++ string #include Using C string #include Functions: strlen() strcpy() strcmp() 18

Operations on C-String #include char Name[21]; // up to 20 chars, ending with ‘\0’ cin >> Name; // Yes, ‘\0’ is inserted cout << name; // Yes, stop at ‘\0’ 19

Operations on C-String Name = “CS2430”; // NO! strcpy(Name, “CS2430”); if (Name > “CS1430”) cout << “Larger than 1430”; // NO! if (strcmp(Name, “CS1430”) > 0) cout << “Larger than 1430”; 20

Array of char vs. C-String const int NUM_QUESTIONS = 50; const int NAME_SIZE = 20; char quizName[NAME_SIZE + 1]; char quizAnswer[NUM_QUESTIONS]; cin >> quizName; // C String! // ‘\0’ at the end for (int i = 0; i < NUM_QUESTIONS; i ++) cin >> quizAnswer[i]; // Not C String! // Not ending with ‘\0’ 21

Operations on struct StudentType s1, s2; // Input cin >> s1; // NO! cin >> s1.gpa; // YES! // Output cout << s1; // NO! cout << s1.gpa; // YES! s2 = s1; // Yes! if (s1 < s2) cout << “s1 is smaller.”; // NO! 22

Function Parameters In The function uses the value of the actual parameter. Out The function updates the value of the actual parameter and passes it back to the calling function. (a function can only return one value using the return statement) InOut Both In and Out 23

Passing Function Parameters Passing basic data types (int, float, char, string, bool) IN: Pass by value (without &) OUT, INOUT: Pass by reference (with &) Passing arrays (including C-String) Always pass by reference (NO &) IN: const (No &) Passing structs (Our programming rule) Always pass by reference with & IN: const (with &) Passing structs (C ++ rule) IN: Pass by value (without &) OUT, INOUT: Pass by reference (with &) 24

Test 3 Wednesday 60 points File I/O Enumeration C-String Selection Sort Structure Dynamic List Deleting 25