Note 1: Reviews the Subject from CSCI 504 and CSCI 515

 

Following are the subjects that you should already know form CSCI 504 and CSCI 515:

 

Data Type Table:

Data Type

Byte

Digits

Range Value

char

1

 

-128 ~ 127

unsigned char

1

 

0 ~ 255

short

2

5

-32,768 ~ 32,767

unsigned short

2

5

0 ~ 65,535

int

2 / 4

5

-32,768 ~ 32,767

unsigned int

2 / 4

5

0 ~ 65,535

long

4

10

-2,147,483,648 ~ 2,147,483,647

unsigned long

4

10

0 ~ 4294967295

float

4

≈ 7 / 8

3.4 x 10-38 ~ 3.4 x 1038

double

8

≈ 15

1.7 x 10-308 ~ 1.7 x 10308

Long double

10

≈ 19

3.4 x 10-4932 ~ 1.1 x 104932

 

 

Input Data and Output Data:

Input data:

cin >> variable;                               //Read input value for variable

cin.get(ch);                                      //Read input value (one character) for ch

cin.ignore(100, ‘\n’);                       //Skip characters in the input stream

cin.getline(inputStr, 100);               //Read and stores an entire input line

getline(cin, inputStr,100);               //Read and stores an entire input line

 

 

Output data:

cout << “Variable is “ << variable; //Print out “Variable is “ and value of variable

 

File Input and Output:

#include<fstream>                                       //Header file for file stream

Input to file:

ifstream    inData;                            //Declaring file stream for input to file

inData.open(“walk.dat”);                //Open file walk.dat in current directory

inData.close( );                                //Close the file

 

Output to file:

ofstream   outData;                          //Declaring file stream for output to file

outData.open(“a:\\out.dat”);            //Open file out.dat in A (drive a:\\) directory

outData.close( );                              //Close the file

 

Conditions Control Statements:

if statements, nested if statements, and switch statements

           

Looping Statements:

Post-test: do … while loop

Pre-test: while loop and for loop

           

Functions:

Function Prototype, fuction Call

 

The usage of arguments and parameters table

Item

Usage

Argument

Appears in the function call.  The corresponding parameter may be either a reference or a value parameter.

Value parameter

Appears in a function heading.  Receives a copy of the value of the corresponding argument.

Reference parameter

Appears in a function heading.  Receives the address of the corresponding argument.

 

C++ Operators:

Combined assignment operators:

+ =     Add and assign

- =      Subtract and assign

* =     Multiply and assign

/ =      Divide and assign

Increment and decrement operators:

++      Pre-increment            Ex: ++variable

++      Post-increment           Ex: variable++

- -      Pre-decrement             Ex: - -variable

- -      Post-decrement           Ex: variable - -

Bitwise operators (integer operands only):

<<      Left shift

>>      Right shift

&        Bitwise AND

|          Bitwise OR

^         Bitwise EXCLUSIVE OR

~         Complement (invert all bits)

More combined assignment operators (integer operands only):

%=       Modulus and assign

<<=      Shift left and assign

>>=      Shift right and assign

&=        Bitwise AND and assign

| =         Bitwise OR and assign

^ =        Bitwis EXCLUSIVE OR and assign

Other operators:

( )          Cast

sizeof     Size of operand in bytes                  Form: sizeof Expr or sizeof (Type)

? :          Conditional operator                        Form: Expr1 ? Expr2 : Expr3

 

Operator Precedence (highest to lowest)

Operator

Associativity

Note

( )

++  - -

Left to right

Right to left

Function call and function-styple cast

++ and - - as postfix operators

++  - -  !  Unary +  Unary –

(cast)  sizeof

Right to left

Right to left

++ and - - as prefix operators

*    /    %

Left to right

 

+    -

Left to right

 

<    <=    >    >=

Left to right

 

= =    !=

Left to right

 

&&

Left to right

 

| |

Left to right

 

? :

Right to left

 

=    +   =    - =    * =    / =

Right to left

 

 

Array and Pointer:

Declaration (compile time array):

//One-dimensional array

DataType   ArrayName[ConstIntExpression] ; 

//Two-dimensional array

DataType   ArrayName[ConstIntExpression][ ConstIntExpression] ;

                                                                                       

                                                  Rows                          Columns

//Three-dimensional array

DataType   ArrayName[ConstIntExpression][ConstIntExpression][ConstIntExpression] ;

                                                                                                                       

                                                   Pages                          Rows                        Columns

Declaration (dynamic array):

DataType    *PtrName;                                            //declaring the pointer for array

PtrName = new DataType [ConstIntExpression] ;  //allocated the memory for array

                                                            

                                            Memory size of array

//The pointer after dynamically allocated the memory could be used as any number //dimensional array

 

Accessing data in array table:

 Type

Dimension

Compile time

Dynamical

Note

One

