Variables

Video class

Exercises

Exercise 1

Write sentences that perform the actions described:

  • Create a variable of type int, name x and value 9.
    Solution

    int x {9}; // Also: int x = 9;
    

  • Create a variable of type double, with name e and value 2.718.
    Solution

    double e {2.718}; // Also: double e = 2.718;
    

  • Create a variable of type char called z with initial value 'z'.
    Solution

    char z {'z'}; // Also: char z = 'z';
    

  • Create a variable of type string called s with value "hello".
    Solution

    string s {"hello"}; // Also: string s = "hello";
    

Exercise 2

Fix the errors:

#include <iostream>
#include <string>

using namespace std;

int main()
{
    n {2};
    double pi {3,14};
    char a {"a"};
    string s ["hola"];
    cout << n << pi << a << s << "\n";
}

Exercise 3

Write in C++:

  • Change the value of x to 1.
    Solution

    x = 1;
    

  • Store the value H in the variable letter.
    Solution

    letter = 'H';
    

  • Create a variable of type int called one with value 1.
    Solution

    int one {1}; // Also: int one = 1;
    

  • Change the value of one to 2:
    Solution

    one = 2;
    

Exercise 4

Fix the errors:

#include <iostream>
#include <string>

using namespace std;

int main()
{
    string saludo {"hola"}
    cout << saludo << "\n";
    saludo = 'hello';
    cout << saludo << "\n";
    saludo = {"buenas tardes"};
    cout << saludo << "\n";
}

Exercise 5

Write in C++:

  • Create a variable called best_climber, of type string, with initial value Honnold.
    Solution

    string best_climber {"Honnold"}; // Also: string best_climber = "Honnold";
    

  • Write to the terminal the value stored in best_climber.
    Solution

    cout << best_climber;
    

  • Store in best_climber the word written by the user in the terminal.
    Solution

    cin >> best_climber;
    

  • Change the value of best_climber to Ginés.
    Solution

    best_climber = "Ginés";
    

Exercise 6

Fix the errors:

#include <iostream>
#include <cmath>

using namespace std;

int main()
{
    double pi {3,14};
    cout << 'Sphere radius? ';
    cin >> radius;
    cout << "Volume = " << 4 * pi * pow(radius, 3) / 3 << "\n"
}

Exercise 7

Write a program, translating each line to a C++ sentence:

#include "ticlib.h"

using namespace std;

int main()
{
    // Write on the terminal the message: "Triangle calculator\n"
    // Write on the terminal the message: "Write the base length: "
    // Create a variable of type 'double' called 'base' and with value 0.0
    // Get a decimal number from the terminal and store it in 'base'
    // Write on the terminal the message: "Write the height: "
    // Create a variable of type 'double' called 'height' and with value 0.0
    // Read from the terminal a decimal number and store it in 'height'
    // Send to the terminal the text "The triangle's area is: "
    // Send to the terminal de result of the operation base * height / 2
}

Exercise 8

Write a program that computes the solutions of a quadratic equation (like, $ax^2 + bx + c = 0$ ). The program should ask the user to input the coefficients of the equation ($a$, $b$ and $c$), and then it will show the solutions of the equation.

Reminder: the solutions of the quadratic equation, $$ax^2+bx+c=0$$ are given by $$x=\dfrac{-b \pm \sqrt{b^2 - 4ac}}{2a}$$

Advanced exercises

Exercise 9

Colors in the terminal.

A terminal is a text-only device: we can only write text on it. But we can write colorized text. In order to stablish the color of the text, we have to send to the terminal some codes (characters) which are interpreted by the terminal as an order to change the color of the text. If we want to go back to the previous color, we must send another special character sequence.

For example,

#include "ticlib.h"

using namespace std;

int main()
{
  cout << "This text ";
  cout << "\033[31m"; // Sequence that changes the text color to red
  cout << "in red ";
  cout << "\033[0m"; // Sequence that resets the original color
  cout << "is an example.\n";
}

The sequence for changing the color start always with \033[, and end with m. Between them, we must write the color code.

This are the allowed color codes:

ColorCode
red31
green32
blue34
yellow33
magenta35
cyan36

We can also include other codes that change the aspect of the text (bold, underline,…):

FormatCode
Bold1
Underline4
Invert7
Normal0

We can send two codes at the same time (in the same sequence), separating them with ;. For example:

#include "ticlib.h"

using namespace std;

int main()
{
cout << "This text ";
cout << "\033[4;31m"; // Sequence that changes the text to underline (4) and color red (31)
cout << "in red and underlined";
cout << "\033[0m"; // Sequence that resets the original color and format
cout << " is an example.\n";
}

Write a program that writes in the console a list of colors, with the name of each color in the color it names:

red
green
blue
yellow
magenta
cyan

Exercise 10

Reading texts from the console.

To represent texts in C++, we use the type string (we can also use the type char[]).

For reading a text from the terminal and storing it in a variable, we use an input sentence:

string text;
cin >> text;

How ever, this input sentence does not work as we might expect. Write this program:

#include <iostream>

using namespace std;

int main()
{
string s;
cout << "Write a text: ";
cin >> s;
cout << "This is your text: " << s << "\n";
}

Compile and run the program, and write This is a test as input. What do you see in the terminal as output? Something like this:

Write a text: This is a test
This is your text: This

Only the first word is written as output. What happened to the rest of the input? The answer is that the sentence cin >> s; only stores in s the first word written in the terminal (i.e., the characters up to the first blank space).

Then, how can we write a text with several words and keep it in a string variable?

The solution is to use a different sentence to read texts from the terminal:

getline(cin, s); // Gets a whole line from the standard input and stores it in s

Replace the sentence cin >> s; with getline(cin, s); in the previous program, and check it again. Now you will see the full text.

Write a program that allows the user to input two lines of text, and then shows the lines in reverse order.

For example, if the user writes:

First line
Second line

the program will output to the terminal:

Second line
First line
Previous