CS 31 Discussion ABDULLAH-AL-ZUBAER IMRAN WEEK 6: C-STRINGS, STRUCT, CLASS AND PROJECT4

Similar documents
CS31 Discussion. Jie(Jay) Wang Week8 Nov.18

CS31 Discussion. Jie(Jay) Wang Week6 Nov.4

Discussion 1E. Jie(Jay) Wang Week 10 Dec.2

Consider the above code. This code compiles and runs, but has an error. Can you tell what the error is?

Week 5. Muhao Chen.

CS 31 Discussion 1A, Week 8. Zengwen Yuan (zyuan [at] cs.ucla.edu) Humanities A65, Friday 10:00 11:50 a.m.

Lecture 7. Log into Linux New documents posted to course webpage

CSC 126 FINAL EXAMINATION Spring Total Possible TOTAL 100

Agenda. The main body and cout. Fundamental data types. Declarations and definitions. Control structures

2 nd Week Lecture Notes

Integer Data Types. Data Type. Data Types. int, short int, long int

CS31 Discussion 1E. Jie(Jay) Wang Week1 Sept. 30

Review: Exam 1. Your First C++ Program. Declaration Statements. Tells the compiler. Examples of declaration statements

CMSC 202 Midterm Exam 1 Fall 2015

CS31 Discussion 1E. Jie(Jay) Wang Week3 Oct.12

CS2141 Software Development using C/C++ C++ Basics

Discussion 1H Notes (Week 3, April 14) TA: Brian Choi Section Webpage:

CSC 126 FINAL EXAMINATION FINAL Spring 2012 B. Name (last, First) Instructor. Total Possible. Received

CE221 Programming in C++ Part 1 Introduction

Object Oriented Design

Jordan University of Science & Technology Department of Computer Science CS 211 Exam #1 (23/10/2010) -- Form A

Introduction to Algorithms and Data Structures. Lecture 6 - Stringing Along - Character and String Manipulation

CS 103 Lab - Party Like A Char Star

CS212 - COMPUTATIONAL STRUCTURES AND ALGORITHMS

Variables. Data Types.

CSCE 110 PROGRAMMING FUNDAMENTALS

C++ For Science and Engineering Lecture 12

Downloaded S. from Kiran, PGT (CS) KV, Malleswaram STRUCTURES. Downloaded from

A First Program - Greeting.cpp

LECTURE 02 INTRODUCTION TO C++

Separate Compilation Model

THE INTEGER DATA TYPES. Laura Marik Spring 2012 C++ Course Notes (Provided by Jason Minski)

Week 8. Muhao Chen

Getting started with C++ (Part 2)

Linked List using a Sentinel

The C++ Language. Arizona State University 1

Characters, c-strings, and the string Class. CS 1: Problem Solving & Program Design Using C++

W3101: Programming Languages C++ Ramana Isukapalli

BITG 1113: Array (Part 2) LECTURE 9

CS31 Discussion 1E. Jie(Jay) Wang Week5 Oct.27

CS 31 Review Sheet. Tau Beta Pi - Boelter Basics 2. 2 Working with Decimals 2. 4 Operators 3. 6 Constants 3.

CS24 Week 3 Lecture 1

Deadline. Purpose. How to submit. Important notes. CS Homework 9. CS Homework 9 p :59 pm on Friday, April 7, 2017

AN OVERVIEW OF C++ 1

2.1. Chapter 2: Parts of a C++ Program. Parts of a C++ Program. Introduction to C++ Parts of a C++ Program

C++ basics Getting started with, and Data Types.

Your first C++ program

Exceptions, Case Study-Exception handling in C++.

Chapter 15 - C++ As A "Better C"

CS32 - Week 4. Umut Oztok. Jul 15, Umut Oztok CS32 - Week 4

Chapter 1 INTRODUCTION

Strings and Streams. Professor Hugh C. Lauer CS-2303, System Programming Concepts

CS31 Discussion 1E Spring 17 : week 08

Assignment #1 (50 points; due 11:59 P.M.)

Computer Programming

Your First C++ Program. September 1, 2010

CMSC202 Computer Science II for Majors

CS 31 Discussion 1A, Week 1. Zengwen Yuan (zyuan [at] cs.ucla.edu) Humanities A65, Friday 10:00 11:50

C++ Support Classes (Data and Variables)

! Pass by value: when an argument is passed to a. ! It is implemented using variable initialization. ! Changes to the parameter in the function body

More Flow Control Functions in C++ CS 16: Solving Problems with Computers I Lecture #4

Lab # 02. Basic Elements of C++ _ Part1

! A literal represents a constant value used in a. ! Numbers: 0, 34, , -1.8e12, etc. ! Characters: 'A', 'z', '!', '5', etc.

Pointers, Dynamic Data, and Reference Types

Chapter 2: Basic Elements of C++

Objectives. Chapter 2: Basic Elements of C++ Introduction. Objectives (cont d.) A C++ Program (cont d.) A C++ Program

