Skip to content

richelbilderbeek/correct_cpp_is_prime

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

82 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

correct_cpp_is_prime

Branch Travis CI Codecov
master Build Status codecov.io

Correct C++ chapter 'is_prime'.

Goal

  • Refactor an application to lower cyclomatic complexity

Prerequisites

Exercise

Write a command-line interface (CLI) program that determines if a number is a prime (see 'What is a prime?').

Here are the outputs and exit statuses the program should give:

Call to is_prime Output Exit status
./is_prime Any 1
./is_prime X, where X is a number that is not prime false (with newline) 0
./is_prime Y, where Y is a number that is prime true (with newline) 0
./is_prime nonsense Any 1
./is_prime 7 42 Any 1

In this exercise, you start with the code below. Yes, that code works perfectly.

#include <cassert>
#include <string>
#include <iostream>

int main(int argc, char* argv[]) 
{
  if (argc != 2) return 1;
  try
  {
    const int value{std::stoi(argv[1])};
    // -1: unknown
    //  0: false
    //  1: true
    int is_prime = -1;

    //Trivial cases
    if (value < 2) is_prime = 0;
    if (is_prime == -1 && value == 2) is_prime = 1;

    //Complex cases
    for (int denominator=2; denominator!=value-1; ++denominator)
    {
      if (is_prime != -1) break;
      if (value % denominator == 0)
      {
        is_prime = 0;
      }
    }
    if (is_prime == -1) is_prime = 1;

    //Display the result
    assert(is_prime != -1); //Must be known now
    if (is_prime == 0)
    {
      std::cout << "false\n";
    }
    else
    {
      std::cout << "true\n";
    }
  }
  catch (const std::invalid_argument&)
  {
    return 1;
  }
  catch (const std::out_of_range&)
  {
    return 1;
  }
}
  • You may start from scratch if you think that is simpler
  • The code has a too high cyclomatic complexity. Simplify it. See how to lower cyclomatic complexity. Tips:
    • put the content of the main function in a seperate function, e.g. do_main
    • a possible function prototype: bool is_prime(const int i) noexcept
    • a possible function prototype: void test() noexcept
  • Your code needs to have 100% code coverage, regardless how the executable is called (that is, with zero, one or more arguments), see how to get 100 percent code coverage

What is a prime?

Any number is a prime if it:

  • is more than 1
  • can only be divided by 1 and itself
Number Is prime?
-1 No
0 No
1 No
2 Yes
3 Yes
4 No
5 Yes
6 No
7 Yes

External links