CS Computer Science IA: Procedural Programming

Slides:



Advertisements
Similar presentations
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 4: Control Structures I (Selection)
Advertisements

Wednesday, 12/11/02, Slide #1 CS 106 Intro to Comp. Sci. 1 Wednesday, 12/11/02  QUESTIONS??  Today: CLOSING CEREMONIES!  HW #5 – Back Monday (12/16)
Monday, 11/18/02, Slide #1 CS 106 Intro to CS 1 Monday, 11/18/02  QUESTIONS??  Today:  Hand back, discuss HW #4  Discussion of Lab 10  Exam #2 Friday.
This set of notes is adapted from that provided by “Computer Science – A Structured Programming Approach Using C++”, B.A. Forouzan & R.F. Gilberg, Thomson.
Admin Office hours 2:45-3:15 today due to department meeting if you change addresses during the semester, please unsubscribe the old one from the.
More on Input Output Input Stream : A sequence of characters from an input device (like the keyboard) to the computer (the program running). Output Stream.
M. Taimoor Khan #include void main() { //This is my first C++ Program /* This program will display a string message on.
Instructor - C. BoyleFall Semester
File I/O ifstreams and ofstreams Sections 11.1 &
CIS-165 C++ Programming I CIS-165 C++ Programming I Bergen Community College Prof. Faisal Aljamal.
1 COMS 261 Computer Science I Title: String Class Date: October 3, 2005 Lecture Number: 14.
CS Midterm Study Guide Fall General topics Definitions and rules Technical names of things Syntax of C++ constructs Meaning of C++ constructs.
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.
File I/O 1 ifstreams and ofstreams Sections 11.1 & 11.2.
An Introduction to Programming with C++ Sixth Edition Chapter 14 Sequential Access Files.
String Class. C-style and C++ string Classes C-style strings, called C-strings, consist of characters stored in an array ( we’ll look at them later) C++
1 Cannon_Chapter9 Strings and the string Class. 2 Overview  Standards for Strings  String Declarations and Assignment  I/O with string Variables 
String Class Mohamed Shehata 1020: Introduction to Programming.
Review for Final Exam. Contents 5 questions (20 points each) + 1 bonus question (20 points) – Basic concepts in Chapters 1-4 – Chapters 5-9 – Bonus: Chapter.
Chapter 11 Standard C++ Strings and File I/O Dept of Computer Engineering Khon Kaen University.
Fundamental Programming Fundamental Programming More Expressions and Data Types.
Chapter 7 Conditional Statements. 7.1 Conditional Expressions Conditions - compare the values of variables, constants and literals using one or more relational.
File I/O in C++ I. Using Input/Output Files A computer file is stored on a secondary storage device (e.g., disk); is permanent; can be used to provide.
1 A more complex example Write a program that sums a sequence of integers and displays the result. Assume that the first integer read specifies the number.
 Data Streams  Numeric Output  Numeric Input  Multiple Numeric Output  Multiple Numeric Input  Character Output  Character Input  String Output.
Chapter 7 Conditional Statements. 7.1 Conditional Expressions Condition – any expression that evaluates to true/false value Relational operators are BINARY.
CS 1428 Final Exam Review. Exam Format 200 Total Points – 60 Points Writing Programs – 45 Points Tracing Algorithms and determining results – 20 Points.
Strings.
Chapter 14: Sequential Access Files
Test 2 Review Outline.
ifstreams and ofstreams
Chapter Topics The Basics of a C++ Program Data Types
Chapter 8 (Part 3) Library Classes for Strings (P.405)
Variables A variable is a placeholder for a value. It is a named memory location where that value is stored. Use the name of a variable to access or update.
Basic Elements of C++.
Review of Strings which include file needs to be used for string manipulation? what using statement needs to be included fro string manipulation? how is.
CS 1430: Programming in C++.
Basic Elements of C++ Chapter 2.
Engineering 1020: Introduction to Programming Fall 2018
مبانی برنامه‌سازی با C++ جلسه دوم
C Stuff CS 2308.
Review for Midterm Exam
Review for Final Exam.
when need to keep permanently
CS 1428 Final Exam Review.
Review of Strings which include file needs to be used for string manipulation? what using statement needs to be included for string manipulation? how is.
Chapter 7 Conditional Statements
Review for Midterm Exam
Review for Midterm Exam
Chapter 4: Control Structures I (Selection)
CS150 Introduction to Computer Science 1
CS 1428 Final Exam Review.
Engineering Problem Solving with C++, Etter
Review for Final Exam.
Review for Midterm Exam
Review for Midterm Exam
CS150 Introduction to Computer Science 1
Engineering Problem Solving with C++ An Object Based Approach
Engineering Problem Solving with C++ An Object Based Approach
CS150 Introduction to Computer Science 1
Fundamental Programming
COMS 261 Computer Science I
Reading from and Writing to Files
CS 144 Advanced C++ Programming January 29 Class Meeting
Predefined Functions Revisited
ifstreams and ofstreams
COMS 261 Computer Science I
COMS 261 Computer Science I
character manipulation
Reading from and Writing to Files
Presentation transcript:

