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

Test 1

  • Upload
    lynch

  • View
    37

  • Download
    0

Embed Size (px)

DESCRIPTION

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. - PowerPoint PPT Presentation

Citation preview

Page 1: Test 1

Test 1

• Gabriel Mayo• Mickey Lee• Lavuen Sears

Page 2: Test 1

C++ Review(Chapter 1 - 7)

Page 3: Test 1

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

Page 4: Test 1

Simple Data TypesThree 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

Page 5: Test 1

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

Page 6: Test 1

Type conversion– static_cast<newdatatype>(expression)

Page 7: Test 1

string

• <string> header file• Declaration• Assignment statement

• string aString = ‘’;• string aString = “”;• string aString = NULL;• aString = “abcde”; aString[0]?

aString[5];aString[4]

Page 8: Test 1

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

Page 9: Test 1

Standard I/O stream

• cin– get()– getline()– >>

• cout– <iomanip>

Page 10: Test 1

File I/O stream

• <fstream>– Ifstream– Ofstreamopen()close()>><<

Page 11: Test 1

Control structures

Page 12: Test 1

Control structures—Selection

Page 13: Test 1

Control structures

Page 14: Test 1

switch Structures

Page 15: Test 1

The while Loop

Page 16: Test 1

The do…while Loop

Page 17: Test 1

The for Loop

for (initial statement; loop condition; update statement) statement

Page 18: Test 1

Function – Predefined functions

• Predefined functions– sqrt(x) – pow(x,y) – floor(x)

• Predefined functions are organized into separate libraries

Page 19: Test 1

User-Defined Functions

• Value-Returning Functions• functionType: type of the value returned by

the function

Page 20: Test 1

void Functions

Page 21: Test 1

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

Page 22: Test 1

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

Page 23: Test 1

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

Page 24: Test 1

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 parameterb,c: Value parameter

Page 25: Test 1

Scope of an Identifier

• Local identifier - identifiers declared within a function (or block)

• Global identifier – identifiers declared outside of every function definition

Page 26: Test 1

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

Page 27: Test 1

Function Overloading• In a C++ program, several functions can have

the same name. • This is called function overloading or

overloading a function name. • A different number of formal parameters, or • If the number of formal parameters is the

same, then the data type of the formal parameters

Page 28: Test 1

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 eyeeye-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. thatat-thay,

– If the string contains no vowels, add “-way” at the end. bcdbcd-ay.– The character is non case sensitive, that means, vowels are a.e.i.

o.u.y.A.E.I.O.U.Y.

Page 29: Test 1

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

Page 30: Test 1

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

Page 31: Test 1

isVowel

Page 32: Test 1

rotate

Page 33: Test 1

pigLatinString

Page 34: Test 1

main

Page 35: Test 1

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!

Page 36: Test 1

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 [email protected] on time!•No late submission will be acceptable!