Advanced Introduction to C++, Scientific Computing and Machine Learning




Claudius Gros, SS 2024

Institut für theoretische Physik
Goethe-University Frankfurt a.M.

C++ : A First Glance


programming languages

compiled


offline integrated development environments (IDE)

online IDEs -- programming c++ in the browser

scripting


computing environments

a first program

a trivial C++ source example

#include <iostream>
using namespace std;

int main() 
  {                        
  cout << "Hello World!" << endl;
  return 0;
  }

commenting the code is the first duty of every programmer

Copy Copy to clipboad
Downlaod Download
// helloWorld.cpp
// a program that prints the immortal saying "hello world"

#include <iostream>          // include utilities for input and output
using namespace std;         // we will be using functions from the 
                             // standard C++ libary

int main()                   // entry point
  {                          // starting a scope
  cout << "Hello World!" << endl;
  return 0;
  }                         // ending a scope

in- and output to console

Copy Copy to clipboad
Downlaod Download
#include <iostream>                       // input-output stream
using namespace std;                      // std == standard

int main ()
{
  int value;                              // define a integer variable
  cout << "enter an integer value: ";     // message to console
  cin  >> value;                          // read  from console
  cout << "      value, 2*(value): " << value;
  cout << "  " << 2*value << "\n";
//
  if (value==7)                           // an if statement
     cout << "   ---seven is magical---\n";
  return 1;
}

example: functions

Copy Copy to clipboad
Downlaod Download
#include <iostream>   /* another way for comments */
#include <stdlib.h>   /* srand, rand */
#include <time.h>     /* for the time */
using namespace std;


// ================== //
// ================== //

int  f_1(int a, int b) {return a - b;} // from function arguments: call by value
void f_2(int &x);                      // forward declaration with call be reference

// ================== //
// ================== //

int main() {
//
  srand( (unsigned)time( NULL ) );  // initializing the random number
                                    // generator with a random seed: 
                                    // the current time
//
  int a = 1;
  int b = 2;
  int z = 333;
//
  cout << "a             :: " << a << endl;
  cout << "b             :: " << b << endl;
  cout << "z             :: " << b << endl << endl;
//
  int c = f_1(a,b);             
  f_2(z);                           // void functions return nothing
//
  cout << "c = f_1(a,b)  :: " << c << endl;
  cout << "z   f_2(z)()  :: " << z << endl;
//
}

// =================================== //
// === definition of function body === //
// =================================== //

void f_2(int &x)                    // '&' for reference (memory address)
 { 
 x = rand() % 10;                   // '%' modulo
 } 

example: scopes

Copy Copy to clipboad
Downlaod Download
#include <iostream>   /* another way for comments */
using namespace std;

int a = 22;                   // global variable, scope: global

// ================== //
// ================== //

int  f_1(int a) {return a;}   // local beats global
int  f_2(int x) {return a;}   // local version does not exist

// ================== //
// ================== //

int main() {
  int x = 777;
  cout << "a, global   :: " << a << endl;
  cout << "x, local    :: " << x << endl << endl;
  cout << "f_1(3)      :: " << f_1(3) << endl;
  cout << "f_2(x)      :: " << f_2(x) << endl << endl;
//
  double z = 6.66;                             // scope: main
  if (1==1)
    {
    double z = 5.55;                           // scope: if 
    cout << "z, local    :: " << z << endl;
    }
//
  return 1;
// end of mail
}

output formatting

Copy Copy to clipboad
Downlaod Download
#include <iostream>
#include <stdio.h>       // for printf
#include <cstdlib>       // for atof
using namespace std;

int main(int argLength, char* argValues[])   // parsing command line arguments
{                          
  printf("number of input arguments: %3d\n",argLength);
  for(int i = 0; i < argLength; i++)
  cout << "argValues[" << i << "] = " << argValues[i] << endl;

//
  double inputDouble  = atof(argValues[1]);    // casting string to double
  int    inputInteger = (int)inputDouble;      // casting double to integer
//
  printf("\n");
  printf("floating-point %10.3f  number\n",inputDouble);
  printf("   exponential %10.3e  number\n",inputDouble);
  printf("   fixed-width %10s  string\n"  ,argValues[2]);
  printf("   fixed-width %10d  integer\n" ,inputInteger);
//          problems with strings
  string ss = "a C++ string can only be printed with printf()\n";
  string tt = "if turned previously into a C-string by .c_str()\n";
  printf("\n");
  printf("%s%s",ss.c_str(),tt.c_str());
//
  return 0;
}  // end of main()

user@pc:~$ ./a.out 3345.345 hello
number of input arguments:   3
argValues[0] = ./a.out
argValues[1] = 3345.345
argValues[2] = hello

floating-point   3345.345  number
   exponential  3.345e+03  number
   fixed-width      hello  string
   fixed-width       3345  integer

a C++ string can only be printed with printf()
if turned previously into a C-string by .c_str()