Lesson of 14 February 2011¶
Some simple C/C++ programs
A first example
File example1.cc
1// to compile on unix/OSX use
2// g++ example1.cc
3// On windows with Visual Studio type
4// cl.exe example1.cc
5//
6
7/*
8 I am a multiline comment!
9*/
10
11// I am a single line comment
12
13// include header for C I/O
14// search stdio.h in standard C/C++ include directory e.g. /usr/include
15#include <stdio.h>
16
17// include header for C++ I/O
18// search iostream in standard C++ include directory e.g. /usr/include
19#include <iostream>
20#include <iomanip> // include objects for manipulators
21
22using namespace std ; // "include" all the items of namespace std
23
24typedef int myInteger ; // myInteger is an alias of int
25typedef float myReal ; // myReal is an alias of float
26
27int // this is the tye type of return value
28main() {
29 printf("ciao sono io!\r\f") ;
30 // \n = new line character (go to the next line)
31 // \r = carriage return character (go to the begin of line)
32 // \f = form feed
33 // (float) i = cast operator, convert integer type to a float
34 // ++i is equivalent of i = i+1
35
36 for ( myInteger i = 0 ; i < 100 ; i=i+1 )
37 printf( "i = %lx (hex) %ld (dec) %50f (float)\n", i, i, (float)i ) ;
38
39 // cout = character out
40 for ( myReal i = 0 ; i < 100 ; ++i )
41 cout << "i = (hex) " << hex << i << " (dec) " << dec << i << '\n' ;
42
43 return 0 ; // return is a statement for returning value to the caller
44}
A second example
File example2.cc
1// to compile on unix/OSX use
2// g++ example1.cc
3// On windows with Visual Studio type
4// cl.exe example1.cc
5//
6
7/*
8 A simple example: bubble sort
9*/
10
11// include header for C I/O
12// search stdio.h in standard C/C++ include directory e.g. /usr/include
13#include <stdio.h>
14
15// include C standard lib for heapsort
16#include <stdlib.h>
17
18
19// include header for C++ I/O
20// search iostream in standard C++ include directory e.g. /usr/include
21#include <iostream>
22#include <iomanip> // include objects for manipulators
23using namespace std ; // "include" all the items of namespace std
24
25typedef int myInteger ; // myInteger is an alias of int
26typedef float myReal ; // myReal is an alias of float
27
28void // nothing to return
29sort( myInteger n, // first argument is an integer (the lenght of the vector)
30 myReal v[] ) { // vector of "unknown length"
31
32 for ( myInteger i = 0 ; i < n-1 ; ++i )
33 for ( myInteger j = i+1 ; j < n ; ++j )
34 if ( v[j] < v[i] ) {
35 myReal tmp = v[i] ; v[i] = v[j] ; v[j] = tmp ;
36 }
37
38}
39
40typedef bool (*PFUN)(myReal,myReal) ;
41
42// Overloading permits to declare another sort routine with "different arguments"!
43void // nothing to return
44sort( myInteger n, // first argument is an integer (the lenght of the vector)
45 myReal v[], // vector of "unknown length"
46 bool (*less_than)(myReal,myReal) ) {
47 // you can use PFUN less_than instead of bool (*less_than)(myReal,myReal)
48
49 for ( myInteger i = 0 ; i < n-1 ; ++i )
50 for ( myInteger j = i+1 ; j < n ; ++j )
51 if ( less_than(v[j],v[i]) ) {
52 myReal tmp = v[i] ; v[i] = v[j] ; v[j] = tmp ;
53 }
54}
55
56bool
57comparator( myReal a, myReal b )
58{ return a > b ; }
59
60int
61C_comparator(const void * a, const void * b) {
62 myReal A = *((myReal*)a) ;
63 myReal B = *((myReal*)b) ;
64 if ( A < B ) return -1 ;
65 if ( A > B ) return +1 ;
66 return 0 ;
67}
68
69int // this is the tye type of return value
70main() {
71
72 // declare v ad a vector, the size is deduced by the initialization with {}
73 myReal v[] = { 4, 1, -0.3, 8.4, 2.3, 88, 1231, 0.1212, -1000 } ;
74 myInteger vsize = sizeof(v)/sizeof(v[0]) ;
75
76 cout << "vsize = " << vsize << '\n' ;
77 cout << "sizeof(v) = " << sizeof(v) << '\n' ;
78 cout << "sizeof(v[0]) = " << sizeof(v[0]) << '\n' ;
79 cout << "sizeof(myReal) = " << sizeof(myReal) << '\n' ;
80 cout << "sizeof(double) = " << sizeof(double) << '\n' ;
81 cout << "sizeof(long int) = " << sizeof(long int) << '\n' ;
82 cout << "sizeof(short int) = " << sizeof(short int) << '\n' ;
83
84 // call the sort routine
85 //sort( vsize, v ) ;
86
87 // call the sort routine
88 sort( vsize, v, comparator ) ;
89
90 // call the C standard routine for ordering
91 int ok = heapsort(v, vsize, sizeof(v[0]), C_comparator );
92
93 // cout = character out
94 for ( myInteger i = 0 ; i < vsize ; ++i )
95 cout << "v[ " << i << "] = " << v[i] << '\n' ;
96
97 return 0 ; // return is a statement for returning value to the caller
98}