Learn How To Understand and Break Down Programming Problems

Break programming problems down into smaller steps that can then be translated into working C++ code

Drew Coleman
Better Programming
Published in
6 min readJul 3, 2019

--

Photo by Volodymyr Hryshchenko on Unsplash

In my last article, I gave some advice and insights about how to overcome programming problems. I realize that I never showed how to put some of the advice I gave to use so I wanted to change that now.

In this post, I want to look specifically at learning how to understand and break down programming problems.

Knowledge Required

  • Basic C++ language.
  • Arrays.
  • If statements.
  • For loop.
  • Variable.

The Problem

I have purposefully picked a problem that requires as little programming knowledge as possible:

Write a program that finds the largest value in an array of numbers and prints it out to the console.

Okay, so let’s get started. First, whenever I am trying to solve something, I usually begin by identifying important aspects of the problem, which, in this case, I will highlight.

Write a program that finds the largest value in an array of numbers and prints it out to the console

As shown by the highlighted text, the important things to note about this problem are as follows:

  1. The numbers are stored in an array.
  2. We must find the largest value in an array of numbers.
  3. We have to print the largest value in the array to the console.

Even though we have essentially just rewritten the problem in bullet form, we can see that we have already broken the problem into two distinct steps. Finding the largest value and printing it.

We also know that to achieve finding the largest value, we must have some knowledge about arrays.

What We Know

Before tackling the last two steps, let’s start by writing down everything we know about arrays.

This can become useful if we are unsure about how to solve the problem. Writing everything you know down might bring about some idea to help you get started.

  • Arrays are a fixed length, they cannot be changed after they are created.
  • Values in an array are accessed using an index.
  • The index for an array starts at zero.
  • Arrays allow us to access values randomly, that is, we can pass in any index that is valid for an array and get the value stored at that location.

Note: It’s a good idea to do your research on anything you don’t know about a problem you are trying to solve, before going any further. Research the things you don’t know, then write down everything you have learned.

What We Don’t Know

One issue I have with this problem is that it doesn’t tell us everything we need to know.

For instance, the problem specifies the numbers come in an array, but how is the array populated? Should I be getting numbers from user input, from a file, or should I just be hardcoding the numbers?

In some situations, you might be told or it will just make sense within the context of the problem. In this situation, I am just going to go with the last option and hardcode an array of integers that I can use to test how the problem works.

int array[] = {3,12, 19,8, 2,1, 7,4};

Now we have written everything we know about arrays and answered something not specified in the problem, we can move onto the next steps.

I am going to start with printing the number to the console as this seems a lot easier than finding the largest value in the array.

Printing the Largest Value

C++ provides functionality that allows us to print out content to the console in the form of cout, which is part of iostream. cout allows us to print out literals, such as “Hello” and 5, as well as content stored in a variable.

I am assuming that the largest value will be stored somewhere before it’s going to be printed, say, in a variable called maxNumber. I will use cout to print the value stored in maxNumber.

std::cout << "Max number = "<< maxNumber;

Finding the Largest Value in Array

Now we are arguably at the biggest challenge in this programming problem. Trying to find the largest value in the array.

Printing to the console is simple in C++, thanks to cout. Finding the largest value in an array can be a bit more involved and involves a few steps to solve.

The problem can be broken down into two additional steps:

  1. Find values in an array.
  2. Compare values to see which is largest.

Finding values in an array

To find values in an array, we already know we can access them directly with an index value that is in range of the array. In our example, array[3] = 8.

If you are familiar with the idea of loops in programming, you might be aware that we can iterate through all the values in our array using a for loop. The code below shows how this is done, whilst also printing each number to the console.

for(int index = 0; index < SIZE ; ++index)
{
std::cout << array[index] << std::endl;
}

Comparing values to see which is largest

Again, to break this down a bit more. If we were to think about just two numbers, 3 and 4, we could determine if 3 is less than 4, by using the less-than operator, 3 < 4.

In C++ we know that the expression 3 < 4 will return either true or false. We can place this expression in an if statement, and if 3 < 4 is true then we can store 4 in maxNumbers.

if(3 < 4)
{
maxNumber = 4;
}

To find the largest value in an array, all we need to do is take one of the values in the array and compare it against whatever we have stored in maxNumber. If the number is greater than what is in maxNumber, we will make maxNumber equal to that value.

if(maxNumber < array[index])
{
maxNumber = array[index];
}

Bringing It All Together

Now we understand how to find a value in the array and how to compare values to see which is largest. All that is needed now is to bring it all together.

To find the largest value, we just iterate through the array and then call the code above. We can set maxNumber to the first value in the array and then this allows us to do one less iteration in our for loop.

for(int index = 1 ; index < SIZE; ++index)
{
if(maxNumber < array[index])
{
maxNumber = array[index];
}
}

Assuming maxNumber is set to the first number in the array, 3. We then iterate through our array starting at the second number in the array.

We don’t need to compare the first number against itself. The code goes through and checks if the value in maxNumber is less than the current value being visited in the array. If it is, then we update maxNumber.

The code loops through all the values in the array and, in the end, the largest value in the array will be stored in maxNumber, ready to be printed.

The code for the whole solution is shown below.

#include <iostream>int main()
{
const int SIZE = 8;
int array[] = {3, 12, 19, 8, 2, 1, 7, 4};
int maxNumber = array[0];
for(int index = 1 ; index < SIZE; ++index)
{
if(maxNumber < array[index])
{
maxNumber = array[index];
}
}
std::cout << "Max number = " << maxNumber; return 0;
}

In this article, we have looked at learning how to understand and break down programming problems into smaller steps that can then be translated into working C++ code.

With plenty of practice, this idea of breaking down problems will get a lot easier, even for problems that are more challenging than the one in this article.

--

--

Drew Coleman
Drew Coleman

Written by Drew Coleman

Game developer writing articles about: game development and programming.

No responses yet