Lecture 1 -- 1Computer Science I - Martin Hardwick Strings #include using namespace std; int main () { string word; cout << "Enter a word" << endl; cin.

Slides:



Advertisements
Similar presentations
Lecture Computer Science I - Martin Hardwick How do Calculators Computer Square Roots? rWhen you enter 29 into your calculator and push the square.
Advertisements

Lecture Computer Science I - Martin Hardwick Arithmetic Expressions With Integers rOperators:result is always an integer SymbolNameExampleValue (x.
Lecture Computer Science I - Martin Hardwick Other Types Of Data rVariables of type short use half a word (16 bits) to represent a value. l everything.
Lecture Computer Science I - Martin Hardwick The Programming Process rUse an editor to create a program file (source file). l contains the text of.
Lecture Computer Science I - Martin Hardwick Odds And Ends – Switch Statement rIt is often necessary in programs to set up a set of cases, where.
Lecture Computer Science I - Martin Hardwick Searching and sorting rReasons for not using the most efficient algorithm include: l The more efficient.
Lecture Computer Science I - Martin Hardwick Recursion rA recursive function must have at least two parts l A part that solves a simple case of the.
Lecture Computer Science I - Martin Hardwick Streams In C++ rA stream is a sequence that you either read from or write to. l example – cin is a stream.
Lecture Computer Science I - Martin Hardwick Making our programs more flexible rSo far we have largely programmed using l Arrays of integers l Arrays.
Lecture Computer Science I - Martin Hardwick Bank Simulation (1) #include using namespace std; // Create bank account data type struct acct { //
Chapter 2: Basic Elements of C++
1 Pointers and Strings Section 5.4, , Lecture 12.
Lecture 14 Today: Overloading: Revision on this Revision on increment operators the assignment operator the [] operator Book: p , 215,
For(int i = 1; i
Computer Science 1620 Math Library. Remember this program? suppose that I invest $25000 into a mutual fund that returns 8% per year. Write a program to.
PHYS707: Special Topics C++ Lectures Lecture 3. Summary of Today’s lecture: 1.Functions (call-by-referencing) 2.Arrays 3.Pointers 4.More Arrays! 5.More.
CS 240 Computer Programming 1
Factorial Preparatory Exercise #include using namespace std; double fact(double); int fact(int); int main(void) { const int n=20; ofstream my_file("results.txt");
Chapter 16 Exception Handling. What is Exception Handling? A method of handling errors that informs the user of the problem and prevents the program from.
Vectors, lists and queues
1 Lecture 16:User-Definded function I Introduction to Computer Science Spring 2006.
Chapter 6 Advanced Function Features Pass by Value Pass by Reference Const parameters Overloaded functions.
CSE202: Lecture 3The Ohio State University1 Assignment.
 2003 Prentice Hall, Inc. All rights reserved. 1 Functions and Recursion Outline some useful problems.
Introduction to Programming Lecture 15. In Today’s Lecture Pointers and Arrays Manipulations Pointers and Arrays Manipulations Pointers Expression Pointers.
Problem You require an algorithm that will receive the length of the hypotenuse and one of the sides of a right triangle, and calculate the length of the.
ELEC 206 Computer Applications for Electrical Engineers Dr. Ron Hayne
Derived data types Dealing with data –Where the information is stored –What value is kept there –What kind of information is stored Address operator Pointers.
Exercise 2.
Data Structures (Second Part) Lecture 2 : Pointers Bong-Soo Sohn Assistant Professor School of Computer Science and Engineering Chung-Ang University.
Riyadh Philanthropic Society For Science Prince Sultan College For Woman Dept. of Computer & Information Sciences CS 102 Computer Programming II (Lab:
第三次小考. #include using namespace std; int aaa(int *ib,int a1,int a2) { int u,v; int m=(a1+a2)/2; if(a1==a2)return ib[a1]; u=aaa(ib,a1,m); cout
Lecture Notes 3 Loops (Repetition)
Tinaliah, S. Kom.. * * * * * * * * * * * * * * * * * #include using namespace std; void main () { for (int i = 1; i
Triana Elizabeth, S.Kom. #include using namespace std; void main () { for (int i = 1; i
General Computer Science for Engineers CISC 106 Lecture 31 Dr. John Cavazos Computer and Information Sciences 05/06/2009.
General Computer Science for Engineers CISC 106 Lecture 34 Dr. John Cavazos Computer and Information Sciences 05/13/2009.
1 Lecture 14:User-Definded function I Introduction to Computer Science Spring 2006.
General Computer Science for Engineers CISC 106 Lecture 30 Dr. John Cavazos Computer and Information Sciences 05/04/2009.
1 CS150 Introduction to Computer Science 1 Exponents & Output page & Section 3.8.
General Computer Science for Engineers CISC 106 Lecture 26 Dr. John Cavazos Computer and Information Sciences 04/24/2009.
1 9/26/07CS150 Introduction to Computer Science 1 Exponents & Output page & Section 3.8.
computer
Functions Pass by Reference Alina Solovyova-Vincent Department of Computer Science & Engineering University of Nevada, Reno Fall 2005.
TEXT FILES. Text Files Files Data saved in external storage and can be referenced by a single name File types Document, image, audio, video, etc. Program.
Subject: Programming in C++ 1. LEARNING OUTCOME  At the end of this slide, student able to:  Know the brief history of C++.  Explore Microsoft Visual.
First steps Jordi Cortadella Department of Computer Science.
Tracing through E01, question 9 – step 1 // p02.cc P. Conrad, for CISC181 07S // Exam question for E01 #include using namespace std; void mysteryFunction(int.
1 Structure of Simple C++ Program Chapter 1 09/09/13.
1 17/4/1435 h Monday Lecture 3 The Parts of a C++ Program.
General Computer Science for Engineers CISC 106 Lecture 27 Dr. John Cavazos Computer and Information Sciences 04/27/2009.
1 09/10/04CS150 Introduction to Computer Science 1 What Actions Do We Have Part 2.
#define #include<iostream> using namespace std; #define GO
Introduction to C++ Programming Language
Intro to Programming Week # 6 Repetition Structure Lecture # 10
solve the following problem...
Dynamic Memory Allocation Reference Variables
Random Number Generation
Starting Out with C++: From Control Structures through Objects
Pointers & Functions.
Code::Block vs Visual C++
Arrays Syntax: type variableName[size];
Arithmetic Operations
CS148 Introduction to Programming II
Pointers & Functions.
COMS 261 Computer Science I
Programming Strings.
Reading from and Writing to Files Part 2
Introduction to Algorithms and Programming COMP151
CSE Module 1 A Programming Primer
Presentation transcript:

Lecture Computer Science I - Martin Hardwick Strings #include using namespace std; int main () { string word; cout << "Enter a word" << endl; cin >> word; if (word == "bang") { cout << "You entered bang!" << endl; } else { cout << "You did not enter bang!" << endl; } return 0; } rString is a type defined in string.h rString is an object rThere are many methods that work on string objects rOne of them is == rAnother is [ ] rFor more see the web l g.html g.html l Is a good page

Lecture Computer Science I - Martin Hardwick Accessing one character in a string #include using namespace std; int main () { string word; cout << "Enter a word" << endl; cin >> word; if (word[0] == 'b') { cout << "You entered bang!" << endl; } else { cout << "You did not enter bang!" << endl; } return 0; } rWord[0] gets the first character in the string rWord[1] would get the second character rEtc. rWhat if I want to get each character in a string? rI will need to know the number of characters rTry typing word. in Visual C++ you should see a long list of methods. rNow make a guess for a function that returns the character size

Lecture Computer Science I - Martin Hardwick Accessing every character in a string #include using namespace std; int main () { string word; cout << "Enter a word" << endl; cin >> word; int n = (int) word.length (); cout << "String length is " << n << endl; for (int i = 0; i < n; i++) { cout << word[i] << endl; } return 0; } rWord.length() returns the length of the string rThe length is returned as a strange data type (size_t) rWe use a coercion to get rid of the warning message rTo visit each character in turn I write a For Loop rThe loop executes with I = 0, then I = 1, then I = 2 etc. rTherefore I use the expression word[i] to get each character.

Lecture Computer Science I - Martin Hardwick The For Loop Statement rSyntax: for (count = 1; count <= 36; count = count + 1) {... } rMeaning: initial value of counter final value of counter counter increment count = 1count = count + 1 count <= 36 statement... done YES NO

Lecture Computer Science I - Martin Hardwick For Loop Variations rThe counter variable can be initialized to anything. rThe terminating condition can be any logical expression involving the counter variable. l comparison operators: rThe increment can be any value, positive or negative. rExamples: for (i = 0; i < 10; i = i + 2) for (count = 10; count >= 0; count = count - 1) for (j = 1; j != 6; j = j + 2) Alert: What happens in the last example? greater than =greater or equal = =equal! =not equal

Lecture Computer Science I - Martin Hardwick What Is Displayed By Each Program Segment? for (i = 0; i < 10; i = i + 2) { cout << i << endl; } for (j = 20; j >= 0; j = j - 3) { cout << j << endl; }

Lecture Computer Science I - Martin Hardwick Nested For Loop Examples for (j = 0; j <= 10; j = j + 5) { for (k = 10; k > 0; k = k - 5) { cout << j << << k << endl; } for (a = 1; a < 3; a = a + 1) { for (b = 5; b <= 6; b = b + 1) { for (c = 2; c > 0; c = c - 1) { cout << a << << b << << c << endl; } How many lines of output are generated? 6 What are the lines of output? How many lines of output are generated? 8 What are the lines of output?

Lecture Computer Science I - Martin Hardwick Visit the characters in a string in reverse order #include using namespace std; int main () { string word; cout << "Enter a word" << endl; cin >> word; int n = (int) word.length (); cout << "String length is " << n << endl; for (int i = ??; ??? ; i--) { cout << word[i] << endl; } return 0; } rStart the loop at the end rUse a decrement (i--) rStop the loop at the beginning rBe careful to get the start and end conditions right.

Lecture Computer Science I - Martin Hardwick Other string facts #include using namespace std; int main () { string word; int n; word = Two lines\nSecond"; n = (int) word.length (); cout << word << endl << length is " << n << endl; word = Tab in line\tSecond"; in = (int) word.length (); cout << word << length is " << n << endl; return 0; } r\n is one character not two r\n is carriage return (endl) r\t is tab r\\ is \ r\r is ???? (look it up but it gets complicated so you are not expected to know)

Lecture Computer Science I - Martin Hardwick Controlling Output Format #include.. M = 2.6; P = 123.4; cout << M << P << endl; cout << M << " " << P << endl; cout << endl; cout << setw(8) << M << setw(12) << P << endl; cout << setw(8) << P << setw(12) << M << endl; cout << setprecision(3) << setiosflags(ios::fixed); cout << setw(8) << M << setw(12) << P << endl; cout << setw(8) << P << setw(12) << M << endl; setw(number) -- specify the width (how many characters) to use to print the next number. by default, numbers are right justified in this width if the width is too small, the compiler uses the minimum it needs setiosflags(ios::fixed) -- float numbers will be printed with a fixed number of digits to the right of the decimal point. defaults to variable setprecision(number) -- specify the number of digits to the right of the decimal point when the ios::fixed flag is set.

Lecture Computer Science I - Martin Hardwick Controlling Output Format (continued)