Condition of the problem: Given the coordinates (as integers from 1 to 8) of two chessboard fields. Check if the knight can move from one field to another in one move. Create a program that uses the function to perform the check. The function should return the value 1 (true) or 0 (false). Ps That is, enter the coordinates where the horse stood, and the coordinates where to move it, if it is according to the rules then return 1 if not then zero.

Waiting for any advice and criticism.

#include "pch.h" #include <iostream> #include "windows.h" #include "iostream" #include "math.h" using namespace std; int ProverkaPravilnostiDanuh(int i1, int j1, int i2, int j2, int r1, int r2) {} int main() { int Starti, Finishi, Startj, Finishj, r1, r2, c; cout << "Vvedite Kordinatu start i Finish " << endl; cin >> Starti >> Startj; cout << endl; cin >> Finishi >> Finishj; cout << endl; c = ProverkaPravilnostiDanuh(Starti, Startj, Finishi, Finishj, r1,r2); if (c == 0 || 1) cout << "Eto vozmojno"; else cout << "Ne vernue kordnatu"; system("pause"); return 0; } int ProverkaPravilnostiDanuh(int i1, int j1, int i2, int j2, int r1, int r2){ r1 = i2 - i1; r2 = j2 - j1; if (abs(r1) == 1 && abs(r2) == 2) return 1; else if (abs(r2) == 1 && abs(r1) == 2) return 1; else; return 0; } 
  • It is worth at least format the code normally. It is impossible to read, well. : / - HolyBlackCat pm
  • Instead of declaring and defining the function ProverkaPravilnostiDanuh , you have two definitions. - ⷶ ⷩ ⷮ ⷪ ⷩ
  • This question is a duplicate: www.stackoverflow.com/questions/418668 (answer from Petr Abdulin) www.stackoverflow.com/questions/765599 - ⷶ ⷩ ⷮ ⷪ ⷩ
  • Three answers, understanding zero. Format the code ... I would learn to work with functions. Definition ads are certainly very good, but you can read more, I did not understand anything. - Vovenishe 1:43 pm
  • @Anton I would gladly understand, but I did not understand. Already asked my question, but I did not find a solution there. - Vovenishe

1 answer 1

The first

 int ProverkaPravilnostiDanuh(int i1, int j1, int i2, int j2, int r1, int r2) {} 

is not a declaration , but a definition . Leave instead just an ad -

 int ProverkaPravilnostiDanuh(int i1, int j1, int i2, int j2, int r1, int r2); 

Otherwise, you break the rule of one definition ...

And yet - this is not a pool, in which you first need to fill in the water, and then learn to swim :) Format the code - you need to acquire this habit immediately.

  • Thanks, it helped. - Vovenishe