Skip to content

3. Editor Configuration

Solving the Exercises

First, we will solve the exercises from the last lecture.

  1. Prompt the user of the program to input a number. Use this number as count of elements of the Fibonacci sequence to be printed on the terminal.
#include <iostream>

using namespace std;

void print_fibonacci(int n);

int main() {
  cout << "Give me the number of elements: ";
  int n;
  cin >> n;

  print_fibonacci(n);
}

void print_fibonacci(int n, int f_1, int f_2) {
  int f_1 = 1;
  int f_2 = 1;

  for (int i = 0; i < n; ++i) {
    cout << "f(" << i + 1 << ") = " << f_1 << "\n";

    int f_tmp = f_1 + f_2;
    f_1 = f_2;
    f_2 = f_tmp;
  }
}
  1. Add a second and a third argument to print_fibonacci function to parameterize the starting values of f_1 and f_2. Test your implementation by calling the function in main several times with different argument values.
#include <iostream>

using namespace std;

void print_fibonacci(int n, int f_1, int f_2);

int main() {
  cout << "Give me the number of elements: ";
  int n;
  cin >> n;

  print_fibonacci(n, 1, 1);
  print_fibonacci(n, 3, 2);
  print_fibonacci(n, -1, 1);
}

void print_fibonacci(int n, int f_1, int f_2) {
  // int f_1 = 1;
  // int f_2 = 1;

  for (int i = 0; i < n; ++i) {
    cout << "f(" << i + 1 << ") = " << f_1 << "\n";

    int f_tmp = f_1 + f_2;
    f_1 = f_2;
    f_2 = f_tmp;
  }
}

Comma versus Semicolon

In C++, we have to use commas and semicolon. Typically, a semicolon is ending an expression and therefore making it a statement. Most of the time, we will use semicolon to mark those statements which are actually executing instructions on the processor. This means, for now, that we will find those semicolon mostly inside of scope blocks marked by the curly braces { and }.

Commas on the other hand can be used to group several expressions into one bigger statement. This is a technique that will not be used at the beginning. So as a consequence for us, commas will be used when declaring, defining, or using functions inside their list of arguments to separate single elements.

void print_fibonacci(int n, int f_1, int f_2);
print_fibonacci(n, 1, 1);
void print_fibonacci(int n, int f_1, int f_2){...}

The for loop is not a function. Hence, we use semicolon to separate the expressions in the parentheses (...).

for (int i = 0; i < n; ++i) {...}

Text Editor Configuration and Clang Format

Most of our work will be done inside the text editor. For a productive workflow, it is therefore necessary to make use of features provided by the text editor, such as automatic file saving, fast file generation, shortcuts, fast file switching, project organization and handling, and automatic formatting. Learning and internalizing such features takes up a huge amount of time. Thus, we will strive for learning them step by step.

For those who use Sublime Text as their main text editor, please take a look at our recommended Sublime Text configuration and carefully read the instructions. Furthermore, make sure to install Clang Format by using your system's package manager. This will enable the automatic formatting of your C++ code every time it is saved.

Arch-based Linux Distributions:

sudo pacman -S clang

Debian-based Linux Distributions:

sudo apt update && sudo apt install clang-format

Sublime Text Useful Shortcuts

If the following shortcuts do not suit your needs, do not hesitate to change them in the Sublime Text settings.

  • ctrl + alt + n: Open the small command palette at the bottom of Sublime Text and type in the name of the file relative to project root that should be created.
  • ctrl + s: Manually save the currently opened file and automatically use Clang Format on it.
  • Focus Lost: Automatically save the file for which we have lost the focus and automatically apply Clang Format.
  • alt + n: Open or close the command palette of Sublime Text.
  • alt + f: Open or close the file palette in Sublime Text to switch between every possible in the currently opened project.
  • ctrl + b: Run the integrated build system of Sublime Text to compile and run single C++ files. You do not have to use it.
  • ctrl + 7: In C++ files, change the selected lines to comments or back to actual code.

Last update: October 22, 2020