In this tutorial you will first learn about sorting, i.e. how to sort vectors and arrays. After that, you will also learn how to read from a file and write to a file using C++.
The easiest way to sort any container is with the sort() function from the algorithm library, which takes two arguments. First argument is the beginning address (container.begin()) of the container and second argument is the ending address (container.end()). Remember, this function doesn't return any value. Calling the sort function modifies the original container and the elements are arranged in sorted order.
The general syntax for sorting is:
sort(start address, end address)start address = the address of the first element of the array / vectorend address = the address of the last element of the array / vector
Every variable you define is stored in memory and thus, has a location or address. Just like you live in your home and your home has an address, similarly variables also need a home, which is memory location in this case and their home also has an address.
That address can be accessed using the ampersand operator (&) (also called the address-of operator), which denotes an address in memory. For example:
type=codeblock|id=cpp_variable_address|autocreate=cppint score = 5;cout << &score << endl;// Output 0x7ffd30e25934
This outputs the memory address of the variable score.
The memory address is not like your home address, which would be very big. Instead, the address is simply a hexadecimal nu...
In this project, you’ll write a program that allows you to Minesweeper — a single-player puzzle video game. The objective of the game is to clear a rectangular board containing hidden "mines" (or bombs) without detonating any of them, with help from clues about the number of neighboring mines in each field.
If you don't have C++ installed on your computer, you can do your work here: C++ Shell. Make sure you keep saving your work! Or, you can see C++ installation instructions here.
Below is brief description of Minesweeper and its rules:
The player is initially presented with a grid of undifferentiated squares. Some randomly selected squares, unknown to the player, are designated to contain mines. Typically, the size of the gr...
Sudoku is a logic puzzle. You can try playing Sudoku yourself here: Play Sudoku Online. In this project, you are going to implement a C++ program which solves Sudoku puzzles for you. Below is brief description of Sudoku and its rules:
Sudoku is a logic puzzle. The objective is to fill a 9×9 grid with digits in such a way such that each column, each row, and each of the nine 3×3 grids that make up the larger 9×9 grid contains all of the digits from 1 to 9 exactly once.
Each Sudoku puzzle begins with some cells filled in. These numbers are chosen such that there is a unique solution to the Sudoku.
This quiz will test your knowledge about C++ strings. The quiz also includes multiple coding exercises.
Feel free to run any provided code snippets to help you answer the questions.
In this quiz, you'll solve 5 practice problems where you will be writing code using the concepts you've learned so far - functions, loops, if statements, vectors, maps, etc. Let's get started!
Feel free to leave the quiz unfinished and come back on a different day if each problem is taking too long.
Here's the C++ solution to Exercise: Cows and Bulls.
#include <iostream>#include <string>#include <stdlib.h>#include <time.h>using namespace std;string generate_secret_number() {// Generates a 4-digit secret number (between 1000 and 9999)srand(time(0));int secret_number = rand() % 9000 + 1000;return to_string(secret_number);}string get_user_input() {int guess = 0;cin >> guess;// Checks if guess is a 4-digit number (between 1000 and 9999)while (guess < 1000 || guess > 9999) {cout << "Invalid input!" << endl;cin >> guess;}
In this exercise, we'll implement a game in C++ known as Cows and Bulls.
If you don't have C++ installed on your computer, you can do your work here: C++ Shell. Make sure you keep saving your work! Or, you can see C++ installation instructions here.
Here's how the game works.
In this exercise, you will be implementing a game known as Hotter or Colder. This exercise will (a) give you a chance to practice the skills you have been learning, such as variables, if-statements, loops, etc and (b) teach you about generating random numbers in C++. Also, by the end of it, you will have a really fun game to play when you're bored!
If you don't have C++ installed on your computer, you can do your work here: C++ Shell. Make sure you keep saving your work! Or, you can see C++ installation instructions here.
Here's how the game works.
We already learnt about strings in C++ Data-types, Operators and Strings, but in this tutorial we are going to learn a little more about C++ strings and also see some helpful functions C++ provides for strings.
String literals are enclosed in either double quotes.
For example, "Hello World" is a string.
To type in some special characters, you have to type in a backslash escape. For example, \" is a double quote, \\ is a backslash, \t is a tab and \n is a newline. For example, the following code:
type=codeblock|id=cpp_string_escaping|autocreate=cppcout << "Alice said \"How do you do?\"\nBob replied, \"Very well thank you!\"";
Output: