Lesson N.1 of July 10, 2014¶
Some simple C/C++ programs
File example1.cc
:open:
1// I am a single line comment line
2
3/*
4 I am a multiline comment
5 bla bla
6 bla bla
7*/
8
9int // int is the name of the type integer and
10 // is the return type of the function main
11main( /* no argument for the moment */ ) { // { open the body block
12 return 0 ; // return 0 to the OS
13} // close the body block
File example2.cc
:open:
1// I am a single line comment line
2
3/*
4 I am a multiline comment
5 bla bla
6 bla bla
7*/
8
9#include <stdio.h>
10
11int
12main( /* no argument for the moment */ ) {
13 int a, b, c ; // declare and instance a,b,c as integer variable
14 printf("I am a string\n") ;
15
16 // now assign the variable a, b, c to some value
17 a = 1 ;
18 b = 234 ;
19 c = -123 ;
20
21 printf("Write a formatted integer a = %i\n",a) ;
22 printf("Write a formatted integer a = %10i, b=%5i\n",a,b) ;
23 printf("Write a formatted integer a = %-10i, b=%5i, c=%6i\n",a,b,c) ;
24
25 float aa = 1.234 ; // declare, instance and initialize aa as floating point variable
26 printf("Write aa formatted aa = %f\n",aa) ;
27 printf("Write aa formatted aa = %10f\n",aa) ;
28 printf("Write aa formatted aa = %10.5f\n",aa) ;
29 printf("Write aa formatted aa = %e\n",aa) ;
30 printf("Write aa formatted aa = %10g\n",aa) ;
31
32 return 0 ; // return 0 to the OS
33}
File example3.cc
:open:
1// I am a single line comment line
2
3/*
4 I am a multiline comment
5 bla bla
6 bla bla
7*/
8
9#include <iostream>
10
11using namespace std ; // for the moment magic ``keyword''
12
13/*
14 cout is a object for the output, means character out
15
16*/
17
18int
19main( /* no argument for the moment */ ) {
20 double a, b, c ; // declare and instance a,b,c as double precision floating point variable
21 cout << "I am a string\n"
22 << "I am another string\n" ;
23
24 cout << "I am a string\n" ;
25 cout << "I am another string\n" ;
26
27 // now assign the variable a, b, c to some value
28 a = 1 ;
29 b = 234 ;
30 c = -123 ;
31
32 cout << "a = " << a << "\n" ;
33 cout << "a = " << a << " b = " << b << "\n" ;
34 cout << "a = " << a << " b = " << b << " c = " << c << "\n" ;
35
36 float aa = 1.234 ; // declare, instance and initialize aa as floating point variable
37 cout << "aa = " << aa << "\n" ;
38
39 return 0 ; // return 0 to the OS
40}
File example4.cc
:open:
1/*
2 A simple program that solve
3 a*x^2 + b*x + c = 0
4*/
5
6#include <iostream>
7#include <cmath>
8using namespace std ; // for the moment magic ``keyword''
9
10int
11main( /* no argument for the moment */ ) {
12 double a, b, c ; // declare and instance a,b,c as double variable
13 cout << "Enter a = " ;
14 cin >> a ;
15
16 cout << "Enter b = " ;
17 cin >> b ;
18
19 cout << "Enter c = " ;
20 cin >> c ;
21
22 cout << "You entered " << a << "*x^2 + " << b << "*x + " << c << '\n' ;
23
24 // check if is a true quadratic
25 if ( a == 0 ) {
26 // the quadratic is "linear"
27 if ( b == 0 ) {
28 cerr << "the quadratic is degeneted\n" ; // use error I/O object
29 } else {
30 cout << "Linear case, one solution x = " << -c/b << '\n' ;
31 }
32 } else {
33 // regular case
34 // evaluate the discriminant of the quadratic
35 double delta = b*b - 4*a*c ;
36 if ( delta > 0 ) {
37 // case 2 distinct real roots
38 delta = sqrt(delta) ;
39 double x1 = (-b+delta)/(2*a) ;
40 double x2 = (-b-delta)/(2*a) ;
41 cout << "Two real roots x1 = " << x1 << " x2 = " << x2 << '\n' ;
42 } else if ( delta < 0 ) {
43 // case 2 complex conjugate roots
44 // case 2 distinct real roots
45 delta = sqrt(-delta) ;
46 double re = -b/(2*a) ;
47 double im = delta/(2*a) ;
48 cout << "Two complex conjugate roots:\n"
49 << " x1 = (" << re << ", " << im << ")\n"
50 << " x1 = (" << re << ", " << -im << ")\n" ;
51 } else {
52 // double root
53 cout << "Doble root x1 = x2 = " << -b/(2*a) << '\n' ;
54 }
55 }
56
57 return 0 ; // return 0 to the OS
58}
File example5.cc
:open:
1/*
2 A simple program that
3 implement newton iterative scheme
4*/
5
6#include <iostream>
7#include <cmath>
8using namespace std ; // for the moment magic ``keyword''
9
10double
11f( double x) {
12 return x*x-2 ;
13}
14
15double
16df( double x) {
17 return 2*x ;
18}
19
20// user function implementing newton scheme
21int
22newton( double & x, // initial guess and returned value
23 double tolerance, // tolerance for stopping criteria
24 int max_iter ) { // maximum iterations allowed
25
26 // cout.precision(10) ;
27 for ( int i=0 ; i < max_iter ; ++i ) {
28 double h = f(x)/df(x) ;
29 x -= h ; // x = x - h
30 // cout << "x[" << i << "] = " << x << " h = " << h << '\n' ;
31 if ( abs(h) < tolerance ) return 0 ; // found solution
32 }
33 return 1 ; // failed to converge
34}
35
36
37int
38main( /* no argument for the moment */ ) {
39 double x, tol = 1e-8 ;
40 cout << "Initial guess x0 = " ;
41 cin >> x ;
42 int ok = newton( x, tol, 100 ) ;
43 if ( ok == 0 ) {
44 cout << "Newton converged with value = " << x << "\n" ;
45 } else {
46 cout << "Newton do not converge\n" ;
47 }
48 return 0 ; // return 0 to the OS
49}
All files in a zip file