C++ Exam
C++ Exam
#include <iostream>
int main() {
std::cout << "Hello, World!" << std::endl;
return 0;
}```
```cpp
int age = 25;
double price = 19.99;
char grade = 'A';
bool isStudent = true;
```
**3. Input/Output:**
You can take input from the user using `std::cin` and display
output using `std::cout`.
```cpp
int num;
std::cout << "Enter a number: ";
std::cin >> num;
std::cout << "You entered: " << num << std::endl;
```
```cpp
int x = 10;
if (x > 5) {
std::cout << "x is greater than 5." << std::endl;
} else {
std::cout << "x is not greater than 5." << std::endl;
}
```
**5. Loops:**
You can use loops like `for` and `while` for repetitive tasks.
```cpp
for (int i = 0; i < 5; i++) {
std::cout << "Iteration " << i << std::endl;
}
int count = 0;
while (count < 3) {
std::cout << "Count: " << count << std::endl;
count++;
}
```
**6. Functions:**
Functions allow you to modularize your code. Here's how to
declare and use functions:
```cpp
int add(int a, int b) {
return a + b;
}
int result = add(3, 4);
std::cout << "Result: " << result << std::endl;
```
```cpp
int arr[5] = {1, 2, 3, 4, 5};
std::vector<int> vec = {10, 20, 30};
```
**9. Libraries:**
For international programming competitions, you may need to
include specific libraries or headers, such as `<bits/stdc++.h>` in
competitive programming, which includes many commonly used
standard headers.
**10. Practice:**
The key to success in programming exams is practice. Solve a
variety of problems on online platforms like Codeforces, AtCoder,
or LeetCode to improve your skills.
- Variables are used to store data. You can declare them with a
type (e.g., `int`, `double`, `char`) followed by a name.
- You can assign initial values while declaring or later using the
assignment operator `=`.
Example:
```cpp
int age = 25;
double price = 19.99;
char grade = 'A';
bool isStudent = true;
```
- You can use `std::cin` to read input from the user and
`std::cout` to display output.
Example:
```cpp
int num;
std::cout << "Enter a number: ";
std::cin >> num;
std::cout << "You entered: " << num << std::endl;
```
Example:
```cpp
int x = 10;
if (x > 5) {
std::cout << "x is greater than 5." << std::endl;
} else {
std::cout << "x is not greater than 5." << std::endl;
}
```
**5. Loops:**
Example:
```cpp
for (int i = 0; i < 5; i++) {
std::cout << "Iteration " << i << std::endl;
}
int count = 0;
while (count < 3) {
std::cout << "Count: " << count << std::endl;
count++;
}
```
**6. Functions:**
Example:
```cpp
int add(int a, int b) {
return a + b;
}
Example:
```cpp
int arr[5] = {1, 2, 3, 4, 5};
std::vector<int> vec = {10, 20, 30};
```
**9. Libraries:**
**10. Practice:**
- Regularly solve problems on online coding platforms to
improve your skills.
- Learn about classes, objects, and how to create your own data
types with methods and attributes.
**15. Recursion:**
- Understand the concept of recursive functions and how to use
them for complex problems.
Start with the basics and gradually work your way up to more
advanced topics as you become comfortable with the
fundamental concepts. As a beginner, focus on understanding and
applying these concepts effectively, and practice is the key to
success. Don't hesitate to ask specific questions or seek help on
programming forums or from experienced programmers as you
prepare for your international programming exam.
Certainly! Here's a simple C++ programming exam to test your
knowledge of the basics we've covered. Each question is followed
by a description of what's expected. Feel free to attempt the
questions, and I'll provide the solutions afterward.
Write a program that takes an integer from the user and prints
the multiplication table for that number up to 10. For example, if
the user enters 5, the program should print:
```
5x1=5
5 x 2 = 10
...
5 x 10 = 50
```
Write a program that prints the numbers from 1 to 100. But for
multiples of 3, print "Fizz" instead of the number, and for
multiples of 5, print "Buzz" instead of the number. For numbers
which are multiples of both 3 and 5, print "FizzBuzz".
---
Feel free to attempt these questions, and I'll provide the solutions
afterward. If you have any questions or get stuck on any problem,
don't hesitate to ask for help or clarification. Good luck!
Of course! Here are 20 C++ programming questions of varying
complexity to test your knowledge and skills. These questions
cover a wide range of topics, from basic syntax to more advanced
concepts:
Write a program that asks the user to enter their name and age,
and then displays a message with their name and age.
Write a program that takes an integer from the user and prints
the multiplication table for that number up to 10.
Write a program that reads data from a text file and performs
some operations, such as counting lines or finding specific words.
For example, given the array `[-2, 1, -3, 4, -1, 2, 1, -5, 4]`, the
contiguous subarray with the largest sum is `[4, -1, 2, 1]`, which
has a sum of 6.
Feel free to attempt this question, and when you're ready, I can
provide you with a sample solution and an explanation of the
algorithm to tackle this problem.
I can provide a sample C++ programming question that is similar
in style and difficulty to what you might encounter in the
International Olympiad in Informatics (IOI). IOI problems are
known for their complexity and require creative algorithmic
solutions. Here's a question:
You are given a grid of size N x M, where each cell can be either
empty (denoted as '0') or a wall (denoted as '1'). A wall is a
rectangular block of '1's. Your task is to calculate the perimeter of
the polygon formed by the empty cells.
Write a C++ program that takes the grid as input and calculates
and prints the perimeter of the polygon.
Input Format:
- The first line contains two integers, N and M, representing the
dimensions of the grid (1 ≤ N, M ≤ 1000).
- The following N lines contain M characters each, representing
the grid. Each character is either '0' or '1'.
Output Format:
- Output a single integer representing the perimeter of the
polygon formed by the empty cells.
Example:
Input:
```
56
001111
011101
000000
101010
000000
```
Output:
```
18
```
Explanation:
In the given grid, the empty cells form a polygon with a perimeter
of 18.
This question involves a combination of graph traversal and
geometry, and it's a typical example of the types of problems
encountered in IOI and other competitive programming contests.
Solutions would often require clever algorithm design and
efficient implementation.