Академический Документы
Профессиональный Документы
Культура Документы
Applied Programming
Lesson 1
C++ Revision
Objectives
Conditional
Iteration
The If Control Structure
if (condition)
{
statement(s)
}
condition
false
true
statement
if (condition)
{
statement(s)
}
else
{
statement(s)
}
condition
false
true
statement
statement
while (condition)
{
statements
}
condition
false
true
statement
do
{
statement
} while (condition);
statement
condition
false
true
condition
false
true
statements
post
int
char
float
Data types and Structure in C++.
For example;
long; // a long integer
short; // a short integer
double; // a large floating point
number
Data types and Structure in C++.
int main()
{
int student1_mark;
int student2_mark;
...
}
short a=1000;
int b = 0;
b = a;
short a=1000;
int b = 0;
b = (int) a;
For the most part in this class you should use static_cast
- the reason will be clearer later when we talk about
classes. It is safer then the other.
Type Casting
Here is a simple example;
#include <iostream>
using namespace std;
int main()
{
float a = 2000.0;
int b = static_cast<int>(a);
cout << b << endl;
return 0;
}
Arrays
array0 array[0]
array1 array[1]
array2 array[2]
array3 array[3]
array4 array[4]
array5 array[5]
As you can see the left hand diagram illustrates arrays and indices. It
right hand side demonstrates how these indices relate to index number
(elements).
Each element can house its own data of a particular type.
Each element is as if it were a single variable.
type array_name[size];
The type of the array could be any C++ basic type or user
defined type (struct, class, enum, typedef).
For example;
int marks[5];
The statement;
marks[0] = 0;
0 ? ? ? ?
mark[0] mark[1] marks[2] marks[3] marks[4]
0 0 0 0 0
mark[0] mark[1] marks[2] marks[3] marks[4]
Arrays
Outputting an element of an array is the same as
outputting a normal variable;
OR
1 2 3 4 5
If we did this;
int marks[5] = { 4, 5 };
4 5 0 0 0
mark[0] mark[1] marks[2] marks[3] marks[4]
Arrays
1 2 3 4 5
int main()
{
const int MAX=5;
int numbers[MAX] = {2, 4, 6, 8, 10};
squares(numbers, MAX);
for (int i = 0; i < MAX; i++)
{
cout << squares[i] << endl;
}
return 0;
}
int table[4][5];
So;
table[0][0] = 100;
Where as;
table[3][4] = 10000;
1 2 3 4 5
table[0][0] table[0][1] table[0][2] table[0][3] table[0][4]
1 2 3 4 5
table[1][0] table[1][1] table[1][2] table[1][3] table[1][4]
1 2 3 4 5
table[2][0] table[2][1] table[2][2] table[2][3] table[2][4]
1 2 3 4 5
table[3][0] table[3][1] table[3][2] table[3][3] table[3][4]
table[4][5] array
Above diagram illustrates this concept of multidimensional
tables.
Arrays
char name[100];
The numerator
The denominator
Structures
struct nameofstruct
{
member declarations;
...
};
Structures
struct fraction{
int numerator;
int denominator;
};
Structures
student mystudent;
struct student{
int studentno;
char name[256]; // should be a CONST
int age;
};
int main()
{
student mystudent;
cout << Enter name of student: ;
cin >> mystudent.name;
cout << Enter student no of student: ;
cin >> mystudent.studentno;
cout << Enter age of student: ;
cin >> mystudent.age;
return 0;
}
Structures
Effectively the structure is placed in memory. The
structure is composed of the components specified
in the structures definition.
Student Structure
Integer for student
Integer for age
Number
struct fraction{
int numerator;
int denominator;
};
int main()
{
fraction myfraction = { 0 , 0 };
cout << member are: << endl;
cout << numerator: << myfraction.numerator
<< endl;
cout << denominator: << myfraction.denominator
<< endl;
return 0;
}
Structures
Arrays of structures can also be made:
#include <iostream>
using namespace std;
struct student{
int studentno;
char name[256]; // should be a const
int age;
};
int main()
{
const int MAX=10;
student mystudent[MAX];
int i = 0;
cout << -1 to terminate input << endl;
while (i < MAX)
{
cout << Enter student number: ;
cin >> mystudent[i].studentno;
if (mystudent[i].studentno == -1)
break;
cout << Enter student name: ;
cin >> mystudent[i].name;
cout << Enter student age: ;
cin >> mystudent[i].age;
i++;
}
Structures
return 0;
}
Structures
void getdata(student[]);
void printdata(student[]);
#include <iostream>
using namespace std;
struct student{
int studentno;
char name[256];
int age;
};
void getdata(student[]);
void printdata(student[]);
int main()
{
student mystudent[MAX];
getdata(mystudent);
printdata(mystudent);
return 0;
}
Structures
struct fraction{
int numerator;
int denominator;
};
Y = LARGE;
Other types in C++.
radiation foobar;
foobar = 10;
Can you guess what this does? How would you be sure?
Functions
// statements
}
Functions
int counter()
{
int count = 0;
char a;
while (cin >> a)
{
count++;
}
return count;
}
int main()
{
float width, length, area;
cout << Enter width:;
cin >> width;
cout << Enter length:;
cin >> length;
area = rectarea(width, length);
cout << Area is: << area << endl;
return 0;
}
Functions
point getpoint()
{
point mypoint;
cout << Enter the x coordinate:;
cin >> mypoint.x;
cout << Enter the y coordinate:;
cin >> mypoint.y;
return mypoint;
}
Functions
Pass by value
The function can not change the value of a variable in the
parameter list.
Pass by reference
The function can change the value of a variable in a parameter
list.
Variations of Parameters
int main()
{
int val1, val2;
cin >> val1 >> val2;
swap(val1, val2);
cout << In main function << endl;
cout << val1 << << val2 << endl;
return 0;
}
void swap(int a, int b)
{
int temp = a;
a = b;
b = temp;
cout << In swap function << endl;
cout << a << << b << endl;
}
Variations of Parameters
When the program is compiled and executed we get
the following if we enter the values 1 and 2 as input.
val1 a
val2 b
function main makes When the function is called
two variables the two variables
are copied for the function
int main()
{
int val1, val2;
cin >> val1 >> val2;
swap(val1, val2);
cout << In main function << endl;
cout << val1 << << val2 << endl;
return 0;
}
void swap(int& a, int& b)
{
int temp = a;
a = b;
b = temp;
cout << In swap function << endl;
cout << a << << b << endl;
}
Why functions?
You will find functions can help reduce the amount of code
you write and maintain.
Inline functions in C++
double abs(double x)
{
if (x < 0)
return -x;
else
return x;
}
Inline functions in C++
y = abs(z);
y = abs(z);
Daniel 65.9
Tom 80.2
Harry 76.3
Sandra 92.0
I/O in C++
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
ifstream ins;
const int MAX = 128;
char name[MAX];
float mark;
ins.open(data.txt);
if (ins.good())
{
while (ins.good())
{
ins >> name >> mark;
cout << name << mark <<
endl;
}
ins.close();
}
return 0;
}