Test 1

Post on 23-Feb-2016

37 views 0 download

Tags:

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

transcript

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

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

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

Type conversion– static_cast<newdatatype>(expression)

string

• <string> header file• Declaration• Assignment statement

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

aString[5];aString[4]

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

Standard I/O stream

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

• cout– <iomanip>

File I/O stream

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

Control structures

Control structures—Selection

Control structures

switch Structures

The while Loop

The do…while Loop

The for Loop

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

Function – Predefined functions

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

• Predefined functions are organized into separate libraries

User-Defined Functions

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

the function

void Functions

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

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

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

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

Scope of an Identifier

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

• Global identifier – identifiers declared outside of every function definition

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

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

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.

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

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

isVowel

rotate

pigLatinString

main

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!

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!