Informatics3-2024/Practical9

A MathWikiből

Tartalomjegyzék

Multiple files

3(4) types of files:

  • main cpp file that contains your main function
  • header file (.h extension), contains headers (only the declarations of functions/classes)
  • cpp file (.cpp extension), contains the definitions of whatever is in its associated header file (same name)

The header files should look like this:

test.h

#pragma once
 
int example(int, float);

The pragma once command makes sure that the file is only included in the final solution once. You might encounter the ifndef define solution to this problem as well. That does basically the same thing.

The cpp file needs to include its .h file. To include files locally (files that are next to each other for example) we can use "" instead of <> in the include:

test.cpp

#include "test.h"
 
int example(int a, float b) {
  if(b > 0.5) {
    return a;
  }
  return -1 * a;
}

The main file only needs the main function and to include any library/include that it uses.

main.cpp:

#include<iostream>
#include "test.h"
 
using namespace std;
 
int main(void) {
  int c = example(5, -2.5);
  cout << c << endl;
  return 0;
}

Those using g++ to compile should write all .cpp files as the input. For example:

g++ main.cpp String.cpp

For those using IDEs you should create a new project and create the new files inside this project. Then the IDE will take care of the compilation.

String separation

Create .h and .cpp files for the String class we used frequently. Write a small main file as well to test it. (So in the end you'll have 3 files: main.cpp, String.h, String.cpp)

Libraries

Many cpp libraries exist. We'll use std for example. Let's check out a container implementation:

https://en.cppreference.com/w/cpp/container/vector

Even numbers using vector

Write a function that takes an std::vector<int> and returns a new vector that only contains the even numbers contained in the vector.

Even/odd numbers using vector

Now extend the above so you return a vector of odd numbers as well. (Try to do it with pointers/references.)

Numeric integral

Write a function that can approximate the integral of a one parameter function. To do this you can approximate the area below the function using rectangles.

Let's say we have a precision of 0.1, then we calculate the function on f(2) then you can create a rectangle out of this with corners (2, f(2)), (2.1, f(2)), (2.1, 0), (2, 0). Do this for each 0.1 sizes chunk of the interval, then the sum of these should approximate the function's integral.

The parameters of the numeric integral function should be:

  • The left side of the interval (float)
  • The right side of the interval (float)
  • The precision (float)

The return value should be a float. And the function itself should be hardcoded at first. You can experiment with the square function for example.

Generic numeric integral

You can actually have functions as parameters as well:

float integral(float (*f)(float), float from, float to, float precision) {

This way the first parameter is a function that takes a float and returns a float. For example this function would fit:

float func(float x) {
  return x * x;
}

You would call the function like so:

float val = integral(func, -5, 5, 0.1);

Modify the previous integral function so that it works with any 1 parameter float function.

Személyes eszközök