Learning C++ is no small feat, but keeping all your new skills fresh requires practice. It's one thing to learn about code fundamentals and syntax, but it's an entirely different thing to use that knowledge to create C++ programs.
In this article, we'll describe where you might use C++ in your next job and why it's such a powerful programming language. To save you time looking for ways to practice putting C++ to work, we've got 10 code challenges for beginners that you can dive right into.
What is C++ used for?
C++ is one of the most widely used programming languages. So, a better question might be, what isn't C++ used for?
Most often, C++ is selected because of its ability to run large applications efficiently. You can also use it to fine-tune how a program uses its hardware. You can find C++ within operating systems, your favorite multiplayer games, powering databases, and even in emerging VR and AR technologies.
Many developers choose C++ because it can efficiently run their programs, even on relatively small hardware provisions. But they also like using the language because so many other developers are familiar with it, allowing for easier collaboration and getting help when you get stuck. In another post, we take a closer look at what C++ is used for.
10 C++ code challenges for beginners
While you were taking courses on C++, you may have used a built-in editor to complete your assignments. If that's what you're most comfortable with, then try these challenges in that editor. But, if you're interested in challenging yourself to use an editor on your local machine, you can execute these challenges there too.
The main goal of these challenges for beginners is to keep you going. So, choose the editor that works best for you and set yourself a target. Maybe it's completing one code challenge each day. Maybe it's a challenge every weekend. Whatever you pick, make it realistic and schedule it into your calendar to make it a priority.
1. Print out a "Greetings!" message
In whichever editor you've chosen to work with, see if you can write a piece of code that displays a string. While this might be a simple task, think of all the ways you'll want to display messages in future programs you create.
Printing error messages or confirmation messages for different pieces of code are important when you're debugging a program. There may also be prompts or results you'll want to print and share with the end-user.
Printing a message is a skill you'll use quite frequently.
2. Create a program that adds two integers
See if you can create a C++ program that takes two integers and adds them together. The result should return the sum. While this may be a rudimentary calculator, imagine its power in a large database where you could use loops to add massive quantities of data very meticulously.
Bonus challenge: Expand on this program to add two columns of a matrix to produce an array of the summed elements.
If you're interested in working in the financial industry as a developer, you'll use programs like this to help your company calculate its figures.
3. Create a program that converts meters to feet
Unit converters are helpful little tools. Can you create a program in C++ that takes a measurement in meters and gives you the exact same measurement in feet as the answer?
If you're considering a career in data science, you'll need to be good at cleaning up and formatting data. Converting data to another unit of measurement is a good first practice test for your future role.
4. Program a temperature conversion tool
Similarly, converting temperature to another unit of measurement is a helpful program to build. See if you can create a program that takes inputted temperature in Celsius and returns the same temperature in Fahrenheit.
If you wanted to make this a more intermediate-level challenge, try making the input both a temperature and the unit of measurement. Have your conversion program return the same temperature but in the other units.
5. Create a floating-point multiplier program
Given two floating-point numbers, create a C++ program that gives you the product of the two.
Looking to take things up to an intermediate level? Build upon this challenge by allowing for any number of inputs (or an array of them) and returning the product of all input floating-point numbers.
6. Convert a string to all uppercase letters
You can decide how long or short your input string will be. This program should take inputted lowercase letters and convert them to uppercase.
Just like the challenge above that was well suited to data science, imagine how useful this tool could be for managing databases or cleaning data.
7. Create an average calculator
Write a C++ program that takes an array of numbers as its input and provides the average of the entries in return. If you wanted to build upon the array input, you could make another average calculator that takes a matrix as its input and provides the average of each row or column.
8. Create a function that trims a string down to 10 characters
Can you construct a function that accepts a string as input and returns a shortened version? There are many applications for data trimming, but consider how a tool like this would help eliminate errors in a database.
For example, in customer address data, any zip code greater than five characters would be incorrect. Trimming entires using a program like this can help keep databases clean.
9. Program an array sorter
Given an array of, let's say, 10 numbers, can you create a program that returns the array in numerical order? Sorting data is a powerful way to use C++. If you're comfortable with a small array, try working with a larger one — or even a matrix.
10. Create a program that counts duplicate array entries
While cleaning data, Data Scientists need to eliminate duplicate data. So, in this short code challenge, you'll create a program that returns the number of elements in an array that are duplicates.
More ways to practice C++
There are many places you can find additional code challenges online, but you might already have plenty of options within reach. Along with these suggestions, go back into your C++ course notes and repeat the same exercises and assignments. Or, if you need a refresher, check out our Learn C++ course.
You can also contribute to open-source projects online if you're looking to keep your C++ skills fresh. And no matter which option you choose, you'll be preparing yourself for your next job and possible technical interview questions.
Remember, just do a little each day, keep your goals manageable, and have fun solving challenges with C++.