Prac 0
// A program to display "Hello World."
#include <iostream>
using namespace std;
int main() {
cout << "Hello World. \n";
}
Prac 0-3
// A program to display "Hello World."
#include <iostream>
using namespace std;
int main() {
int x; // variable to store read integer number
cout << "Type an integer number: "; // ask a user to type an integer number
cin >> x; // read the typed number and store it in the variable x.
cout << "You typed : " << x << endl;
}
Prac 0-4
// A program to display "Hello World."
#include <iostream>
using namespace std;
int main() {
int x; // variable to store read integer number
cout << "Type an integer number: "; // ask a user to type an integer number
cin >> x; // read the typed number and store it in the variable x.
int absx = x; // store absolute value of x
if (x < 0) {
absx = -x;
}
cout << "The absolute value of the number you typed is : " << absx << endl;
}
Prac 1
// compute an average of elements from two arrays
#include <iostream>
int main() {
int narray1[]={16,22,38,46,51,69,70,88,95,102};
int narray2[]={13,29,33,45,57,62,75,84,91,105};
int size = sizeof narray1 / sizeof(int);
for (int i = 0; i < size; i++) {
std::cout << "The average of "
<< "narray1[" << i << "] and narray2[" << i << "]: "
<< (narray1[i] + narray2[i]) / 2 << std::endl;
}
return 0;
}
Prac 2
/* find max and min value of three arrays */
#include <iostream>
// using directive
using namespace std;
int main() {
double narray1[] = { 16.21, 22.50, 38.33, 46.00, 51.71,
69.09, 70.89, 88.05, 95.77, 102.01 };
double narray2[] = { 13.44, 29.78, 33.45, 45.03, 57.66,
62.07, 75.66, 84.00, 91.82, 106.00 };
double narray3[] = { 10.05, 29.44, 34.35, 43.56, 55.25,
69.44, 72.00, 89.80, 97.45, 105.02 };
int size = sizeof narray1 / sizeof(double), i;
cout << showpoint;
// find the max
for (i = 0; i < size; i++) {
cout << "The max value of "
<< narray1[i] << ',' << narray2[i] << ',' << narray3[i] << " : "
<< ((narray1[i] > narray2[i] ? narray1[i]:narray2[i]) >
(narray2[i] > narray3[i] ? narray2[i]:narray3[i]) ?
(narray1[i] > narray2[i] ? narray1[i]:narray2[i]):
(narray2[i] > narray3[i] ? narray2[i]:narray3[i]))
<< endl;
}
cout << noshowpoint;
// find the min
for (i = 0; i < size; i++) {
cout << "The min value of "
<< narray1[i] << ',' << narray2[i] << ',' << narray3[i] << " : "
<< ((narray1[i] < narray2[i] ? narray1[i]:narray2[i]) <
(narray2[i] < narray3[i] ? narray2[i]:narray3[i]) ?
(narray1[i] < narray2[i] ? narray1[i]:narray2[i]):
(narray2[i] < narray3[i] ? narray2[i]:narray3[i]))
<< endl;
}
return 0;
}
Prac 3
/* reverse pointers */
#include <iostream>
// declare a const visible only within this file
namespace {
const int size = 10;
}
int main() {
int narray[] = { 100, 200, 300, 400, 500, 600, 700, 800, 900, 1000 };
int *pnarray[size], i, j;
for (i = 0, j = size - 1; i < size; i++, j--)
pnarray[j] = &narray[i];
for (j = 0; j < size; j++)
std::cout << "pnarray[" << j << "] is : "
<< *pnarray[j] << std::endl;
return 0;
}
Practice 4
In C++, it is not guaranteed that the mix uses of C functions and C++ functions result in a consistent behaviour (although most cases, it seems to work ok..)
On the other hand, C fnctions like exit(.) are defined in C++ to behave in the same manner.