1 / 36

Test 1

Test 1. Gabriel Mayo Mickey Lee Lavuen Sears. C++ Review (Chapter 1 - 7). Data Types. Data Type : set of values together with a set of operations is called a data type C++ data can be classified into three categories: Simple data type Structured data type Pointers.

lynch
Télécharger la présentation

Test 1

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Test 1 • Gabriel Mayo • Mickey Lee • Lavuen Sears

  2. C++ Review(Chapter 1 - 7)

  3. Data Types • Data Type: set of values together with a set of operations is called a data type • C++ data can be classified into three categories: • Simple data type • Structured data type • Pointers

  4. Simple Data Types Three categories of simple data • Integral: integers (numbers without a decimal) • Question: how many integral data type in C++ • Char;short;int;long;bool;unsigned char; unsigned short;unsigned int; unssigned long • Floating-point: decimal numbers • Question: how many floating-point data type in C++ • Float (32) with 6-7 decimal place;double(64) (15);long double(128) • Enumeration type: user-defined data type

  5. Build-in operators • C++ Operators + addition -  subtraction * multiplication / division % remainder (mod operator) +, -, *, and / can be used with integral and floating-point data types •Unary operator - has only one operand •Binary Operator - has two operands

  6. Type conversion • static_cast<newdatatype>(expression)

  7. string • <string> header file • Declaration • Assignment statement • string aString = ‘’; • string aString = “”; • string aString = NULL; • aString = “abcde”; aString[0]?aString[5];aString[4]

  8. Input/Output Streams •I/O: sequence of bytes (stream of bytes) from source to destination •Bytes are usually characters, unless program requires other types of information •Stream: sequence of characters from source to destination •Input Stream: sequence of characters from an input device to the computer •Output Stream: sequence of characters from the computer to an output device

  9. Standard I/O stream • cin • get() • getline() • >> • cout • <iomanip>

  10. File I/O stream • <fstream> • Ifstream • Ofstream open() close() >> <<

  11. Control structures

  12. Control structures—Selection

  13. Control structures

  14. switch Structures

  15. The while Loop

  16. The do…while Loop

  17. The for Loop for (initial statement; loop condition; update statement) statement

  18. Function – Predefined functions • Predefined functions • sqrt(x) • pow(x,y) • floor(x) • Predefined functions are organized into separate libraries

  19. User-Defined Functions • Value-Returning Functions • functionType: type of the value returned by the function

  20. void Functions

  21. Formal Parameters • Value Parameters --- The value of the corresponding actual parameter is copied into it • Reference Parameter --- a formal parameter that receives the location (memory address) of the corresponding actual parameter

  22. Reference Variables as Parameters • Returning more than one value • Changing the actual parameter • When passing the address would save memory space and time • Stream variables (for example, ifstream and ofstream) should be passed by reference to a function

  23. Parameter passing by value & reference // function to compute an expression using int value parameters #include<iostream> using namespace std; int abc(int a, int b, int c) { return a + b * c; } int main() { int x=2, y=3, z=4; cout << abc(x,y,z) << endl; return 0; } a,b,c: Value parameter

  24. Parameter passing by value & reference // function to compute an expression using int value parameters #include<iostream> using namespace std; int abc(int& a, int b, int c) { return a + b * c; } int main() { int x=2, y=3, z=4; cout << abc(x,y,z) << endl; return 0; } a, reference parameter b,c: Value parameter

  25. Scope of an Identifier • Local identifier - identifiers declared within a function (or block) • Global identifier – identifiers declared outside of every function definition

  26. Static and Automatic Variables • Automatic variable - memory is allocated at block entry and deallocated at block exit • Static variable - memory remains allocated as long as the program executes

  27. Function Overloading • In a C++ program, several functions can have the same name. • This is called functionoverloading or overloading a functionname. • A different number of formal parameters, or • If the number of formal parameters is the same, then the data type of the formal parameters

  28. Example • Write a program that prompts user to input a string and then outputs the string in the pig latin form. The rules for converting a string into pig latin form are as following: • If the string begins with a vowel (y is treated as a vowel). Add “-way” at the end of the string. E.g eyeeye-way • If the string does not begin with a vowel, first add “-” at the end of the string. Then rotate the string one character at a time until the first character becomes a vowel, then add “ay” at the end. E.g. thatat-thay, • If the string contains no vowels, add “-way” at the end. bcdbcd-ay. • The character is non case sensitive, that means, vowels are a.e.i. o.u.y.A.E.I.O.U.Y.

  29. Problem Analysis • If str denotes a string • Check the first character, str[0], of str • If str[0] is a vowel, add "-way" at the end of str • If the first character of str, str[0], is not a vowel • First add "-" at the end of the string • Remove the first character of str from str and put it at end of str • Now the second character of str becomes the first character of str • This process is repeated until either • The first character of str is a vowel • All characters of str are processed, in which case str does not contain any vowels

  30. Algorithm Design • The program contains the following functions: • isVowel - to determine whether a character is a vowel • rotate - to move first character of str to the end of str • pigLatinString - to find the pig Latin form of str • Steps in the Algorithm: • Get str • Use the function pigLatinString to find the pig Latin form of str • Output the pig Latin form of str

  31. isVowel

  32. rotate

  33. pigLatinString

  34. main

  35. Homework 1 • Posted in course website • Due date: Next Tuesday! • TA: • Yan Peng • TEC 348 • Hours will be told by email • NO LATE ASSIGNMENT WILL BE ACCEPTED!

  36. HW 1 In HW1 folder, there is a sample code for reading contents of a directory. Input folder contains all input matrix. Notes: • Please zip your assignment if it contains more than one file, name it as yourstudentid_assignment1.zip, e.g. yourid is 123456, your first programming assignment will be named as 123456_assignment1.zip. • Submission should contain readme file to show how to compile your program in ORCA. • Submit your source code to csc307spring2010@gmail.com on time! • No late submission will be acceptable!

More Related