C++ Basics - Practice

Muhammad Magdi

What is an Online Judge?

Is an online system that tests the correctness of the solutions of programming problems.

URI Registration

Open:
https://www.urionlinejudge.com.br/

Then Follow this Video:

https://youtu.be/9kt2zv2An_o

Square Area

Write a C++ Program such that, given an integer number 's' representing the side length of a square prints its area.

S

Square Area

[Solution]

#include <iostream>

using namespace std;

int main() {
  int s;
  cin >> s;
  cout << s*s << endl;
  return 0;
}
#include <iostream>

using namespace std;

int main() {
  int a, b;
  cin >> a >> b;
  int x = a + b;
  cout << "X = " << x << endl;
  return 0;
}
  • Make sure to follow the problem description!
  • You must print the message "X = "
    • Before the sum answer.
    • With capital x.
    • With a space before and after the equal sign.
    • Followed by an end of line ( endl ).
    • Otherwise your solution won't be correct.
#include <iostream>

using namespace std;

int main() {
  int a, b;
  cin >> a >> b;
  cout << "PROD = " << a*b << endl;
  return 0;
}
  • We could save the value a*b in a variable before printing it, both ways are correct.
  • You must print the message "PROD = "
    • Before the product answer.
    • With all capital letters.
    • With a space before and after the equal sign.
    • Followed by an end of line ( endl ).
    • Otherwise your solution won't be correct.
  int c = a*b;
  cout << "PROD = " << c << endl;

URI 1013 - The Greatest

[Incomplete Solution]

#include <iostream>
#include <cmath>

using namespace std;

int main() {
  int a, b;
  cin >> a >> b;
  int max = (a + b + abs(a - b)) / 2;
  cout << max << endl;
  return 0;
}
  • Note that we added a new library in our code called cmath using #include.
  • cmath is used here to tell the compiler how abs works.
  • abs(5) = 5, abs(-5) = 5, abs(3) = 3, abs(-3) = 3, ...
#include <cmath>

Assignment

Thanks

Content

  • URI Online Judge
  • Square Area
  • Extremely Basic
  • Simple Product
  • The Greatest
Made with Slides.com