Fundamentals of Programming CS-110. Lecture 2

Computer Programming

Abstract Data Types (ADTs) 1. Legal Values. Client Code for Rational ADT. ADT Design. CS 247: Software Engineering Principles

Chapter 2: Basic Elements of C++ Objectives. Objectives (cont d.) A C++ Program. Introduction

Topics. Functions. Functions

Review Questions for Final Exam

Fast Introduction to Object Oriented Programming and C++

Introduction to C++ Professor Hugh C. Lauer CS-2303, System Programming Concepts

Name. CPTR246 Spring '17 (100 total points) Exam 2

C++ Basics. Lecture 2 COP 3014 Spring January 8, 2018

! A program is a set of instructions that the. ! It must be translated. ! Variable: portion of memory that stores a value. char

PIC 10A Objects/Classes

CS 247: Software Engineering Principles. ADT Design

G52CPP C++ Programming Lecture 20

l Determine if a number is odd or even l Determine if a number/character is in a range - 1 to 10 (inclusive) - between a and z (inclusive)

PIC 10A Pointers, Arrays, and Dynamic Memory Allocation. Ernest Ryu UCLA Mathematics

A class is a user-defined type. It is composed of built-in types, other user-defined types and

GE U111 Engineering Problem Solving & Computation Lecture 6 February 2, 2004

Introduction to Programming using C++

Intro to Programming & C Why Program? 1.2 Computer Systems: Hardware and Software. Why Learn to Program?

System Design and Programming II

EECS 183, Week 5. General. Variables I/O. 0. At which location do you have to take the exam? 1. Source code vs. object code? 2. What s a library?

3. Functions. Modular programming is the dividing of the entire problem into small sub problems that can be solved by writing separate programs.

C++ For Science and Engineering Lecture 27

Multiple Choice (Questions 1 14) 28 Points Select all correct answers (multiple correct answers are possible)

ME240 Computation for Mechanical Engineering. Lecture 4. C++ Data Types

Procedures, Parameters, Values and Variables. Steven R. Bagley

Computer Science II Lecture 1 Introduction and Background

CS 376b Computer Vision

COEN244: Class & function templates

Implementing an ADT with a Class

University of Technology. Laser & Optoelectronics Engineering Department. C++ Lab.

Operations. Making Things Happen

Ch. 17: Linked Lists. Introduction to Linked Lists

Transcription:

CS 31 Discussion ABDULLAH-AL-ZUBAER IMRAN WEEK 6: C-STRINGS, STRUCT, CLASS AND PROJECT4

Recap Functions Parameter passing: pass by reference Strings Letter to digit and vice-versa Library functions: string and ctype Array Declaration and initialization 1D and 2D arrays Arrays in a function

Discussion Objectives Review and practice things covered during lectures C Strings Struct Class Coding examples Project4 Required Functions Important Points Programming Challenge Time for you to ask questions!

Functions for C Strings #include <cstring> Operation What it does strlen(s) Returns the length of s, not counting \0. strcpy(t,s) strncpy(t,s,n) strcat(t,s) Copies the string s to t. (Notes: t=s won t do the job. Also, strcpy doesn t do the size check for you. You must make sure there s enough space in t yourself.) Copies the first n characters of s to t. (Note: No size check.) Appends s to the end of t. (Notes: No size check. t += s won t do the job.) strcmp(t,s) Compares t and s. Returns 0 if they are equal, something greater than 0 if t > s, and something less than 0 if t < s. (Note: t == s or t < s won t do the job.)

C Strings Two alternatives to traverse a C string. char s[10] = "HOWAREYOU"; for (int k = 0; t[k]!= '\0'; k++) cout << t[k] << end; #include<cstring>... char s[10] = "HOWAREYOU"; for (int k = 0; k < strlen(s); k++) cout << t[k] << end;

C Strings An array of C strings char s[10][20]; In s, we can store up to C strings, and each C string can be at most characters long. 10 19 s[3]; // refer to the string in position 3 s[3][5]; // refer to the letter in position 5 of the string in position 3

C Strings Convert a C string into a C++ string, and vice versa. char cs[10] = "hello"; string cpps; cpps = cs; string cpps = cs; string cpps(cs); C string to string char cs[10]; string cpps = "hello"; strcpy(cs, cpps.c_str()); string to c string You cannot: char cs[10] = cpps.c_str(); char cs[10]; cs = cpps.c_str();

C Strings Question: Will this code compile? What s the output? #include <iostream> #include <cstring> using namespace std; int main () { char a[]= "sup"; char A[] = "SUP"; cout << strcmp(a, A) << endl; cout << strcmp(a, a) << endl; Output: 32-32 ASCII table