ArrayName[r]

*(PtrName+r)

r is row subscript

Two

ArrayName[r][c]

*(ArrayName[r]+c)

*(*(ArrayName+r)+c)

*(PtrName+r*col_size+c)

 

r is row subscript

c is column subscript

Three

ArrayName[p][r][c]

*(ArrayName[p][r]+c)

*(*(ArrayName[p]+r)+c)

*(*(*(ArrayName+p)+r)+c)

*(PtrName+p*col_size*row_size+r*col_size+c)

p is page subscript

r is row subscript

c is column subscript

 

Aggregate Operations Table for Array and Structs:

Aggregate Operation

Arrays

Structs

I / O

No(except C strings)

No

Assignment

No

Yes

Arithmetic

No

No

Comparison

No

No

Argument passage

Yes, by reference

Yes, by value or by reference

Return as a function’s return value

No

Yes

 

 

Pass by value, Pass by reference, and Pass by address:

short    np, *p, **pp, ***ppp;   //np is non-pointer vairable

p = &np;                                    //p is pointer variable which has address of np

pp = &p;                                    //pp is pointer variable which has address of p

ppp = &pp;                                //ppp is pointer variable which has address of pp

 

 

 


                                                      

     ppp              pp               p               np

   

Argument and Parameter Table

 

Pass by Reference

Pass by Value #

Pass by Address #

Parameter

NP

P

PP

PPP

NP

P

PP

P

PP

Argument

np

p

pp

ppp

np

*p

**pp

***ppp

p

*pp

**ppp

pp

*ppp

&np

p

*pp

**ppp

&p

pp

*ppp

Variable which can be changed

np

np

p

np

p

pp

np

p

pp

ppp

none

np

np

p

np

np

p

#  Passing the value of the argument to the parameter; the value of argument may be a non-pointer or pointer (address).

 

Argument Type Table:

 

Pass by Value

Pass by Reference

Pass by Address

Constant

X

 

 

Variable

X

X

X

Pointer

X

X

X

Array Elements

X

X

X

Array Name

 

 

X

Expression

X

 

 

Function Call

X

 

 

Function Name

 

 

X

Structure:

Struct Declaration:

struct TypeName                                  //Define structure name and members

{

            DataType     MemberName;     //Define structure member

            DataType     MemberName;     //Define structure member

            StructType   MemberName;     //Define structure member

            .

            .

};

 

Structure Delcare and Access Data Method Table:

Structure Type Variable

Structure Delcare

Access Method

Single Structure Variable

student   record

record.Last

Structure Array

student   record [n]

record [i].Last

Structure Pointer

student  * record

record  – > Last

Declare Structure Pointer Type

typedef    struct  student  * student_ptr;

student_ptr   record;

(record + i) – > Last

*(record + i ).Last

Note: student is struct type, record is variable name, record[n] is student type array, Last is member of student structure

 

Data Search Algorithms:

Sequential Search:

            short   i = 0, find = 0;

            while (i < End)

            {

                        if (value = = Data[i])

                        {

                                    find = 1;

                                    i = End;

                        }

                        else

                                    i ++;

            }

 

Binary Search:

            while (Begin <= End)

            {

                        mid = (Begin + End) / 2;

                        if (value > Data[mid])

                                    Begin = mid + 1;

                        else if (value < Data[mid])

                                    End = mid – 1;

                        else

                                    break;

            }

 

Data Sorting Algorithms:

Selection Sort:

            i = 0;

            while (i < N-1)

            {

                        j = i + 1;

                        small = x[i];

                        loc = i;

                        while (j < N)

                        {

                                    if (x[j] < small)

                                    {

                                                small = x[j];

                                                loc = j;

                                    }

                                    j ++;

                        }

                        if (loc != i)

                        {

                                    x[loc] = x[i];

                                    x[i] = small;

                        }

                        i++;

            }

 

Insertion Sort:

//Element 0 contains smallest value of the data for ascending order, or contains biggest

//value of the data for descending order.

            i = 1;

            N_element = 1;

            while ( more value)

            {

                        N_value = x[i];

                        j = N_element – 1;

                        while (N_value < x[j])

                        {

                                    x[j+1] = x[j];

                                    j --;

                        }

                        x[j + 1] = N_value;

                        N_element ++;

                        i ++;

            }

 

 

Insertion Sort without moving data by using tag:

//Element 0 contains smallest value of the data for ascending order, or contains biggest //value of the data for descending order.  Element 0 of tag array initialized to 0.

            i = 1;

            N_element = 1;

            while  (more value)

            {

                        N_value = tag[i];

                        J = N_element – 1;

                        while (x[N_value] < x[tag[j]])

                        {

                                    tag[j + 1] = tag[j];

                                    j --;

                        }

                        tag[j + 1] = N_value;

                        N_element ++;

                        i ++;

            }