CS 13011 Computer Science IA: Procedural Programming Final Exam 1

C++ Basics Variable I/O Operators Namespace Declarations Variable naming conventions Assignment of values to variables I/O Operators cin>> cout<< #include <iostream> using std::cout; using std::endl; Namespace Different programmers write their own source code in different namespaces using namespace ns1; using namespace ns2; Use namespaces to refer to variables (possibly with the same name) ns1::x ns2::x

Operations Data types Constant (vs. variable) Operators in C++ int, double, float, bool, char, string, … Constant (vs. variable) Literal constant Named constant Operators in C++ +, - (unary) +, -, *, /, % (binary) Conditional operator: ? = (ternary) Precedence among operators How to evaluate an expression with operators?

Operations (cont'd) Assignment operator Compound operators = Compound operators += -= *= /= %= Increment/decrement operators ++, --

Operations (cont'd) Comparison operators Logical operators ==, != >, <, >=, <= Logical operators &&, ||, !

Selection Statements Boolean expression if statement (syntax) Evaluation if statement (syntax) if … if … else … Nested if: if … else if … else … if … else switch Conversion between switch and nested if

Repetition Statements while do … while for nested loops: for … for while … while do do … while while

Functions Pre-defined functions Programmer-defined functions pow(), sqrt(), abs() Type changing function: static_cast<…>() srand(), rand() Range of rand()? rand()%100? time() Programmer-defined functions function name argument(s) return value function call function invocation nested function call

Functions (cont'd) Function prototype Function declaration int add1(int i); int add1(int); Function declaration int add1(int i) { ... } Local vs. global variables Global constants/variables Call-by-value vs. call-by-reference Value-returning function vs. void function return x; vs. return; (or simply without this statement)

Arrays Array declarations Initialization int x[index]; index must be integer and constant Initialization Initialization list int x[] = {1, 2, 3}; int x[3] = {1, 2, 3}; int x[10] = {1, 2, 3}; for loop for (int i=0; i<size; i++) Access to an element in the array x[i] – range of index I Assignment: x[i] = 1; Read values: int a = x[i]+1; Iterate over all elements sum, max, min, avg, search a key, etc.

Arrays (cont'd) Passing array into the function Arrays E.g., void func(int x[]); Passing by reference const array: do not modify contents in the array Arrays

Strings String declarations Assignment Functions string str; #include <string> Assignment str="hello"; cin>>str; getline(cin, str); Functions cin.peek(); cin.get(); cin.unget();

Strings (cont'd) Operators String functions +, += comparison operators (>, <, >=, <=, ==, !=) String functions size() - current string size (number of characters currently stored in string length()- same as size() max_size() - maximum number of characters in string allowed on this computer empty() - true if string is empty str[i] – element in the string Range of index i

Strings (cont'd) String functions find(substring) - returns the position of the first character of substring in the string rfind(substring) - same as find, search in reverse find_first_of(substring) - find first occurrence of any character of substring in the string find_last_of(substring) - find last occurrence of any character of substr in the string insert(start, substring)- inserts substring starting from position start insert(start, number, character) – inserts number of character starting from position start replace (start, number, substring)- replaces number of characters starting from start with substring the number of characters replaced need not be the same append(string2)- appends string2 to the end of the string erase(start, number)- removes number of characters starting from start

Strings (cont'd) Passing string to function By value By reference Return string as the output of the function

File Input/Output <fstream> using std::ifstream; and using std::ofstream; ifstream fin; fin.open("infilename.txt"); fin>>str; while (getline(fin, line)) { ... } fin.close(); ofstream fout; fout.open("outfilename.txt"); fout << str; fout.close(); fail(); exit(0); void myfunc(ifstream &myinfile); // passing by reference