Struct Structs are objects in C++ that represent "data structures", or variables, functions, etc. that are organized under a categorizing identifier. Data Members are variable components of a given struct; they can be of any variable type. struct <structname> { <member1_type> <member1_name>; <member2_type> <member2_name>; //...etc. ; // Remember the semicolon! struct Student { string name; int id; string email; char grade; ;

Struct struct Student { string name; int id; string email; char grade; ; The element/member selection operation (.) const int NUM_STUDENTS = 32; Student st Student students[num_students]; st.name = "Joe Bruin"; // st s name is set to Joe Bruin students[10].id = 123456789; // the 10-th Student in students array is assigned an ID cout << st.grade << endl; // print the grade of st cout << students[0].email << endl; // print the 0-th Stduent s email address

Member Functions (Method) #include <iostream> #include <string> using namespace std; struct Student { string name; int id; string email; char grade; Student() { name = "Jane Doe"; id = 0; email = "aa@a.com"; grade = 'A'; void printname() { cout << name << endl; ; #include <iostream> #include <string> using namespace std; struct Student { string name; int id; string email; char grade; Student() { name = "Jane Doe"; id = 0; email = "aa@a.com"; grade = 'A'; ; void Student::printName() { cout << name << endl; int main() { Student p; p.printname();

What s the output? #include <iostream> #include <cstring> #include <cctype> #include <string> using namespace std; struct trickz { int i[5]; trickz () { for (int j = 0; j < 5; j++) { // Assume ASCII encoding i[j] = j; ; void trickzinc (trickz*& t, int count) { for (int j = 0; j < count; j++) { t->i[j]++; ; int main () { trickz t; trickz* ptr = &t; t.trickzinc(ptr, 5); for (int j = 0; j < 5; j++) { cout << t.i[j] << endl; // True or false? cout << (ptr == &t) << endl;

Class A class is a construct used to group related fields (variables) and methods (functions). class Cat { int m_age; void meow(); ; Cat meowth; One can access these members by using a dot. meowth.m_age meowth.meow()

Class member functions Two ways to define the member functions class Cat { int m_age; void meow() { cout << "Meow~" << endl; ; 1. Inside the class definition Scope resolution operator void Cat::meow() { cout << "Meow~" << endl; 2. outside of the class definition

Class access specifiers The output of this code? class Cat { int m_age; void meow() { cout << "Meow~" << endl; ; int main(){ Cat meowth; meowth.m_age = 3; meowth.meow(); The class members have the private access specifier by default and cannot be accessed by an outside class or function. class Cat { public: int m_age; void meow() { cout << "Meow~" << endl; ;

Class access specifiers meowth.m_age = -3; Public members Private members int main(){ Cat meowth; meowth.m_age = 3; meowth.meow(); class Cat { public: void meow() { cout << "Meow~" << endl; ; private: int m_age; ;

Class Accessors class Cat { public: int age(); void meow(); private: int m_age; ; int Cat::age() { return m_age; Modifiers (mutators) class Cat { public: int age(); void setage(int newage); void meow(); private: int m_age; ; void Cat::setAge(int newage) { m_age = newage;

Class vs. Structs Technically, the only difference between a class and a struct is the default access specifier. By convention, we use structs to represent simple collections of data and classes to represent complex objects. This comes from C, which has only structs and no member functions.

Project4: Six Functions bool hasduplicates( const string array[ ], int size ) Returns true when an element in the array is found more than once; false otherwise or if there are no elements because size is less than or equal to zero. int countalldigits( const string array[ ], int size ) Returns the total number of digit characters (that is, the characters '0' - '9') found in all the elements of the passed array argument. Return -1 if there are no elements because size is less than or equal to zero. bool isindecreasingorder( const string array[ ], int size ) If every value in the array is smaller than the one that precedes it, return true; otherwise return false or if size is less than zero return false. int shiftright( string array[ ], int size, int amount, string placeholder ) Adjust the items found in the array, shifting each value to the right by amount parameter, filling the resulting first amount elements of the array with the placeholder parameter and returning the number of times the placeholder value was used after all the shifting has been performed or -1 if the size or amount is less than zero. int find( const string array[ ], int size, string match ) Return the smallest index of the element whose value equals the match parameter in the array. Return -1 if the item was not found or there are no elements because size is less than zero. int replaceallcharacters( string array[ ], int size, char findletter, char replaceletter ) This function should replace every occurrence of the findletter value with the replaceletter value found in all the array elements of the passed array argument. Return the number of letters that were replaced or -1 if the array has no elements because size is less than or equal to zero.

Important Notes Time due: 9:00 PM Tuesday, February 19th You cannot use any function templates from the algorithms portion of the Standard C++ library. Your implementations must not use any global variables whose values may be changed during execution. Your program must build successfully under both Visual C++ and either clang++ or g++. You should write useful comments for any non-obvious code. A zip containing two files: array.cpp and report.doc/report.txt.

Thanks! Questions? Today s discussion slides can be found at http://web.cs.ucla.edu/~aimran/winter19_cs31_w6.pdf Some of the materials presented have been taken from earlier TA discussions