B
B
biohazardbenzg2020-11-28 17:02:31
C++ / C#
biohazardbenzg, 2020-11-28 17:02:31

How to determine the smallest and largest value with two variables, as well as the sum, difference, etc.?

Hello .

Started learning C++.

I read the chapter - " Objects Types and Values ", from the book I'm learning from by Bjarne Stroustrup.

Task : Write a program that prompts the user to enter two integer values ​​+ the definition of the smallest and largest values, as well as the sum, difference, product and quotient of these values.

#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
#include<cmath>
using namespace std;
int main()
{
  setlocale(LC_ALL, "Russian");
  int val1, val2;
  cout << "Введите два целочисленных значения.\n";
  cin >> val1, val2;
  
  
  system("pause");
}


* The directives suggested to me that some are superfluous, but according to the book they are indicated, the file location is std_lib_facilities.

Started doing it and hung up on it.
Questions :
How to assign the entered value to variables? If this is unacceptable, what should be done?
How to perform all these operations with these variables?
Do I need to use the "if" command in this program?
Which sections (for example, Variables, Input, and type...) should be returned to and analyzed in more detail?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexey, 2020-11-28
@biohazardbenzg

cin >> val1;
cin >> val2;
This line assigns the entered values.
For such a simple program, it is enough and it is worth going back or going through conditions and cycles. And the program will look like this.#include <iostream>
#include <iostream>

using namespace std;
int main()
{
  setlocale(LC_ALL, "Russian");
  int val1, val2;
  cout << "Введите два целочисленных значения.\n";
  cin >> val1;
  cin >> val2;
  if (val1 > val2) {                          // - делаем условие для проверки (если 1 значение больше 2, то 1- максимальное, 2 - минимальное)
    cout << "Большее " << val1 << endl
      << "Меньшее " << val2 << endl;

  }
  if (val1 < val2) {
    cout << "Большее " << val2 << endl
      << "Меньшее " << val1<< endl;
  }
  else {                                  // - если никакое из предыдущих условий не сработало, значит выводить что значения равны, так как другого не дано)
    cout << "Значения равны" << endl;
  }

}

The difference can be calculated in the condition, because there we already know which of them is greater, so that the difference does not turn out to be negative, in order to calculate it is necessary to declare a new variable, let's say
int razn;
razn = val1-val2;
cout << "Разница = " << razn << endl;

And everything else follows the same principle, there are, of course, ready-made functions that determine the maximum and minimum, but if you just started learning c ++, you should know how to do banal things.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question