MCIS 6204 - Data Structure & Algorithms (Labs) - Day 2
MCIS 6204 - Data Structure & Algorithms (Labs) - Day 2
MCIS 2016
Overview
Increase
In
Returns
! ( 5 == 5)
! ( 6 <= 4 )
!true
!false
Evaluates to false
true
false
true
Conditional Operator ( ? )
The
Returns
7==5 ? 4 : 3
7==5+2 ? 4: 3
5>3 ? a:b
a >b ? a: b
6
Conditional Operator ( ? )
Comma operator ( , )
The
10
Cout Examples
The
Constant
11
Cout Examples-1
Print
12
Cout Examples-2
Line
First sentence.
Second sentence.
Third Sentence.
Additionally, to
Standard Input(cin)
The
Control Structures
16
17
18
19
20
21
while loop
Loops
condition
true
statement
false
format
22
#include <iostream.h>
void main(void)
{
int num = 1;
// Initialize counter
cout << "number
number Squared\n";
cout << "-------------------------\n";
while (num <= 10)
{
cout << num << "\t\t" << (num * num) << endl;
num++;
// Increment counter
}
}
23
program output -1
number
number Squared
------------------------1
1
2
4
3
9
4
16
5
25
6
36
7
49
8
64
9
81
10
100
24
do while loop
Its
Format
do while loop -1
// number echoer
#include <iostream>
using namespace std;
int main ()
{
unsigned long n;
do {
cout << "Enter number (0 to
end): ";
cin >> n;
cout << "You entered: " << n
<< "\n";
} while (n != 0);
return 0;
}26
for loop -1
// number echoer
#include <iostream>
using namespace std;
int main ( )
{
unsigned long n;
do {
cout << "Enter number (0 to
end): ";
cin >> n;
cout << "You entered: " << n
<< "\n";
} while (n != 0);
return 0;
}27
for loop
Specially
Format
29
initialization
condition
for ( n=0, i = 100;
n! = i ;
{
whatever statements here
}
Increase
n ++, i -- )
This
break statement
Using
int main ()
{
int n;
for (n=10; n>0; n--)
{
cout << n << ", ";
If (n==3)
{
cout << "countdown
aborted!";
break;
}
}
return 0;
}
31
10,9,8,7,6,5,4,3,countdown
aborted!
continue statement
Using
int main ()
{
int n;
for (n=10; n>0; n--)
{
cout << n << ", ";
If (n==5) continue;
cout << fire"
}
}
return 0;
}
32
10,9,8,7,6,5,4,3,2,1,fire
33
of switch
Switch Example
switch (x) {
case 1:
cout << "x is 1";
break;
case 2:
cout << "x is 2";
break;
default:
cout << "value of x
unknown";
}
35
If-else equivalent
if (x == 1) {
cout << "x is 1";
}
else if (x == 2) {
cout << "x is 2";
}
else {
cout << "value of x unknown";
}
Stream IO
C++
36
File Output
The
steps are:
1. Construct an ofstream object.
2. Connect it to a file(i.e., file open) and set the mode of file
operation.
3. Perform output operation via insertion >> operator or
write( ), put( ) functions.
4. Disconnect and free the ofstream object.
37
File Input
The
steps are:
1. Construct an ifstream object.
2. Connect it to a file(i.e., file open) and set the mode of file
operation.
3. Perform output operation via extraction << operator or
read( ), get( ), getline functions.
4. Disconnect and free the ifstream object.
38
ios::in
ios::out
ios::app
ios::trunc
39
#include <iostream>
#include <fstream> //To use C++s function for File I/O.
using namespace std;
int main ( )
{
ofstream SaveFile;
SaveFile.open (D:\\C++\\Day4\\Lab4.txt",
ios::out);
SaveFile << Save some text into file ;
SaveFile.close( );
return 0;
}
40
#include <iostream>
#include <fstream>
//To use C++s function for
File I/O
using namespace std;
int main ( )
{
ifstream InFile;
InFile.open ("D:\\C++\\Day4\\Lab4.txt",ios::in);
double aNumber;
while (!InFile.eof()) //
{
InFile >> aNumber;
cout << aNumber << " \n";
}
OpenFile.close(); cin >> aNumber;
return 0;
Southern Arkansas University
41
}
Exception Handling
An exception is a problem that arises during the
execution of a program. A C++ exception is a
responses to an exceptional circumstance that
arise while a program is running, such as an
attempt to divide by zero.
Exception provide a way to transfer control from
one part of a program to another. C++ exception
handling is built upon three keywords: try, catch,
and throw.
42
http://www.tutorialspoint.com/cplusplus/cpp_exceptions_handling.htm
Exception Handling
throw A program throws an exception when a
problem shows up.
catch A program catches an exception with an
exception handler at the place in a program where
you want handle problem.
try A try block identifies a block of code for
which particular exceptions will be activated
43
http://www.tutorialspoint.com/cplusplus/cpp_exceptions_handling.htm
http://www.codeproject.com/Articles/4681/Exception-handling-inManaged-C
http://www.codeproject.com/Articles/4681/Exception-handling-inManaged-C
Formatting Output
setw
-Output the value of an expression in specific columns.
-The value of the expression can be either string or a number.
-The output is right-justified.
cout << setw(5) << x << endl;
setpression
Formatting Output
Limit
47
Lab2 #B
#include <iostream>
#include <fstream>
//To use C++s function for File I/O
using namespace std;
int main ( )
{
try {
ifstream InputFile;
InputFile.open ("D:\\C++\\Day4\\Lab4.txt,ios::in);
if(!InputFile) throw 40;
double aNumber;
while (!OpenFile.eof()) //
{
OpenFile >> aNumber;
cout << aNumber << " \n";
}
InputFile.close();
}
catch(int e)
{
cout << File not found << e << endl;
}
return 0;
}
Southern Arkansas University
48
Labs
Lab2A
If Statement
Lab2B While Loops
49